mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-09-17 05:52:39 +00:00
feat: restore printer agent implementations
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "QidiPrinterAgent.hpp"
|
||||
#include "Http.hpp"
|
||||
#include "IPrinterAgent.hpp"
|
||||
#include "libslic3r/PresetBundle.hpp"
|
||||
#include "slic3r/GUI/GUI_App.hpp"
|
||||
|
||||
@@ -8,6 +9,7 @@
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
@@ -27,6 +29,15 @@ bool has_visible_base_preset(const PresetCollection& filaments, const std::strin
|
||||
return false;
|
||||
}
|
||||
|
||||
// RAII decrement for MoonrakerPrinterAgent::filament_fetch_in_flight — guarantees the
|
||||
// counter drops back down on every exit path (early return or fall-through) inside the
|
||||
// detached fetch thread below, so ~MoonrakerPrinterAgent()'s wait loop can't stall forever.
|
||||
struct InFlightGuard
|
||||
{
|
||||
std::atomic<int>& counter;
|
||||
~InFlightGuard() { counter.fetch_sub(1, std::memory_order_relaxed); }
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
const std::string QidiPrinterAgent_VERSION = "0.0.1";
|
||||
@@ -40,42 +51,141 @@ AgentInfo QidiPrinterAgent::get_agent_info_static()
|
||||
return AgentInfo{"qidi", "Qidi", QidiPrinterAgent_VERSION, "Qidi printer agent"};
|
||||
}
|
||||
|
||||
bool QidiPrinterAgent::fetch_filament_info(std::string dev_id, FilamentSyncMode /*sync_mode*/)
|
||||
FilamentSyncMode QidiPrinterAgent::get_filament_sync_mode() const
|
||||
{
|
||||
std::string error;
|
||||
if (GUI::wxGetApp().app_config->get_bool("use_printer_agents"))
|
||||
return FilamentSyncMode::subscription;
|
||||
return FilamentSyncMode::pull;
|
||||
}
|
||||
|
||||
// 1. Fetch device info and infer series_id
|
||||
std::string series_id;
|
||||
{
|
||||
MoonrakerDeviceInfo info;
|
||||
if (fetch_device_info(device_info.base_url, device_info.api_key, info, error)) {
|
||||
series_id = infer_series_id(info.model_id, info.dev_name);
|
||||
bool QidiPrinterAgent::fetch_filament_info(std::string dev_id, FilamentSyncMode sync_mode)
|
||||
{
|
||||
if (sync_mode != get_filament_sync_mode())
|
||||
return false;
|
||||
|
||||
// Snapshot only what the fetch needs, rather than reading device_info live from the
|
||||
// background thread below — device_info can be concurrently rewritten by a reconnect
|
||||
// on another thread while this fetch is still in flight.
|
||||
std::string base_url = device_info.base_url;
|
||||
std::string api_key = device_info.api_key;
|
||||
std::string model_id = device_info.model_id;
|
||||
std::string model_name = device_info.model_name;
|
||||
|
||||
filament_fetch_in_flight.fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
std::thread([this, base_url, api_key, model_id, model_name]() {
|
||||
InFlightGuard guard{filament_fetch_in_flight};
|
||||
|
||||
std::string error;
|
||||
|
||||
// 1. Fetch device info and infer series_id
|
||||
std::string series_id;
|
||||
{
|
||||
MoonrakerDeviceInfo info;
|
||||
if (fetch_device_info(base_url, api_key, info, error)) {
|
||||
series_id = infer_series_id(info.model_id, info.dev_name);
|
||||
}
|
||||
}
|
||||
if (series_id.empty()) {
|
||||
// Fall back to the configured Orca model if Moonraker doesn't expose a usable identifier.
|
||||
series_id = infer_series_id(model_id, model_name);
|
||||
}
|
||||
}
|
||||
if (series_id.empty()) {
|
||||
// Fall back to the configured Orca model if Moonraker doesn't expose a usable identifier.
|
||||
series_id = infer_series_id(device_info.model_id, device_info.model_name);
|
||||
}
|
||||
|
||||
// 2. Fetch filament dictionary
|
||||
QidiFilamentDict dict;
|
||||
if (!fetch_filament_dict(device_info.base_url, device_info.api_key, dict, error)) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "QidiPrinterAgent::fetch_filament_info: Failed to fetch filament dict: " << error;
|
||||
}
|
||||
// 2. Fetch filament dictionary
|
||||
QidiFilamentDict dict;
|
||||
if (!fetch_filament_dict(base_url, api_key, dict, error)) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "QidiPrinterAgent::fetch_filament_info: Failed to fetch filament dict: " << error;
|
||||
}
|
||||
|
||||
// 3. Fetch slot info and build AmsTrayData directly
|
||||
std::vector<AmsTrayData> trays;
|
||||
int box_count = 0;
|
||||
if (!fetch_slot_info(device_info.base_url, device_info.api_key, dict, series_id, trays, box_count, error)) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "QidiPrinterAgent::fetch_filament_info: Failed to fetch slot info: " << error;
|
||||
// 3. Fetch slot info and build AmsTrayData directly
|
||||
std::vector<AmsTrayData> trays;
|
||||
int box_count = 0;
|
||||
if (!fetch_slot_info(base_url, api_key, dict, series_id, trays, box_count, error)) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "QidiPrinterAgent::fetch_filament_info: Failed to fetch slot info: " << error;
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Build the AMS payload
|
||||
build_ams_payload(box_count, box_count * 4 - 1, trays);
|
||||
}).detach();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QidiPrinterAgent::apply_box_mapping(const PrintParams& params) const
|
||||
{
|
||||
// enable_box mirrors task_use_ams: engage the multi-color box only when this
|
||||
// job actually routes filament through it. (See qidi-ams-findings.md §2/§8.3 —
|
||||
// if firmware treats enable_box as "a box exists" rather than "use it this job",
|
||||
// switch this gate to HasAms()/box_count instead.)
|
||||
const int enable = params.task_use_ams ? 1 : 0;
|
||||
if (!send_gcode(device_info.dev_id, "SAVE_VARIABLE VARIABLE=enable_box VALUE=" + std::to_string(enable))) {
|
||||
BOOST_LOG_TRIVIAL(error) << "QidiPrinterAgent::apply_box_mapping: failed to set enable_box";
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4. Build the AMS payload
|
||||
build_ams_payload(box_count, box_count * 4 - 1, trays);
|
||||
// When the box isn't used this job, leave the existing value_t<tool> slot
|
||||
// assignments untouched (enable_box=0 is enough to disengage it).
|
||||
if (!enable)
|
||||
return true;
|
||||
|
||||
if (params.ams_mapping.empty()) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "QidiPrinterAgent::apply_box_mapping: enable_box set but ams_mapping is empty";
|
||||
return true;
|
||||
}
|
||||
|
||||
// ams_mapping (v0) is a JSON array indexed by filament/tool; each value is the
|
||||
// physical box slot (-1 = unmapped). Mirror it onto the printer's value_t<tool>
|
||||
// variables: SAVE_VARIABLE VARIABLE=value_t<tool> VALUE='slot<n>'.
|
||||
auto mapping = nlohmann::json::parse(params.ams_mapping, nullptr, /*allow_exceptions*/ false);
|
||||
if (mapping.is_discarded() || !mapping.is_array()) {
|
||||
BOOST_LOG_TRIVIAL(error) << "QidiPrinterAgent::apply_box_mapping: invalid ams_mapping: " << params.ams_mapping;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t tool = 0; tool < mapping.size(); ++tool) {
|
||||
if (!mapping[tool].is_number_integer())
|
||||
continue;
|
||||
const int slot = mapping[tool].get<int>();
|
||||
if (slot < 0)
|
||||
continue; // unmapped filament — skip
|
||||
const std::string gcode = "SAVE_VARIABLE VARIABLE=value_t" + std::to_string(tool) +
|
||||
" VALUE=\"'slot" + std::to_string(slot) + "'\"";
|
||||
if (!send_gcode(device_info.dev_id, gcode)) {
|
||||
BOOST_LOG_TRIVIAL(error) << "QidiPrinterAgent::apply_box_mapping: failed to set value_t" << tool;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int QidiPrinterAgent::start_local_print(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn)
|
||||
{
|
||||
if (!apply_box_mapping(params))
|
||||
return BAMBU_NETWORK_ERR_PRINT_LP_PUBLISH_MSG_FAILED;
|
||||
return MoonrakerPrinterAgent::start_local_print(std::move(params), update_fn, cancel_fn);
|
||||
}
|
||||
|
||||
int QidiPrinterAgent::start_print(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn, OnWaitFn wait_fn)
|
||||
{
|
||||
if (!apply_box_mapping(params))
|
||||
return BAMBU_NETWORK_ERR_PRINT_LP_PUBLISH_MSG_FAILED;
|
||||
return MoonrakerPrinterAgent::start_print(std::move(params), update_fn, cancel_fn, wait_fn);
|
||||
}
|
||||
|
||||
int QidiPrinterAgent::start_local_print_with_record(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn, OnWaitFn wait_fn)
|
||||
{
|
||||
if (!apply_box_mapping(params))
|
||||
return BAMBU_NETWORK_ERR_PRINT_WR_UPLOAD_FTP_FAILED;
|
||||
return MoonrakerPrinterAgent::start_local_print_with_record(std::move(params), update_fn, cancel_fn, wait_fn);
|
||||
}
|
||||
|
||||
int QidiPrinterAgent::start_sdcard_print(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn)
|
||||
{
|
||||
if (!apply_box_mapping(params))
|
||||
return BAMBU_NETWORK_ERR_PRINT_LP_PUBLISH_MSG_FAILED;
|
||||
return MoonrakerPrinterAgent::start_sdcard_print(std::move(params), update_fn, cancel_fn);
|
||||
}
|
||||
|
||||
bool QidiPrinterAgent::fetch_slot_info(const std::string& base_url,
|
||||
const std::string& api_key,
|
||||
const QidiFilamentDict& dict,
|
||||
@@ -120,20 +230,10 @@ bool QidiPrinterAgent::fetch_slot_info(const std::string& base_url,
|
||||
return false;
|
||||
}
|
||||
|
||||
auto json = nlohmann::json::parse(response_body, nullptr, false, true);
|
||||
if (json.is_discarded()) {
|
||||
error = "Invalid JSON response";
|
||||
nlohmann::json status;
|
||||
nlohmann::json variables;
|
||||
if (!parse_slot_response(response_body, status, variables, error))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!json.contains("result") || !json["result"].contains("status") || !json["result"]["status"].contains("save_variables") ||
|
||||
!json["result"]["status"]["save_variables"].contains("variables")) {
|
||||
error = "Unexpected JSON structure";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& variables = json["result"]["status"]["save_variables"]["variables"];
|
||||
auto& status = json["result"]["status"];
|
||||
|
||||
box_count = variables.value("box_count", 1);
|
||||
if (box_count < 0) {
|
||||
@@ -208,6 +308,31 @@ bool QidiPrinterAgent::fetch_slot_info(const std::string& base_url,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QidiPrinterAgent::parse_slot_response(const std::string& response_body,
|
||||
nlohmann::json& status,
|
||||
nlohmann::json& variables,
|
||||
std::string& error)
|
||||
{
|
||||
auto json = nlohmann::json::parse(response_body, nullptr, false, true);
|
||||
if (json.is_discarded()) {
|
||||
error = "Invalid JSON response";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!json.is_object() || !json.contains("result") || !json["result"].is_object() || !json["result"].contains("status") ||
|
||||
!json["result"]["status"].is_object() || !json["result"]["status"].contains("save_variables") ||
|
||||
!json["result"]["status"]["save_variables"].is_object() || !json["result"]["status"]["save_variables"].contains("variables") ||
|
||||
!json["result"]["status"]["save_variables"]["variables"].is_object()) {
|
||||
// why: Qidi firmware may send null here, but json::value() throws for it.
|
||||
error = "Unexpected JSON structure: save_variables.variables must be an object";
|
||||
return false;
|
||||
}
|
||||
|
||||
status = json["result"]["status"];
|
||||
variables = status["save_variables"]["variables"];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QidiPrinterAgent::fetch_filament_dict(const std::string& base_url,
|
||||
const std::string& api_key,
|
||||
QidiFilamentDict& dict,
|
||||
|
||||
Reference in New Issue
Block a user