-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.js
1314 lines (1157 loc) · 67.9 KB
/
main.js
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
'use strict';
const utils = require('@iobroker/adapter-core');
const https = require('node:https');
const axios = require('axios').default;
const adapterName = require('./package.json').name.split('.').pop();
const pluginDisplayLayerProgress = require('./lib/plugins/displaylayerprogress');
const pluginSlicerThumbnails = require('./lib/plugins/slicerthumbnails');
class OctoPrint extends utils.Adapter {
constructor(options) {
super({
...options,
name: adapterName,
useFormatDate: true,
});
this.supportedVersion = '1.10.3';
this.displayedVersionWarning = false;
this.apiConnected = false;
this.systemCommands = [];
this.printerStatus = 'API not connected';
this.printerOperational = false;
this.printerPrinting = false;
this.refreshStateTimeout = null;
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
this.setApiConnected(false);
if (!this.config.octoprintIp) {
this.log.warn(`OctoPrint ip / hostname not configured - check instance configuration`);
return;
}
if (!this.config.octoprintApiKey) {
this.log.warn(`API key not configured - check instance configuration`);
return;
}
if (this.config.customName) {
this.setStateChangedAsync('name', { val: this.config.customName, ack: true });
} else {
this.setStateChangedAsync('name', { val: '', ack: true });
}
await this.subscribeStatesAsync('*');
// Delete old (unused) namespace on startup
await this.delObjectAsync('printjob.progress.printtime_left');
await this.delObjectAsync('temperature', { recursive: true });
this.refreshState('onReady');
}
/**
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
// No ack = changed by user
if (id && state && !state.ack) {
const idNoNamespace = this.removeNamespace(id);
if (this.apiConnected) {
if (idNoNamespace.match(new RegExp('tools.tool[0-9]{1}.(targetTemperature|extrude)'))) {
const matches = idNoNamespace.match(/tools\.(tool[0-9]{1})\.(targetTemperature|extrude)$/);
const toolId = matches[1];
const command = matches[2];
if (command === 'targetTemperature') {
this.log.debug(`changing target "${toolId}" temperature to ${state.val}`);
const targetObj = {};
targetObj[toolId] = state.val;
// https://docs.octoprint.org/en/master/api/printer.html#issue-a-tool-command
this.buildServiceRequest('printer/tool', {
command: 'target',
targets: targetObj,
})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 400 Bad Request – If targets or offsets contains a property or tool contains a value not matching the format tool{n}, the target/offset temperature, extrusion amount or flow rate factor is not a valid number or outside of the supported range, or if the request is otherwise invalid.
// 409 Conflict – If the printer is not operational or – in case of select or extrude – currently printing.
this.log.error(`(printer/tool) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/tool) error: ${err}`);
});
} else if (command === 'extrude') {
this.log.debug(`extruding ${state.val}mm`);
// https://docs.octoprint.org/en/master/api/printer.html#issue-a-tool-command
this.buildServiceRequest('printer/tool', {
command: 'extrude',
amount: state.val,
})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 400 Bad Request – If targets or offsets contains a property or tool contains a value not matching the format tool{n}, the target/offset temperature, extrusion amount or flow rate factor is not a valid number or outside of the supported range, or if the request is otherwise invalid.
// 409 Conflict – If the printer is not operational or – in case of select or extrude – currently printing.
this.log.error(`(printer/tool) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/tool) error: ${err}`);
});
}
} else if (idNoNamespace === 'tools.bed.targetTemperature') {
this.log.debug(`changing target bed temperature to ${state.val}°C`);
// https://docs.octoprint.org/en/master/api/printer.html#issue-a-bed-command
this.buildServiceRequest('printer/bed', {
command: 'target',
target: state.val,
})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 400 Bad Request – If target or offset is not a valid number or outside of the supported range, or if the request is otherwise invalid.
// 409 Conflict – If the printer is not operational or the selected printer profile does not have a heated bed.
this.log.error(`(printer/bed) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/bed) error: ${err}`);
});
} else if (idNoNamespace === 'command.printer') {
const allowedCommandsConnection = ['connect', 'disconnect', 'fake_ack'];
const allowedCommandsPrinter = ['home'];
if (allowedCommandsConnection.indexOf(state.val) > -1) {
this.log.debug(`sending printer connection command: ${state.val}`);
// https://docs.octoprint.org/en/master/api/connection.html#issue-a-connection-command
this.buildServiceRequest('connection', {
command: state.val,
})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
this.refreshState('onStateChange command.printer');
} else {
// 400 Bad Request – If the selected port or baudrate for a connect command are not part of the available options.
this.log.error(`(connection) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(connection) error: ${err}`);
});
} else if (allowedCommandsPrinter.indexOf(state.val) > -1) {
this.log.debug(`sending printer command: ${state.val}`);
// https://docs.octoprint.org/en/master/api/printer.html#issue-a-print-head-command
this.buildServiceRequest('printer/printhead', {
command: state.val,
axes: ['x', 'y', 'z'],
})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 400 Bad Request – Invalid axis specified, invalid value for travel amount for a jog command or factor for feed rate or otherwise invalid request.
// 409 Conflict – If the printer is not operational or currently printing.
this.log.error(`(printer/printhead) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/printhead) error: ${err}`);
});
} else {
this.log.error('printer command not allowed: ' + state.val + '. Choose one of: ' + allowedCommandsConnection.concat(allowedCommandsPrinter).join(', '));
}
} else if (idNoNamespace === 'command.printjob') {
const allowedCommands = ['start', 'pause', 'resume', 'cancel', 'restart'];
if (allowedCommands.indexOf(state.val) > -1) {
this.log.debug(`sending printjob command: ${state.val}`);
const printjobCommand = {
command: state.val,
};
// Pause command needs an action
if (state.val === 'pause') {
printjobCommand.action = 'pause';
}
// Workaround: Resume is a pause command with resume action
if (state.val === 'resume') {
printjobCommand.command = 'pause';
printjobCommand.action = 'resume';
}
// https://docs.octoprint.org/en/master/api/job.html#issue-a-job-command
this.buildServiceRequest('job', printjobCommand)
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 409 Conflict – If the printer is not operational or the current print job state does not match the preconditions for the command.
this.log.error(`(job) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(job) error: ${err}`);
});
} else {
this.log.error('print job command not allowed: ' + state.val + '. Choose one of: ' + allowedCommands.join(', '));
}
} else if (idNoNamespace === 'command.sd') {
const allowedCommands = ['init', 'refresh', 'release'];
if (allowedCommands.indexOf(state.val) > -1) {
this.log.debug(`sending sd card command: ${state.val}`);
// https://docs.octoprint.org/en/master/api/printer.html#issue-an-sd-command
this.buildServiceRequest('printer/sd', {
command: state.val,
})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 409 Conflict – If a refresh or release command is issued but the SD card has not been initialized (e.g. via init).
this.log.error(`(printer/sd) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/sd) error: ${err}`);
});
} else {
this.log.error('sd card command not allowed: ' + state.val + '. Choose one of: ' + allowedCommands.join(', '));
}
} else if (idNoNamespace === 'command.custom') {
this.log.debug(`sending custom command: ${state.val}`);
// https://docs.octoprint.org/en/master/api/printer.html#send-an-arbitrary-command-to-the-printer
this.buildServiceRequest('printer/command', {
command: state.val,
})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 409 Conflict – If the printer is not operational
this.log.error(`(printer/command) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/command) error: ${err}`);
});
} else if (idNoNamespace === 'command.system') {
if (this.systemCommands.indexOf(state.val) > -1) {
this.log.debug(`sending system command: ${state.val}`);
// https://docs.octoprint.org/en/master/api/system.html#execute-a-registered-system-command
this.buildServiceRequest('system/commands/' + state.val, {})
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 400 Bad Request – If a divider is supposed to be executed or if the request is malformed otherwise
// 404 Not Found – If the command could not be found for source and action
// 500 Internal Server Error – If the command didn’t define a command to execute, the command returned a non-zero return code and ignore was not true or some other internal server error occurred
this.log.error(`(system/commands/*) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/commands/*) error: ${err}`);
});
} else {
this.log.error(`system command not allowed: ${state.val}. Choose one of: ${this.systemCommands.join(', ')}`);
}
} else if (idNoNamespace.indexOf('command.jog.') === 0) {
// Validate jog value
if (state.val !== 0) {
const axis = id.split('.').pop(); // Last element of the object id is the axis
const jogCommand = {
command: 'jog',
};
// Add axis
jogCommand[axis] = state.val;
this.log.debug(`sending jog ${axis} command: ${state.val}`);
// https://docs.octoprint.org/en/master/api/printer.html#issue-a-print-head-command
this.buildServiceRequest('printer/printhead', jogCommand)
.then((response) => {
if (response.status === 204) {
this.setStateAsync(idNoNamespace, { val: state.val, ack: true });
} else {
// 400 Bad Request – Invalid axis specified, invalid value for travel amount for a jog command or factor for feed rate or otherwise invalid request.
// 409 Conflict – If the printer is not operational or currently printing.
this.log.error(`(printer/printhead) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(printer/printhead) error: ${err}`);
});
} else {
this.log.error('Jog: provide non-zero jog value');
}
} else if (idNoNamespace.match(new RegExp('files.[a-zA-Z0-9_]+.(select|print)'))) {
const matches = idNoNamespace.match(/files\.([a-zA-Z0-9_]+)\.(select|print)$/);
const fileId = matches[1];
const action = matches[2];
this.log.debug(`selecting/printing file "${fileId}" - action: "${action}"`);
this.getState(`files.${fileId}.path`, (err, state) => {
const fullPath = state?.val;
this.log.debug(`selecting/printing file with path "${fullPath}"`);
// https://docs.octoprint.org/en/master/api/files.html#issue-a-file-command
this.buildServiceRequest(`files/${fullPath}`, {
command: 'select',
print: action === 'print',
})
.then((response) => {
if (response.status === 204) {
this.log.debug('selecting/printing file successful');
this.refreshState(`onStateChange file.${action}`);
} else {
this.log.error(`(files/*) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(files/*) error: ${err}`);
});
});
}
}
}
}
setApiConnected(connection) {
this.setStateChangedAsync('info.connection', { val: connection, ack: true });
this.apiConnected = connection;
if (!connection) {
this.log.debug('API is offline');
this.printerStatus = 'API not connected';
this.setStateChangedAsync('printer_status', { val: this.printerStatus, ack: true });
}
}
async refreshState(source) {
this.log.debug(`refreshState: started from "${source}"`);
// https://docs.octoprint.org/en/master/api/version.html
this.buildServiceRequest('version', null)
.then((response) => {
if (response.status === 200) {
this.setApiConnected(true);
this.log.debug(`connected to OctoPrint API - online! - status: ${response.status}`);
this.setStateChangedAsync('meta.version', { val: response.data.server, ack: true });
this.setStateChangedAsync('meta.api_version', { val: response.data.api, ack: true });
if (this.isNewerVersion(response.data.server, this.supportedVersion) && !this.displayedVersionWarning) {
this.log.warn(
`You should update your OctoPrint installation - supported version of this adapter is ${this.supportedVersion} (or later). Your current version is ${response.data.server}`,
);
this.displayedVersionWarning = true; // Just show once
}
this.refreshStateDetails();
} else {
this.log.error(`(version) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((error) => {
this.log.debug(`(version) received error - API is now offline: ${JSON.stringify(error)}`);
this.setApiConnected(false);
});
// Delete old timer
if (this.refreshStateTimeout) {
this.log.debug(`refreshStateTimeout: CLEARED by ${source}`);
this.clearTimeout(this.refreshStateTimeout);
}
// Start a new timeout in any case
if (!this.apiConnected) {
const notConnectedTimeout = 10;
this.refreshStateTimeout = this.setTimeout(() => {
this.refreshStateTimeout = null;
this.refreshState('timeout (API not connected)');
}, notConnectedTimeout * 1000);
this.log.debug(`refreshStateTimeout: re-created refresh timeout (API not connected): id ${this.refreshStateTimeout} - seconds: ${notConnectedTimeout}`);
} else if (this.printerPrinting) {
this.refreshStateTimeout = this.setTimeout(() => {
this.refreshStateTimeout = null;
this.refreshState('timeout (printing)');
}, this.config.apiRefreshIntervalPrinting * 1000); // Default 10 sec
this.log.debug(`refreshStateTimeout: re-created refresh timeout (printing): id ${this.refreshStateTimeout} - seconds: ${this.config.apiRefreshIntervalPrinting}`);
} else if (this.printerOperational) {
this.refreshStateTimeout = this.setTimeout(() => {
this.refreshStateTimeout = null;
this.refreshState('timeout (operational)');
}, this.config.apiRefreshIntervalOperational * 1000); // Default 30 sec
this.log.debug(`refreshStateTimeout: re-created refresh timeout (operational): id ${this.refreshStateTimeout} - seconds: ${this.config.apiRefreshIntervalOperational}`);
} else {
this.refreshStateTimeout = this.setTimeout(() => {
this.refreshStateTimeout = null;
this.refreshState('timeout (default)');
}, this.config.apiRefreshInterval * 1000); // Default 60 sec
this.log.debug(`refreshStateTimeout: re-created refresh timeout (default): id ${this.refreshStateTimeout} - seconds: ${this.config.apiRefreshInterval}`);
}
}
async refreshStateDetails() {
if (this.apiConnected) {
// https://docs.octoprint.org/en/master/api/connection.html
this.buildServiceRequest('connection', null)
.then((response) => {
if (response.status === 200) {
this.updatePrinterStatus(response.data.current.state);
if (!this.printerPrinting) {
this.refreshFiles();
}
// Try again in 2 seconds
if (this.printerStatus === 'Detecting serial connection') {
this.setTimeout(() => {
this.refreshState('detecting serial connection');
}, 2000);
}
} else {
this.log.error(`(connection) status ${response.status}: ${JSON.stringify(response.data)}`);
}
})
.catch((err) => {
this.log.debug(`(connection) error: ${err}`);
});
if (this.printerOperational) {
this.buildServiceRequest('printer', null)
.then(async (response) => {
const content = response.data;
if (content?.temperature) {
for (const key of Object.keys(content.temperature)) {
const obj = content.temperature[key];
const isTool = key.indexOf('tool') > -1;
const isBed = key == 'bed';
if (isTool || isBed) {
// Tool + bed information
// Create tool channel
await this.setObjectNotExistsAsync(`tools.${key}`, {
type: 'channel',
common: {
name: key,
},
native: {},
});
// Set actual temperature
await this.setObjectNotExistsAsync(`tools.${key}.actualTemperature`, {
type: 'state',
common: {
name: {
en: 'Actual temperature',
de: 'Tatsächliche Temperatur',
ru: 'Фактическая температура',
pt: 'Temperatura real',
nl: 'Werkelijke temperatuur',
fr: 'Température réelle',
it: 'Temperatura effettiva',
es: 'Temperatura real',
pl: 'Rzeczywista temperatura',
uk: 'Погода',
'zh-cn': '实际温度',
},
type: 'number',
role: 'value.temperature',
unit: '°C',
read: true,
write: false,
def: 0,
},
native: {},
});
await this.setStateChangedAsync(`tools.${key}.actualTemperature`, { val: obj.actual, ack: true });
// Set target temperature
await this.setObjectNotExistsAsync(`tools.${key}.targetTemperature`, {
type: 'state',
common: {
name: {
en: 'Target temperature',
de: 'Zieltemperatur',
ru: 'Целевая температура',
pt: 'Temperatura alvo',
nl: 'Doeltemperatuur',
fr: 'Température cible',
it: 'Temperatura obiettivo',
es: 'Temperatura objetivo',
pl: 'Temperatura docelowa',
uk: 'Цільова температура',
'zh-cn': '目标温度',
},
type: 'number',
role: 'value.temperature',
unit: '°C',
read: true,
write: true,
},
native: {},
});
await this.setStateChangedAsync(`tools.${key}.targetTemperature`, { val: obj.target, ack: true });
// Set offset temperature
await this.setObjectNotExistsAsync(`tools.${key}.offsetTemperature`, {
type: 'state',
common: {
name: {
en: 'Offset temperature',
de: 'Offset-Temperatur',
ru: 'Смещение температуры',
pt: 'Temperatura compensada',
nl: 'Offset temperatuur',
fr: 'Température de décalage',
it: 'Temperatura di compensazione',
es: 'Temperatura de compensación',
pl: 'Temperatura przesunięcia',
uk: 'Температура офсету',
'zh-cn': '偏移温度',
},
type: 'number',
role: 'value.temperature',
unit: '°C',
read: true,
write: false,
def: 0,
},
native: {},
});
await this.setStateChangedAsync(`tools.${key}.offsetTemperature`, { val: obj.target, ack: true });
}
if (isTool) {
// Set extrude
await this.setObjectNotExistsAsync(`tools.${key}.extrude`, {
type: 'state',
common: {
name: {
en: 'Extrude',
de: 'Extrudieren',
ru: 'Выдавливание',
pt: 'Extrudar',
nl: 'extruderen',
fr: 'Extruder',
it: 'Estrudere',
es: 'Extrudir',
pl: 'Wyrzucać',
uk: 'Екструдед',
'zh-cn': '拉伸',
},
type: 'number',
role: 'value',
unit: 'mm',
read: true,
write: true,
def: 0,
},
native: {},
});
}
}
}
})
.catch((err) => {
this.log.debug(`(printer) error: ${err}`);
});
} else {
// https://docs.octoprint.org/en/master/api/system.html#list-all-registered-system-commands
this.buildServiceRequest('system/commands', null)
.then((response) => {
if (response.status === 200) {
this.systemCommands = [];
for (const key of Object.keys(response.data)) {
const arr = response.data[key];
arr.forEach((e) => this.systemCommands.push(`${e.source}/${e.action}`));
}
this.log.debug(`(system/commands) registered commands: ${this.systemCommands.join(', ')}`);
}
})
.catch((err) => {
this.log.debug(`(system/commands) error: ${err}`);
});
}
// Plugin Display Layer Progress
// https://github.com/OllisGit/OctoPrint-DisplayLayerProgress
if (this.config.pluginDisplayLayerProgress) {
this.log.debug('[plugin display layer progress] plugin activated - fetching details');
pluginDisplayLayerProgress.refreshValues(this);
} else {
await this.delObjectAsync('plugins.displayLayerProgress', { recursive: true });
}
if (this.printerOperational || this.printerPrinting) {
// https://docs.octoprint.org/en/master/api/job.html#retrieve-information-about-the-current-job
this.buildServiceRequest('job', null)
.then(async (response) => {
if (response.status === 200) {
const content = response.data;
if (content?.error) {
this.log.warn(`print job error: ${content.error}`);
}
if (content?.job?.file) {
const filePath = `${content.job.file.origin}/${content.job.file.path}`;
if (this.config.pluginSlicerThumbnails) {
await this.setObjectNotExistsAsync('printjob.file.thumbnail_url', {
type: 'state',
common: {
name: {
en: 'Thumbnail URL',
de: 'Miniaturbild-URL',
ru: 'URL миниатюры',
pt: 'URL da miniatura',
nl: 'Miniatuur-URL',
fr: 'URL de la miniature',
it: 'URL miniatura',
es: 'URL de la miniatura',
pl: 'URL miniatury',
uk: 'Веб-сайт',
'zh-cn': '缩略图网址',
},
type: 'string',
role: 'url',
read: true,
write: false,
},
native: {},
});
this.log.debug(`[plugin slicer thumbnails] trying to find current print job thumbnail url`);
const fileObjectsView = await this.getObjectViewAsync('system', 'channel', {
startkey: this.namespace + '.files.',
endkey: this.namespace + '.files.\u9999',
});
let foundThumbnail = false;
if (fileObjectsView && fileObjectsView.rows) {
// File file where native.path matches current jobs file path
const currentFileObject = fileObjectsView.rows.find((fileObj) => fileObj.value?.native?.path === filePath);
if (currentFileObject) {
const currentFileId = this.removeNamespace(currentFileObject.id);
try {
this.log.debug(`[plugin slicer thumbnails] found current file: ${currentFileId}`);
const currentFileThumbnailUrlState = await this.getStateAsync(`${currentFileId}.thumbnail.url`);
if (currentFileThumbnailUrlState && currentFileThumbnailUrlState.val) {
foundThumbnail = true;
await this.setStateChangedAsync('printjob.file.thumbnail_url', { val: currentFileThumbnailUrlState.val, ack: true });
}
} catch {
this.log.debug(`[plugin slicer thumbnails] unable to get value of state ${currentFileId}.thumbnail.url`);
}
}
}
if (!foundThumbnail) {
this.log.debug(`[plugin slicer thumbnails] unable to find file which matches current job file`);
await this.setStateChangedAsync('printjob.file.thumbnail_url', { val: null, ack: true });
}
} else {
await this.delObjectAsync('printjob.file.thumbnail_url');
}
await this.setStateChangedAsync('printjob.file.name', { val: content.job.file.name, ack: true });
await this.setStateChangedAsync('printjob.file.origin', { val: content.job.file.origin, ack: true });
await this.setStateChangedAsync('printjob.file.size', { val: Number((content.job.file.size / 1024).toFixed(2)), ack: true });
await this.setStateChangedAsync('printjob.file.date', { val: new Date(content.job.file.date * 1000).getTime(), ack: true });
if (content?.job?.filament) {
let filamentLength = 0;
let filamentVolume = 0;
if (content.job.filament?.tool0) {
filamentLength = content.job.filament?.tool0?.length ?? 0;
filamentVolume = content.job.filament?.tool0?.volume ?? 0;
} else {
filamentLength = content.job.filament?.length ?? 0;
filamentVolume = content.job.filament?.volume ?? 0;
}
if (typeof filamentLength == 'number' && typeof filamentVolume == 'number') {
await this.setStateChangedAsync('printjob.filament.length', { val: Number((filamentLength / 1000).toFixed(2)), ack: true });
await this.setStateChangedAsync('printjob.filament.volume', { val: Number(filamentVolume.toFixed(2)), ack: true });
} else {
this.log.debug('Filament length and/or volume contains no valid number');
await this.setStateChangedAsync('printjob.filament.length', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.filament.volume', { val: 0, ack: true });
}
} else {
await this.setStateChangedAsync('printjob.filament.length', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.filament.volume', { val: 0, ack: true });
}
}
if (content?.progress) {
await this.setStateChangedAsync('printjob.progress.completion', { val: Math.round(content.progress.completion), ack: true });
await this.setStateChangedAsync('printjob.progress.filepos', { val: Number((content.progress.filepos / 1024).toFixed(2)), ack: true });
await this.setStateChangedAsync('printjob.progress.printtime', { val: content.progress.printTime, ack: true });
await this.setStateChangedAsync('printjob.progress.printtimeLeft', { val: content.progress.printTimeLeft, ack: true });
await this.setStateChangedAsync('printjob.progress.printtimeFormat', { val: this.printtimeString(content.progress.printTime), ack: true });
await this.setStateChangedAsync('printjob.progress.printtimeLeftFormat', { val: this.printtimeString(content.progress.printTimeLeft), ack: true });
const finishedAt = new Date();
finishedAt.setSeconds(finishedAt.getSeconds() + content.progress.printTimeLeft);
await this.setStateChangedAsync('printjob.progress.finishedAt', { val: finishedAt.getTime(), ack: true });
await this.setStateChangedAsync('printjob.progress.finishedAtFormat', { val: this.formatDate(finishedAt), ack: true });
}
}
})
.catch((err) => {
this.log.debug(`(job) error: ${err}`);
});
} else {
this.log.debug('refreshing job state: skipped detail refresh (not printing)');
// Reset all values
await this.setStateChangedAsync('printjob.file.name', { val: '', ack: true });
await this.setStateChangedAsync('printjob.file.origin', { val: '', ack: true });
await this.setStateChangedAsync('printjob.file.size', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.file.date', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.filament.length', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.filament.volume', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.progress.completion', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.progress.filepos', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.progress.printtime', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.progress.printtimeLeft', { val: 0, ack: true });
await this.setStateChangedAsync('printjob.progress.printtimeFormat', { val: this.printtimeString(0), ack: true });
await this.setStateChangedAsync('printjob.progress.printtimeLeftFormat', { val: this.printtimeString(0), ack: true });
}
} else {
this.log.debug('refreshing state: skipped detail refresh (API not connected)');
}
}
flattenFiles(files) {
let fileArr = [];
if (Array.isArray(files)) {
for (const file of files) {
if (file.type == 'machinecode' && file.origin == 'local') {
const fileObj = {
name: file.display,
path: file.origin + '/' + file.path,
date: file.date ? new Date(file.date * 1000).getTime() : 0,
size: file.size ? Number(Math.round(file.size / 1024).toFixed(2)) : 0,
thumbnail: null,
};
// Plugin Slicer Thumbnails
if (this.config.pluginSlicerThumbnails) {
if (file?.thumbnail_src === 'prusaslicerthumbnails') {
fileObj.thumbnail = file.thumbnail;
}
}
fileArr.push(fileObj);
} else if (file.type == 'folder') {
fileArr = fileArr.concat(this.flattenFiles(file.children));
}
}
}
return fileArr;
}
async refreshFiles() {
if (this.apiConnected) {
this.log.debug('[refreshFiles] started');
const filesAll = [];
const filesKeep = [];
try {
const fileChannels = await this.getChannelsOfAsync('files');
// Collect all existing files
if (fileChannels) {
for (let i = 0; i < fileChannels.length; i++) {
const idNoNamespace = this.removeNamespace(fileChannels[i]._id);
// Check if the state is a direct child (e.g. files.MyCustomFile)
if (idNoNamespace.split('.').length === 2) {
if (!fileChannels[i].native.path) {
// Force recreation of files without native path (upgraded from older version)
await this.delObjectAsync(idNoNamespace, { recursive: true });
this.log.debug(`[refreshFiles] found file channel without native.path - deleted ${idNoNamespace}`);
} else {
filesAll.push(idNoNamespace);
}
}
}
}
} catch (err) {
this.log.warn(err);
}
this.buildServiceRequest('files?recursive=true', null)
.then(async (response) => {
if (response.status === 200) {
const content = response.data;
const fileList = this.flattenFiles(content.files);
this.log.debug(`[refreshFiles] found ${fileList.length} files`);
for (const f in fileList) {
const file = fileList[f];
const fileNameClean = this.cleanNamespace(file.path.replace('.gcode', '').replace('/', ' '));
this.log.debug(`[refreshFiles] found file "${fileNameClean}" (clean name) - location: ${file.path}`);
filesKeep.push(`files.${fileNameClean}`);
await this.setObjectNotExistsAsync(`files.${fileNameClean}`, {
type: 'channel',
common: {
name: file.name,
},
native: {
path: file.path,
},
});
await this.setObjectNotExistsAsync(`files.${fileNameClean}.name`, {
type: 'state',
common: {
name: {
en: 'File name',
de: 'Dateiname',
ru: 'Имя файла',
pt: 'Nome do arquivo',
nl: 'Bestandsnaam',
fr: 'Nom de fichier',
it: 'Nome del file',
es: 'Nombre del archivo',
pl: 'Nazwa pliku',
uk: `Ім'я файла`,
'zh-cn': '文档名称',
},
type: 'string',
role: 'text',
read: true,
write: false,
},
native: {},
});
await this.setStateChangedAsync(`files.${fileNameClean}.name`, { val: file.name, ack: true });
await this.setObjectNotExistsAsync(`files.${fileNameClean}.path`, {
type: 'state',
common: {
name: {
en: 'File path',
de: 'Dateipfad',
ru: 'Путь файла',
pt: 'Caminho de arquivo',
nl: 'Bestandspad',
fr: 'Chemin du fichier',
it: 'Percorso del file',
es: 'Ruta de archivo',
pl: 'Ścieżka pliku',
uk: 'Шлях до файлу',
'zh-cn': '文件路径',
},
type: 'string',
role: 'text',
read: true,
write: false,
},
native: {},
});
await this.setStateChangedAsync(`files.${fileNameClean}.path`, { val: file.path, ack: true });
await this.setObjectNotExistsAsync(`files.${fileNameClean}.size`, {
type: 'state',
common: {
name: {
en: 'File size',
de: 'Dateigröße',
ru: 'Размер файла',
pt: 'Tamanho do arquivo',
nl: 'Bestandsgrootte',
fr: 'Taille du fichier',
it: 'Dimensione del file',
es: 'Tamaño del archivo',
pl: 'Rozmiar pliku',
uk: 'Розмір файлу',
'zh-cn': '文件大小',
},
type: 'number',
role: 'value',
unit: 'KiB',
read: true,
write: false,
},
native: {},
});
await this.setStateChangedAsync(`files.${fileNameClean}.size`, { val: file.size, ack: true });
await this.setObjectNotExistsAsync(`files.${fileNameClean}.date`, {
type: 'state',
common: {
name: {
en: 'File date',
de: 'Dateidatum',
ru: 'Дата файла',
pt: 'Data do arquivo',
nl: 'Bestandsdatum',
fr: 'Date du fichier',
it: 'Data file',
es: 'Fecha de archivo',
pl: 'Data pliku',
uk: 'Дата файлу',
'zh-cn': '文件日期',
},
type: 'number',
role: 'date',
read: true,
write: false,
},
native: {},
});
await this.setStateChangedAsync(`files.${fileNameClean}.date`, { val: file.date, ack: true });
if (this.config.pluginSlicerThumbnails) {
await this.setObjectNotExistsAsync(`files.${fileNameClean}.thumbnail`, {
type: 'channel',
common: {
name: {
en: 'Thumbnail',
de: 'Miniaturansicht',
ru: 'Миниатюра',
pt: 'Miniatura',
nl: 'Miniatuur',
fr: 'Vignette',
it: 'Miniatura',
es: 'Miniatura',
pl: 'Miniaturka',
uk: 'Напляскване',
'zh-cn': '缩略图',
},
},
native: {},
});