Preset name as identifier 2

This commit is contained in:
David Kocik 2024-12-11 15:25:14 +01:00 committed by Lukas Matena
parent fcc3d2c3fc
commit 9aea5bc054
5 changed files with 80 additions and 154 deletions

View File

@ -977,38 +977,18 @@ static std::string get_connect_state_suffix_for_printer(const Preset& printer_pr
if (auto printer_state_map = wxGetApp().plater()->get_user_account()->get_printer_state_map();
!printer_state_map.empty()) {
for (const auto& [printer_model_nozzle_pair, states] : printer_state_map) {
std::string printer_model = printer_preset.config.opt_string("printer_model");
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(printer_preset);
printer_model = printer_preset.trim_vendor_repo_prefix(printer_model, printer_with_vendor.vendor);
if (printer_preset.config.has("nozzle_diameter")) {
double nozzle_diameter = static_cast<const ConfigOptionFloats*>(printer_preset.config.option("nozzle_diameter"))->values[0];
wxString nozzle_diameter_serialized = double_to_string(nozzle_diameter);
nozzle_diameter_serialized.Replace(L",", L".");
if (printer_model_nozzle_pair.first == printer_model
&& printer_model_nozzle_pair.second == GUI::into_u8(nozzle_diameter_serialized))
{
PrinterStatesCount states_cnt = get_printe_states_count(states);
if (states_cnt.available_cnt > 0)
return "_available";
if (states_cnt.busy_cnt > 0)
return "_busy";
return "_offline";
}
} else {
if (printer_model_nozzle_pair.first == printer_model) {
PrinterStatesCount states_cnt = get_printe_states_count(states);
if (states_cnt.available_cnt > 0)
return "_available";
if (states_cnt.busy_cnt > 0)
return "_busy";
return "_offline";
}
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(printer_preset);
const std::string trimmed_preset_name = printer_preset.trim_vendor_repo_prefix(printer_preset.name, printer_with_vendor.vendor);
for (const auto& [preset_name_from_map, states] : printer_state_map) {
if (trimmed_preset_name != preset_name_from_map) {
continue;
}
PrinterStatesCount states_cnt = get_printe_states_count(states);
if (states_cnt.available_cnt > 0)
return "_available";
if (states_cnt.busy_cnt > 0)
return "_busy";
return "_offline";
}
}
@ -1029,47 +1009,24 @@ static bool fill_data_to_connect_info_line( const Preset& printer_preset,
if (auto printer_state_map = wxGetApp().plater()->get_user_account()->get_printer_state_map();
!printer_state_map.empty()) {
for (const auto& [printer_model_nozzle_pair, states] : printer_state_map) {
// get printer_model without repo prefix
std::string printer_model = printer_preset.config.opt_string("printer_model");
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(printer_preset);
printer_model = printer_preset.trim_vendor_repo_prefix(printer_model, printer_with_vendor.vendor);
if (printer_preset.config.has("nozzle_diameter")) {
double nozzle_diameter = static_cast<const ConfigOptionFloats*>(printer_preset.config.option("nozzle_diameter"))->values[0];
wxString nozzle_diameter_serialized = double_to_string(nozzle_diameter);
nozzle_diameter_serialized.Replace(L",", L".");
if (printer_model_nozzle_pair.first == printer_model
&& printer_model_nozzle_pair.second == GUI::into_u8(nozzle_diameter_serialized))
{
PrinterStatesCount states_cnt = get_printe_states_count(states);
#ifdef _WIN32
connect_available_info->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.available_cnt), _L("available")));
connect_offline_info ->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.offline_cnt), _L("offline")));
connect_printing_info ->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.busy_cnt), _L("printing")));
#else
connect_available_info->SetLabel(format_wxstr("%1% ", states_cnt.available_cnt));
connect_offline_info ->SetLabel(format_wxstr("%1% ", states_cnt.offline_cnt));
connect_printing_info ->SetLabel(format_wxstr("%1% ", states_cnt.busy_cnt));
#endif
return true;
}
} else {
if (printer_model_nozzle_pair.first == printer_model) {
PrinterStatesCount states_cnt = get_printe_states_count(states);
#ifdef _WIN32
connect_available_info->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.available_cnt), _L("available")));
connect_offline_info ->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.offline_cnt), _L("offline")));
connect_printing_info ->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.busy_cnt), _L("printing")));
#else
connect_available_info->SetLabel(format_wxstr("%1% ", states_cnt.available_cnt));
connect_offline_info ->SetLabel(format_wxstr("%1% ", states_cnt.offline_cnt));
connect_printing_info ->SetLabel(format_wxstr("%1% ", states_cnt.busy_cnt));
#endif
return true;
}
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(printer_preset);
const std::string trimmed_preset_name = printer_preset.trim_vendor_repo_prefix(printer_preset.name, printer_with_vendor.vendor);
for (const auto& [preset_name_from_map, states] : printer_state_map) {
if (trimmed_preset_name != preset_name_from_map) {
continue;
}
PrinterStatesCount states_cnt = get_printe_states_count(states);
#ifdef _WIN32
connect_available_info->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.available_cnt), _L("available")));
connect_offline_info ->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.offline_cnt), _L("offline")));
connect_printing_info ->SetLabelMarkup(format_wxstr("%1% %2%", format("<b>%1%</b>", states_cnt.busy_cnt), _L("printing")));
#else
connect_available_info->SetLabel(format_wxstr("%1% ", states_cnt.available_cnt));
connect_offline_info ->SetLabel(format_wxstr("%1% ", states_cnt.offline_cnt));
connect_printing_info ->SetLabel(format_wxstr("%1% ", states_cnt.busy_cnt));
#endif
return true;
}
}
return false;

