Files
OrcaSlicer/docs/HLSD/printer-agent.md
SoftFever 8f3e8a002e Name the OPCP submodule folder docs/OPCP
The specification is checked out at docs/OPCP, matching the repository's name.
2026-11-30 20:36:42 +08:00

20 KiB

Printer agents

Printer agents isolate printer-specific communication from the rest of OrcaSlicer. The GUI and DeviceManager operate on a shared set of printer operations and device state; a selected printer agent implements those operations for a particular printer ecosystem. The agent boundary allows Bambu, Moonraker-based printers, built-in integrations, and Python-provided integrations to use the same application workflow without making the GUI understand every printer protocol.

The current boundary is an adapter boundary around the existing application contract. In particular, some request fields and message payloads still use the Bambu-shaped representation that existing MachineObject and DeviceManager code consumes. The printer agent is responsible for translating that representation into the protocol spoken by its printer. This is an intentional compatibility constraint of the current design; the interface is not yet a neutral printer protocol.

The v1 dialect migration path is deliberately narrow. DeviceManager currently speaks the Bambu JSON dialect because that is the payload shape already used throughout the command and state workflow. The v1 OrcaPrinterAgent also accepts that Bambu dialect. Its transport path places the small translation needed for the target printer at deliver_to_sink, keeping the compatibility code at the edge rather than spreading it through DeviceManager or the agent interface.

On the wire, OrcaPrinterAgent speaks the Orca Printing Communication Protocol (OPCP), the protocol OrcaSlicer, printers and the OrcaCloud gateway use with each other. The agent speaks it with a printer directly on the LAN, and with the OrcaCloud gateway, over the MQTT connection OrcaCloudServiceAgent owns, when it reaches a printer through OrcaCloud. The protocol's specification, JSON Schemas and conformance traces are in docs/OPCP/, a git submodule of OrcaSlicer/OPCP that follows its main branch. Run git submodule update --init docs/OPCP to check it out, and git submodule update --remote docs/OPCP to move it to the latest specification.

The eventual direction is for DeviceManager to produce an Orca JSON dialect. The Bambu agent will then own the translation from Orca JSON to Bambu's protocol, while OrcaPrinterAgent can forward the Orca payload directly to its sink. The v1 translation at deliver_to_sink can then be removed without changing DeviceManager, the command callers, or the rest of the agent workflow.

Components

The system has four relevant layers:

GUI / DeviceManager / MachineObject
              |
        NetworkAgent
          /       \
 IPrinterAgent   ICloudServiceAgent
      |                 |
 printer protocol   authentication and cloud services

DeviceManager and MachineObject

DeviceManager owns the application-facing printer workflow. It maintains MachineObject instances, updates their state, filters devices for the active printer agent, and initiates operations such as homing, temperature changes, printing, subscriptions, and camera playback.

MachineObject remains the shared state model used by the GUI. It does not contain the implementation of a printer protocol. When a device is discovered or returned by a cloud query, the device is tagged with the printer_agent_id of the agent that found it. Device lists and selected-machine operations use that tag to avoid sending an operation through an agent that does not own the device.

NetworkAgent

NetworkAgent is the façade used by the GUI and DeviceManager. It owns:

  • the currently selected IPrinterAgent;
  • the registered cloud-service instances, indexed by provider;
  • callbacks shared by the active printer agent and the application;
  • the forwarding methods for printer commands and cloud operations.

There is one active printer agent for the currently selected printer preset. Switching the preset increments the machine-list generation, disconnects the old printer agent, removes its callbacks, and installs the newly selected agent. The façade then forwards printer operations to that agent.

Discovery is not tied to the active agent. See Discovery.

Cloud operations are selected separately using a provider key. NetworkAgent forwards a cloud request to the matching ICloudServiceAgent, and forwards cloud camera operations with a device ID. The printer agent receives a cloud-agent pointer through set_cloud_agent() when it is created, allowing printer communication to obtain cloud tokens without depending on a concrete cloud implementation.

