fix: ams sync info and periodic ams sync via subscription workflow

This commit is contained in:
Ian Chua
2026-08-19 18:29:19 +08:00
parent 96092ef2d3
commit 738b30a743
3 changed files with 25 additions and 3 deletions

View File

@@ -3176,6 +3176,7 @@ SyncAmsInfoDialog::~SyncAmsInfoDialog() {
void SyncAmsInfoDialog::set_info(SyncInfo &info)
{
m_input_info = info;
reinit_dialog();
}
void SyncAmsInfoDialog::update_lan_machine_list()

View File

@@ -246,6 +246,7 @@ int MoonrakerPrinterAgent::connect_printer(std::string dev_id, std::string dev_i
}
ws_last_emit_ms.store(0);
ws_last_dispatch_ms.store(0);
ams_last_fetch_ms.store(0);
last_print_state.clear();
// Launch connection in background thread (capture by value to avoid data races)
@@ -2090,6 +2091,12 @@ void MoonrakerPrinterAgent::run_status_stream(std::string dev_id, std::string ba
subscribe["id"] = 1;
ws.write(net::buffer(subscribe.dump()));
// Eager fetch so AMS data is available immediately after connecting,
// without waiting on the loop's own refresh clock below.
fetch_filament_info(dev_id, FilamentSyncMode::subscription);
ams_last_fetch_ms.store(static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count()));
// Read loop
while (!ws_stop.load()) {
ws.next_layer().expires_after(std::chrono::seconds(2));
@@ -2101,8 +2108,6 @@ void MoonrakerPrinterAgent::run_status_stream(std::string dev_id, std::string ba
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count());
const auto last_ms = ws_last_emit_ms.load();
if (last_ms == 0 || now_ms - last_ms >= 10000) {
fetch_filament_info(dev_id, FilamentSyncMode::subscription);
nlohmann::json message;
{
std::lock_guard<std::recursive_mutex> lock(payload_mutex);
@@ -2122,8 +2127,19 @@ void MoonrakerPrinterAgent::run_status_stream(std::string dev_id, std::string ba
connection_lost = true;
break;
}
// AMS/filament refresh on its own clock: gated independently of
// ws_last_emit_ms so a steady telemetry stream can't starve it.
{
const auto now_ms = static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count());
const auto last_ams_ms = ams_last_fetch_ms.load();
if (last_ams_ms == 0 || now_ms - last_ams_ms >= AMS_REFRESH_INTERVAL_MS) {
fetch_filament_info(dev_id, FilamentSyncMode::subscription);
ams_last_fetch_ms.store(now_ms);
}
}
handle_ws_message(dev_id, beast::buffers_to_string(buffer.data()), base_url, api_key);
// Check if handle_ws_message triggered reconnection request
// Check if handle_ws_message triggered reconnection request`
if (ws_reconnect_requested.exchange(false)) {
connection_lost = true;
break;

View File

@@ -233,6 +233,11 @@ private:
std::atomic<uint64_t> ws_last_emit_ms{0};
std::thread ws_thread;
// AMS/filament refresh cadence, independent of telemetry dispatch so a steady
// stream of status updates can't starve it (ws_last_emit_ms is reset by those).
static constexpr uint64_t AMS_REFRESH_INTERVAL_MS = 10000;
std::atomic<uint64_t> ams_last_fetch_ms{0};
// Throttling configuration for WebSocket updates
// Critical changes (state transitions) dispatch immediately; telemetry is throttled
static constexpr uint64_t STATUS_UPDATE_INTERVAL_MS = 1000; // 1 update/sec for telemetry