From 58fc8a7c1f904bec835c9423c8aef93bd30e7375 Mon Sep 17 00:00:00 2001 From: Ilia Kuzmin Date: Wed, 25 Nov 2026 01:01:28 +0300 Subject: [PATCH] Fix AMS sync for Happy Hare printers with an unpopulated lane_data skeleton (#14798) Happy Hare can write a lane_data entry for every gate with only the lane number set. Orca took that as the answer and showed every slot empty without asking Happy Hare's mmu object. When every lane is empty, sync now tries Happy Hare and then Creality CFS, and keeps the empty lane layout if neither reports filament. Lane data with filament in it keeps its priority. --- src/slic3r/Utils/MoonrakerFilamentSources.hpp | 44 +++++++ src/slic3r/Utils/MoonrakerPrinterAgent.cpp | 46 ++++--- tests/slic3rutils/CMakeLists.txt | 1 + .../test_moonraker_filament_sources.cpp | 122 ++++++++++++++++++ 4 files changed, 189 insertions(+), 24 deletions(-) create mode 100644 src/slic3r/Utils/MoonrakerFilamentSources.hpp create mode 100644 tests/slic3rutils/test_moonraker_filament_sources.cpp diff --git a/src/slic3r/Utils/MoonrakerFilamentSources.hpp b/src/slic3r/Utils/MoonrakerFilamentSources.hpp new file mode 100644 index 0000000000..46038d9440 --- /dev/null +++ b/src/slic3r/Utils/MoonrakerFilamentSources.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +namespace Slic3r { + +enum class MoonrakerFilamentSource { None, Lanes, HappyHare, CrealityCfs, EmptyLanes }; + +// Select the first usable filament source, preserving empty lane topology. +// Each reader fills trays and the highest slot index; failed readers own no result. +template +MoonrakerFilamentSource select_moonraker_filament_data( + Trays& trays, int& max_lane_index, LaneReader read_lanes, HappyHareReader read_happy_hare, CfsReader read_cfs) +{ + trays.clear(); + max_lane_index = 0; + int empty_lane_data_max_index = -1; + if (read_lanes(trays, max_lane_index)) { + if (std::any_of(trays.begin(), trays.end(), [](const auto& tray) { return tray.has_filament; })) + return MoonrakerFilamentSource::Lanes; + empty_lane_data_max_index = max_lane_index; + } + + trays.clear(); + max_lane_index = 0; + if (read_happy_hare(trays, max_lane_index)) { + max_lane_index = std::max(max_lane_index, empty_lane_data_max_index); + return MoonrakerFilamentSource::HappyHare; + } + + trays.clear(); + max_lane_index = 0; + if (read_cfs(trays, max_lane_index)) + return MoonrakerFilamentSource::CrealityCfs; + + trays.clear(); + max_lane_index = empty_lane_data_max_index; + if (empty_lane_data_max_index >= 0) + return MoonrakerFilamentSource::EmptyLanes; + max_lane_index = 0; + return MoonrakerFilamentSource::None; +} + +} // namespace Slic3r diff --git a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp index 0e17a1f35b..68be2dc7ee 100644 --- a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp +++ b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp @@ -1,4 +1,5 @@ #include "MoonrakerPrinterAgent.hpp" +#include "MoonrakerFilamentSources.hpp" #include "AsioUtils.hpp" #include "CrealityCfs.hpp" #include "Http.hpp" @@ -747,38 +748,35 @@ bool MoonrakerPrinterAgent::fetch_filament_info(std::string dev_id, FilamentSync { std::vector trays; int max_lane_index = 0; + const auto source = select_moonraker_filament_data(trays, max_lane_index, + [this](auto& out, int& index) { return fetch_moonraker_filament_data(out, index); }, + [this](auto& out, int& index) { return fetch_hh_filament_info(out, index); }, + [this](auto& out, int& index) { return fetch_creality_cfs_data(out, index); }); - // Try Moonraker filament data (more generic, supports any filament changer - // software that reports lane data to Moonraker like AFC and recent Happy - // Hare as of Feb 15, 2026) - if (fetch_moonraker_filament_data(trays, max_lane_index)) { + switch (source) { + case MoonrakerFilamentSource::Lanes: BOOST_LOG_TRIVIAL(info) << "MoonrakerPrinterAgent::fetch_filament_info: Detected Moonraker filament system with " << (max_lane_index + 1) << " lanes"; - int ams_count = (max_lane_index + 4) / 4; - build_ams_payload(ams_count, max_lane_index, trays); - return true; - } - - // Attempt Happy Hare first (more widely adopted, supports more filament changers) - if (fetch_hh_filament_info(trays, max_lane_index)) { + break; + case MoonrakerFilamentSource::HappyHare: BOOST_LOG_TRIVIAL(info) << "MoonrakerPrinterAgent::fetch_filament_info: Detected Happy Hare MMU with " - << (max_lane_index + 1) << " gates"; - int ams_count = (max_lane_index + 4) / 4; - build_ams_payload(ams_count, max_lane_index, trays); - return true; - } - - // Creality Hi exposes CFS slots as a Moonraker box printer object. - if (fetch_creality_cfs_data(trays, max_lane_index)) { + << trays.size() << " loaded gates and " << (max_lane_index + 1) << " slots"; + break; + case MoonrakerFilamentSource::CrealityCfs: BOOST_LOG_TRIVIAL(info) << "MoonrakerPrinterAgent::fetch_filament_info: Detected Creality CFS with " << (max_lane_index + 1) << " slots"; - build_ams_payload((max_lane_index + 4) / 4, max_lane_index, trays); - return true; + break; + case MoonrakerFilamentSource::EmptyLanes: + BOOST_LOG_TRIVIAL(info) << "MoonrakerPrinterAgent::fetch_filament_info: Preserving empty lane_data topology with " + << (max_lane_index + 1) << " lanes"; + break; + case MoonrakerFilamentSource::None: + BOOST_LOG_TRIVIAL(info) << "MoonrakerPrinterAgent::fetch_filament_info: No MMU system detected"; + return false; } - // No MMU detected - this is normal for printers without MMU, not an error. - BOOST_LOG_TRIVIAL(info) << "MoonrakerPrinterAgent::fetch_filament_info: No MMU system detected"; - return false; + build_ams_payload((max_lane_index + 4) / 4, max_lane_index, trays); + return true; } std::string MoonrakerPrinterAgent::trim_and_upper(const std::string& input) diff --git a/tests/slic3rutils/CMakeLists.txt b/tests/slic3rutils/CMakeLists.txt index 300344c158..79ae79edfe 100644 --- a/tests/slic3rutils/CMakeLists.txt +++ b/tests/slic3rutils/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(${_TEST_NAME}_tests test_dev_mapping.cpp test_device_access.cpp test_moonraker_printer_agent.cpp + test_moonraker_filament_sources.cpp test_snapmaker_filament_match.cpp test_network_discovery.cpp test_filament_bitmap_utils.cpp diff --git a/tests/slic3rutils/test_moonraker_filament_sources.cpp b/tests/slic3rutils/test_moonraker_filament_sources.cpp new file mode 100644 index 0000000000..55f4fd6de9 --- /dev/null +++ b/tests/slic3rutils/test_moonraker_filament_sources.cpp @@ -0,0 +1,122 @@ +#include +#include + +#include +#include + +#include "slic3r/Utils/MoonrakerFilamentSources.hpp" + +using namespace Slic3r; + +namespace { +struct Tray +{ + bool has_filament; + int slot_index; + std::string material; +}; + +struct Reader +{ + bool success = false; + std::vector result; + int max_index = 0; + int calls = 0; + bool operator()(std::vector& trays, int& max_lane_index) + { + ++calls; + trays = result; + max_lane_index = max_index; + return success; + } +}; + +struct Sources +{ + Reader lanes, hh, cfs; + std::vector trays; + int max_index = 0; + MoonrakerFilamentSource read() + { + return select_moonraker_filament_data( + trays, max_index, [&](auto& out, int& index) { return lanes(out, index); }, + [&](auto& out, int& index) { return hh(out, index); }, [&](auto& out, int& index) { return cfs(out, index); }); + } +}; +} // namespace + +TEST_CASE("Populated lane data keeps priority over Happy Hare and CFS", "[MoonrakerFilamentSources]") +{ + Sources sources; + sources.lanes = {true, {{false, 0, ""}, {true, 4, "PLA"}}, 7}; + sources.hh = {true, {{true, 2, "PETG"}}, 2}; + sources.cfs = {true, {{true, 1, "ABS"}}, 1}; + REQUIRE(sources.read() == MoonrakerFilamentSource::Lanes); + REQUIRE(sources.trays.size() == 2); + CHECK(sources.trays[1].material == "PLA"); + CHECK(sources.max_index == 7); + CHECK(sources.hh.calls == 0); + CHECK(sources.cfs.calls == 0); +} + +TEST_CASE("Empty lane data lets Happy Hare supply loaded gates and keeps the widest topology", "[MoonrakerFilamentSources][Regression]") +{ + Sources sources; + const int loaded_gate = GENERATE(2, 9); + sources.lanes = {true, {{false, 0, ""}, {false, 7, ""}}, 7}; + sources.hh = {true, {{true, loaded_gate, "PETG"}}, loaded_gate}; + REQUIRE(sources.read() == MoonrakerFilamentSource::HappyHare); + REQUIRE(sources.trays.size() == 1); + CHECK(sources.trays.front().has_filament); + CHECK(sources.trays.front().slot_index == loaded_gate); + CHECK(sources.trays.front().material == "PETG"); + CHECK(sources.max_index == (loaded_gate > 7 ? loaded_gate : 7)); + CHECK(sources.cfs.calls == 0); +} + +TEST_CASE("An empty lane topology survives an unavailable Happy Hare and CFS", "[MoonrakerFilamentSources][Regression]") +{ + Sources sources; + sources.lanes = {true, {{false, 0, ""}, {false, 7, ""}}, 7}; + sources.hh = {false, {{true, 19, "discard partial HH"}}, 19}; + sources.cfs = {false, {{true, 23, "discard partial CFS"}}, 23}; + REQUIRE(sources.read() == MoonrakerFilamentSource::EmptyLanes); + CHECK(sources.trays.empty()); + CHECK(sources.max_index == 7); + CHECK(sources.hh.calls == 1); + CHECK(sources.cfs.calls == 1); +} + +TEST_CASE("CFS stays reachable after empty lanes and failed Happy Hare", "[MoonrakerFilamentSources][Regression]") +{ + Sources sources; + sources.lanes = {true, {{false, 7, ""}}, 7}; + sources.hh = {false, {{true, 12, "discard partial HH"}}, 12}; + sources.cfs = {true, {{true, 1, "ABS"}}, 3}; + REQUIRE(sources.read() == MoonrakerFilamentSource::CrealityCfs); + REQUIRE(sources.trays.size() == 1); + CHECK(sources.trays.front().material == "ABS"); + CHECK(sources.max_index == 3); +} + +TEST_CASE("A missing lane namespace leaves the existing fallback sources available", "[MoonrakerFilamentSources]") +{ + Sources sources; + sources.lanes = {false, {{true, 27, "discard invalid lanes"}}, 27}; + sources.hh = {true, {{true, 4, "PETG"}}, 4}; + REQUIRE(sources.read() == MoonrakerFilamentSource::HappyHare); + REQUIRE(sources.trays.size() == 1); + CHECK(sources.max_index == 4); + CHECK(sources.cfs.calls == 0); +} + +TEST_CASE("No successful source leaves no filament system or partial reader data", "[MoonrakerFilamentSources]") +{ + Sources sources; + sources.lanes = {false, {{true, 27, "invalid lanes"}}, 27}; + sources.hh = {false, {{true, 19, "partial HH"}}, 19}; + sources.cfs = {false, {{true, 23, "partial CFS"}}, 23}; + CHECK(sources.read() == MoonrakerFilamentSource::None); + CHECK(sources.trays.empty()); + CHECK(sources.max_index == 0); +}