IPrinterAgent

IPrinterAgent is the printer-facing contract. It covers:

  • cloud-relay and direct-LAN message delivery;
  • LAN connection, discovery, binding, and certificates;
  • printer subscriptions and callbacks;
  • print operations;
  • filament synchronization;
  • camera capability and local camera URL reporting;
  • printer command methods.

Concrete built-in implementations include the Bambu wrapper, the native Orca/Moonraker path, and other printer-agent implementations registered by the application. A printer agent may use either the cloud agent, a direct LAN connection, or both.

An agent may report tool_display_order(dev_id, tool_count) from cached device state. Each entry is a zero-based device tool ID in display order; for a two-tool printer the first entry is the left tool. An empty result leaves the order unspecified. The GUI uses identity order for generic devices and the Bambu two-tool convention when the corresponding agent is unavailable. Invalid permutations are rejected. This order controls presentation, not the preset's physical_extruder_map, which routes logical tools to physical tools.

ICloudServiceAgent

ICloudServiceAgent owns authentication and services provided by a cloud backend. It covers login state, tokens, user and printer lists, settings synchronization, model services, cloud messages, and cloud camera operations.

Cloud camera operations are device-scoped:

  • get_camera_url(dev_id, callback) obtains a stream URL for one device;
  • create_camera_signaling_channel(dev_id) creates signaling for one device where the provider supports it.

This is separate from the local camera URL exposed by IPrinterAgent, which is currently scoped to the active printer agent because a normal LAN agent represents one physical printer connection.

Agent registration and selection

NetworkAgentFactory maintains the printer-agent registry. Each registry entry contains an agent ID, a display name, and a factory function. Built-in agents register during application initialization. Python printer-agent capabilities register dynamically and contribute an agent ID and factory entry.

The selected printer preset contains the printer-agent choice. If no explicit choice is stored, the application preserves the existing default behavior: Bambu presets select the Bambu agent and other presets select the native Orca agent. When a preset is changed, GUI_App resolves the effective agent ID, obtains the corresponding cloud agent, creates the printer agent through the registry, and installs it in NetworkAgent.

The registry rejects conflicting agent IDs. This matters for Python plugins because an agent ID is the stable identity used by presets and device ownership; two enabled plugin capabilities must not claim the same ID.

Message and command flow

There are two low-level message paths:

  • send_message() publishes a command through the printer's cloud relay;
  • send_message_to_printer() sends a command directly to the printer over the LAN path.

Both paths accept a JSON string, quality-of-service and flag values, and return the existing network status code domain. The agent owns the conversion from that JSON contract to its native transport.

The typed command_* methods are the application-facing convenience layer. The five generic defaults currently implemented by IPrinterAgent construct the existing JSON dialect and route through the same message path:

Method Default operation
command_xyz_abs() Send G90 for absolute positioning
command_auto_leveling() Send G29 for bed leveling
command_go_home() Use the supported homing operation or send G28
command_set_bed() Use the supported bed control or send M140
command_set_nozzle() Send M104 for nozzle temperature

These are compatibility defaults for common printer workflows, not a guarantee that every firmware implements every command identically. An agent can override a method when its protocol needs another operation. For example, a Klipper configuration may use BED_MESH_CALIBRATE instead of G29.

The remaining common command methods default to ORCA_NETWORK_ERR_CMD_NOT_SUPPORTED because their existing behavior is vendor-specific or has no portable implementation:

  • AMS RFID refresh;
  • AMS calibration;
  • AMS tray selection;
  • camera start;
  • axis control.

The methods remain on the common interface so an agent that supports them can override them explicitly. sequence_id remains part of the command contract because DeviceManager creates and tracks it as the command ID. Device-layer commands draw it from one process-wide atomic counter, PrinterSequenceIds, inside the 20000-29999 band that marks a reply as an answer to this app. The counter wraps inside the band, so replies still count as the app's own however many commands a session sends. It starts at a random point in the band at each launch, so a printer that remembers recent ids does not take a new launch's commands for repeats. The Orca agent's connect requests keep their fixed ids in the band.