View File

@ -269,40 +269,36 @@ bool UserAccount::on_connect_uiid_map_success(const std::string& data, AppConfig
return false;
}
for (const auto& printer_tree : printers_ptree) {
const auto printer_uuid = printer_tree.second.get_optional<std::string>("uuid");
for (const auto& printer_sub : printers_ptree) {
const pt::ptree &printer_ptree = printer_sub.second;
const auto printer_uuid = printer_ptree.get_optional<std::string>("uuid");
if (!printer_uuid) {
continue;
}
const auto printer_model = printer_tree.second.get_optional<std::string>("printer_model");
const auto printer_model = printer_ptree.get_optional<std::string>("printer_model");
if (!printer_model) {
continue;
}
std::map<std::string, std::vector<std::string>> config_options_to_match;
UserAccountUtils::fill_config_options_from_json(ptree, config_options_to_match);
UserAccountUtils::fill_config_options_from_json(printer_ptree, config_options_to_match);
const Preset* printer_preset = UserAccountUtils::find_preset_by_nozzle_and_options(wxGetApp().preset_bundle->printers, *printer_model, config_options_to_match);
BOOST_LOG_TRIVIAL(error) << (printer_preset ? printer_preset->name : std::string("UNKNOWN")) << " " << *printer_uuid;
if (printer_preset) {
m_printer_uuid_map[*printer_uuid] = printer_preset->name;
// Preset can have repo prefix
std::string trimmed_name = printer_preset->config.opt_string("printer_model");
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(*printer_preset);
trimmed_name = printer_preset->trim_vendor_repo_prefix(trimmed_name, printer_with_vendor.vendor);
m_printer_uuid_map[*printer_uuid] = trimmed_name;
BOOST_LOG_TRIVIAL(error) << trimmed_name << " " << *printer_uuid;
} else {
//assert(false);
BOOST_LOG_TRIVIAL(error) << "Failed to find preset for printer model: " << *printer_model;
}
/*
const auto nozzle_diameter_opt = printer_tree.second.get_optional<std::string>("nozzle_diameter");
const std::string nozzle_diameter = (nozzle_diameter_opt && *nozzle_diameter_opt != "0.0") ? *nozzle_diameter_opt : std::string();
std::pair<std::string, std::string> model_nozzle_pair = { *printer_model, nozzle_diameter };
m_printer_uuid_map[*printer_uuid] = model_nozzle_pair;
*/
}
m_communication->on_uuid_map_success();
return on_connect_printers_success(data, app_config, out_printers_changed);
}
std::string UserAccount::get_current_printer_uuid_from_connect(const std::string &selected_printer_id
) const {
std::string UserAccount::get_current_printer_uuid_from_connect(const std::string &selected_printer_id) const {
if (m_current_printer_data_json_from_connect.empty() || m_current_printer_uuid_from_connect.empty()) {
return {};
}

View File

@ -305,7 +305,7 @@ std::string UserAccountCommunication::get_shared_session_key()
void UserAccountCommunication::set_polling_enabled(bool enabled)
{
return m_session->set_polling_action(enabled ? UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS : UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY);
return m_session->set_polling_action(enabled ? UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS : UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY);
}
void UserAccountCommunication::on_uuid_map_success()

View File

@ -42,10 +42,10 @@ pt::ptree parse_tree_for_subtree(const pt::ptree& tree, const std::string& param
return pt::ptree();
}
void json_to_ptree(boost::property_tree::ptree& ptree, const std::string& json) {
void json_to_ptree(boost::property_tree::ptree& out_ptree, const std::string& json) {
try {
std::stringstream ss(json);
pt::read_json(ss, ptree);
pt::read_json(ss, out_ptree);
} catch (const std::exception &e) {
BOOST_LOG_TRIVIAL(error) << "Failed to parse json to ptree. " << e.what();
BOOST_LOG_TRIVIAL(error) << "json: " << json;
@ -54,7 +54,7 @@ void json_to_ptree(boost::property_tree::ptree& ptree, const std::string& json)
} // namespace
std::string get_nozzle_from_json(boost::property_tree::ptree& ptree) {
std::string get_nozzle_from_json(const boost::property_tree::ptree& ptree) {
assert(!ptree.empty());
std::string out = parse_tree_for_param(ptree, "nozzle_diameter");
@ -78,7 +78,7 @@ std::string get_keyword_from_json(boost::property_tree::ptree& ptree, const std:
return parse_tree_for_param(ptree, keyword);
}
void fill_supported_printer_models_from_json(boost::property_tree::ptree& ptree, std::vector<std::string>& result)
void fill_supported_printer_models_from_json(const boost::property_tree::ptree& ptree, std::vector<std::string>& result)
{
assert(!ptree.empty());
std::string printer_model = parse_tree_for_param(ptree, "printer_model");
@ -107,7 +107,7 @@ std::string json_var_to_opt_string(const std::string& json_var)
return json_var;
}
void fill_config_options_from_json_inner(boost::property_tree::ptree& ptree, std::map<std::string, std::vector<std::string>>& result, const std::map<std::string, std::string>& parameters)
void fill_config_options_from_json_inner(const boost::property_tree::ptree& ptree, std::map<std::string, std::vector<std::string>>& result, const std::map<std::string, std::string>& parameters)
{
pt::ptree slots = parse_tree_for_subtree(ptree, "tools");
for (const auto &subtree : slots) {
@ -143,7 +143,7 @@ void fill_config_options_from_json_inner(boost::property_tree::ptree& ptree, std
}
}
void fill_config_options_from_json(boost::property_tree::ptree& ptree, std::map<std::string, std::vector<std::string>>& result)
void fill_config_options_from_json(const boost::property_tree::ptree& ptree, std::map<std::string, std::vector<std::string>>& result)
{
assert(!ptree.empty());
/*
@ -334,72 +334,45 @@ const Preset* find_preset_by_nozzle_and_options(
for (const Preset &preset : collection) {
// trim repo prefix
std::string printer_model = preset.config.opt_string("printer_model");
std::string vendor_repo_prefix;
if (preset.vendor) {
vendor_repo_prefix = preset.vendor->repo_prefix;
} else if (std::string inherits = preset.inherits(); !inherits.empty()) {
const Preset *parent = collection.find_preset(inherits);
if (parent && parent->vendor) {
vendor_repo_prefix = parent->vendor->repo_prefix;
}
}
if (printer_model.find(vendor_repo_prefix) == 0) {
printer_model = printer_model.substr(vendor_repo_prefix.size()
);
boost::trim_left(printer_model);
}
const PresetWithVendorProfile& printer_with_vendor = collection.get_preset_with_vendor_profile(preset);
printer_model = preset.trim_vendor_repo_prefix(printer_model, printer_with_vendor.vendor);
if (!preset.is_system || printer_model != model_id)
continue;
if (preset.printer_technology() == ptFFF) {
// options (including nozzle_diameter)
bool failed = false;
for (const auto& opt : options) {
assert(preset.config.has(opt.first));
// We compare only first value now, but options contains data for all (some might be empty tho)
std::string opt_val;
if (preset.config.option(opt.first)->is_scalar()) {
opt_val = preset.config.option(opt.first)->serialize();
} else {
switch (preset.config.option(opt.first)->type()) {
case coInts: opt_val = std::to_string(static_cast<const ConfigOptionInts*>(preset.config.option(opt.first))->values[0]); break;
case coFloats:
opt_val = GUI::into_u8(double_to_string(static_cast<const ConfigOptionFloats*>(preset.config.option(opt.first))->values[0]));
if (size_t pos = opt_val.find(",") != std::string::npos)
opt_val.replace(pos, 1, 1, '.');
break;
case coStrings: opt_val = static_cast<const ConfigOptionStrings*>(preset.config.option(opt.first))->values[0]; break;
case coBools: opt_val = static_cast<const ConfigOptionBools*>(preset.config.option(opt.first))->values[0] ? "1" : "0"; break;
default:
assert(false);
continue;
}
}
if (opt_val != opt.second[0])
{
failed = true;
// options (including nozzle_diameter)
bool failed = false;
for (const auto& opt : options) {
assert(preset.config.has(opt.first));
// We compare only first value now, but options contains data for all (some might be empty tho)
std::string opt_val;
if (preset.config.option(opt.first)->is_scalar()) {
opt_val = preset.config.option(opt.first)->serialize();
} else {
switch (preset.config.option(opt.first)->type()) {
case coInts: opt_val = std::to_string(static_cast<const ConfigOptionInts*>(preset.config.option(opt.first))->values[0]); break;
case coFloats:
opt_val = GUI::into_u8(double_to_string(static_cast<const ConfigOptionFloats*>(preset.config.option(opt.first))->values[0]));
if (size_t pos = opt_val.find(",") != std::string::npos)
opt_val.replace(pos, 1, 1, '.');
break;
case coStrings: opt_val = static_cast<const ConfigOptionStrings*>(preset.config.option(opt.first))->values[0]; break;
case coBools: opt_val = static_cast<const ConfigOptionBools*>(preset.config.option(opt.first))->values[0] ? "1" : "0"; break;
default:
assert(false);
continue;
}
}
if (!failed) {
results.push_back(&preset);
}
} else {
if (opt_val != opt.second[0])
{
failed = true;
break;
}
}
}
// find visible without prefix
for (const Preset *preset : results) {
if (preset->is_visible && preset->config.opt_string("printer_model") == model_id) {
return preset;
}
}
// find one visible
for (const Preset *preset : results) {
if (preset->is_visible) {
return preset;
if (!failed) {
results.push_back(&preset);
}
}
// find one without prefix
for (const Preset* preset : results) {

View File

@ -17,9 +17,9 @@ namespace UserAccountUtils {
// If ptree is non-epty, json parameter is not used.
std::string get_keyword_from_json(boost::property_tree::ptree& ptree, const std::string& json, const std::string& keyword);
// Only ptree is passed since these functions are called on places that already has the ptree from get_keyword_from_json call
std::string get_nozzle_from_json(boost::property_tree::ptree &ptree);
void fill_supported_printer_models_from_json(boost::property_tree::ptree& ptree, std::vector<std::string>& result);
void fill_config_options_from_json(boost::property_tree::ptree& ptree, std::map<std::string,std::vector<std::string>>& result);
std::string get_nozzle_from_json(const boost::property_tree::ptree &ptree);
void fill_supported_printer_models_from_json(const boost::property_tree::ptree& ptree, std::vector<std::string>& result);
void fill_config_options_from_json(const boost::property_tree::ptree& ptree, std::map<std::string,std::vector<std::string>>& result);
// Since fill_material_from_json is called only from one place where ptree doesnt need to be shared, it is not always read from json.
void fill_material_from_json(const std::string& json, std::vector<std::string>& material_result, std::vector<bool>& avoid_abrasive_result);