Implemented UI to show a state of printers in PrusaConnect

This commit is contained in:
YuSanka 2023-12-08 11:32:26 +01:00 committed by David Kocik
parent 2276cedc10
commit e774d25f7a
10 changed files with 170 additions and 57 deletions

View File

@ -1211,6 +1211,12 @@ Preset* PresetCollection::find_preset(const std::string &name, bool first_visibl
first_visible_if_not_found ? &this->first_visible() : nullptr; first_visible_if_not_found ? &this->first_visible() : nullptr;
} }
size_t PresetCollection::get_preset_idx_by_name(const std::string name) const
{
auto it = this->find_preset_internal(name);
return it != m_presets.end() ? it - m_presets.begin() : size_t(-1);
}
// Return index of the first visible preset. Certainly at least the '- default -' preset shall be visible. // Return index of the first visible preset. Certainly at least the '- default -' preset shall be visible.
size_t PresetCollection::first_visible_idx() const size_t PresetCollection::first_visible_idx() const
{ {

View File

@ -437,6 +437,8 @@ public:
const Preset* find_preset(const std::string &name, bool first_visible_if_not_found = false, bool respect_active_preset = true) const const Preset* find_preset(const std::string &name, bool first_visible_if_not_found = false, bool respect_active_preset = true) const
{ return const_cast<PresetCollection*>(this)->find_preset(name, first_visible_if_not_found, respect_active_preset); } { return const_cast<PresetCollection*>(this)->find_preset(name, first_visible_if_not_found, respect_active_preset); }
size_t get_preset_idx_by_name(const std::string preset_name) const;
size_t first_visible_idx() const; size_t first_visible_idx() const;
// Return index of the first compatible preset. Certainly at least the '- default -' preset shall be compatible. // Return index of the first compatible preset. Certainly at least the '- default -' preset shall be compatible.
// If one of the prefered_alternates is compatible, select it. // If one of the prefered_alternates is compatible, select it.

View File

@ -3676,71 +3676,45 @@ void GUI_App::open_wifi_config_dialog(bool forced, const wxString& drive_path/*
m_wifi_config_dialog_shown = false; m_wifi_config_dialog_shown = false;
} }
void GUI_App::select_printer_with_load(Preset* prst, const std::string& preset_name, const std::string& model_name, const std::string& nozzle_name, const std::string& nozzle) bool GUI_App::select_printer_from_connect(const Preset* preset)
{ {
assert(prst); assert(preset);
if (prst->is_visible)
bool suc = get_tab(Preset::Type::TYPE_PRINTER)->select_preset(preset_name);
else {
AppConfig appconfig_new(AppConfig::EAppMode::Editor);
appconfig_new.set_vendors(*app_config);
prst->vendor->models;
if (auto it = std::find_if(prst->vendor->models.begin(), prst->vendor->models.end(), [model_name](const VendorProfile::PrinterModel& a) {
if (a.name == model_name)
return true;
else
return false;
}); it != prst->vendor->models.end())
{
appconfig_new.set_variant("PrusaResearch", it->id, nozzle, true);
app_config->set_vendors(appconfig_new);
preset_bundle->load_presets(*app_config, ForwardCompatibilitySubstitutionRule::EnableSilentDisableSystem, bool is_installed{ false };
{ it->id, nozzle, "", "" });
load_current_presets(); if (!preset->is_visible) {
} size_t preset_id = preset_bundle->printers.get_preset_idx_by_name(preset->name);
assert(preset_id != size_t(-1));
preset_bundle->printers.select_preset(preset_id);
is_installed = true;
} }
get_tab(Preset::Type::TYPE_PRINTER)->select_preset(preset->name);
return is_installed;
} }
void GUI_App::handle_web_request(std::string cmd) void GUI_App::handle_web_request(std::string cmd)
{ {
BOOST_LOG_TRIVIAL(error) << "Handling web request: " << cmd; BOOST_LOG_TRIVIAL(error) << "Handling web request: " << cmd;
// return to plater // return to plater
//this->mainframe->select_tab(size_t(0)); this->mainframe->select_tab(size_t(0));
// parse message // parse message
std::string model_name = plater()->get_user_account()->get_model_from_json(cmd); std::string model_name = plater()->get_user_account()->get_model_from_json(cmd);
std::string nozzle = plater()->get_user_account()->get_nozzle_from_json(cmd); std::string nozzle = plater()->get_user_account()->get_nozzle_from_json(cmd);
std::string nozzle_name = nozzle.empty() ? "" : (nozzle +" nozzle");
assert(!model_name.empty()); assert(!model_name.empty());
assert(!nozzle_name.empty()); if (model_name.empty())
if (model_name.empty() && nozzle_name.empty())
return; return;
// select printer // select printer
std::string preset_name = nozzle.empty() ? model_name : format("%1% %2%",model_name, nozzle_name); const Preset* preset = preset_bundle->printers.find_system_preset_by_model_and_variant(model_name, nozzle);
Preset* prst = preset_bundle->printers.find_preset(preset_name, false); bool is_installed = preset && select_printer_from_connect(preset);
if (!prst) {
model_name = std::string(*preset_bundle->printers.get_preset_name_renamed(model_name));
preset_name = nozzle.empty() ? model_name : format("%1% %2%", model_name, nozzle_name);
prst = preset_bundle->printers.find_preset(preset_name, false);
}
if (!prst) {
preset_name = model_name;
prst = preset_bundle->printers.find_preset(preset_name, false);
}
if (prst) {
select_printer_with_load(prst, preset_name, model_name, nozzle_name, nozzle);
// notification // notification
std::string out = GUI::format("Select Printer:\n%1%", preset_name); std::string out = preset ?
(is_installed ? GUI::format(_L("Installed and Select Printer:\n%1%"), preset->name) :
GUI::format(_L("Select Printer:\n%1%"), preset->name) ):
GUI::format(_L("Printer not found:\n%1%"), model_name);
this->plater()->get_notification_manager()->close_notification_of_type(NotificationType::PrusaAuthUserID); this->plater()->get_notification_manager()->close_notification_of_type(NotificationType::PrusaAuthUserID);
this->plater()->get_notification_manager()->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out); this->plater()->get_notification_manager()->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out);
} else {
// notification
std::string out = GUI::format("Printer not found:\n%1%", preset_name);
this->plater()->get_notification_manager()->close_notification_of_type(NotificationType::PrusaAuthUserID);
this->plater()->get_notification_manager()->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out);
}
} }
} // GUI } // GUI

View File

@ -413,7 +413,8 @@ public:
void request_user_logout() {} void request_user_logout() {}
int request_user_unbind(std::string dev_id) { return 0; } int request_user_unbind(std::string dev_id) { return 0; }
void handle_web_request(std::string cmd); void handle_web_request(std::string cmd);
void select_printer_with_load(Preset* prst, const std::string& preset_name, const std::string& printer_name, const std::string& nozzle_name, const std::string& nozzle ); // return true if preset vas invisible and we have to installed it to make it selectable
bool select_printer_from_connect(const Preset* printer_preset);
void handle_script_message(std::string msg) {} void handle_script_message(std::string msg) {}
void request_model_download(std::string import_json) {} void request_model_download(std::string import_json) {}
void download_project(std::string project_id) {} void download_project(std::string project_id) {}

View File

@ -913,6 +913,8 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
std::string out = GUI::format( "Printers in your PrusaConnect team:\n%1%", text); std::string out = GUI::format( "Printers in your PrusaConnect team:\n%1%", text);
this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID);
this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out); this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out);
sidebar->update_printer_presets_combobox();
}); });
wxGetApp().other_instance_message_handler()->init(this->q); wxGetApp().other_instance_message_handler()->init(this->q);

View File

@ -20,6 +20,7 @@
#include <wx/menu.h> #include <wx/menu.h>
#include <wx/odcombo.h> #include <wx/odcombo.h>
#include <wx/listbook.h> #include <wx/listbook.h>
#include <wx/generic/stattextg.h>
#ifdef _WIN32 #ifdef _WIN32
#include <wx/msw/dcclient.h> #include <wx/msw/dcclient.h>
@ -44,6 +45,7 @@
#include "BitmapCache.hpp" #include "BitmapCache.hpp"
#include "PhysicalPrinterDialog.hpp" #include "PhysicalPrinterDialog.hpp"
#include "MsgDialog.hpp" #include "MsgDialog.hpp"
#include "UserAccount.hpp"
#include "Widgets/ComboBox.hpp" #include "Widgets/ComboBox.hpp"
@ -618,6 +620,12 @@ bool PresetComboBox::selection_is_changed_according_to_physical_printers()
// *** PlaterPresetComboBox *** // *** PlaterPresetComboBox ***
// --------------------------------- // ---------------------------------
static bool is_active_connect()
{
auto user_account = wxGetApp().plater()->get_user_account();
return user_account && user_account->is_logged();
}
PlaterPresetComboBox::PlaterPresetComboBox(wxWindow *parent, Preset::Type preset_type) : PlaterPresetComboBox::PlaterPresetComboBox(wxWindow *parent, Preset::Type preset_type) :
PresetComboBox(parent, preset_type, wxSize(15 * wxGetApp().em_unit(), -1)) PresetComboBox(parent, preset_type, wxSize(15 * wxGetApp().em_unit(), -1))
{ {
@ -657,6 +665,9 @@ PlaterPresetComboBox::PlaterPresetComboBox(wxWindow *parent, Preset::Type preset
else else
switch_to_tab(); switch_to_tab();
}); });
if (m_type == Preset::TYPE_PRINTER)
connect_info = new wxGenericStaticText(parent, wxID_ANY, "Info about <b>Connect</b> for printer preset");
} }
PlaterPresetComboBox::~PlaterPresetComboBox() PlaterPresetComboBox::~PlaterPresetComboBox()
@ -832,6 +843,83 @@ wxString PlaterPresetComboBox::get_preset_name(const Preset& preset)
return from_u8(name + suffix(preset)); return from_u8(name + suffix(preset));
} }
struct PrinterStatesCount
{
size_t offline_cnt { 0 };
size_t busy_cnt { 0 };
size_t available_cnt { 0 };
size_t total { 0 };
};
static PrinterStatesCount get_printe_states_count(const std::vector<size_t>& states)
{
PrinterStatesCount states_cnt;
for (size_t i = 0; i < states.size(); i++) {
if (states[i] == 0)
continue;
ConnectPrinterState state = static_cast<ConnectPrinterState>(i);
if (state == ConnectPrinterState::CONNECT_PRINTER_OFFLINE)
states_cnt.offline_cnt += states[i];
else if (state == ConnectPrinterState::CONNECT_PRINTER_PAUSED ||
state == ConnectPrinterState::CONNECT_PRINTER_STOPED ||
state == ConnectPrinterState::CONNECT_PRINTER_PRINTING)
states_cnt.busy_cnt += states[i];
else
states_cnt.available_cnt += states[i];
}
states_cnt.total = states_cnt.offline_cnt + states_cnt.busy_cnt + states_cnt.available_cnt;
return states_cnt;
}
static std::string get_connect_state_suffix_for_printer(const Preset& printer_preset)
{
// process real data from Connect
if (auto printer_state_map = wxGetApp().plater()->get_user_account()->get_printer_state_map();
!printer_state_map.empty()) {
for (const auto& [printer_model_id, states] : printer_state_map) {
if (printer_model_id == printer_preset.config.opt_string("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";
}
}
}
return "";
}
static wxString get_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_id, states] : printer_state_map) {
if (printer_model_id == printer_preset.config.opt_string("printer_model")) {
PrinterStatesCount states_cnt = get_printe_states_count(states);
return format_wxstr(_L("Available: %1%, Offline: %2%, Busy: %3%. Total: %4% printers"),
format("<b><span color=\"green\">%1%</span></b>" , states_cnt.available_cnt),
format("<b><span color=\"red\">%1%</span></b>" , states_cnt.offline_cnt),
format("<b><span color=\"yellow\">%1%</span></b>", states_cnt.busy_cnt),
format("<b>%1%</b>", states_cnt.total));
}
}
}
return " "; // to correct update of strinh height don't use empty string
}
// Only the compatible presets are shown. // Only the compatible presets are shown.
// If an incompatible preset is selected, it is shown as well. // If an incompatible preset is selected, it is shown as well.
void PlaterPresetComboBox::update() void PlaterPresetComboBox::update()
@ -904,6 +992,12 @@ void PlaterPresetComboBox::update()
std::string bitmap_key, filament_rgb, extruder_rgb, material_rgb; std::string bitmap_key, filament_rgb, extruder_rgb, material_rgb;
std::string bitmap_type_name = bitmap_key = m_type == Preset::TYPE_PRINTER && preset.printer_technology() == ptSLA ? "sla_printer" : m_main_bitmap_name; std::string bitmap_type_name = bitmap_key = m_type == Preset::TYPE_PRINTER && preset.printer_technology() == ptSLA ? "sla_printer" : m_main_bitmap_name;
if (m_type == Preset::TYPE_PRINTER) {
bitmap_type_name = bitmap_key += get_connect_state_suffix_for_printer(preset);
if (is_selected)
connect_info->SetLabelMarkup(get_connect_info_line(preset));
}
bool single_bar = false; bool single_bar = false;
if (m_type == Preset::TYPE_FILAMENT) if (m_type == Preset::TYPE_FILAMENT)
{ {
@ -1032,6 +1126,8 @@ void PlaterPresetComboBox::update()
validate_selection(data.selected); validate_selection(data.selected);
} }
} }
connect_info->Show(is_active_connect());
} }
if (m_type == Preset::TYPE_PRINTER || m_type == Preset::TYPE_FILAMENT || m_type == Preset::TYPE_SLA_MATERIAL) { if (m_type == Preset::TYPE_PRINTER || m_type == Preset::TYPE_FILAMENT || m_type == Preset::TYPE_SLA_MATERIAL) {

View File

@ -16,6 +16,7 @@
class wxString; class wxString;
class wxTextCtrl; class wxTextCtrl;
class wxStaticText; class wxStaticText;
class wxGenericStaticText;
class ScalableButton; class ScalableButton;
class wxBoxSizer; class wxBoxSizer;
class wxComboBox; class wxComboBox;
@ -153,6 +154,7 @@ public:
~PlaterPresetComboBox(); ~PlaterPresetComboBox();
ScalableButton* edit_btn { nullptr }; ScalableButton* edit_btn { nullptr };
wxGenericStaticText* connect_info { nullptr };
void set_extruder_idx(const int extr_idx) { m_extruder_idx = extr_idx; } void set_extruder_idx(const int extr_idx) { m_extruder_idx = extr_idx; }
int get_extruder_idx() const { return m_extruder_idx; } int get_extruder_idx() const { return m_extruder_idx; }

View File

@ -32,6 +32,7 @@
#include <wx/statbox.h> #include <wx/statbox.h>
#include <wx/statbmp.h> #include <wx/statbmp.h>
#include <wx/wupdlock.h> #include <wx/wupdlock.h>
#include "wx/generic/stattextg.h"
#ifdef _WIN32 #ifdef _WIN32
#include <wx/richtooltip.h> #include <wx/richtooltip.h>
#include <wx/custombgwin.h> #include <wx/custombgwin.h>
@ -381,6 +382,9 @@ Sidebar::Sidebar(Plater *parent)
wxBOTTOM, 1); wxBOTTOM, 1);
(void)margin_5; // supress unused capture warning (void)margin_5; // supress unused capture warning
#endif // __WXGTK3__ #endif // __WXGTK3__
if ((*combo)->connect_info)
sizer_presets->Add((*combo)->connect_info, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT | wxBOTTOM,
int(0.3 * wxGetApp().em_unit()));
} else { } else {
sizer_filaments->Add(combo_and_btn_sizer, 0, wxEXPAND | sizer_filaments->Add(combo_and_btn_sizer, 0, wxEXPAND |
#ifdef __WXGTK3__ #ifdef __WXGTK3__
@ -603,6 +607,12 @@ void Sidebar::update_all_preset_comboboxes()
} }
} }
void Sidebar::update_printer_presets_combobox()
{
m_combo_printer->update();
Layout();
}
void Sidebar::update_presets(Preset::Type preset_type) void Sidebar::update_presets(Preset::Type preset_type)
{ {
PresetBundle &preset_bundle = *wxGetApp().preset_bundle; PresetBundle &preset_bundle = *wxGetApp().preset_bundle;

View File

@ -133,6 +133,7 @@ public:
void update_objects_list_extruder_column(size_t extruders_count); void update_objects_list_extruder_column(size_t extruders_count);
void update_presets(Preset::Type preset_type); void update_presets(Preset::Type preset_type);
void update_mode_markers(); void update_mode_markers();
void update_printer_presets_combobox();
void msw_rescale(); void msw_rescale();
void sys_color_changed(); void sys_color_changed();

View File

@ -12,9 +12,12 @@ namespace GUI{
enum class ConnectPrinterState { enum class ConnectPrinterState {
CONNECT_PRINTER_OFFLINE, CONNECT_PRINTER_OFFLINE,
CONNECT_PRINTER_IDLE,
CONNECT_PRINTER_PRINTING, CONNECT_PRINTER_PRINTING,
CONNECT_PRINTER_PAUSED,//?
CONNECT_PRINTER_STOPED,//?
CONNECT_PRINTER_IDLE,
CONNECT_PRINTER_FINISHED, CONNECT_PRINTER_FINISHED,
CONNECT_PRINTER_READY, //?
CONNECT_PRINTER_STATE_COUNT CONNECT_PRINTER_STATE_COUNT
}; };
@ -61,17 +64,33 @@ private:
ConnectPrinterStateMap m_printer_map; ConnectPrinterStateMap m_printer_map;
const std::map<std::string, std::string> printer_type_and_name_table = { const std::map<std::string, std::string> printer_type_and_name_table = {
{"1.3.0", "Original Prusa i3 MK3"}, {"1.3.0", "MK3" },
{"1.3.1", "Original Prusa i3 MK3S & MK3S+"}, {"1.3.1", "MK3S" },
{"1.4.0", "Original Prusa MK4"}, {"1.4.0", "MK4" },
{"2.1.0", "Original Prusa MINI & MINI+"}, {"2.1.0", "MINI" },
// ysFIXME : needs to add Connect ids for next printers
{"0.0.0", "MK4IS" },
{"0.0.0", "MK3SMMU2S" },
{"0.0.0", "MK3MMU2" },
{"0.0.0", "XL" },
{"0.0.0", "MK2.5S" },
{"0.0.0", "MK2.5" },
{"0.0.0", "MK2.5SMMU2S" },
{"0.0.0", "MK2.5MMU2" },
{"0.0.0", "MK2S" },
{"0.0.0", "MK2SMM" },
{"0.0.0", "SL1" },
{"0.0.0", "SL1S" },
}; };
const std::map<std::string, ConnectPrinterState> printer_state_table = { const std::map<std::string, ConnectPrinterState> printer_state_table = {
{"OFFLINE" , ConnectPrinterState::CONNECT_PRINTER_OFFLINE}, {"OFFLINE" , ConnectPrinterState::CONNECT_PRINTER_OFFLINE},
{"IDLE" , ConnectPrinterState::CONNECT_PRINTER_IDLE},
{"PRINTING" , ConnectPrinterState::CONNECT_PRINTER_PRINTING}, {"PRINTING" , ConnectPrinterState::CONNECT_PRINTER_PRINTING},
{"PAUSED" , ConnectPrinterState::CONNECT_PRINTER_PAUSED},
{"STOPED" , ConnectPrinterState::CONNECT_PRINTER_STOPED},
{"IDLE" , ConnectPrinterState::CONNECT_PRINTER_IDLE},
{"FINISHED" , ConnectPrinterState::CONNECT_PRINTER_FINISHED}, {"FINISHED" , ConnectPrinterState::CONNECT_PRINTER_FINISHED},
{"READY" , ConnectPrinterState::CONNECT_PRINTER_READY},
}; };
}; };
}} // namespace slic3r::GUI }} // namespace slic3r::GUI