The JSON commands the device layer sends are built in one place, Utils/OpcpCommands, with one builder per wire message grouped by the message's top-level namespace. MachineObject and the DeviceCore controls call the builders and keep their own gating and state updates; the builders hold the field names and types, which are the contract every agent translates. A test pins the exact text each command hands its agent, so moving a builder cannot change what a printer receives.

Discovery

Every registered printer agent that scans the LAN discovers at the same time, whether or not it is the active agent, and each device it finds is recorded under that agent.

IPrinterAgent::discovers_on_lan() says whether start_discovery() really scans. It defaults to false. The Orca agent returns true. The Bambu agent returns true only while the network plug-in is loaded, because its discovery runs inside the plug-in. The Moonraker family does not scan: it reports the host the user configured in the preset. A Python agent opts in by overriding discovers_on_lan(); without it, the agent discovers only while it is the active agent, as before.

Every registered printer agent exists up front. NetworkAgentFactory::create_printer_agents() creates the built-in ones when GUI_App::on_init_network() builds networking, and hands each cached built-in agent the rebuilt cloud agent when networking is rebuilt; a Python agent is put in the cache when its plugin registers it, since its capability object is the agent. NetworkAgentFactory::get_discovery_candidates() only reads that cache, so an agent that scans discovers without a preset having selected it, and discovers_on_lan() is the only thing that says whether it scans. Creating the agents is cheap: the built-in constructors do no I/O and start no threads.

The cache also records which agents create_printer_agent_by_id() handed out to be selected. Only those are disconnected when the cache lets an agent go, at shutdown or when a Python plugin is reloaded or unloaded, since the others never connected.

