fix: update lifecycle contract for printer connection

This commit is contained in:
peachismomo
2026-09-23 03:09:03 +08:00
parent 4a4c649dbb
commit ff96cce7c3
6 changed files with 39 additions and 60 deletions
+4 -6
View File
@@ -49,11 +49,10 @@ namespace Slic3r
// Printer/device // Printer/device
PrintStateChanged, PrintStateChanged,
DeviceOnlineChanged, DeviceOnline,
DeviceOffline,
DeviceDiscovered, DeviceDiscovered,
DeviceSelected, DeviceSelected,
DeviceConnected,
DeviceDisconnected,
UploadStarted, UploadStarted,
UploadFinished, UploadFinished,
@@ -141,11 +140,10 @@ namespace Slic3r
case LifecycleEvent::PresetSaved: return "PresetSaved"; case LifecycleEvent::PresetSaved: return "PresetSaved";
case LifecycleEvent::PrintStateChanged: return "PrintStateChanged"; case LifecycleEvent::PrintStateChanged: return "PrintStateChanged";
case LifecycleEvent::DeviceOnlineChanged: return "DeviceOnlineChanged"; case LifecycleEvent::DeviceOnline: return "DeviceOnline";
case LifecycleEvent::DeviceOffline: return "DeviceOffline";
case LifecycleEvent::DeviceDiscovered: return "DeviceDiscovered"; case LifecycleEvent::DeviceDiscovered: return "DeviceDiscovered";
case LifecycleEvent::DeviceSelected: return "DeviceSelected"; case LifecycleEvent::DeviceSelected: return "DeviceSelected";
case LifecycleEvent::DeviceConnected: return "DeviceConnected";
case LifecycleEvent::DeviceDisconnected: return "DeviceDisconnected";
case LifecycleEvent::UploadStarted: return "UploadStarted"; case LifecycleEvent::UploadStarted: return "UploadStarted";
case LifecycleEvent::UploadFinished: return "UploadFinished"; case LifecycleEvent::UploadFinished: return "UploadFinished";
+2 -2
View File
@@ -357,7 +357,7 @@ namespace Slic3r
obj->last_alive = Slic3r::Utils::get_current_time_utc(); obj->last_alive = Slic3r::Utils::get_current_time_utc();
// Route through set_online_state() (rather than writing m_is_online directly) so the // Route through set_online_state() (rather than writing m_is_online directly) so the
// DeviceOnlineChanged lifecycle event fires consistently; same effective value/behavior // DeviceOnline lifecycle event fires consistently; same effective value/behavior
// here since the object was already online in the common case. // here since the object was already online in the common case.
obj->set_online_state(true); obj->set_online_state(true);
obj->set_dev_name(dev_name); obj->set_dev_name(dev_name);
@@ -380,7 +380,7 @@ namespace Slic3r
// Discovery establishes the initial reachability state. Do not report it as an // Discovery establishes the initial reachability state. Do not report it as an
// online transition; DeviceDiscovered below is the lifecycle event for a new // online transition; DeviceDiscovered below is the lifecycle event for a new
// device. Subsequent updates route through set_online_state(), so a known device // device. Subsequent updates route through set_online_state(), so a known device
// still emits DeviceOnlineChanged when its reachability actually changes. // still emits DeviceOnline/DeviceOffline when its reachability actually changes.
obj->m_is_online = true; obj->m_is_online = true;
//load access code //load access code
+26 -31
View File
@@ -2657,11 +2657,10 @@ int MachineObject::connect(bool use_openssl)
int MachineObject::disconnect() int MachineObject::disconnect()
{ {
if (m_agent) { if (m_agent) {
LifecycleEventContext ctx; const int result = m_agent->disconnect_printer();
ctx.name = dev_id; if (result == 0)
ctx.code = LifecycleEvtCode::Ok; set_online_state(false);
fire_lifecycle_event(LifecycleEvent::DeviceDisconnected, ctx); return result;
return m_agent->disconnect_printer();
} }
return -1; return -1;
} }
@@ -2699,7 +2698,7 @@ void MachineObject::set_online_state(bool on_off)
ctx.name = dev_id; ctx.name = dev_id;
ctx.code = LifecycleEvtCode::Ok; ctx.code = LifecycleEvtCode::Ok;
ctx.msg = on_off ? "online" : "offline"; ctx.msg = on_off ? "online" : "offline";
fire_lifecycle_event(LifecycleEvent::DeviceOnlineChanged, ctx); fire_lifecycle_event(on_off ? LifecycleEvent::DeviceOnline : LifecycleEvent::DeviceOffline, ctx);
} }
} }
@@ -2827,13 +2826,6 @@ int MachineObject::parse_json(std::string tunnel, std::string payload, bool key_
parse_msg_count++; parse_msg_count++;
std::chrono::system_clock::time_point clock_start = std::chrono::system_clock::now(); std::chrono::system_clock::time_point clock_start = std::chrono::system_clock::now();
this->set_online_state(true);
std::chrono::system_clock::time_point curr_time = std::chrono::system_clock::now();
auto diff1 = std::chrono::duration_cast<std::chrono::microseconds>(curr_time - last_update_time);
/* update last received time */
last_update_time = std::chrono::system_clock::now();
json j_pre; json j_pre;
bool parse_ok = false; bool parse_ok = false;
@@ -2846,8 +2838,29 @@ int MachineObject::parse_json(std::string tunnel, std::string payload, bool key_
/* post process payload */ /* post process payload */
sanitizeToUtf8(payload); sanitizeToUtf8(payload);
BOOST_LOG_TRIVIAL(info) << "parse_json: sanitize to utf8"; BOOST_LOG_TRIVIAL(info) << "parse_json: sanitize to utf8";
try {
j_pre = json::parse(payload);
parse_ok = true;
}
catch (...) {}
} }
bool client_disconnected = false;
if (parse_ok && j_pre.is_object() && j_pre.contains("event") && j_pre["event"].is_object() &&
j_pre["event"].contains("event") && j_pre["event"]["event"].is_string()) {
client_disconnected = j_pre["event"]["event"].get<std::string>() == "client.disconnected";
}
// A disconnect notification is a transport message too, but it must not first mark an
// already-offline device as online through the generic message-received path.
set_online_state(!client_disconnected);
std::chrono::system_clock::time_point curr_time = std::chrono::system_clock::now();
auto diff1 = std::chrono::duration_cast<std::chrono::microseconds>(curr_time - last_update_time);
/* update last received time */
last_update_time = std::chrono::system_clock::now();
try { try {
bool restored_json = false; bool restored_json = false;
json j; json j;
@@ -4618,24 +4631,6 @@ int MachineObject::parse_json(std::string tunnel, std::string payload, bool key_
} }
} }
// event info
try {
if (j.contains("event")) {
if (j["event"].contains("event")) {
if (j["event"]["event"].get<std::string>() == "client.disconnected") {
set_online_state(false);
LifecycleEventContext ctx;
ctx.name = dev_id;
ctx.code = LifecycleEvtCode::Ok;
fire_lifecycle_event(LifecycleEvent::DeviceDisconnected, ctx);
}
else if (j["event"]["event"].get<std::string>() == "client.connected")
set_online_state(true);
}
}
}
catch (...) {}
if (!key_field_only) { if (!key_field_only) {
BOOST_LOG_TRIVIAL(trace) << "parse_json m_active_state =" << m_active_state; BOOST_LOG_TRIVIAL(trace) << "parse_json m_active_state =" << m_active_state;
parse_state_changed_event(); parse_state_changed_event();
+1
View File
@@ -897,6 +897,7 @@ public:
bool is_connected(); bool is_connected();
bool is_connecting(); bool is_connecting();
// Emits DeviceOnline or DeviceOffline only when the reachability state changes.
void set_online_state(bool on_off); void set_online_state(bool on_off);
bool is_online() { return m_is_online; } bool is_online() { return m_is_online; }
bool is_info_ready(bool check_version = true) const; bool is_info_ready(bool check_version = true) const;
+4 -18
View File
@@ -2183,10 +2183,7 @@ void GUI_App::init_networking_callbacks()
if (m_agent) if (m_agent)
m_agent->install_device_cert(obj->get_dev_id(), obj->is_lan_mode_printer()); m_agent->install_device_cert(obj->get_dev_id(), obj->is_lan_mode_printer());
LifecycleEventContext ctx; obj->set_online_state(true);
ctx.name = obj->get_dev_id();
ctx.code = LifecycleEvtCode::Ok;
fire_lifecycle_event(LifecycleEvent::DeviceConnected, ctx);
} }
}); });
}); });
@@ -2226,10 +2223,7 @@ void GUI_App::init_networking_callbacks()
event.SetInt(0); event.SetInt(0);
event.SetString(obj->get_dev_id()); event.SetString(obj->get_dev_id());
LifecycleEventContext ctx; obj->set_online_state(true);
ctx.name = obj->get_dev_id();
ctx.code = LifecycleEvtCode::Ok;
fire_lifecycle_event(LifecycleEvent::DeviceConnected, ctx);
} else if (state == ConnectStatus::ConnectStatusFailed) { } else if (state == ConnectStatus::ConnectStatusFailed) {
// Orca: only update status if same device id // Orca: only update status if same device id
if (m_device_manager->selected_machine != dev_id) return; if (m_device_manager->selected_machine != dev_id) return;
@@ -2246,21 +2240,13 @@ void GUI_App::init_networking_callbacks()
} }
event.SetInt(-1); event.SetInt(-1);
{ obj->set_online_state(false);
LifecycleEventContext ctx;
ctx.name = dev_id;
ctx.code = LifecycleEvtCode::Ok;
fire_lifecycle_event(LifecycleEvent::DeviceDisconnected, ctx);
}
} else if (state == ConnectStatus::ConnectStatusLost) { } else if (state == ConnectStatus::ConnectStatusLost) {
m_device_manager->set_selected_machine(""); m_device_manager->set_selected_machine("");
event.SetInt(-1); event.SetInt(-1);
BOOST_LOG_TRIVIAL(info) << "set_on_local_connect_fn: state = lost"; BOOST_LOG_TRIVIAL(info) << "set_on_local_connect_fn: state = lost";
LifecycleEventContext ctx; obj->set_online_state(false);
ctx.name = dev_id;
ctx.code = LifecycleEvtCode::Ok;
fire_lifecycle_event(LifecycleEvent::DeviceDisconnected, ctx);
} else { } else {
event.SetInt(-1); event.SetInt(-1);
BOOST_LOG_TRIVIAL(info) << "set_on_local_connect_fn: state = " << state; BOOST_LOG_TRIVIAL(info) << "set_on_local_connect_fn: state = " << state;
+2 -3
View File
@@ -401,11 +401,10 @@ void bind_python_api(pybind11::module_& m)
.value("PresetSelected", LifecycleEvent::PresetSelected) .value("PresetSelected", LifecycleEvent::PresetSelected)
.value("PresetSaved", LifecycleEvent::PresetSaved) .value("PresetSaved", LifecycleEvent::PresetSaved)
.value("PrintStateChanged", LifecycleEvent::PrintStateChanged) .value("PrintStateChanged", LifecycleEvent::PrintStateChanged)
.value("DeviceOnlineChanged", LifecycleEvent::DeviceOnlineChanged) .value("DeviceOnline", LifecycleEvent::DeviceOnline)
.value("DeviceOffline", LifecycleEvent::DeviceOffline)
.value("DeviceDiscovered", LifecycleEvent::DeviceDiscovered) .value("DeviceDiscovered", LifecycleEvent::DeviceDiscovered)
.value("DeviceSelected", LifecycleEvent::DeviceSelected) .value("DeviceSelected", LifecycleEvent::DeviceSelected)
.value("DeviceConnected", LifecycleEvent::DeviceConnected)
.value("DeviceDisconnected", LifecycleEvent::DeviceDisconnected)
.value("UploadStarted", LifecycleEvent::UploadStarted) .value("UploadStarted", LifecycleEvent::UploadStarted)
.value("UploadFinished", LifecycleEvent::UploadFinished) .value("UploadFinished", LifecycleEvent::UploadFinished)
.value("PrintJobStarted", LifecycleEvent::PrintJobStarted) .value("PrintJobStarted", LifecycleEvent::PrintJobStarted)