Files
OrcaSlicer/src/slic3r/Utils/NetworkAgentFactory.hpp
Andrew dd2cb92685 Gate agent mode behind use_printer_agents toggle
Replace per-printer auto-activation
(is_current_printer_agent_plugin)
with a global experimental AppConfig
toggle, default off: legacy
print-host behavior is unchanged
until the user opts in. The toggle
drives device-tab routing, print
button defaults, connect-button
visibility and sidebar layout, and
dedups machine-select dialog opens.
2026-08-04 18:12:20 +08:00

196 lines
7.9 KiB
C++

#ifndef __NETWORK_AGENT_FACTORY_HPP__
#define __NETWORK_AGENT_FACTORY_HPP__
#include "ICloudServiceAgent.hpp"
#include "IPrinterAgent.hpp"
#include "NetworkAgent.hpp"
#include "OrcaCloudServiceAgent.hpp"
#include "BBLCloudServiceAgent.hpp"
#include "BBLNetworkPlugin.hpp"
#include "libslic3r/AppConfig.hpp"
#include <memory>
#include <string>
#include <functional>
#include <vector>
namespace Slic3r {
static constexpr char ORCA_PRINTER_AGENT_ID[] = "orca";
static constexpr char BBL_PRINTER_AGENT_ID[] = "bbl";
// Factory function type for creating printer agents
using PrinterAgentFactory =
std::function<std::shared_ptr<IPrinterAgent>(std::shared_ptr<ICloudServiceAgent> cloud_agent, const std::string& log_dir)>;
// Information about a registered printer agent
struct PrinterAgentInfo
{
std::string id; // Registry/config key, e.g. "orca" or a plugin AgentInfo::id
std::string display_name; // e.g., "Orca Native", "Bambu Lab"
std::string plugin_identifier; // Empty for built-ins, otherwise <plugin_key>;<uuid>;<capability_name>
PrinterAgentFactory factory; // Function to create the agent
bool is_plugin() const { return !plugin_identifier.empty(); }
PrinterAgentInfo(const std::string& id_, const std::string& display_name_, PrinterAgentFactory factory_)
: id(id_), display_name(display_name_), factory(std::move(factory_))
{}
PrinterAgentInfo(const std::string& id_, const std::string& display_name_, const std::string& plugin_identifier, PrinterAgentFactory factory_)
: id(id_), display_name(display_name_), plugin_identifier(plugin_identifier), factory(std::move(factory_))
{}
};
/**
* NetworkAgentFactory - Factory for creating network agent instances
*
* This factory creates cloud agents and printer agents for the networking subsystem.
* The architecture separates cloud services (authentication, project sync) from
* printer communication (device discovery, print jobs).
*
* Startup flow:
* 1. Call register_all_agents() during app initialization
* 2. Cloud agent created at startup via create_agent_from_config()
* 3. Printer agent created on-demand when a printer is selected
*
* Usage:
* // At app startup (before any agent creation)
* NetworkAgentFactory::register_all_agents();
*
* // Create NetworkAgent with cloud agent only
* auto agent = create_agent_from_config(log_dir, app_config);
*
* // When printer is selected - create printer agent from registry
* auto printer = NetworkAgentFactory::create_printer_agent_by_id("orca", cloud, log_dir);
*/
class NetworkAgentFactory
{
public:
// ========================================================================
// Printer Agent Registry
// ========================================================================
/**
* Register all built-in printer agents.
* Must be called once during application initialization, before any
* calls to get_registered_printer_agents() or create_printer_agent_by_id().
*/
static void register_all_agents();
/**
* Register a printer agent type
*
* @param id Unique identifier for the agent (e.g., "orca", "bbl")
* @param display_name Human-readable name for UI
* @param factory Factory function to create the agent
* @return true if registration succeeded, false if already registered
*/
static bool register_printer_agent(const std::string& id, const std::string& display_name, PrinterAgentFactory factory);
/**
* Check if an agent registry/config key is registered
*/
static bool is_printer_agent_registered(const std::string& id);
/**
* Get info about a registered agent by registry/config key
*/
static const PrinterAgentInfo* get_printer_agent_info(const std::string& id);
/**
* Return the full plugin reference for a plugin-backed printer agent ID, or empty for built-ins.
*/
static std::string get_printer_agent_plugin_identifier(const std::string& id);
/**
* Get all registered printer agents (for UI population)
*/
static std::vector<PrinterAgentInfo> get_registered_printer_agents();
/**
* Create a printer agent by registry/config key
*
* Returns a cached instance if one exists for the given key, otherwise
* creates a new agent via the registered factory and caches it.
*
* @param id Agent registry/config key to create
* @param cloud_agent Cloud agent for token access
* @param log_dir Directory for log files
* @return Shared pointer to IPrinterAgent, or nullptr if ID not found
*/
static std::shared_ptr<IPrinterAgent> create_printer_agent_by_id(const std::string& id,
std::shared_ptr<ICloudServiceAgent> cloud_agent,
const std::string& log_dir);
/**
* Clear the printer agent cache.
* Calls disconnect_printer() on each cached agent and releases all shared_ptrs.
* Should be called during application shutdown before destroying the NetworkAgent.
*/
static void clear_printer_agent_cache();
// ========================================================================
// Cloud Agent Factory
// ========================================================================
/**
* Create a cloud service agent based on provider type.
* Handles authentication, project sync, and other cloud services.
*
* @param provider Which implementation to use (Orca or BBL)
* @param log_dir Directory for log files
* @return Shared pointer to ICloudServiceAgent implementation
*/
static std::shared_ptr<ICloudServiceAgent> create_cloud_agent(const std::string& provider, const std::string& log_dir)
{
if (provider == ORCA_CLOUD_PROVIDER) {
return std::make_shared<OrcaCloudServiceAgent>(log_dir);
} else if (provider == BBL_CLOUD_PROVIDER) {
auto& plugin = BBLNetworkPlugin::instance();
if (!plugin.is_loaded()) {
return nullptr;
}
if (!plugin.has_agent()) {
plugin.create_agent(log_dir);
}
if (!plugin.has_agent()) {
return nullptr;
}
return std::make_shared<BBLCloudServiceAgent>();
}
return nullptr;
}
// Plugin printer agents
static void register_python_plugin(const std::string& plugin_key);
static void deregister_python_plugin(const std::string& plugin_key);
static void register_python_printer_agent(const std::string& plugin_key, const std::string& capability_name);
static void deregister_python_printer_agent(const std::string& plugin_key, const std::string& capability_name);
private:
// Factory is not instantiable
NetworkAgentFactory() = delete;
~NetworkAgentFactory() = delete;
NetworkAgentFactory(const NetworkAgentFactory&) = delete;
NetworkAgentFactory& operator=(const NetworkAgentFactory&) = delete;
};
/**
* Create a NetworkAgent from AppConfig settings (main entry point)
*
* Creates a NetworkAgent with cloud agent only. The printer agent is created
* separately when a printer is selected, via create_printer_agent_by_id().
*
* Cloud provider: Always creates OrcaCloudServiceAgent as the primary provider.
* Third-party cloud agents (e.g., Bambu) are created from the cloud_providers
* AppConfig setting and added via NetworkAgent::add_cloud_agent().
*
* @param log_dir Directory for log files
* @param app_config Application configuration object
* @return NetworkAgent with cloud agent, or nullptr on failure
*/
std::unique_ptr<NetworkAgent> create_agent_from_config(const std::string& log_dir, AppConfig* app_config);
} // namespace Slic3r
#endif // __NETWORK_AGENT_FACTORY_HPP__