Save binded local machines

This commit is contained in:
Noisyfox
2025-01-31 11:35:35 +08:00
parent 82ce8c94b4
commit 2e64cc2f7e
5 changed files with 129 additions and 12 deletions

View File

@@ -607,6 +607,19 @@ std::string AppConfig::load()
for (auto& j_model : it.value()) {
m_printer_settings[j_model["machine"].get<std::string>()] = j_model;
}
} else if (it.key() == "local_machines") {
for (auto m = it.value().begin(); m != it.value().end(); ++m) {
const auto& p = m.value();
BBLocalMachine local_machine;
local_machine.dev_id = m.key();
if (p.contains("dev_name"))
local_machine.dev_name = p["dev_name"].get<std::string>();
if (p.contains("dev_ip"))
local_machine.dev_ip = p["dev_ip"].get<std::string>();
if (p.contains("printer_type"))
local_machine.printer_type = p["printer_type"].get<std::string>();
m_local_machines[local_machine.dev_id] = local_machine;
}
} else {
if (it.value().is_object()) {
for (auto iter = it.value().begin(); iter != it.value().end(); iter++) {
@@ -783,6 +796,14 @@ void AppConfig::save()
for (const auto& preset : m_printer_settings) {
j["orca_presets"].push_back(preset.second);
}
for (const auto& local_machine : m_local_machines) {
json m_json;
m_json["dev_name"] = local_machine.second.dev_name;
m_json["dev_ip"] = local_machine.second.dev_ip;
m_json["printer_type"] = local_machine.second.printer_type;
j["local_machines"][local_machine.first] = m_json;
}
boost::nowide::ofstream c;
c.open(path_pid, std::ios::out | std::ios::trunc);
c << std::setw(4) << j << std::endl;
@@ -791,7 +812,7 @@ void AppConfig::save()
// WIN32 specific: The final "rename_file()" call is not safe in case of an application crash, there is no atomic "rename file" API
// provided by Windows (sic!). Therefore we save a MD5 checksum to be able to verify file corruption. In addition,
// we save the config file into a backup first before moving it to the final destination.
c << appconfig_md5_hash_line({j.dump(4)});
c << appconfig_md5_hash_line(j.dump(4));
#endif
c.close();

View File

@@ -24,6 +24,22 @@ using namespace nlohmann;
namespace Slic3r {
// Connected LAN mode BambuLab printer
struct BBLocalMachine
{
std::string dev_name;
std::string dev_ip;
std::string dev_id; /* serial number */
std::string printer_type; /* model_id */
bool operator==(const BBLocalMachine& other) const
{
return dev_name == other.dev_name && dev_ip == other.dev_ip && dev_id == other.dev_id && printer_type == other.printer_type;
}
bool operator!=(const BBLocalMachine& other) const { return !operator==(other); }
};
class AppConfig
{
public:
@@ -152,7 +168,8 @@ public:
{
auto it = m_storage.find(section);
if (it != m_storage.end()) {
it->second.erase(key);
it->second.erase(key);
m_dirty = true;
}
}
@@ -194,11 +211,34 @@ public:
return "";
return m_printer_settings[printer][name];
}
std::string set_printer_setting(std::string printer, std::string name, std::string value) {
return m_printer_settings[printer][name] = value;
m_dirty = true;
void set_printer_setting(std::string printer, std::string name, std::string value) {
m_printer_settings[printer][name] = value;
m_dirty = true;
}
const std::map<std::string, BBLocalMachine>& get_local_machines() const { return m_local_machines; }
void erase_local_machine(std::string dev_id)
{
auto it = m_local_machines.find(dev_id);
if (it != m_local_machines.end()) {
m_local_machines.erase(it);
m_dirty = true;
}
}
void update_local_machine(const BBLocalMachine& machine)
{
auto it = m_local_machines.find(machine.dev_id);
if (it != m_local_machines.end()) {
const auto& current = it->second;
if (machine != current) {
m_local_machines[machine.dev_id] = machine;
m_dirty = true;
}
} else {
m_local_machines[machine.dev_id] = machine;
m_dirty = true;
}
}
const std::vector<std::string> &get_filament_presets() const { return m_filament_presets; }
void set_filament_presets(const std::vector<std::string> &filament_presets){
@@ -335,6 +375,8 @@ private:
std::vector<std::string> m_filament_colors;
std::vector<PrinterCaliInfo> m_printer_cali_infos;
std::map<std::string, BBLocalMachine> m_local_machines;
};
} // namespace Slic3r