NetworkAgent::sync_discovery() takes those agents (from the factory's cache, so the active agent and the discovering agent are the same object) and makes the discovering set: each agent that scans and is not disabled, plus the active agent. Agents that join are started, agents that leave are stopped, and the others are left running, so an agent switch never stops or re-registers another agent's discovery. GUI_App::refresh_discovery_agents() calls it at startup, after an agent switch, after the network plug-in is reloaded, and when a Python printer agent is registered or deregistered.

Each discovering agent has its own SSDP callback, which carries the agent's id, and its own session token. A callback only queues work onto the UI thread while its session is alive, and the queued work checks the session again, so nothing from an agent fires after it was stopped, replaced, deregistered or its NetworkAgent destroyed. Shutdown clears the listener first and then stops every discovering agent.

The disabled_discovery_agents app setting turns a scanning agent's background discovery off. It holds agent ids joined by ; and is empty by default, so every scanning agent discovers. It does not affect an active agent that only reports its configured host. Preferences shows it on the Online tab under Printer discovery as one checkbox per agent whose discovers_on_lan() is true, checked while it discovers: the Orca agent, the Bambu agent while its network plug-in is loaded, and any Python agent that opts in.

DeviceManager::on_machine_alive() receives the id of the agent that found the device and uses it instead of the active agent. A discovery never takes over a device that another agent owns, whether the device is in the local list or saved in the app config; it is logged and ignored. A device with no owner takes the finder as its owner. Changing the owner is deliberate and goes through DeviceManager::insert_local_device(), called for the agent the user selected. clear_other_devices() removes only devices with no owner, so an agent switch keeps what the other agents discovered.

Device ownership and stale responses

Printer-agent ownership is represented by printer_agent_id on device records and MachineObject instances. The finding agent's ID is attached when a device is discovered, the active agent's ID when one is returned by a cloud list, and the active agent's ID replaces the owner when the user selects the device through that agent. Local-machine configuration also persists the agent ID so a saved LAN device is not silently reused by an unrelated agent.

Cloud printer-list responses carry three pieces of request context added by NetworkAgent:

provider   cloud provider used for the request
agent_id   active printer agent when the request was made
generation machine-list generation when the request was made

DeviceManager accepts the response only when those values still match the current provider, active agent, and generation. This prevents a slow response from the previous preset or provider from repopulating the current device list.

The provider mapping is currently selected by GUI_App: the Bambu agent maps to the Bambu cloud provider and other agents map to the Orca cloud provider. The generation check protects that existing selection from races; it does not make cloud-provider ownership intrinsic to an agent. Cloud-printer ownership and the broader Orca cloud services are therefore still separate architectural concerns.

Printer model identity

Model identity is separate from printer-agent ownership. MachineObject::printer_type can contain a saved or preset-derived value, but printer_model_advertised marks whether the model was actually reported by the printer. Saved and manually supplied values are not proof of the connected model. Agents must provide a printer-reported model in binding and discovery responses, or leave it empty; Moonraker also accepts a model reported by /server/info.

DevPrinterConfigUtil resolves reported IDs and names using installed vendor models and legacy protocol aliases to obtain a canonical model ID; ambiguous aliases are not resolved. An empty report makes no new model claim, while a nonempty unrecognized report leaves the model unknown. Compatibility checks compare the canonical ID with the selected preset's model ID, while retaining the existing compatible-machine and upgrade-kit rules; an installed upgrade kit changes the shown model used for comparison, not the advertised identity. A missing or unrecognized model is optional: the user is warned, but model compatibility does not block printing. Model sync does not choose a printer or nozzle preset for an unknown model, nor an arbitrary nozzle variant for a recognized model.

Python printer agents

PrinterAgentPluginCapability implements IPrinterAgent directly. The live capability object is registered with NetworkAgentFactory and handed out as the printer agent when its agent ID is selected. The plugin receives the selected ICloudServiceAgent through set_cloud_agent() just like a built-in printer agent.

Python plugins must implement the core communication and lifecycle methods required by the interface, including agent metadata, printer connection, discovery callbacks, and the two message-send methods. Methods that are meaningful only to a particular printer are optional overrides where the C++ base class provides a default.

All ten command_* methods are available in the Python binding and in the trampoline. Their override status is intentionally optional:

  • the five generic commands use the C++ default when Python does not override them;
  • the five vendor-specific commands return NOT_SUPPORTED unless Python supplies an implementation;
  • a Python implementation can replace either behavior for its own protocol.

The Python camera binding exposes HTTP, HTTPS, RTSP, and HTTP-snapshot modes. WebRTC remains a built-in C++ camera mode, but is not exposed as a Python mode because the current Python capability does not provide the corresponding cloud signaling-channel contract.

Camera playback boundary

The camera stream mode describes how a stream is obtained; it does not by itself define ownership of the wxWidgets view that renders it. MediaPlayCtrl selects and tears down the active backend, while the wx parent owns the child window or renderer. This is important because a web view, native media control, and frame-based/WebRTC renderer have different wx window-lifetime requirements.

Cloud URL and signaling requests are routed through NetworkAgent to the cloud provider selected for the device. Local URL requests are routed to the active printer agent. The distinction keeps cloud account services device-scoped while preserving the current one-LAN-agent/one-printer model.

Compatibility constraints

The printer-agent boundary intentionally preserves several existing application contracts:

  • Bambu-shaped JSON is still the shared command representation;
  • existing network status codes are reused, with Orca-specific unsupported/capability errors added in the Orca-reserved range;
  • MachineObject remains the shared device-state model;
  • preset and local-machine data retain compatibility with the existing agent-selection behavior;
  • Python plugins use the existing capability and pybind11 registration system.

The agent abstraction is therefore responsible for containing vendor differences, not for pretending that all vendor protocols are identical. The planned Orca JSON dialect is the protocol-neutral command model for the DeviceManager/agent boundary. Once it is introduced, Bambu-specific translation remains inside the Bambu agent and the Orca agent's v1 sink adapter can be removed as a self-contained cleanup.

Main implementation locations