mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-05 14:51:06 +00:00
* Add developer flag for printer agents
* Parse user print info on the UI thread to prevent heap corruption (#119)
get_user_print_info()'s HTTP fetch can run on a worker thread (e.g. BindJob),
but parse_user_print_info() mutates userMachineList (insert/erase/delete
MachineObject). on_machine_alive (SSDP) mutates the same maps on the UI thread
without locking, so parsing off-thread races the map and frees MachineObjects
out from under it -> heap corruption.
Keep all device-list mutation on the UI thread: parse inline when already on
the main thread, otherwise marshal via CallAfter so it stays serialized with
on_machine_alive.
* Prevent loss of user access code on LAN reselect
Keep user access code intact to maintain access rights even if
device slot is unpopulated, ensuring continuous connection
and status message reception.
* Harden send flow and separate upload failure recovery (#111)
* fix(send): harden FT send path + IP pre-flight UX
* Remove early returns
* Working Moonraker and Qidi printer agent transport (#104)
Folds the Qidi AMS box-mapping print
overrides (apply_box_mapping +
start_* wrappers) that the transport
fix builds on.
* Add support for runtime error status in plugins
Distinguish a loaded plugin whose
capability errored (RuntimeError,
warn-styled, stays checked) from a
load-time Error. Status now derives
via resolve_plugin_status(); enum
ordinal keeps dialog sort priority.
Unloading clears stale errors.
* Resolve duplicate agent ID conflicts
Reject a printer-agent capability
whose agent ID is already owned by
another capability or built-in:
flag the plugin error, disable the
capability, and warn the user
instead of silently ignoring it.
* fix: checkbox should depend on plugin is_loaded status
* Replace fake-enum printer agent dropdown (#121)
A dedicated PrinterAgentChoice field
reads rows straight from the live
agent registry and stores the agent
id string, replacing the fake-coEnum
index mapping. The field moves to
TabPrinter and registers with the
searcher so UnsavedChanges renders
it; the PhysicalPrinterDialog copy
and its update hook are removed
(#125). switch_printer_agent now
resolves ids via
resolve_printer_agent_id.
* Reset device selection on agent swap or unload (#124)
set_live_printer_agent centralizes
the swap: deselect the machine,
clear stale sidebar state and the
previous agent's Other Devices, then
install the new agent (or null when
its provider vanished). Plugin
load/unload callbacks refresh the
dropdown and re-run agent selection.
load_last_machine no longer falls
back to the first available machine.
* 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.
* Track BBLPrinterAgentPlugin.py
* Add printer-agent and plugin status tests
Ports the agent lifecycle, duplicate
agent-id, built-in-id clash and
status-resolution tests. The loader
runs on a detached worker thread, so
the lifecycle tests live in their own
executable. Tests install the
production unload-side registry
wiring themselves (no GUI in the
test binary) and register agents
manually so concurrent loads stay
deterministic.
* fix: pin HTTP to prevent connection refusal
Set `use_ssl` to false to ensure Moonraker
connectivity, as the service uses HTTP rather
than HTTPS, preventing connection issues. Initialize
device info early for reliable name resolution.
* Bring Moonraker device panel to feature parity
The monitor panel showed wrong or missing
data for Moonraker printers, and its
controls did nothing.
Push payload now carries layer number and
total layers. Remaining time replaces the
wrong total_duration - print_duration
formula. The chamber light toggle maps to
Klipper SET_PIN / SET_LED, and pause,
resume and stop post to
/printer/print/{action}. Task thumbnails
resolve via /server/files/thumbnails onto
a new MachineObject thumbnail url.
Filament sync switches to pull mode so the
agent is queried on demand.
Not compiled or run.
* Stop blocking print on unreported nozzle data
* fix: make Klipper macro lamp control reliable
* Surface Moonraker webcams and gate unrunnable controls
* Keep Bambu AMS dialect out of the agent waist
M620 is Bambu firmware dialect, not a
neutral command. Composing it in
MachineObject let non-Bambu agents
(Moonraker/Klipper) forward it and
report success on firmware that
cannot run it.
Agents now own the dialect: the
default refusal on IPrinterAgent
returns not-supported so the UI
can say so; BBLPrinterAgent keeps
the byte-identical composition.
* Fix multi-color filament logic
Reuse color decoding across functions to improve
code readability and maintain consistency in
multi-color filament handling.
* Move Moonraker commands off the UI thread
Pause/resume/stop, g-code sends, temps, and
light ran synchronous HTTP on the UI thread,
freezing the app up to 10s per click on slow
or unreachable printers.
Run them on a single agent-owned FIFO worker
so g-code ordering is preserved, while command
translation stays synchronous so unsupported-
command dialogs still work.
Add a pending-disabled state to the pause,
resume, and abort buttons for Moonraker-family
printers: the icon only flips once the
WebSocket reports the real state, which also
rules out double-click races.
* Show Snapmaker U1 camera in Device tab
The U1 exposes no /server/webcams/list entry;
its camera only captures after an explicit
camera.start_monitor RPC, which the Moonraker
websocket executes unauthenticated but only
answers over MQTT - so the call is fire and
forget.
Start the camera when the camera view is
shown and renew every 300 s: the printer
retires the capture task at ~362 s and
stop_monitor is accepted but ineffective,
so teardown is simply to stop renewing.
Frames land in monitor.jpg as still JPEGs
(~2 fps at interval 0), so the webview loads
a local HTML wrapper that repolls with a
cache buster.
* fix: start stream when camera URL changes
* fix: stop Qidi slot parse throwing on null
* Keep printer-agent progress in sync
Keep the shared task progress aligned with agent
reports that lack Bambu cloud task identity.
Release the lazily allocated task during reset to
avoid leaks when machine objects reconnect.
* docs: document the printer-agent subsystem
* fix: merge access codes into one
* Reconcile implementation split with PR tip
* feat: abstract remaining gcode commands in devicemanager
* refactor: abstract bambu specific protocol to printer agent
* refactor: push bbl workflows to bbl printer agent
* remove unused
* fix: default impl
* fix callback error
* fix: remove redundant cache
* specify api for getting file transfer url
* revert file transfer abstraction
* fix: ams filament mapping workflow
* feat: update qidi to use subscription based filament sync mode
* fix: resolve stubgen byte header conflict
* fix: ams sync info and periodic ams sync via subscription workflow
* fix: skip filament sync dialog if filamentSyncMode is none
* feat: parse nozzle information for qidi and moonraker printer agents
* fix: extend access code requirements t 0, 8 or more characters.
* remove irrelevant docs
* fix: remove heavy includes from IPrinterAgent
* fix: defer filesystem and camera abstractions
* fix: remote do_fetch_filament_info from tests
* cleanup moonraker and snapmaker printer agents
* fix: access codes regression
* fix: tests
* fix: printer agent switching on preset change
* fix: remove unused variable
* fix: snapmaker U1 SelectMachineDialog blocking print
* fix: merge artifact
* fix: clear up some unrelated changes
* feat: connect to cloud printer and monitor
* feat: connect to cloud printer and monitor
* feat: generic camera stream support for http snapshot and rtsp
* fix: build & access code UI
* feat: generic camera stream support for http snapshot and rtsp
* fix: build & access code UI
* feat: connect to cloud printer and monitor
* feat: connect to cloud printer and monitor
* fix: build errors
* feat: camera via webrtc
* fix: build
* fix: cmake
* feat: remove frame assembler and change config to set protocol
* fix: orcaprinteragent refactor
* fix: LAN paths and camera stream
* feat: use ffmpeg to render http camera stream
* fix: make model_id/dev_type optional instead of blocking
* fix: connect via ip dialog
* feat: LAN impl for Orca Printer Agent
* fix: model_id resolution method for non bambu printers
* fix: ffmpeg http camera stream jittering due to incomplete frames
* fix: revert sdcard check
* feat: check printer storage status before sending
* fix: moonraker printer agent hang on printer power cut
* fix: shim layer for any compatibiliity changes
* fix: cloud printers were using the wrong MQTT endpoint
* feat: cloud download via HTTP
* temp: doc for intended change
* fix: warnings
* fix: warnings
* fix: camera auto-play on startup
* fix: split infra from impl
* fix: uninitialized ams state blocking print
* Fixes nullptr deref
* Log first before std::move
* fix: printer agent virutal optional functions
* fix: parameterize orcaslicer_copy_test_dlls() for printer_agent_plugin_tests
* Revert "fix: parameterize orcaslicer_copy_test_dlls() for printer_agent_plugin_tests"
This reverts commit 2f566e3779.
* Guard libdatachannel. Remove unused code
* fix: unit tests & unused variables
* fix(ci): deps build order for datachannel
* Resolve printer agent first before getting cloud printer agent
* fix(ci): set depends openssl
* fix: re-include apply header guarded by ifdef __APPLE__
* fix(ci): add libdatachannel to flatpak manifest
* fix: add internal_developer_mode chekc back to MediaPlayCtrl::load()
* fix: invoke js clearInterval on WebMediaController::stop
* fix: change rtc log level
* fix: inject provider, agent id and generation to get_user_print_info to ensure correct metadata
* fix: revert moonraker specific behavior
* fix: remove stale comment
* fix: use ORCA_CLOUD_PROVIDER instead of hardcoded string
* fix: remove hardcoded ICE servers
* feat: enable https camera stream mode
* fix: move non-mandatory printer agent function stubs to IPrinterAgent
* fix: dedupe compatible printer type check
* fix: stop the correct media controller
* fix: scope get_my_machine_list to printers listed under the current printer agent
* fix: move printer agent plugin tests into test_plugin_lifecycle.cpp
* fix: always build bundled DataChannel dep
* fix: disable unused DataChannel media support
* revert: filament sync work
* fix: wrap command_* with small wrapper
* fix: regression bug, connecting to bambu needs bblp username
* fix: default impl for vendor agnostic gcode commansd
* refactor: media controller playback routing and ownership
* fix: bump libdatachannel ver & update flatpak to use tar instead
* fix: stop flatpak DataChannel build from re-cloning over the sandboxed network
* fix: update windows ffmpeg prebuild
* fix: update printer agent plugin API
* fix: shift camera signaling channel to network agent
* fix: follow external-packages for flatpak libdatachannel deps & add flatpak path to use source_dir
* feat: add printer-agent.md doc to HLSD
* fix: guard DeviceManager command dispatch when no printer agent is bound
* fix: port BBL implementations from #15711
* refactor: connect_printer api and dialog
* fix(tests): make omitted printer agent operations answer like a missing agent
* fix: preserve printer agent defaults in PrinterAgentPluginCapabilityTrampoline
* test: cover printer agent default command dispatch
* fix: validate windows FFmpeg avformat library
* feat: extend optional printer model warnings to calibration & ams workflows
* fix: handle malformed printer progress values safely
* fix: clear webview document on stop
* chore: reduce diagnostic logging level to trace
* refactor: centralize printer compatibility checks
* tests: add device manager integration coverage
* tests: cover WebMediaController lifecycle with wxWebView stub
* fix: make integration tests headless
* refactor: collapse command_ams_refresh_rfid and command_ams_refresh_rfid2
* refactor: make printer connection SSL agent-specific
* fix: persist input printer host and port
* fix: use correct device id for Moonraker connections
* fix: make moonraker gcode commands asynchronous
* fix: preserve moonraker device names
* fix: add include for non BBL_RELEASE_TO_PUBLIC path in BBLPrinterAgent
---------
Co-authored-by: Andrew <159703254+andrewsoonqn@users.noreply.github.com>
Co-authored-by: SoftFever <softfeverever@gmail.com>
Co-authored-by: Lam Wei Lun <weilun.lam@gmail.com>
1176 lines
48 KiB
C++
1176 lines
48 KiB
C++
#include "DevManager.h"
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <exception>
|
|
|
|
#include <libslic3r/AppConfig.hpp>
|
|
#include "CloudProvider.hpp"
|
|
#include "DevUtil.h"
|
|
|
|
// TODO: remove this include
|
|
#include "slic3r/GUI/DeviceManager.hpp"
|
|
#include "slic3r/GUI/I18N.hpp"
|
|
#include "slic3r/GUI/GUI_App.hpp"
|
|
#include "slic3r/GUI/Plater.hpp"
|
|
#include "slic3r/plugin/PluginManager.hpp"
|
|
#include "slic3r/Utils/NetworkAgentFactory.hpp"
|
|
|
|
#include "libslic3r/Time.hpp"
|
|
|
|
#include "IPrinterAgent.hpp"
|
|
|
|
using namespace nlohmann;
|
|
|
|
namespace {
|
|
// Orca: access_code lives on BBLocalMachine::access_code (keyed by dev_id via
|
|
// get_local_machines(), scoped by the record's own printer_agent_id field) - so binding a
|
|
// printer under one agent doesn't silently appear as already-bound under a different,
|
|
// independent agent. This only covers LAN devices (BBLocalMachine's own scope); access_code
|
|
// and user_access_code used to be the only, flat dev_id-only AppConfig keys before
|
|
// BBLocalMachine::access_code existed, and codes saved back then are still stored flat (no
|
|
// agent association at all). Since BBL was the only agent that existed at the time, honor
|
|
// those flat legacy keys as implicitly BBL's - but only for the BBL agent, so they aren't
|
|
// leaked to other agents that never bound the device themselves.
|
|
std::string get_access_code_with_legacy_fallback(Slic3r::AppConfig* config, const std::string& dev_id, const std::string& agent_id)
|
|
{
|
|
const auto& machines = config->get_local_machines();
|
|
auto it = machines.find(dev_id);
|
|
if (it != machines.end() && it->second.printer_agent_id == agent_id && !it->second.access_code.empty())
|
|
return it->second.access_code;
|
|
|
|
if (agent_id == Slic3r::BBL_PRINTER_AGENT_ID || agent_id.empty()) {
|
|
std::string code = config->get("access_code", dev_id);
|
|
if (code.empty())
|
|
code = config->get("user_access_code", dev_id);
|
|
return code;
|
|
}
|
|
return "";
|
|
}
|
|
}
|
|
|
|
namespace Slic3r
|
|
{
|
|
DeviceManager::DeviceManager(NetworkAgent* agent, bool enable_refresher, AppConfig* app_config)
|
|
{
|
|
m_agent = agent;
|
|
m_app_config = app_config;
|
|
if (enable_refresher)
|
|
m_refresher = new DeviceManagerRefresher(this);
|
|
|
|
DevPrinterConfigUtil::InitFilePath(resources_dir());
|
|
|
|
// Load saved local machines (needs an agent; when built without one the load is
|
|
// deferred to set_agent()).
|
|
if (agent) {
|
|
load_local_machines_from_config();
|
|
}
|
|
}
|
|
|
|
AppConfig* DeviceManager::get_app_config() const
|
|
{
|
|
return m_app_config ? m_app_config : GUI::wxGetApp().app_config;
|
|
}
|
|
|
|
void DeviceManager::load_local_machines_from_config()
|
|
{
|
|
AppConfig* config = get_app_config();
|
|
if (!config)
|
|
return;
|
|
const auto local_machines = config->get_local_machines();
|
|
for (auto& it : local_machines) {
|
|
const auto& m = it.second;
|
|
if (localMachineList.count(m.dev_id))
|
|
continue;
|
|
MachineObject* obj = new MachineObject(this, m_agent, m.dev_name, m.dev_id, m.dev_ip);
|
|
obj->printer_type = m.printer_type;
|
|
obj->printer_agent_id = m.printer_agent_id;
|
|
obj->dev_connection_type = "lan";
|
|
obj->bind_state = "free";
|
|
obj->bind_sec_link = "secure";
|
|
obj->m_is_online = true;
|
|
obj->last_alive = Slic3r::Utils::get_current_time_utc();
|
|
obj->set_access_code(get_access_code_with_legacy_fallback(config, m.dev_id, obj->printer_agent_id), false);
|
|
if (obj->has_access_right()) {
|
|
localMachineList.insert(std::make_pair(m.dev_id, obj));
|
|
} else {
|
|
config->erase_local_machine(m.dev_id);
|
|
delete obj;
|
|
}
|
|
}
|
|
}
|
|
|
|
void DeviceManager::update_local_machine(const MachineObject& m, AppConfig* config)
|
|
{
|
|
if (config) {
|
|
if (m.is_lan_mode_printer()) {
|
|
if (m.has_access_right()) {
|
|
BBLocalMachine local_machine;
|
|
local_machine.dev_id = m.get_dev_id();
|
|
local_machine.dev_name = m.get_dev_name();
|
|
local_machine.dev_ip = m.get_dev_ip();
|
|
local_machine.printer_type = m.printer_type;
|
|
local_machine.printer_agent_id = m.printer_agent_id;
|
|
local_machine.access_code = m.get_access_code();
|
|
config->update_local_machine(local_machine);
|
|
}
|
|
} else {
|
|
config->erase_local_machine(m.get_dev_id());
|
|
}
|
|
}
|
|
}
|
|
|
|
DeviceManager::~DeviceManager()
|
|
{
|
|
if (m_refresher)
|
|
delete m_refresher;
|
|
|
|
for (auto it = localMachineList.begin(); it != localMachineList.end(); it++)
|
|
{
|
|
if (it->second)
|
|
{
|
|
delete it->second;
|
|
it->second = nullptr;
|
|
}
|
|
}
|
|
localMachineList.clear();
|
|
|
|
for (auto it = userMachineList.begin(); it != userMachineList.end(); it++)
|
|
{
|
|
if (it->second)
|
|
{
|
|
delete it->second;
|
|
it->second = nullptr;
|
|
}
|
|
}
|
|
userMachineList.clear();
|
|
}
|
|
|
|
void DeviceManager::set_agent(NetworkAgent* agent)
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": updating agent for "
|
|
<< localMachineList.size() << " local and "
|
|
<< userMachineList.size() << " user machines";
|
|
|
|
// First real agent after an agent-less construction (network plugin wasn't ready at
|
|
// startup): run the persisted-LAN-printer load the constructor had to skip. See
|
|
// load_local_machines_from_config().
|
|
const bool first_real_agent = (m_agent == nullptr && agent != nullptr);
|
|
m_agent = agent;
|
|
|
|
std::lock_guard<std::mutex> lock(listMutex);
|
|
for (auto& it : localMachineList) {
|
|
if (it.second) {
|
|
it.second->set_agent(agent);
|
|
}
|
|
}
|
|
for (auto& it : userMachineList) {
|
|
if (it.second) {
|
|
it.second->set_agent(agent);
|
|
}
|
|
}
|
|
|
|
if (first_real_agent) {
|
|
load_local_machines_from_config();
|
|
}
|
|
}
|
|
|
|
std::string DeviceManager::get_current_printer_agent_id() const
|
|
{
|
|
if (!m_agent)
|
|
return "";
|
|
auto printer_agent = m_agent->get_printer_agent();
|
|
return printer_agent ? printer_agent->get_agent_info().id : "";
|
|
}
|
|
|
|
std::string DeviceManager::get_current_cloud_provider() const
|
|
{
|
|
const std::string agent_id = get_current_printer_agent_id();
|
|
if (!agent_id.empty())
|
|
return agent_id == BBL_PRINTER_AGENT_ID ? BBL_CLOUD_PROVIDER : ORCA_CLOUD_PROVIDER;
|
|
return GUI::wxGetApp().get_printer_cloud_provider();
|
|
}
|
|
|
|
void DeviceManager::EnableMultiMachine(bool enable)
|
|
{
|
|
m_agent->enable_multi_machine(enable);
|
|
m_enable_mutil_machine = enable;
|
|
}
|
|
|
|
void DeviceManager::start_refresher() { if (m_refresher) m_refresher->Start(); }
|
|
void DeviceManager::stop_refresher() { if (m_refresher) m_refresher->Stop(); }
|
|
|
|
|
|
void DeviceManager::keep_alive()
|
|
{
|
|
MachineObject* obj = this->get_selected_machine();
|
|
if (obj)
|
|
{
|
|
if (obj->keep_alive_count == 0)
|
|
{
|
|
obj->last_keep_alive = std::chrono::system_clock::now();
|
|
}
|
|
obj->keep_alive_count++;
|
|
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
|
|
auto internal = std::chrono::duration_cast<std::chrono::milliseconds>(start - obj->last_keep_alive);
|
|
if (internal.count() > TIMEOUT_FOR_KEEPALIVE && (internal.count() < 1000 * 60 * 60 * 300))
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << "keep alive = " << internal.count() << ", count = " << obj->keep_alive_count;
|
|
obj->command_request_push_all();
|
|
obj->last_keep_alive = start;
|
|
}
|
|
else if (obj->m_push_count == 0)
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << "keep alive = " << internal.count() << ", push_count = 0, count = " << obj->keep_alive_count;
|
|
obj->command_request_push_all();
|
|
obj->last_keep_alive = start;
|
|
}
|
|
}
|
|
}
|
|
|
|
void DeviceManager::check_pushing()
|
|
{
|
|
keep_alive();
|
|
MachineObject* obj = this->get_selected_machine();
|
|
if (!obj) {
|
|
BOOST_LOG_TRIVIAL(warning) << "DeviceManager::check_pushing selected machine not found";
|
|
return;
|
|
}
|
|
|
|
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
|
|
auto internal = std::chrono::duration_cast<std::chrono::milliseconds>(start - obj->last_update_time);
|
|
|
|
if (!obj->is_support_mqtt_alive)
|
|
{
|
|
if (internal.count() > TIMEOUT_FOR_STRAT && internal.count() < 1000 * 60 * 60 * 300)
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << "command_pushing: diff = " << internal.count();
|
|
obj->command_pushing("start");
|
|
}
|
|
}
|
|
}
|
|
|
|
void DeviceManager::on_machine_alive(std::string json_str)
|
|
{
|
|
try {
|
|
json j = json::parse(json_str);
|
|
std::string dev_name = j["dev_name"].get<std::string>();
|
|
std::string dev_id = j["dev_id"].get<std::string>();
|
|
std::string dev_ip = j["dev_ip"].get<std::string>();
|
|
std::string printer_type_str= j["dev_type"].get<std::string>();
|
|
std::string printer_signal = j["dev_signal"].get<std::string>();
|
|
std::string connect_type = j["connect_type"].get<std::string>();
|
|
std::string bind_state = j["bind_state"].get<std::string>();
|
|
|
|
if (connect_type == "farm") {
|
|
connect_type ="lan";
|
|
bind_state = "free";
|
|
}
|
|
|
|
std::string sec_link = "";
|
|
std::string ssdp_version = "";
|
|
if (j.contains("sec_link")) {
|
|
sec_link = j["sec_link"].get<std::string>();
|
|
}
|
|
if (j.contains("ssdp_version")) {
|
|
ssdp_version = j["ssdp_version"].get<std::string>();
|
|
}
|
|
std::string connection_name = "";
|
|
if (j.contains("connection_name")) {
|
|
connection_name = j["connection_name"].get<std::string>();
|
|
}
|
|
|
|
MachineObject* obj;
|
|
|
|
/* update userMachineList info */
|
|
auto it = userMachineList.find(dev_id);
|
|
if (it != userMachineList.end()) {
|
|
// A reused entry may have been created while another printer agent was active.
|
|
// The response was obtained through the current agent, so move ownership with
|
|
// the entry; otherwise agent-scoped lists hide it after a preset switch.
|
|
it->second->printer_agent_id = get_current_printer_agent_id();
|
|
if (it->second->get_dev_ip() != dev_ip ||
|
|
it->second->bind_state != bind_state ||
|
|
it->second->bind_sec_link != sec_link ||
|
|
it->second->dev_connection_type != connect_type ||
|
|
it->second->bind_ssdp_version != ssdp_version)
|
|
{
|
|
if (it->second->bind_state != bind_state) {
|
|
OnMachineBindStateChanged(it->second, bind_state);
|
|
}
|
|
|
|
it->second->set_dev_ip(dev_ip);
|
|
it->second->bind_state = bind_state;
|
|
it->second->bind_sec_link = sec_link;
|
|
it->second->dev_connection_type = connect_type;
|
|
it->second->bind_ssdp_version = ssdp_version;
|
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " UpdateUserMachineInfo"
|
|
<< ", dev_id= " << dev_id
|
|
<< ", ip = " <<dev_ip
|
|
<< ", printer_name= " << dev_name
|
|
<< ", con_type= " << connect_type << ", signal= " << printer_signal
|
|
<< ", bind_state= " << bind_state;
|
|
}
|
|
}
|
|
|
|
/* update localMachineList */
|
|
it = localMachineList.find(dev_id);
|
|
AppConfig* config = get_app_config();
|
|
|
|
if (it != localMachineList.end()) {
|
|
// update properties
|
|
/* ip changed */
|
|
obj = it->second;
|
|
// A reused LAN entry may have been discovered while another printer agent was
|
|
// active. The current discovery message establishes ownership for this agent.
|
|
obj->printer_agent_id = get_current_printer_agent_id();
|
|
|
|
if (obj->get_dev_ip().compare(dev_ip) != 0) {
|
|
if ( connection_name.empty() ) {
|
|
BOOST_LOG_TRIVIAL(info) << "MachineObject IP changed from " << obj->get_dev_ip()
|
|
<< " to " << dev_ip;
|
|
obj->set_dev_ip(dev_ip);
|
|
}
|
|
else {
|
|
if ( obj->dev_connection_name.empty() || obj->dev_connection_name.compare(connection_name) == 0) {
|
|
BOOST_LOG_TRIVIAL(info) << "MachineObject IP changed from " << obj->get_dev_ip()
|
|
<< " to " << dev_ip << " connection_name is " << connection_name;
|
|
if(obj->dev_connection_name.empty()){obj->dev_connection_name = connection_name;}
|
|
obj->set_dev_ip(dev_ip);
|
|
}
|
|
}
|
|
/* ip changed reconnect mqtt */
|
|
}
|
|
|
|
if (obj->wifi_signal != printer_signal ||
|
|
obj->dev_connection_type != connect_type ||
|
|
obj->bind_state != bind_state ||
|
|
obj->bind_sec_link != sec_link ||
|
|
obj->bind_ssdp_version != ssdp_version ||
|
|
obj->printer_type != _parse_printer_type(printer_type_str))
|
|
{
|
|
if (obj->dev_connection_type != connect_type ||
|
|
obj->bind_state != bind_state ||
|
|
obj->bind_sec_link != sec_link ||
|
|
obj->bind_ssdp_version != ssdp_version ||
|
|
obj->printer_type != _parse_printer_type(printer_type_str))
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " UpdateLocalMachineInfo"
|
|
<< ", dev_id= " << dev_id
|
|
<< ", ip = " << dev_ip
|
|
<< ", printer_name= " << dev_name
|
|
<< ", con_type= " << connect_type << ", signal= " << printer_signal
|
|
<< ", bind_state= " << bind_state;
|
|
}
|
|
else
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " UpdateLocalMachineInfo_WIFI"
|
|
<< ", dev_id= " << dev_id
|
|
<< ", ip = " << dev_ip
|
|
<< ", printer_name= " << dev_name
|
|
<< ", con_type= " << connect_type << ", signal= " << printer_signal
|
|
<< ", bind_state= " << bind_state;
|
|
}
|
|
|
|
obj->wifi_signal = printer_signal;
|
|
obj->dev_connection_type = connect_type;
|
|
obj->bind_state = bind_state;
|
|
obj->bind_sec_link = sec_link;
|
|
obj->bind_ssdp_version = ssdp_version;
|
|
obj->printer_type = _parse_printer_type(printer_type_str);
|
|
}
|
|
|
|
// U0 firmware
|
|
if (obj->dev_connection_type.empty() && obj->bind_state.empty())
|
|
obj->bind_state = "free";
|
|
|
|
obj->last_alive = Slic3r::Utils::get_current_time_utc();
|
|
// Route through set_online_state() (rather than writing m_is_online directly) so the
|
|
// DeviceOnline lifecycle event fires consistently; same effective value/behavior
|
|
// here since the object was already online in the common case.
|
|
obj->set_online_state(true);
|
|
obj->set_dev_name(dev_name);
|
|
/* if (!obj->dev_ip.empty()) {
|
|
Slic3r::GUI::wxGetApp().app_config->set_str("ip_address", obj->dev_id, obj->dev_ip);
|
|
Slic3r::GUI::wxGetApp().app_config->save();
|
|
}*/
|
|
}
|
|
else {
|
|
/* insert a new machine */
|
|
obj = new MachineObject(this, m_agent, dev_name, dev_id, dev_ip);
|
|
obj->printer_type = _parse_printer_type(printer_type_str);
|
|
obj->printer_agent_id = get_current_printer_agent_id();
|
|
obj->wifi_signal = printer_signal;
|
|
obj->dev_connection_type = connect_type;
|
|
obj->bind_state = bind_state;
|
|
obj->bind_sec_link = sec_link;
|
|
obj->dev_connection_name = connection_name;
|
|
obj->bind_ssdp_version = ssdp_version;
|
|
// Discovery establishes the initial reachability state. Do not report it as an
|
|
// online transition; DeviceDiscovered below is the lifecycle event for a new
|
|
// device. Subsequent updates route through set_online_state(), so a known device
|
|
// still emits DeviceOnline/DeviceOffline when its reachability actually changes.
|
|
obj->m_is_online = true;
|
|
|
|
//load access code
|
|
if (config) {
|
|
obj->set_access_code(get_access_code_with_legacy_fallback(config, dev_id, obj->printer_agent_id), false);
|
|
}
|
|
localMachineList.insert(std::make_pair(dev_id, obj));
|
|
|
|
/* if (!obj->dev_ip.empty()) {
|
|
Slic3r::GUI::wxGetApp().app_config->set_str("ip_address", obj->dev_id, obj->dev_ip);
|
|
Slic3r::GUI::wxGetApp().app_config->save();
|
|
}*/
|
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " New Machine, dev_id= " << dev_id
|
|
<< ", ip = " << dev_ip <<", printer_name = " << dev_name
|
|
<< ", con_type= " << connect_type <<", signal= " << printer_signal << ", bind_state= " << bind_state;
|
|
|
|
// First discovery of a genuinely new device (not a periodic SSDP/heartbeat update to
|
|
// an already-known one, which is handled in the branch above).
|
|
{
|
|
LifecycleEventContext ctx;
|
|
ctx.name = dev_id;
|
|
ctx.code = LifecycleEvtCode::Ok;
|
|
fire_lifecycle_event(LifecycleEvent::DeviceDiscovered, ctx);
|
|
}
|
|
}
|
|
update_local_machine(*obj, config);
|
|
}
|
|
catch (...) {
|
|
;
|
|
}
|
|
}
|
|
|
|
MachineObject* DeviceManager::insert_local_device(const BBLocalMachine& machine,
|
|
std::string connection_type, std::string bind_state,
|
|
std::string version, std::string access_code)
|
|
{
|
|
MachineObject* obj;
|
|
auto it = localMachineList.find(machine.dev_id);
|
|
if (it != localMachineList.end()) {
|
|
obj = it->second;
|
|
// insert_local_device is called by the active agent, so a reused entry must follow
|
|
// that agent as well; otherwise the agent-scoped printer list hides it.
|
|
obj->printer_agent_id = get_current_printer_agent_id();
|
|
} else {
|
|
obj = new MachineObject(this, m_agent, machine.dev_name, machine.dev_id, machine.dev_ip);
|
|
obj->printer_agent_id = get_current_printer_agent_id();
|
|
localMachineList.insert(std::make_pair(machine.dev_id, obj));
|
|
}
|
|
if (machine.printer_type.empty())
|
|
obj->printer_type = _parse_printer_type("C11");
|
|
else
|
|
obj->printer_type = _parse_printer_type(machine.printer_type);
|
|
obj->dev_connection_type = connection_type == "farm" ? "lan":connection_type;
|
|
obj->bind_state = connection_type == "farm" ? "free":bind_state;
|
|
obj->bind_sec_link = "secure";
|
|
obj->bind_ssdp_version = version;
|
|
obj->m_is_online = true;
|
|
obj->last_alive = Slic3r::Utils::get_current_time_utc();
|
|
obj->set_access_code(access_code, false);
|
|
|
|
update_local_machine(*obj, get_app_config());
|
|
|
|
return obj;
|
|
}
|
|
|
|
void DeviceManager::update_local_machine(const MachineObject& m)
|
|
{
|
|
update_local_machine(m, GUI::wxGetApp().app_config);
|
|
}
|
|
|
|
int DeviceManager::query_bind_status(std::string& msg, const std::string& provider)
|
|
{
|
|
if (!m_agent)
|
|
{
|
|
msg = "";
|
|
return -1;
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(trace) << "DeviceManager::query_bind_status";
|
|
std::map<std::string, MachineObject*>::iterator it;
|
|
std::vector<std::string> query_list;
|
|
for (it = localMachineList.begin(); it != localMachineList.end(); it++)
|
|
{
|
|
query_list.push_back(it->first);
|
|
}
|
|
|
|
unsigned int http_code;
|
|
std::string http_body;
|
|
int result = m_agent->query_bind_status(query_list, &http_code, &http_body, provider);
|
|
|
|
if (result < 0)
|
|
{
|
|
msg = (boost::format("code=%1%,body=%2") % http_code % http_body).str();
|
|
}
|
|
else
|
|
{
|
|
msg = "";
|
|
try
|
|
{
|
|
json j = json::parse(http_body);
|
|
if (j.contains("bind_list"))
|
|
{
|
|
|
|
for (auto& item : j["bind_list"])
|
|
{
|
|
auto it = localMachineList.find(item["dev_id"].get<std::string>());
|
|
if (it != localMachineList.end())
|
|
{
|
|
if (!item["user_id"].is_null())
|
|
it->second->bind_user_id = item["user_id"].get<std::string>();
|
|
if (!item["user_name"].is_null())
|
|
it->second->bind_user_name = item["user_name"].get<std::string>();
|
|
else
|
|
it->second->bind_user_name = "Free";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (...)
|
|
{
|
|
;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
MachineObject* DeviceManager::get_user_machine(std::string dev_id, const std::string& provider)
|
|
{
|
|
if (!m_agent || !m_agent->is_user_login(provider))
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
std::map<std::string, MachineObject*>::iterator it = userMachineList.find(dev_id);
|
|
if (it == userMachineList.end()) return nullptr;
|
|
return it->second;
|
|
}
|
|
|
|
MachineObject* DeviceManager::get_my_machine(std::string dev_id)
|
|
{
|
|
auto list = get_my_machine_list(get_current_printer_agent_id());
|
|
auto it = list.find(dev_id);
|
|
if (it != list.end())
|
|
{
|
|
return it->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void DeviceManager::clean_user_info(bool keep_local_selection)
|
|
{
|
|
BOOST_LOG_TRIVIAL(trace) << "DeviceManager::clean_user_info";
|
|
const std::string previous_selected_machine = selected_machine;
|
|
const bool keep_selected_machine = keep_local_selection &&
|
|
!selected_machine.empty() &&
|
|
localMachineList.find(selected_machine) != localMachineList.end();
|
|
|
|
// clean user list
|
|
for (auto it = userMachineList.begin(); it != userMachineList.end(); it++)
|
|
{
|
|
if (it->second)
|
|
{
|
|
it->second->set_access_code("");
|
|
delete it->second;
|
|
it->second = nullptr;
|
|
}
|
|
}
|
|
userMachineList.clear();
|
|
|
|
if (!keep_selected_machine) {
|
|
selected_machine = "";
|
|
local_selected_machine = "";
|
|
}
|
|
|
|
OnSelectedMachineChanged(previous_selected_machine, selected_machine);
|
|
}
|
|
|
|
void DeviceManager::clear_other_devices()
|
|
{
|
|
// Device entries are now scoped by printer_agent_id when they are presented. Keep
|
|
// agent-owned discoveries across a switch so agents without automatic discovery (and
|
|
// plugins whose devices have not received an access code yet) do not lose their list.
|
|
// Entries without an owner are legacy/unscoped and cannot safely be shown.
|
|
for (auto it = localMachineList.begin(); it != localMachineList.end();)
|
|
{
|
|
if (!it->second || it->second->printer_agent_id.empty())
|
|
{
|
|
delete it->second;
|
|
it = localMachineList.erase(it);
|
|
}
|
|
else
|
|
{
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool DeviceManager::set_selected_machine(std::string dev_id)
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << "set_selected_machine=" << dev_id
|
|
<< " cur_selected=" << selected_machine;
|
|
auto my_machine_list = get_my_machine_list(get_current_printer_agent_id());
|
|
auto it = my_machine_list.find(dev_id);
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: set_selected_machine lookup dev_id=" << dev_id
|
|
<< " found=" << (it != my_machine_list.end())
|
|
<< " my_machine_count=" << my_machine_list.size()
|
|
<< " current_agent=" << get_current_printer_agent_id()
|
|
<< " provider=" << GUI::wxGetApp().get_printer_cloud_provider();
|
|
if (it != my_machine_list.end() && it->second) {
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: target machine dev_id=" << it->second->get_dev_id()
|
|
<< " printer_agent_id=" << it->second->printer_agent_id
|
|
<< " connection_type=" << it->second->connection_type()
|
|
<< " dev_connection_type=" << it->second->dev_connection_type;
|
|
} else if (!dev_id.empty()) {
|
|
BOOST_LOG_TRIVIAL(warning) << "Orca diagnostic: target machine was not found in the current agent's machine list";
|
|
return false;
|
|
}
|
|
|
|
// disconnect last if dev_id difference from previous one
|
|
auto last_selected = my_machine_list.find(selected_machine);
|
|
if (last_selected != my_machine_list.end() && selected_machine != dev_id)
|
|
{
|
|
if (last_selected->second->connection_type() == "lan")
|
|
{
|
|
m_agent->disconnect_printer();
|
|
}
|
|
else if (last_selected->second->connection_type() == "cloud") {
|
|
const int result = m_agent->set_user_selected_machine("");
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: cleared previous cloud selection dev_id="
|
|
<< selected_machine << " result=" << result;
|
|
}
|
|
}
|
|
|
|
// connect curr
|
|
if (it != my_machine_list.end())
|
|
{
|
|
if (selected_machine == dev_id)
|
|
{
|
|
// same dev_id, cloud => reset update time
|
|
if (it->second->connection_type() != "lan")
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << "set_selected_machine: same cloud machine, dev_id =" << dev_id
|
|
<< ", just reset update time";
|
|
|
|
// only reset update time
|
|
it->second->reset_update_time();
|
|
|
|
// check subscribe state
|
|
Slic3r::GUI::wxGetApp().on_start_subscribe_again(dev_id);
|
|
|
|
return true;
|
|
}
|
|
// same dev_id, lan => disconnect and reconnect
|
|
else
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << "set_selected_machine: same lan machine, dev_id =" << dev_id
|
|
<< ", disconnect and reconnect";
|
|
|
|
// lan mode printer reconnect printer
|
|
if (m_agent)
|
|
{
|
|
m_agent->disconnect_printer();
|
|
it->second->reset();
|
|
|
|
it->second->connect();
|
|
it->second->set_lan_mode_connection_state(true);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (m_agent)
|
|
{
|
|
if (it->second->connection_type() != "lan" || it->second->connection_type().empty())
|
|
{
|
|
// diff dev_id, cloud => set_user_selected_machine(new)
|
|
BOOST_LOG_TRIVIAL(info) << "set_selected_machine: select new cloud machine, dev_id =" << dev_id;
|
|
const int result = m_agent->set_user_selected_machine(dev_id);
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: set new cloud selection dev_id="
|
|
<< dev_id << " result=" << result;
|
|
it->second->reset();
|
|
}
|
|
else
|
|
{
|
|
BOOST_LOG_TRIVIAL(info) << "set_selected_machine: select new lan machine, dev_id =" << dev_id;
|
|
it->second->reset();
|
|
it->second->connect();
|
|
it->second->set_lan_mode_connection_state(true);
|
|
}
|
|
}
|
|
}
|
|
for (auto& data : it->second->m_nozzle_filament_data)
|
|
{
|
|
data.second.checked_filament.clear();
|
|
}
|
|
}
|
|
|
|
if (selected_machine != dev_id) {
|
|
OnSelectedMachineChanged(selected_machine, dev_id);
|
|
}
|
|
|
|
selected_machine = dev_id;
|
|
record_user_last_machine(selected_machine);
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: DeviceManager selection complete selected_machine="
|
|
<< selected_machine;
|
|
return true;
|
|
}
|
|
|
|
MachineObject* DeviceManager::get_selected_machine()
|
|
{
|
|
if (selected_machine.empty()) return nullptr;
|
|
|
|
MachineObject* obj = get_user_machine(selected_machine, GUI::wxGetApp().get_printer_cloud_provider());
|
|
if (obj)
|
|
return obj;
|
|
|
|
// return local machine has access code
|
|
auto it = localMachineList.find(selected_machine);
|
|
if (it != localMachineList.end())
|
|
{
|
|
if (it->second->has_access_right())
|
|
return it->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void DeviceManager::add_user_subscribe()
|
|
{
|
|
/* user machine */
|
|
std::vector<std::string> dev_list;
|
|
for (auto it = userMachineList.begin(); it != userMachineList.end(); it++)
|
|
{
|
|
dev_list.push_back(it->first);
|
|
BOOST_LOG_TRIVIAL(trace) << "add_user_subscribe: " << it->first;
|
|
}
|
|
const int result = m_agent->add_subscribe(dev_list);
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: add_user_subscribe count=" << dev_list.size()
|
|
<< " result=" << result;
|
|
}
|
|
|
|
|
|
void DeviceManager::del_user_subscribe()
|
|
{
|
|
/* user machine */
|
|
std::vector<std::string> dev_list;
|
|
for (auto it = userMachineList.begin(); it != userMachineList.end(); it++)
|
|
{
|
|
dev_list.push_back(it->first);
|
|
BOOST_LOG_TRIVIAL(trace) << "del_user_subscribe: " << it->first;
|
|
}
|
|
const int result = m_agent->del_subscribe(dev_list);
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: del_user_subscribe count=" << dev_list.size()
|
|
<< " result=" << result;
|
|
}
|
|
|
|
void DeviceManager::subscribe_device_list(std::vector<std::string> dev_list)
|
|
{
|
|
std::vector<std::string> unsub_list;
|
|
subscribe_list_cache.clear();
|
|
for (auto& it : subscribe_list_cache)
|
|
{
|
|
if (it != selected_machine)
|
|
{
|
|
unsub_list.push_back(it);
|
|
BOOST_LOG_TRIVIAL(trace) << "subscribe_device_list: unsub dev id = " << it;
|
|
}
|
|
}
|
|
BOOST_LOG_TRIVIAL(trace) << "subscribe_device_list: unsub_list size = " << unsub_list.size();
|
|
|
|
if (!selected_machine.empty())
|
|
{
|
|
subscribe_list_cache.push_back(selected_machine);
|
|
}
|
|
for (auto& it : dev_list)
|
|
{
|
|
subscribe_list_cache.push_back(it);
|
|
BOOST_LOG_TRIVIAL(trace) << "subscribe_device_list: sub dev id = " << it;
|
|
}
|
|
BOOST_LOG_TRIVIAL(trace) << "subscribe_device_list: sub_list size = " << subscribe_list_cache.size();
|
|
if (!unsub_list.empty())
|
|
m_agent->del_subscribe(unsub_list);
|
|
if (!dev_list.empty())
|
|
m_agent->add_subscribe(subscribe_list_cache);
|
|
}
|
|
|
|
std::map<std::string, MachineObject*> DeviceManager::get_my_machine_list(const std::string& agent_id)
|
|
{
|
|
std::map<std::string, MachineObject*> result;
|
|
|
|
for (auto it = userMachineList.begin(); it != userMachineList.end(); it++)
|
|
{
|
|
if (!it->second || (!agent_id.empty() && it->second->printer_agent_id != agent_id))
|
|
continue;
|
|
|
|
if (!it->second->is_lan_mode_printer())
|
|
{
|
|
result.insert(std::make_pair(it->first, it->second));
|
|
}
|
|
}
|
|
|
|
for (auto it = localMachineList.begin(); it != localMachineList.end(); it++)
|
|
{
|
|
if (!it->second || (!agent_id.empty() && it->second->printer_agent_id != agent_id))
|
|
continue;
|
|
|
|
if (it->second->has_access_right() && it->second->is_avaliable() && it->second->is_lan_mode_printer())
|
|
{
|
|
// remove redundant in userMachineList
|
|
if (result.find(it->first) == result.end())
|
|
{
|
|
result.emplace(std::make_pair(it->first, it->second));
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::map<std::string, MachineObject*> DeviceManager::get_my_cloud_machine_list(const std::string& agent_id)
|
|
{
|
|
std::map<std::string, MachineObject*> result;
|
|
for (auto it = userMachineList.begin(); it != userMachineList.end(); it++)
|
|
{
|
|
if (!it->second || (!agent_id.empty() && it->second->printer_agent_id != agent_id))
|
|
continue;
|
|
|
|
if (!it->second->is_lan_mode_printer()) { result.emplace(*it); }
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::string DeviceManager::get_first_online_user_machine() const
|
|
{
|
|
for (auto it = userMachineList.begin(); it != userMachineList.end(); it++)
|
|
{
|
|
if (it->second && it->second->is_online())
|
|
{
|
|
return it->second->get_dev_id();
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
void DeviceManager::modify_device_name(std::string dev_id, std::string dev_name, const std::string& provider)
|
|
{
|
|
BOOST_LOG_TRIVIAL(trace) << "modify_device_name";
|
|
if (m_agent)
|
|
{
|
|
int result = m_agent->modify_printer_name(dev_id, dev_name, provider);
|
|
if (result == 0)
|
|
{
|
|
update_user_machine_list_info(provider);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DeviceManager::parse_user_print_info(std::string body)
|
|
{
|
|
BOOST_LOG_TRIVIAL(trace) << "DeviceManager::parse_user_print_info";
|
|
std::lock_guard<std::mutex> lock(listMutex);
|
|
|
|
if (device_subseries.size() <= 0) {
|
|
device_subseries = DevPrinterConfigUtil::get_all_subseries();
|
|
if (device_subseries.size() <= 0) {
|
|
device_subseries.insert(std::pair<std::string, std::vector<std::string>>("", std::vector<std::string>()));
|
|
}
|
|
}
|
|
|
|
std::set<std::string> new_list;
|
|
try
|
|
{
|
|
json j = json::parse(body);
|
|
|
|
const bool has_request_context = j.contains("provider") && j.contains("agent_id") && j.contains("generation");
|
|
const std::string current_provider = get_current_cloud_provider();
|
|
const std::string provider = j.contains("provider") ? j["provider"].get<std::string>()
|
|
: current_provider;
|
|
const std::string agent_id = j.contains("agent_id") ? j["agent_id"].get<std::string>()
|
|
: get_current_printer_agent_id();
|
|
const std::uint64_t generation = j.value("generation", std::uint64_t(0));
|
|
|
|
if (has_request_context &&
|
|
(provider != current_provider ||
|
|
agent_id != get_current_printer_agent_id() ||
|
|
generation != (m_agent ? m_agent->get_user_machine_list_generation() : 0))) {
|
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ": ignoring stale response provider="
|
|
<< provider << " agent_id=" << agent_id
|
|
<< " generation=" << generation;
|
|
return;
|
|
}
|
|
|
|
#if !BBL_RELEASE_TO_PUBLIC
|
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": " << j;
|
|
#endif
|
|
|
|
if (j.contains("devices") && !j["devices"].is_null())
|
|
{
|
|
for (auto& elem : j["devices"])
|
|
{
|
|
MachineObject* obj = nullptr;
|
|
std::string dev_id;
|
|
if (!elem["dev_id"].is_null())
|
|
{
|
|
dev_id = elem["dev_id"].get<std::string>();
|
|
new_list.insert(dev_id);
|
|
}
|
|
std::map<std::string, MachineObject*>::iterator iter = userMachineList.find(dev_id);
|
|
if (iter != userMachineList.end())
|
|
{
|
|
/* update field */
|
|
obj = iter->second;
|
|
obj->set_dev_id(dev_id);
|
|
// A device can be rediscovered by a different agent after a preset
|
|
// switch while retaining the same MachineObject instance.
|
|
obj->printer_agent_id = agent_id;
|
|
}
|
|
else
|
|
{
|
|
obj = new MachineObject(this, m_agent, "", "", "");
|
|
obj->printer_agent_id = agent_id;
|
|
if (m_agent)
|
|
{
|
|
obj->set_bind_status(m_agent->get_user_name(provider));
|
|
}
|
|
|
|
if (obj->get_dev_ip().empty())
|
|
{
|
|
if (AppConfig* config = get_app_config())
|
|
obj->get_dev_ip() = config->get("ip_address", dev_id);
|
|
}
|
|
userMachineList.insert(std::make_pair(dev_id, obj));
|
|
}
|
|
|
|
if (!obj) continue;
|
|
|
|
// Orca cloud printers are only ever delivered through this REST
|
|
// account list; tag them so DeviceManager's cloud/lan branches
|
|
// (subscribe + deselect in set_selected_machine) treat them right.
|
|
if (provider == ORCA_CLOUD_PROVIDER)
|
|
obj->dev_connection_type = "cloud";
|
|
|
|
if (!elem["dev_id"].is_null())
|
|
obj->set_dev_id(elem["dev_id"].get<std::string>());
|
|
if (!elem["dev_name"].is_null())
|
|
obj->set_dev_name(elem["dev_name"].get<std::string>());
|
|
if (!elem["dev_online"].is_null())
|
|
obj->m_is_online = elem["dev_online"].get<bool>();
|
|
if (elem.contains("dev_model_name") && !elem["dev_model_name"].is_null()) {
|
|
auto printer_type = elem["dev_model_name"].get<std::string>();
|
|
for (const auto &pair : device_subseries) {
|
|
auto it = std::find(pair.second.begin(), pair.second.end(), printer_type);
|
|
if (it != pair.second.end())
|
|
{
|
|
obj->printer_type = Slic3r::_parse_printer_type(pair.first);
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
obj->printer_type = Slic3r::_parse_printer_type(printer_type);
|
|
}
|
|
}
|
|
}
|
|
if (!elem["task_status"].is_null())
|
|
obj->iot_print_status = elem["task_status"].get<std::string>();
|
|
if (elem.contains("dev_product_name") && !elem["dev_product_name"].is_null())
|
|
obj->dev_product_name = elem["dev_product_name"].get<std::string>();
|
|
if (elem.contains("dev_access_code") && !elem["dev_access_code"].is_null())
|
|
{
|
|
std::string acc_code = elem["dev_access_code"].get<std::string>();
|
|
acc_code.erase(std::remove(acc_code.begin(), acc_code.end(), '\n'), acc_code.end());
|
|
obj->set_access_code(acc_code);
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: parsed cloud machine dev_id=" << dev_id
|
|
<< " name=" << obj->get_dev_name()
|
|
<< " agent_id=" << obj->printer_agent_id
|
|
<< " connection_type=" << obj->connection_type()
|
|
<< " online=" << obj->m_is_online;
|
|
}
|
|
|
|
//remove MachineObject from userMachineList
|
|
std::map<std::string, MachineObject*>::iterator iterat;
|
|
for (iterat = userMachineList.begin(); iterat != userMachineList.end(); )
|
|
{
|
|
if (new_list.find(iterat->first) == new_list.end())
|
|
{
|
|
iterat = userMachineList.erase(iterat);
|
|
}
|
|
else
|
|
{
|
|
iterat++;
|
|
}
|
|
}
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: parse_user_print_info complete provider=" << provider
|
|
<< " parsed_count=" << new_list.size()
|
|
<< " stored_count=" << userMachineList.size();
|
|
}
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << " exception=" << e.what();
|
|
}
|
|
}
|
|
|
|
void DeviceManager::update_user_machine_list_info(const std::string& provider)
|
|
{
|
|
if (!m_agent) return;
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "update_user_machine_list_info";
|
|
unsigned int http_code;
|
|
std::string body;
|
|
int result = m_agent->get_user_print_info(&http_code, &body, provider);
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: get_user_print_info provider=" << provider
|
|
<< " result=" << result << " http_code=" << http_code
|
|
<< " body_bytes=" << body.size();
|
|
if (result == 0)
|
|
{
|
|
// parse_user_print_info and on_machine_alive (SSDP for discovery) both mutate the same userMachineList map.
|
|
// on_machine_alive mutates the map on the UI thread, do the same for parse_user_print_info.
|
|
BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: queueing parse_user_print_info on UI thread";
|
|
Slic3r::GUI::wxGetApp().CallAfter([this, body]() { parse_user_print_info(body); });
|
|
}
|
|
}
|
|
|
|
void DeviceManager::record_user_last_machine(const std::string& dev_id)
|
|
{
|
|
if (AppConfig* config = get_app_config()) {
|
|
config->set("user_last_selected_machine", dev_id);
|
|
}
|
|
}
|
|
|
|
std::string DeviceManager::get_user_last_machine() const
|
|
{
|
|
if (AppConfig* config = get_app_config()) {
|
|
const auto& user_last_machine = config->get("user_last_selected_machine");
|
|
if (!user_last_machine.empty()) {
|
|
return user_last_machine;
|
|
} else if (m_agent) {
|
|
return m_agent->get_user_selected_machine();
|
|
}
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
void DeviceManager::load_last_machine()
|
|
{
|
|
// Only reconnect the remembered cloud machine. Do not select an arbitrary
|
|
// first machine: agent swaps intentionally leave the selection empty until
|
|
// the new agent explicitly selects its configured printer.
|
|
if (userMachineList.empty())
|
|
return;
|
|
|
|
const auto& last_monitor_machine = get_user_last_machine();
|
|
if (userMachineList.find(last_monitor_machine) != userMachineList.end())
|
|
set_selected_machine(last_monitor_machine);
|
|
}
|
|
|
|
void DeviceManager::OnMachineBindStateChanged(MachineObject* obj, const std::string& new_state)
|
|
{
|
|
if (!obj) { return; }
|
|
if (obj->get_dev_id() == selected_machine)
|
|
{
|
|
if (new_state == "free") { OnSelectedMachineLost(); }
|
|
}
|
|
}
|
|
|
|
void DeviceManager::OnSelectedMachineLost()
|
|
{
|
|
GUI::wxGetApp().sidebar().update_sync_status(nullptr);
|
|
GUI::wxGetApp().sidebar().load_ams_list(nullptr);
|
|
}
|
|
|
|
void DeviceManager::OnSelectedMachineChanged(const std::string& /*pre_dev_id*/,
|
|
const std::string& new_dev_id)
|
|
{
|
|
{
|
|
LifecycleEventContext ctx;
|
|
ctx.name = new_dev_id; // empty string is a valid deselection
|
|
ctx.code = LifecycleEvtCode::Ok;
|
|
fire_lifecycle_event(LifecycleEvent::DeviceSelected, ctx);
|
|
}
|
|
if (MachineObject* obj_ = get_selected_machine()) {
|
|
GUI::wxGetApp().sidebar().update_sync_status(obj_);
|
|
if(m_agent->get_filament_sync_mode() == FilamentSyncMode::subscription)
|
|
{
|
|
GUI::wxGetApp().sidebar().load_ams_list(obj_);
|
|
}
|
|
};
|
|
}
|
|
|
|
void DeviceManager::reload_printer_settings()
|
|
{
|
|
for (auto obj : this->userMachineList) { obj.second->reload_printer_settings(); };
|
|
}
|
|
|
|
|
|
DeviceManagerRefresher::DeviceManagerRefresher(DeviceManager* manger) : wxObject()
|
|
{
|
|
m_manager = manger;
|
|
m_timer = new wxTimer();
|
|
m_timer->Bind(wxEVT_TIMER, &DeviceManagerRefresher::on_timer, this);
|
|
}
|
|
|
|
DeviceManagerRefresher::~DeviceManagerRefresher()
|
|
{
|
|
m_timer->Stop();
|
|
delete m_timer;
|
|
}
|
|
|
|
void DeviceManagerRefresher::on_timer(wxTimerEvent& event)
|
|
{
|
|
if (!m_manager) { return; }
|
|
|
|
NetworkAgent* agent = m_manager->get_agent();
|
|
if (!agent) { return; }
|
|
|
|
// reset to active
|
|
Slic3r::GUI::wxGetApp().reset_to_active();
|
|
|
|
MachineObject* obj = m_manager->get_selected_machine();
|
|
if (!obj) { return; }
|
|
|
|
// check valid machine
|
|
if (obj && m_manager->get_my_machine(obj->get_dev_id()) == nullptr)
|
|
{
|
|
m_manager->set_selected_machine("");
|
|
agent->set_user_selected_machine("");
|
|
return;
|
|
}
|
|
|
|
// do some refresh
|
|
const auto cloud_provider = Slic3r::GUI::wxGetApp().get_printer_cloud_provider();
|
|
if (Slic3r::GUI::wxGetApp().is_user_login(cloud_provider))
|
|
{
|
|
try {
|
|
m_manager->check_pushing();
|
|
} catch (const std::exception& e) {
|
|
BOOST_LOG_TRIVIAL(error) << "DeviceManagerRefresher::on_timer check_pushing exception="
|
|
<< e.what();
|
|
} catch (...) {
|
|
BOOST_LOG_TRIVIAL(error) << "DeviceManagerRefresher::on_timer check_pushing unknown exception";
|
|
}
|
|
|
|
try {
|
|
agent->refresh_connection(cloud_provider);
|
|
} catch (const std::exception& e) {
|
|
BOOST_LOG_TRIVIAL(error) << "DeviceManagerRefresher::on_timer refresh_connection exception="
|
|
<< e.what();
|
|
} catch (...) {
|
|
BOOST_LOG_TRIVIAL(error) << "DeviceManagerRefresher::on_timer refresh_connection unknown exception";
|
|
}
|
|
}
|
|
|
|
// certificate
|
|
try {
|
|
agent->install_device_cert(obj->get_dev_id(), obj->is_lan_mode_printer());
|
|
} catch (const std::exception& e) {
|
|
BOOST_LOG_TRIVIAL(error) << "DeviceManagerRefresher::on_timer install_device_cert exception="
|
|
<< e.what();
|
|
} catch (...) {
|
|
BOOST_LOG_TRIVIAL(error) << "DeviceManagerRefresher::on_timer install_device_cert unknown exception";
|
|
}
|
|
}
|
|
}
|