forked from bambulab/BambuStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceManager.cpp
More file actions
5008 lines (4396 loc) · 193 KB
/
Copy pathDeviceManager.cpp
File metadata and controls
5008 lines (4396 loc) · 193 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "libslic3r/libslic3r.h"
#include "DeviceManager.hpp"
#include "libslic3r/Time.hpp"
#include "libslic3r/Thread.hpp"
#include "slic3r/Utils/BBLUtil.hpp"
#include "slic3r/Utils/NetworkAgent.hpp"
#include "GuiColor.hpp"
#include "GUI_App.hpp"
#include "MainFrame.hpp"
#include "MsgDialog.hpp"
#include "DeviceErrorDialog.hpp"
#include "Plater.hpp"
#include "GUI_App.hpp"
#include "ReleaseNote.hpp"
#include <thread>
#include <mutex>
#include <codecvt>
#include <boost/foreach.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/algorithm/string.hpp>
/* mac need the macro while including <boost/stacktrace.hpp>*/
#ifdef __APPLE__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#endif
#include <boost/stacktrace.hpp>
#include <wx/dir.h>
#include "fast_float/fast_float.h"
#include "DeviceCore/DevAxis.h"
#include "DeviceCore/DevChamber.h"
#include "DeviceCore/DevFilaSystem.h"
#include "DeviceCore/DevFilaSwitch.h"
#include "DeviceCore/DevExtensionTool.h"
#include "DeviceCore/DevExtruderSystem.h"
#include "DeviceCore/DevNozzleSystem.h"
#include "DeviceCore/DevMappingNozzle.h"
#include "DeviceCore/DevBed.h"
#include "DeviceCore/DevLamp.h"
#include "DeviceCore/DevFan.h"
#include "DeviceCore/DevStatus.h"
#include "DeviceCore/DevStorage.h"
#include "DeviceCore/DevNozzleRack.h"
#include "DeviceCore/DevConfig.h"
#include "DeviceCore/DevCtrl.h"
#include "DeviceCore/DevInfo.h"
#include "DeviceCore/DevPrintOptions.h"
#include "DeviceCore/DevPrintTaskInfo.h"
#include "DeviceCore/DevHMS.h"
#include "DeviceCore/DevUpgrade.h"
#include "DeviceCore/DevMapping.h"
#include "DeviceCore/DevManager.h"
#include "DeviceCore/DevUtil.h"
#define CALI_DEBUG
#define MINUTE_30 1800000 //ms
#define TIME_OUT 5000 //ms
namespace pt = boost::property_tree;
float string_to_float(const std::string& str_value) {
float value = 0.0;
fast_float::from_chars(str_value.c_str(), str_value.c_str() + str_value.size(), value);
return value;
}
int get_tray_id_by_ams_id_and_slot_id(int ams_id, int slot_id)
{
if (ams_id == VIRTUAL_TRAY_MAIN_ID || ams_id == VIRTUAL_TRAY_DEPUTY_ID) {
return ams_id;
} else {
return ams_id * 4 + slot_id;
}
}
namespace {
// Stringing-prone filament IDs per nozzle-diameter bucket.
// Mirrors the printer firmware tables (see g_leak_pron_idx_for_0_4 / _0_6_0_8).
// Keep these in sync with firmware when new stringing-prone filaments are added.
const std::unordered_set<std::string> g_stringing_prone_for_0_4 = {
"GFA11", // Bambu PLA Aero
"GFU90", // Bambu TPU 90A
"GFU00", // Bambu TPU 95A HF
"GFU02", // Generic TPU for AMS
"GFU98", // Bambu TPU for AMS
};
const std::unordered_set<std::string> g_stringing_prone_for_0_6_0_8 = {
"GFA11", // Bambu PLA Aero
"GFU00", // Bambu TPU 95A HF
};
// Pick the right table for the given nozzle diameter; returns nullptr if the
// nozzle bucket has no entries (e.g. 0.2 mm) or the diameter is invalid.
const std::unordered_set<std::string>* pick_stringing_set(float nozzle_diameter)
{
if (!(nozzle_diameter > 0.f)) return nullptr;
if (nozzle_diameter < 0.3f) return nullptr; // 0.2 nozzle: empty
if (nozzle_diameter < 0.5f) return &g_stringing_prone_for_0_4;
return &g_stringing_prone_for_0_6_0_8; // 0.6 / 0.8 nozzles
}
} // namespace
bool Slic3r::is_stringing_prone_filament(const std::string& filament_id, float nozzle_diameter)
{
if (filament_id.empty()) return false;
const auto* set = pick_stringing_set(nozzle_diameter);
if (!set) return false;
return set->count(filament_id) > 0;
}
wxString Slic3r::get_stage_string(int stage)
{
switch(stage) {
case 0:
return _L("Printing");
case 1:
return _L("Auto bed leveling");
case 2:
return _L("Heatbed preheating");
case 3:
return _L("Vibration compensation");
case 4:
return _L("Changing filament");
case 5:
return _L("M400 pause");
case 6:
return _L("Paused (filament ran out)");
case 7:
return _L("Heating nozzle");
case 8:
return _L("Calibrating dynamic flow");
case 9:
return _L("Scanning bed surface");
case 10:
return _L("Inspecting first layer");
case 11:
return _L("Identifying build plate type");
case 12:
return _L("Calibrating Micro Lidar");
case 13:
return _L("Homing toolhead");
case 14:
return _L("Cleaning nozzle tip");
case 15:
return _L("Checking extruder temperature");
case 16:
return _L("Paused by the user");
case 17:
return _L("Pause (front cover fall off)");
case 18:
return _L("Calibrating the micro lidar");
case 19:
return _L("Calibrating flow ratio");
case 20:
return _L("Pause (nozzle temperature malfunction)");
case 21:
return _L("Pause (heatbed temperature malfunction)");
case 22:
return _L("Filament unloading");
case 23:
return _L("Pause (step loss)");
case 24:
return _L("Filament loading");
case 25:
return _L("Motor noise cancellation");
case 26:
return _L("Pause (AMS offline)");
case 27:
return _L("Pause (low speed of the heatbreak fan)");
case 28:
return _L("Pause (chamber temperature control problem)");
case 29:
return _L("Cooling chamber");
case 30:
return _L("Pause (Gcode inserted by user)");
case 31:
return _L("Motor noise showoff");
case 32:
return _L("Pause (nozzle clumping)");
case 33:
return _L("Pause (cutter error)");
case 34:
return _L("Pause (first layer error)");
case 35:
return _L("Pause (nozzle clog)");
case 36:
return _L("Measuring motion precision");
case 37:
return _L("Enhancing motion precision");
case 38:
return _L("Measure motion accuracy");
case 39:
return _L("Nozzle offset calibration");
case 40:
return _L("high temperature auto bed levelling");
case 41:
return _L("Auto Check: Quick Release Lever");
case 42:
return _L("Auto Check: Door and Upper Cover");
case 43:
return _L("Laser Calibration");
case 44:
return _L("Auto Check: Platform");
case 45:
return _L("Confirming BirdsEye Camera location");
case 46:
return _L("Calibrating BirdsEye Camera");
case 47:
return _L("Auto bed leveling -phase 1");
case 48:
return _L("Auto bed leveling -phase 2");
case 49:
return _L("Heating chamber");
case 50:
return _L("Adjusting heatbed temperature");
case 51:
return _L("Printing calibration lines");
case 52:
return _L("Auto Check: Material");
case 53:
return _L("Live View Camera Calibration");
case 54:
return _L("Waiting for heatbed to reach target temperature");
case 55:
return _L("Auto Check: Material Position");
case 56:
return _L("Cutting Module Offset Calibration");
case 57:
return _L("Measuring Surface");
case 58:
return _L("Thermal Preconditioning for first layer optimization");
case 59:
return _L("Homing Blade Holder"); // O1C
case 60:
return _L("Calibrating Camera Offset"); // O1C
case 61:
return _L("Calibrating Blade Holder Position"); // O1C
case 62:
return _L("Hotend Pick and Place Test"); // O1C
case 63:
return _L("Waiting for the Chamber temperature to equalize"); // O1S O1E/U1 O1D/U2.5
case 64:
return _L("Preparing Hotend");//O1C/U0
case 65:
return _L("Calibrating the detection position of nozzle clumping"); // N7
case 66:
return _L("Purifying the chamber air");
case 67:
return _L("Measuring Rotary Attachment");
case 68:
return _L("The toolhead moves above the purge chute");
case 69:
return _L("Cooling down the nozzle");
case 70:
return _L("The toolhead moves to the center of the heatbed");
case 71:
return _L("Active Arc Fitting");
case 72:
return _L("Hotend Type Detection");
case 73:
return _L("Build plate alignment detection");
case 74:
return _L("Heatbed surface foreign object detection");
case 75:
return _L("Heatbed underside foreign object detection");
case 76:
return _L("Pre-extrusion before printing");
case 77:
return _L("Preparing AMS");
default:
BOOST_LOG_TRIVIAL(info) << "stage = " << stage;
}
return "";
}
std::string to_string_nozzle_diameter(float nozzle_diameter)
{
float eps = 1e-3;
if (abs(nozzle_diameter - 0.2) < eps) {
return "0.2";
}
else if (abs(nozzle_diameter - 0.4) < eps) {
return "0.4";
}
else if (abs(nozzle_diameter - 0.6) < eps) {
return "0.6";
}
else if (abs(nozzle_diameter - 0.8) < eps) {
return "0.8";
}
return "0";
}
void sanitizeToUtf8(std::string& str) {
std::string result;
size_t i = 0;
while (i < str.size()) {
unsigned char c = str[i];
size_t remainingBytes = 0;
bool valid = true;
if ((c & 0x80) == 0x00) { // 1-byte character (ASCII)
remainingBytes = 0;
}
else if ((c & 0xE0) == 0xC0) { // 2-byte character
remainingBytes = 1;
}
else if ((c & 0xF0) == 0xE0) { // 3-byte character
remainingBytes = 2;
}
else if ((c & 0xF8) == 0xF0) { // 4-byte character
remainingBytes = 3;
}
else {
valid = false; // Invalid first byte
}
if (valid && i + remainingBytes < str.size()) {
for (size_t j = 1; j <= remainingBytes; ++j) {
if ((str[i + j] & 0xC0) != 0x80) {
valid = false; // Invalid continuation byte
break;
}
}
}
else {
valid = false; // Truncated character
}
if (valid) {
// Append valid UTF-8 character
result.append(str, i, remainingBytes + 1);
i += remainingBytes + 1;
}
else {
// Replace invalid character with space
result += ' ';
++i; // Skip the invalid byte
}
}
str = std::move(result);
}
namespace Slic3r {
/* Common Functions */
void split_string(std::string s, std::vector<std::string>& v) {
std::string t = "";
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ',') {
v.push_back(t);
t = "";
}
else {
t.push_back(s[i]);
}
}
v.push_back(t);
}
static wxString _generate_nozzle_id(NozzleVolumeType nozzle_type, const std::string& diameter)
{
// HS00-0.4
std::string nozzle_id = "H";
switch (nozzle_type) {
case NozzleVolumeType::nvtStandard: {
nozzle_id += "S";
break;
}
case NozzleVolumeType::nvtHighFlow: {
nozzle_id += "H";
break;
}
case NozzleVolumeType::nvtTPUHighFlow: {
nozzle_id += "U";
break;
}
default:
nozzle_id += "H";
break;
}
nozzle_id += "00";
nozzle_id += "-";
nozzle_id += diameter;
return nozzle_id;
}
NozzleVolumeType convert_to_nozzle_type(const std::string &str)
{
if (str.size() < 8) {
assert(false);
return NozzleVolumeType::nvtStandard;
}
NozzleVolumeType res = NozzleVolumeType::nvtStandard;
if (str[1] == 'S')
res = NozzleVolumeType::nvtStandard;
else if (str[1] == 'H')
res = NozzleVolumeType::nvtHighFlow;
return res;
}
wxString MachineObject::get_printer_type_display_str() const
{
std::string display_name = DevPrinterConfigUtil::get_printer_display_name(printer_type);
if (!display_name.empty())
return display_name;
else
return _L("Unknown");
}
std::string MachineObject::get_printer_thumbnail_img_str() const
{
std::string img_str = DevPrinterConfigUtil::get_printer_thumbnail_img(printer_type);
std::string img_url;
if (!img_str.empty())
{
img_url = Slic3r::resources_dir() + "\\images\\" + img_str ;
if (fs::exists(img_url + ".svg"))
{
return img_url;
}
else
{
img_url = img_str;
}
}
else
{
img_url = "printer_thumbnail";
}
return img_url;
}
std::string MachineObject::get_auto_pa_cali_thumbnail_img_str() const
{
return DevPrinterConfigUtil::get_printer_auto_pa_cali_image(printer_type);
}
std::string MachineObject::get_ftp_folder()
{
return DevPrinterConfigUtil::get_ftp_folder(printer_type);
}
bool MachineObject::HasRecentCloudMessage()
{
auto curr_time = std::chrono::system_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - last_cloud_msg_time_);
return diff.count() < 5000;
}
bool MachineObject::HasRecentLanMessage()
{
auto curr_time = std::chrono::system_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - last_lan_msg_time_);
return diff.count() < 5000;
}
std::string MachineObject::get_access_code() const
{
if (get_user_access_code().empty())
return access_code;
return get_user_access_code();
}
void MachineObject::set_access_code(std::string code, bool only_refresh)
{
this->access_code = code;
if (only_refresh) {
AppConfig* config = GUI::wxGetApp().app_config;
if (config) {
if (!code.empty()) {
GUI::wxGetApp().app_config->set_str("access_code", get_dev_id(), code);
} else {
GUI::wxGetApp().app_config->erase("access_code", get_dev_id());
}
}
}
}
void MachineObject::erase_user_access_code()
{
this->user_access_code = "";
AppConfig* config = GUI::wxGetApp().app_config;
if (config) {
GUI::wxGetApp().app_config->erase("user_access_code", get_dev_id());
//GUI::wxGetApp().app_config->save();
}
}
void MachineObject::set_user_access_code(std::string code, bool only_refresh)
{
this->user_access_code = code;
if (only_refresh && !code.empty()) {
AppConfig* config = GUI::wxGetApp().app_config;
if (config && !code.empty()) {
GUI::wxGetApp().app_config->set_str("user_access_code", get_dev_id(), code);
}
}
}
std::string MachineObject::get_user_access_code() const
{
AppConfig* config = GUI::wxGetApp().app_config;
if (config) {
return GUI::wxGetApp().app_config->get("user_access_code", get_dev_id());
}
return "";
}
void MachineObject::record_user_access_dev_ip()
{
if (!is_lan_mode_printer()) {
assert(false);
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": should not record un-lan dev";
return;
}
if (GUI::wxGetApp().app_config) {
if (!dev_ip.empty()) {
const auto& dev_ip_str = BBLCrossTalk::Encode_DevIp(dev_ip, GUI::wxGetApp().app_config->get("slicer_uuid"));
GUI::wxGetApp().app_config->set_str("user_access_dev_ip", get_dev_id(), dev_ip_str);
} else {
GUI::wxGetApp().app_config->erase("user_access_dev_ip", get_dev_id());
}
}
}
void MachineObject::erase_user_access_dev_ip()
{
if (GUI::wxGetApp().app_config) {
GUI::wxGetApp().app_config->erase("user_access_dev_ip", get_dev_id());
}
}
std::string MachineObject::get_show_printer_type() const
{
std::string printer_type = this->printer_type;
if (this->is_support_upgrade_kit && this->installed_upgrade_kit)
printer_type = "C12";
return printer_type;
}
PrinterSeries MachineObject::get_printer_series() const
{
std::string series = DevPrinterConfigUtil::get_printer_series_str(printer_type);
if (series == "series_x1" || series == "series_o")
return PrinterSeries::SERIES_X1;
else if (series == "series_p1p")
return PrinterSeries::SERIES_P1P;
else
return PrinterSeries::SERIES_P1P;
}
PrinterArch MachineObject::get_printer_arch() const
{
return DevPrinterConfigUtil::get_printer_arch(printer_type);
}
std::string MachineObject::get_printer_ams_type() const
{
return DevPrinterConfigUtil::get_printer_use_ams_type(printer_type);
}
bool MachineObject::is_series_n(const std::string& series_str) { return series_str == "series_n"; }
bool MachineObject::is_series_p(const std::string& series_str) { return series_str == "series_p1p";}
bool MachineObject::is_series_x(const std::string& series_str) { return series_str == "series_x1"; }
bool MachineObject::is_series_o(const std::string& series_str) { return series_str == "series_o"; }
bool MachineObject::is_series_n() const { return is_series_n(DevPrinterConfigUtil::get_printer_series_str(printer_type)); }
bool MachineObject::is_series_p() const { return is_series_p(DevPrinterConfigUtil::get_printer_series_str(printer_type)); }
bool MachineObject::is_series_x() const { return is_series_x(DevPrinterConfigUtil::get_printer_series_str(printer_type)); }
bool MachineObject::is_series_o() const { return is_series_o(DevPrinterConfigUtil::get_printer_series_str(printer_type)); }
std::string MachineObject::get_printer_series_str() const{ return DevPrinterConfigUtil::get_printer_series_str(printer_type);};
void MachineObject::reload_printer_settings()
{
print_json.load_compatible_settings("", "");
parse_json("cloud", "{}");
}
MachineObject::MachineObject(DeviceManager* manager, NetworkAgent* agent, std::string name, std::string id, std::string ip)
:dev_name(name),
dev_ip(ip),
subtask_(nullptr),
model_task(nullptr),
slice_info(nullptr),
m_is_online(false)
{
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " called for dev_id=" << BBLCrossTalk::Crosstalk_DevId(id) << ", main_thread=" << wxThread::IsMain();
if (!wxThread::IsMain()) {
assert(false && "critical warning");
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << "called from other thread, callstack: " << boost::stacktrace::stacktrace();
}
m_manager = manager;
m_agent = agent;
m_dev_info = DevInfo::Create(this);
m_dev_info->SetDevId(id);
reset();
/* temprature fields */
frame_temp = 0.0f;
/* ams fileds */
ams_exist_bits = 0;
tray_exist_bits = 0;
tray_is_bbl_bits = 0;
/* signals */
wifi_signal = "";
/* printing */
mc_print_stage = 0;
mc_print_error_code = 0;
print_error = 0;
mc_print_line_number = 0;
mc_print_percent = 0;
mc_print_sub_stage = 0;
mc_left_time = 0;
hw_switch_state = 0;
m_print_error_img_id = "";
has_ipcam = true; // default true
auto vslot = DevAmsTray(std::to_string(VIRTUAL_TRAY_MAIN_ID));
vt_slot.push_back(vslot);
{
m_axis = DevAxis::Create(this);
m_chamber = DevChamber::Create(this);
m_lamp = new DevLamp(this);
m_fan = new DevFan(this);
m_bed = new DevBed(this);
m_storage = new DevStorage(this);
m_extder_system = new DevExtderSystem(this);
m_extension_tool = DevExtensionTool::Create(this);
m_nozzle_system = new DevNozzleSystem(this);
m_fila_system = std::make_shared<DevFilaSystem>(this);
m_fila_switch = std::make_shared<DevFilaSwitch>(this);
m_upgrade = DevUpgrade::Create(this);
m_hms_system = new DevHMS(this);
m_config = new DevConfig(this);
m_status = new DevStatus(this);
m_ctrl = new DevCtrl(this);
m_print_options = new DevPrintOptions(this);
m_calib = new DevCalib(this);
m_nozzle_mapping_ptr = std::make_shared<DevNozzleMappingCtrl>(this);
}
}
MachineObject::~MachineObject()
{
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " called for dev_id=" << BBLCrossTalk::Crosstalk_DevId(get_dev_id()) << ", main_thread=" << wxThread::IsMain();
if (!wxThread::IsMain()) {
assert(false && "critical warning");
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << " called from other thread, callstack: " << boost::stacktrace::stacktrace();
}
if (subtask_) {
delete subtask_;
subtask_ = nullptr;
}
if (model_task) {
delete model_task;
model_task = nullptr;
}
free_slice_info();
while (!m_command_error_code_dlgs.empty()) {
delete *m_command_error_code_dlgs.begin();/*element will auto remove from m_command_error_code_dlgs on deleted*/
}
{
delete m_lamp;
m_lamp = nullptr;
delete m_fan;
m_fan = nullptr;
delete m_bed;
m_bed = nullptr;
delete m_extder_system;
m_extder_system = nullptr;
delete m_nozzle_system;
m_nozzle_system = nullptr;
delete m_ctrl;
m_ctrl = nullptr;
delete m_hms_system;
m_hms_system = nullptr;
delete m_config;
m_config = nullptr;
delete m_print_options;
m_print_options = nullptr;
delete m_calib;
m_calib = nullptr;
delete m_status;
m_status = nullptr;
}
}
bool MachineObject::is_in_extrusion_cali()
{
auto curr_time = std::chrono::system_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - last_extrusion_cali_start_time);
if (diff.count() < EXTRUSION_OMIT_TIME) {
mc_print_percent = 0;
return true;
}
if (is_in_printing_status(print_status)
&& print_type == "system"
&& boost::contains(m_gcode_file, "extrusion_cali")
)
{
return true;
}
return false;
}
bool MachineObject::is_extrusion_cali_finished()
{
auto curr_time = std::chrono::system_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - last_extrusion_cali_start_time);
if (diff.count() < EXTRUSION_OMIT_TIME) {
return false;
}
if (boost::contains(m_gcode_file, "extrusion_cali")
&& this->mc_print_percent == 100)
return true;
else
return false;
}
DevAmsTray *MachineObject::get_curr_tray()
{
const std::string& cur_ams_id = m_extder_system->GetCurrentAmsId();
if (cur_ams_id.compare(std::to_string(VIRTUAL_TRAY_MAIN_ID)) == 0) {
return &vt_slot[0];
}
DevAms* curr_ams = get_curr_Ams();
if (!curr_ams) return nullptr;
auto it = curr_ams->GetTrays().find(m_extder_system->GetCurrentSlotId());
if (it != curr_ams->GetTrays().end())
{
return it->second;
}
return nullptr;
}
std::string MachineObject::get_filament_id(std::string ams_id, std::string tray_id) const {
const auto& tray = this->get_tray(ams_id, tray_id);
return tray.has_value() ? tray->get_filament_id() : "";
}
std::string MachineObject::get_filament_type(const std::string& ams_id, const std::string& tray_id) const {
auto tray = this->get_tray(ams_id, tray_id);
return tray.has_value() ? tray->get_filament_type() : "";
}
std::string MachineObject::get_filament_display_type(const std::string& ams_id, const std::string& tray_id) const {
const auto& tray = this->get_tray(ams_id, tray_id);
return tray.has_value() ? tray->get_display_filament_type() : "";
}
bool MachineObject::any_loaded_filament_is_stringing_prone() const
{
if (print_job_filament_mapping.empty()) return false;
std::vector<float> nozzle_diameters;
if (m_extder_system) {
for (const auto& ext : m_extder_system->GetExtruders()) {
const float d = ext.GetNozzleDiameter();
if (d > 0.f) nozzle_diameters.push_back(d);
}
}
if (nozzle_diameters.empty()) return false;
for (uint16_t v : print_job_filament_mapping) {
if (v == 0xFFFF) continue;
const int ams_id = (v >> 8) & 0xFF;
const int slot_id = v & 0xFF;
const std::string fid = this->get_filament_id(std::to_string(ams_id), std::to_string(slot_id));
if (fid.empty()) continue;
for (float d : nozzle_diameters) {
if (Slic3r::is_stringing_prone_filament(fid, d))
return true;
}
}
return false;
}
void MachineObject::_parse_ams_status(int ams_status)
{
ams_status_sub = ams_status & 0xFF;
int ams_status_main_int = (ams_status & 0xFF00) >> 8;
if (ams_status_main_int == (int)AmsStatusMain::AMS_STATUS_MAIN_IDLE) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_IDLE;
} else if (ams_status_main_int == (int) AmsStatusMain::AMS_STATUS_MAIN_FILAMENT_CHANGE) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_FILAMENT_CHANGE;
} else if (ams_status_main_int == (int) AmsStatusMain::AMS_STATUS_MAIN_RFID_IDENTIFYING) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_RFID_IDENTIFYING;
} else if (ams_status_main_int == (int) AmsStatusMain::AMS_STATUS_MAIN_ASSIST) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_ASSIST;
} else if (ams_status_main_int == (int) AmsStatusMain::AMS_STATUS_MAIN_CALIBRATION) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_CALIBRATION;
} else if (ams_status_main_int == (int) AmsStatusMain::AMS_STATUS_MAIN_SELF_CHECK) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_SELF_CHECK;
} else if (ams_status_main_int == (int) AmsStatusMain::AMS_STATUS_MAIN_DEBUG) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_DEBUG;
} else if (ams_status_main_int == (int) AmsStatusMain::AMS_STATUS_MAIN_COLD_PULL) {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_COLD_PULL;
} else {
ams_status_main = AmsStatusMain::AMS_STATUS_MAIN_UNKNOWN;
}
BOOST_LOG_TRIVIAL(trace) << "ams_debug: main = " << ams_status_main_int << ", sub = " << ams_status_sub;
}
bool MachineObject::can_unload_filament()
{
bool result = false;
if (!HasAms())
return true;
if (ams_status_main == AMS_STATUS_MAIN_IDLE && hw_switch_state == 1 && m_extder_system->GetCurrentAmsId() == "255") {
return true;
}
return result;
}
static float calc_color_distance(wxColour c1, wxColour c2)
{
float lab[2][3];
RGB2Lab(c1.Red(), c1.Green(), c1.Blue(), &lab[0][0], &lab[0][1], &lab[0][2]);
RGB2Lab(c2.Red(), c2.Green(), c2.Blue(), &lab[1][0], &lab[1][1], &lab[1][2]);
return DeltaE76(lab[0][0], lab[0][1], lab[0][2], lab[1][0], lab[1][1], lab[1][2]);
}
void MachineObject::get_ams_colors(std::vector<wxColour> &ams_colors) {
m_fila_system->CollectAmsColors(ams_colors);
}
std::string MachineObject::get_firmware_type_str()
{
// return product by default;
// always return product, printer do not push this field
return "product";
}
std::string MachineObject::get_lifecycle_type_str()
{
// return product by default;
// always return product, printer do not push this field
return "product";
}
bool MachineObject::is_in_upgrading() const
{
return m_upgrade->IsUpgrading();
}
std::string MachineObject::get_ota_version()
{
auto it = module_vers.find("ota");
if (it != module_vers.end()) {
//double check name
if (it->second.name == "ota") {
return it->second.sw_ver;
}
}
return "";
}
bool MachineObject::check_version_valid()
{
bool valid = true;
for (auto module : module_vers) {
if (module.second.sn.empty()
&& module.first != "ota"
&& module.first != "xm")
return false;
if (module.second.sw_ver.empty())
return false;
}
get_version_retry = 0;
return valid;
}
std::map<int, DevFirmwareVersionInfo> MachineObject::get_ams_version()
{
std::vector<std::string> multi_tray_ams_type = {"ams", "n3f"};
std::map<int, DevFirmwareVersionInfo> result;
for (int i = 0; i < 8; i++) {
std::string ams_id;
for (auto type : multi_tray_ams_type)
{
ams_id = type + "/" + std::to_string(i);
auto it = module_vers.find(ams_id);
if (it != module_vers.end()) {
result.emplace(std::pair(i, it->second));
}
}
}
std::string single_tray_ams_type = "n3s";
int n3s_start_id = 128;
for (int i = n3s_start_id; i < n3s_start_id + 8; i++) {
std::string ams_id;
ams_id = single_tray_ams_type + "/" + std::to_string(i);
auto it = module_vers.find(ams_id);
if (it != module_vers.end()) {
result.emplace(std::pair(i, it->second));
}
}
return result;
}
void MachineObject::clear_version_info()
{
air_pump_version_info = DevFirmwareVersionInfo();
laser_version_info = DevFirmwareVersionInfo();
cutting_module_version_info = DevFirmwareVersionInfo();
extinguish_version_info = DevFirmwareVersionInfo();
rotary_version_info = DevFirmwareVersionInfo();
amshub_version_info = DevFirmwareVersionInfo();
filatrack_version_info = DevFirmwareVersionInfo();
module_vers.clear();
m_nozzle_system->ClearFirmwareInfoWTM();
extinguish_version_info = DevFirmwareVersionInfo();
}
void MachineObject::store_version_info(const DevFirmwareVersionInfo& info)
{
if (info.isAirPump()) {
air_pump_version_info = info;
} else if (info.isLaszer()) {
laser_version_info = info;
} else if (info.isCuttingModule()) {
cutting_module_version_info = info;
} else if (info.isExtinguishSystem()) {
extinguish_version_info = info;
} else if (info.isRotary()) {
rotary_version_info = info;
}else if (info.isWTM()) {
m_nozzle_system->AddFirmwareInfoWTM(info);
}else if (info.isExhaustFan()){
exhaustfan_version_info = info;
}else if (info.isHmshub()){
amshub_version_info = info;
}else if (info.isFilaTrackSwitch()){
filatrack_version_info = info;
}
module_vers.emplace(info.name, info);
}
bool MachineObject::is_system_printing()
{
if (is_in_calibration() && is_in_printing_status(print_status))
return true;
//FIXME