flashforge-python-api 1.6.0__py3-none-any.whl → 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flashforge/__init__.py +337 -256
- flashforge/_removed.py +65 -0
- flashforge/api/__init__.py +12 -18
- flashforge/api/constants/__init__.py +11 -11
- flashforge/api/constants/commands.py +18 -15
- flashforge/api/constants/endpoints.py +11 -11
- flashforge/api/misc/__init__.py +8 -8
- flashforge/api/network/__init__.py +11 -11
- flashforge/api/network/utils.py +71 -71
- flashforge/backends/__init__.py +80 -0
- flashforge/backends/connect.py +275 -0
- flashforge/backends/creator5_backend.py +191 -0
- flashforge/backends/dual_api_backend.py +170 -0
- flashforge/backends/dual_api_models.py +145 -0
- flashforge/backends/http_core.py +431 -0
- flashforge/backends/http_printer_backend.py +254 -0
- flashforge/backends/legacy_backend.py +436 -0
- flashforge/backends/printer_backend.py +217 -0
- flashforge/backends/types.py +125 -0
- flashforge/capabilities/__init__.py +98 -0
- flashforge/capabilities/models.py +67 -0
- flashforge/capabilities/palettes/__init__.py +28 -0
- flashforge/capabilities/palettes/ad5x_palette.py +70 -0
- flashforge/capabilities/palettes/creator5_palette.py +78 -0
- flashforge/capabilities/palettes/palette.py +54 -0
- flashforge/{api/controls/creator5_palette.py → capabilities/palettes/palette_snap.py} +226 -258
- flashforge/capabilities/printer_capabilities.py +145 -0
- flashforge/capabilities/registry.py +125 -0
- flashforge/capabilities/resolve_model.py +61 -0
- flashforge/discovery/__init__.py +39 -39
- flashforge/discovery/discovery.py +730 -763
- flashforge/exceptions.py +90 -51
- flashforge/models/__init__.py +52 -55
- flashforge/models/machine_info.py +540 -543
- flashforge/{api/controls/info.py → models/machine_info_parser.py} +35 -242
- flashforge/models/responses.py +119 -254
- flashforge/status/__init__.py +43 -0
- flashforge/status/printer_snapshot.py +396 -0
- flashforge/tcp/__init__.py +55 -54
- flashforge/tcp/a3_client.py +427 -427
- flashforge/tcp/a4_client.py +304 -304
- flashforge/tcp/ff_client.py +609 -590
- flashforge/tcp/gcode/__init__.py +15 -13
- flashforge/tcp/gcode/a3_gcode_controller.py +109 -109
- flashforge/tcp/gcode/gcode_controller.py +292 -308
- flashforge/tcp/gcode/gcodes.py +81 -99
- flashforge/tcp/gcode/legacy_gcode_controller.py +56 -0
- flashforge/tcp/parsers/__init__.py +27 -27
- flashforge/tcp/parsers/endstop_status.py +248 -248
- flashforge/tcp/parsers/location_info.py +83 -83
- flashforge/tcp/parsers/print_status.py +180 -180
- flashforge/tcp/parsers/printer_info.py +146 -146
- flashforge/tcp/parsers/temp_info.py +218 -218
- flashforge/tcp/parsers/thumbnail_info.py +274 -274
- flashforge/tcp/tcp_client.py +52 -12
- flashforge/threemf/__init__.py +40 -40
- flashforge/threemf/parser.py +560 -560
- flashforge/threemf/warnings.py +58 -58
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/METADATA +46 -47
- flashforge_python_api-3.0.0.dist-info/RECORD +69 -0
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/WHEEL +1 -1
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/licenses/LICENSE +21 -21
- flashforge/api/controls/__init__.py +0 -17
- flashforge/api/controls/control.py +0 -413
- flashforge/api/controls/files.py +0 -210
- flashforge/api/controls/job_control.py +0 -896
- flashforge/api/controls/temp_control.py +0 -309
- flashforge/client.py +0 -609
- flashforge_python_api-1.6.0.dist-info/RECORD +0 -52
- {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,763 +1,730 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Universal FlashForge printer discovery using UDP broadcast and multicast.
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from __future__ import annotations
|
|
6
|
-
|
|
7
|
-
import asyncio
|
|
8
|
-
import logging
|
|
9
|
-
import socket
|
|
10
|
-
from collections.abc import Callable
|
|
11
|
-
from dataclasses import dataclass, field
|
|
12
|
-
from enum import IntEnum, StrEnum
|
|
13
|
-
from typing import Any
|
|
14
|
-
|
|
15
|
-
import ifaddr
|
|
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
|
-
UNKNOWN =
|
|
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
|
-
self
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
self.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
self.
|
|
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
|
-
buffer:
|
|
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
|
-
if
|
|
520
|
-
return
|
|
521
|
-
|
|
522
|
-
upper_name
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
if
|
|
534
|
-
return
|
|
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
|
-
return
|
|
573
|
-
|
|
574
|
-
def
|
|
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
|
-
ip_address
|
|
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
|
-
print(f"Response length: {len(response)} bytes")
|
|
732
|
-
print("Hex dump:")
|
|
733
|
-
for index in range(0, len(response), 16):
|
|
734
|
-
parts = [f"{index:04x} "]
|
|
735
|
-
for offset in range(16):
|
|
736
|
-
if index + offset < len(response):
|
|
737
|
-
parts.append(f"{response[index + offset]:02x} ")
|
|
738
|
-
else:
|
|
739
|
-
parts.append(" ")
|
|
740
|
-
if offset == 7:
|
|
741
|
-
parts.append(" ")
|
|
742
|
-
|
|
743
|
-
parts.append(" ")
|
|
744
|
-
for offset in range(16):
|
|
745
|
-
if index + offset < len(response):
|
|
746
|
-
byte = response[index + offset]
|
|
747
|
-
parts.append(chr(byte) if 32 <= byte <= 126 else ".")
|
|
748
|
-
print("".join(parts))
|
|
749
|
-
|
|
750
|
-
print("ASCII dump:")
|
|
751
|
-
print(repr(response.decode("ascii", errors="replace")))
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
async def _async_main() -> None:
|
|
755
|
-
discovery = FlashForgePrinterDiscovery()
|
|
756
|
-
printers = await discovery.discover_printers_async()
|
|
757
|
-
for printer in printers:
|
|
758
|
-
print(printer)
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
def main() -> None:
|
|
762
|
-
"""CLI entry point for `flashforge-discover`."""
|
|
763
|
-
asyncio.run(_async_main())
|
|
1
|
+
"""
|
|
2
|
+
Universal FlashForge printer discovery using UDP broadcast and multicast.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import logging
|
|
9
|
+
import socket
|
|
10
|
+
from collections.abc import Callable
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from enum import IntEnum, StrEnum
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
import ifaddr
|
|
16
|
+
|
|
17
|
+
from ..capabilities.models import BackendFamily, PrinterModelId, model_from_pid
|
|
18
|
+
from ..capabilities.registry import get_model_spec
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
MULTICAST_ADDRESS = "225.0.0.9"
|
|
23
|
+
MODERN_PROTOCOL_SIZE = 276
|
|
24
|
+
LEGACY_PROTOCOL_SIZE = 140
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class DiscoveryError(Exception):
|
|
28
|
+
"""Base error for discovery-related failures."""
|
|
29
|
+
|
|
30
|
+
def __init__(self, message: str, code: str) -> None:
|
|
31
|
+
super().__init__(message)
|
|
32
|
+
self.code = code
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class InvalidResponseError(DiscoveryError):
|
|
36
|
+
"""Raised when a discovery response has an unexpected size."""
|
|
37
|
+
|
|
38
|
+
def __init__(self, size: int, address: str) -> None:
|
|
39
|
+
super().__init__(f"Invalid response size: {size} bytes from {address}", "INVALID_RESPONSE")
|
|
40
|
+
self.response_size = size
|
|
41
|
+
self.address = address
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class SocketCreationError(DiscoveryError):
|
|
45
|
+
"""Raised when a UDP discovery socket cannot be created."""
|
|
46
|
+
|
|
47
|
+
def __init__(self, message: str) -> None:
|
|
48
|
+
super().__init__(message, "SOCKET_CREATION_FAILED")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class DiscoveryTimeoutError(DiscoveryError):
|
|
52
|
+
"""Raised when discovery times out without any responses."""
|
|
53
|
+
|
|
54
|
+
def __init__(self, timeout_ms: int) -> None:
|
|
55
|
+
super().__init__(f"Discovery timeout after {timeout_ms}ms", "DISCOVERY_TIMEOUT")
|
|
56
|
+
self.timeout_ms = timeout_ms
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class DiscoveryProtocol(StrEnum):
|
|
60
|
+
"""Discovery response wire formats."""
|
|
61
|
+
|
|
62
|
+
MODERN = "modern"
|
|
63
|
+
LEGACY = "legacy"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class PrinterStatus(IntEnum):
|
|
67
|
+
"""Printer status reported via UDP discovery."""
|
|
68
|
+
|
|
69
|
+
READY = 0
|
|
70
|
+
BUSY = 1
|
|
71
|
+
ERROR = 2
|
|
72
|
+
UNKNOWN = 3
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass(slots=True)
|
|
76
|
+
class DiscoveredPrinter:
|
|
77
|
+
"""Typed printer metadata returned by universal discovery."""
|
|
78
|
+
|
|
79
|
+
#: Resolved model (USB product ID first, then name). UNKNOWN when not recognised.
|
|
80
|
+
model: PrinterModelId
|
|
81
|
+
protocol_format: DiscoveryProtocol
|
|
82
|
+
name: str
|
|
83
|
+
ip_address: str
|
|
84
|
+
command_port: int
|
|
85
|
+
serial_number: str | None = None
|
|
86
|
+
event_port: int | None = None
|
|
87
|
+
vendor_id: int | None = None
|
|
88
|
+
product_id: int | None = None
|
|
89
|
+
product_type: int | None = None
|
|
90
|
+
status_code: int | None = None
|
|
91
|
+
status: PrinterStatus | None = None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass(slots=True)
|
|
95
|
+
class DiscoveryOptions:
|
|
96
|
+
"""Configuration options for printer discovery."""
|
|
97
|
+
|
|
98
|
+
timeout: int = 10000
|
|
99
|
+
idle_timeout: int = 1500
|
|
100
|
+
max_retries: int = 3
|
|
101
|
+
use_multicast: bool = True
|
|
102
|
+
use_broadcast: bool = True
|
|
103
|
+
ports: list[int] = field(default_factory=lambda: [8899, 19000, 48899])
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass(slots=True)
|
|
107
|
+
class FlashForgePrinter:
|
|
108
|
+
"""
|
|
109
|
+
Backward-compatible legacy discovery result.
|
|
110
|
+
|
|
111
|
+
This keeps the historical Python surface used by ff-5mp-hass while the richer
|
|
112
|
+
DiscoveredPrinter metadata is exposed via PrinterDiscovery.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
name: str = ""
|
|
116
|
+
serial_number: str = ""
|
|
117
|
+
ip_address: str = ""
|
|
118
|
+
|
|
119
|
+
@classmethod
|
|
120
|
+
def from_discovered_printer(cls, printer: DiscoveredPrinter) -> FlashForgePrinter:
|
|
121
|
+
return cls(
|
|
122
|
+
name=printer.name,
|
|
123
|
+
serial_number=printer.serial_number or "",
|
|
124
|
+
ip_address=printer.ip_address,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
def __str__(self) -> str:
|
|
128
|
+
return f"Name: {self.name}, Serial: {self.serial_number}, IP: {self.ip_address}"
|
|
129
|
+
|
|
130
|
+
def __repr__(self) -> str:
|
|
131
|
+
return (
|
|
132
|
+
"FlashForgePrinter("
|
|
133
|
+
f"name='{self.name}', serial_number='{self.serial_number}', ip_address='{self.ip_address}')"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class _DiscoveryDatagramProtocol(asyncio.DatagramProtocol):
|
|
138
|
+
"""Asyncio protocol used to push UDP datagrams into an async queue."""
|
|
139
|
+
|
|
140
|
+
def __init__(
|
|
141
|
+
self,
|
|
142
|
+
message_queue: asyncio.Queue[tuple[bytes, tuple[str, int]]],
|
|
143
|
+
error_queue: asyncio.Queue[Exception],
|
|
144
|
+
) -> None:
|
|
145
|
+
self.message_queue = message_queue
|
|
146
|
+
self.error_queue = error_queue
|
|
147
|
+
|
|
148
|
+
def datagram_received(self, data: bytes, addr: tuple[str, int]) -> None:
|
|
149
|
+
self.message_queue.put_nowait((data, addr))
|
|
150
|
+
|
|
151
|
+
def error_received(self, exc: Exception) -> None:
|
|
152
|
+
self.error_queue.put_nowait(exc)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class DiscoveryMonitor:
|
|
156
|
+
"""Event-based continuous discovery monitor."""
|
|
157
|
+
|
|
158
|
+
def __init__(self, discovery: PrinterDiscovery, options: DiscoveryOptions) -> None:
|
|
159
|
+
self._discovery = discovery
|
|
160
|
+
self._options = options
|
|
161
|
+
self._listeners: dict[str, list[Callable[..., Any]]] = {
|
|
162
|
+
"discovered": [],
|
|
163
|
+
"end": [],
|
|
164
|
+
"error": [],
|
|
165
|
+
}
|
|
166
|
+
self._transport: asyncio.DatagramTransport | None = None
|
|
167
|
+
self._task = asyncio.create_task(self._run())
|
|
168
|
+
self._stopped = False
|
|
169
|
+
self._end_emitted = False
|
|
170
|
+
|
|
171
|
+
def on(self, event: str, callback: Callable[..., Any]) -> DiscoveryMonitor:
|
|
172
|
+
"""Register an event callback."""
|
|
173
|
+
self._listeners.setdefault(event, []).append(callback)
|
|
174
|
+
return self
|
|
175
|
+
|
|
176
|
+
def stop(self) -> None:
|
|
177
|
+
"""Stop monitoring and clean up resources."""
|
|
178
|
+
if self._stopped:
|
|
179
|
+
return
|
|
180
|
+
self._stopped = True
|
|
181
|
+
if self._transport:
|
|
182
|
+
self._transport.close()
|
|
183
|
+
self._transport = None
|
|
184
|
+
if not self._task.done():
|
|
185
|
+
self._task.cancel()
|
|
186
|
+
self._emit_end_if_needed()
|
|
187
|
+
|
|
188
|
+
async def _run(self) -> None:
|
|
189
|
+
discovered_keys: set[str] = set()
|
|
190
|
+
message_queue: asyncio.Queue[tuple[bytes, tuple[str, int]]] = asyncio.Queue()
|
|
191
|
+
error_queue: asyncio.Queue[Exception] = asyncio.Queue()
|
|
192
|
+
last_response_at: float | None = None
|
|
193
|
+
send_interval_seconds = self._options.timeout / 1000.0
|
|
194
|
+
total_timeout_seconds = (self._options.timeout * self._options.max_retries) / 1000.0
|
|
195
|
+
|
|
196
|
+
try:
|
|
197
|
+
self._transport, _ = await self._discovery._create_endpoint(message_queue, error_queue)
|
|
198
|
+
self._discovery._send_discovery_packets(self._transport, self._options)
|
|
199
|
+
|
|
200
|
+
loop = asyncio.get_running_loop()
|
|
201
|
+
started_at = loop.time()
|
|
202
|
+
next_send_at = started_at + send_interval_seconds
|
|
203
|
+
|
|
204
|
+
while not self._stopped:
|
|
205
|
+
now = loop.time()
|
|
206
|
+
if now - started_at >= total_timeout_seconds:
|
|
207
|
+
break
|
|
208
|
+
|
|
209
|
+
if (
|
|
210
|
+
last_response_at is not None
|
|
211
|
+
and now - last_response_at >= self._options.idle_timeout / 1000.0
|
|
212
|
+
):
|
|
213
|
+
break
|
|
214
|
+
|
|
215
|
+
if now >= next_send_at:
|
|
216
|
+
self._discovery._send_discovery_packets(self._transport, self._options)
|
|
217
|
+
next_send_at = now + send_interval_seconds
|
|
218
|
+
|
|
219
|
+
wait_timeout = min(0.1, max(total_timeout_seconds - (now - started_at), 0.0))
|
|
220
|
+
try:
|
|
221
|
+
data, addr = await asyncio.wait_for(message_queue.get(), timeout=wait_timeout)
|
|
222
|
+
except TimeoutError:
|
|
223
|
+
if not error_queue.empty():
|
|
224
|
+
error = error_queue.get_nowait()
|
|
225
|
+
self._emit("error", error)
|
|
226
|
+
continue
|
|
227
|
+
|
|
228
|
+
printer = self._discovery.parse_discovery_response(data, addr[0])
|
|
229
|
+
if printer is None:
|
|
230
|
+
continue
|
|
231
|
+
|
|
232
|
+
last_response_at = loop.time()
|
|
233
|
+
key = f"{printer.ip_address}:{printer.command_port}"
|
|
234
|
+
if key not in discovered_keys:
|
|
235
|
+
discovered_keys.add(key)
|
|
236
|
+
self._emit("discovered", printer)
|
|
237
|
+
except asyncio.CancelledError:
|
|
238
|
+
raise
|
|
239
|
+
except Exception as error:
|
|
240
|
+
self._emit("error", error)
|
|
241
|
+
finally:
|
|
242
|
+
if self._transport:
|
|
243
|
+
self._transport.close()
|
|
244
|
+
self._transport = None
|
|
245
|
+
self._emit_end_if_needed()
|
|
246
|
+
|
|
247
|
+
def _emit(self, event: str, *args: Any) -> None:
|
|
248
|
+
listeners = list(self._listeners.get(event, []))
|
|
249
|
+
if event == "error" and not listeners:
|
|
250
|
+
logger.error("Discovery monitor error: %s", args[0] if args else "unknown error")
|
|
251
|
+
return
|
|
252
|
+
for callback in listeners:
|
|
253
|
+
callback(*args)
|
|
254
|
+
|
|
255
|
+
def _emit_end_if_needed(self) -> None:
|
|
256
|
+
if self._end_emitted:
|
|
257
|
+
return
|
|
258
|
+
self._end_emitted = True
|
|
259
|
+
for callback in list(self._listeners.get("end", [])):
|
|
260
|
+
callback()
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
class PrinterDiscovery:
|
|
264
|
+
"""Universal FlashForge printer discovery using UDP broadcast and multicast."""
|
|
265
|
+
|
|
266
|
+
async def discover(self, options: DiscoveryOptions | None = None) -> list[DiscoveredPrinter]:
|
|
267
|
+
config = options or DiscoveryOptions()
|
|
268
|
+
printers: dict[str, DiscoveredPrinter] = {}
|
|
269
|
+
|
|
270
|
+
for attempt in range(config.max_retries):
|
|
271
|
+
message_queue: asyncio.Queue[tuple[bytes, tuple[str, int]]] = asyncio.Queue()
|
|
272
|
+
error_queue: asyncio.Queue[Exception] = asyncio.Queue()
|
|
273
|
+
transport: asyncio.DatagramTransport | None = None
|
|
274
|
+
try:
|
|
275
|
+
transport, _ = await self._create_endpoint(message_queue, error_queue)
|
|
276
|
+
self._send_discovery_packets(transport, config)
|
|
277
|
+
discovered_printers = await self._receive_responses(
|
|
278
|
+
message_queue, error_queue, config
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
for printer in discovered_printers:
|
|
282
|
+
key = f"{printer.ip_address}:{printer.command_port}"
|
|
283
|
+
existing = printers.get(key)
|
|
284
|
+
if existing is None or printer.protocol_format == DiscoveryProtocol.MODERN:
|
|
285
|
+
printers[key] = printer
|
|
286
|
+
|
|
287
|
+
if printers:
|
|
288
|
+
break
|
|
289
|
+
finally:
|
|
290
|
+
if transport is not None:
|
|
291
|
+
transport.close()
|
|
292
|
+
|
|
293
|
+
if attempt < config.max_retries - 1:
|
|
294
|
+
await asyncio.sleep(1.0)
|
|
295
|
+
|
|
296
|
+
return list(printers.values())
|
|
297
|
+
|
|
298
|
+
def monitor(self, options: DiscoveryOptions | None = None) -> DiscoveryMonitor:
|
|
299
|
+
"""Create an event-based continuous discovery monitor."""
|
|
300
|
+
return DiscoveryMonitor(self, options or DiscoveryOptions())
|
|
301
|
+
|
|
302
|
+
async def _create_endpoint(
|
|
303
|
+
self,
|
|
304
|
+
message_queue: asyncio.Queue[tuple[bytes, tuple[str, int]]],
|
|
305
|
+
error_queue: asyncio.Queue[Exception],
|
|
306
|
+
) -> tuple[asyncio.DatagramTransport, _DiscoveryDatagramProtocol]:
|
|
307
|
+
loop = asyncio.get_running_loop()
|
|
308
|
+
try:
|
|
309
|
+
transport, protocol = await loop.create_datagram_endpoint(
|
|
310
|
+
lambda: _DiscoveryDatagramProtocol(message_queue, error_queue),
|
|
311
|
+
local_addr=("0.0.0.0", 0),
|
|
312
|
+
allow_broadcast=True,
|
|
313
|
+
)
|
|
314
|
+
except Exception as error:
|
|
315
|
+
raise SocketCreationError(str(error)) from error
|
|
316
|
+
return transport, protocol
|
|
317
|
+
|
|
318
|
+
async def _receive_responses(
|
|
319
|
+
self,
|
|
320
|
+
message_queue: asyncio.Queue[tuple[bytes, tuple[str, int]]],
|
|
321
|
+
error_queue: asyncio.Queue[Exception],
|
|
322
|
+
options: DiscoveryOptions,
|
|
323
|
+
) -> list[DiscoveredPrinter]:
|
|
324
|
+
printers: list[DiscoveredPrinter] = []
|
|
325
|
+
loop = asyncio.get_running_loop()
|
|
326
|
+
started_at = loop.time()
|
|
327
|
+
last_response_at = started_at
|
|
328
|
+
total_timeout_seconds = options.timeout / 1000.0
|
|
329
|
+
idle_timeout_seconds = options.idle_timeout / 1000.0
|
|
330
|
+
|
|
331
|
+
while True:
|
|
332
|
+
now = loop.time()
|
|
333
|
+
if now - started_at >= total_timeout_seconds:
|
|
334
|
+
break
|
|
335
|
+
if now - last_response_at >= idle_timeout_seconds:
|
|
336
|
+
break
|
|
337
|
+
|
|
338
|
+
try:
|
|
339
|
+
data, addr = await asyncio.wait_for(message_queue.get(), timeout=0.1)
|
|
340
|
+
except TimeoutError:
|
|
341
|
+
if not error_queue.empty():
|
|
342
|
+
logger.error("Socket error during discovery: %s", error_queue.get_nowait())
|
|
343
|
+
continue
|
|
344
|
+
|
|
345
|
+
last_response_at = loop.time()
|
|
346
|
+
printer = self.parse_discovery_response(data, addr[0])
|
|
347
|
+
if printer is not None:
|
|
348
|
+
printers.append(printer)
|
|
349
|
+
|
|
350
|
+
return printers
|
|
351
|
+
|
|
352
|
+
def _send_discovery_packets(
|
|
353
|
+
self,
|
|
354
|
+
transport: asyncio.DatagramTransport,
|
|
355
|
+
options: DiscoveryOptions,
|
|
356
|
+
) -> None:
|
|
357
|
+
packet = b""
|
|
358
|
+
|
|
359
|
+
if options.use_multicast:
|
|
360
|
+
self._join_multicast_group(transport)
|
|
361
|
+
for port in options.ports:
|
|
362
|
+
if port in {8899, 19000}:
|
|
363
|
+
try:
|
|
364
|
+
transport.sendto(packet, (MULTICAST_ADDRESS, port))
|
|
365
|
+
except Exception as error:
|
|
366
|
+
logger.warning(
|
|
367
|
+
"Discovery: Failed to send multicast to %s:%s - %s",
|
|
368
|
+
MULTICAST_ADDRESS,
|
|
369
|
+
port,
|
|
370
|
+
error,
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
if options.use_broadcast:
|
|
374
|
+
for address in self.get_broadcast_addresses():
|
|
375
|
+
for port in options.ports:
|
|
376
|
+
if port == 48899:
|
|
377
|
+
try:
|
|
378
|
+
transport.sendto(packet, (address, port))
|
|
379
|
+
except Exception as error:
|
|
380
|
+
logger.warning(
|
|
381
|
+
"Discovery: Failed to send broadcast to %s:%s - %s",
|
|
382
|
+
address,
|
|
383
|
+
port,
|
|
384
|
+
error,
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
for port in options.ports:
|
|
388
|
+
try:
|
|
389
|
+
transport.sendto(packet, ("255.255.255.255", port))
|
|
390
|
+
except Exception as error:
|
|
391
|
+
logger.warning(
|
|
392
|
+
"Discovery: Failed to send to broadcast 255.255.255.255:%s - %s",
|
|
393
|
+
port,
|
|
394
|
+
error,
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
def parse_discovery_response(
|
|
398
|
+
self,
|
|
399
|
+
buffer: bytes,
|
|
400
|
+
ip_address: str,
|
|
401
|
+
) -> DiscoveredPrinter | None:
|
|
402
|
+
"""Parse a UDP discovery response and return typed printer metadata."""
|
|
403
|
+
if not buffer:
|
|
404
|
+
return None
|
|
405
|
+
|
|
406
|
+
try:
|
|
407
|
+
if len(buffer) >= MODERN_PROTOCOL_SIZE:
|
|
408
|
+
return self.parse_modern_protocol(buffer, ip_address)
|
|
409
|
+
if len(buffer) >= LEGACY_PROTOCOL_SIZE:
|
|
410
|
+
return self.parse_legacy_protocol(buffer, ip_address)
|
|
411
|
+
|
|
412
|
+
logger.warning("Invalid discovery response: %s bytes from %s", len(buffer), ip_address)
|
|
413
|
+
return None
|
|
414
|
+
except Exception as error:
|
|
415
|
+
logger.error("Error parsing discovery response: %s", error)
|
|
416
|
+
return None
|
|
417
|
+
|
|
418
|
+
def parse_modern_protocol(self, buffer: bytes, ip_address: str) -> DiscoveredPrinter:
|
|
419
|
+
"""Parse a modern 276-byte (0x114) discovery response."""
|
|
420
|
+
if len(buffer) < MODERN_PROTOCOL_SIZE:
|
|
421
|
+
raise InvalidResponseError(len(buffer), ip_address)
|
|
422
|
+
|
|
423
|
+
name = buffer[0:0x84].decode("utf-8", errors="ignore").split("\x00", 1)[0]
|
|
424
|
+
command_port = int.from_bytes(buffer[0x84:0x86], byteorder="big")
|
|
425
|
+
vendor_id = int.from_bytes(buffer[0x86:0x88], byteorder="big")
|
|
426
|
+
product_id = int.from_bytes(buffer[0x88:0x8A], byteorder="big")
|
|
427
|
+
status_code = int.from_bytes(buffer[0x8A:0x8C], byteorder="big")
|
|
428
|
+
product_type = int.from_bytes(buffer[0x8C:0x8E], byteorder="big")
|
|
429
|
+
event_port = int.from_bytes(buffer[0x8E:0x90], byteorder="big")
|
|
430
|
+
serial_number = (
|
|
431
|
+
buffer[0x92 : 0x92 + 128].decode("utf-8", errors="ignore").split("\x00", 1)[0]
|
|
432
|
+
)
|
|
433
|
+
model = self.detect_modern_model(name, product_type, product_id)
|
|
434
|
+
|
|
435
|
+
return DiscoveredPrinter(
|
|
436
|
+
model=model,
|
|
437
|
+
protocol_format=DiscoveryProtocol.MODERN,
|
|
438
|
+
name=name,
|
|
439
|
+
ip_address=ip_address,
|
|
440
|
+
command_port=command_port,
|
|
441
|
+
serial_number=serial_number,
|
|
442
|
+
event_port=event_port,
|
|
443
|
+
vendor_id=vendor_id,
|
|
444
|
+
product_id=product_id,
|
|
445
|
+
product_type=product_type,
|
|
446
|
+
status_code=status_code,
|
|
447
|
+
status=self.map_status_code(status_code),
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
def parse_legacy_protocol(self, buffer: bytes, ip_address: str) -> DiscoveredPrinter:
|
|
451
|
+
"""Parse a legacy 140-byte discovery response."""
|
|
452
|
+
if len(buffer) < LEGACY_PROTOCOL_SIZE:
|
|
453
|
+
raise InvalidResponseError(len(buffer), ip_address)
|
|
454
|
+
|
|
455
|
+
name = buffer[0:0x80].decode("utf-8", errors="ignore").split("\x00", 1)[0]
|
|
456
|
+
command_port = int.from_bytes(buffer[0x84:0x86], byteorder="big")
|
|
457
|
+
vendor_id = int.from_bytes(buffer[0x86:0x88], byteorder="big")
|
|
458
|
+
product_id = int.from_bytes(buffer[0x88:0x8A], byteorder="big")
|
|
459
|
+
status_code = int.from_bytes(buffer[0x8A:0x8C], byteorder="big")
|
|
460
|
+
model = self.detect_legacy_model(name, product_id)
|
|
461
|
+
|
|
462
|
+
return DiscoveredPrinter(
|
|
463
|
+
model=model,
|
|
464
|
+
protocol_format=DiscoveryProtocol.LEGACY,
|
|
465
|
+
name=name,
|
|
466
|
+
ip_address=ip_address,
|
|
467
|
+
command_port=command_port,
|
|
468
|
+
vendor_id=vendor_id,
|
|
469
|
+
product_id=product_id,
|
|
470
|
+
status_code=status_code,
|
|
471
|
+
status=self.map_status_code(status_code),
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
def detect_modern_model(
|
|
475
|
+
self,
|
|
476
|
+
name: str,
|
|
477
|
+
product_type: int,
|
|
478
|
+
product_id: int | None = None,
|
|
479
|
+
) -> PrinterModelId:
|
|
480
|
+
"""Detect a modern printer model from discovery metadata.
|
|
481
|
+
|
|
482
|
+
Resolution order:
|
|
483
|
+
1. USB product ID (offset 0x88) -- authoritative, user-immutable, and
|
|
484
|
+
the only signal that distinguishes 5M / 5M Pro / AD5X / Creator 5.
|
|
485
|
+
2. product_type 0x5A02 (5M *family*) + name -- fallback for firmware
|
|
486
|
+
reporting an unknown/zero product ID.
|
|
487
|
+
3. Name heuristics -- last resort.
|
|
488
|
+
"""
|
|
489
|
+
# Product ID is authoritative (firmware-set, not user-mutable).
|
|
490
|
+
by_pid = model_from_pid(product_id)
|
|
491
|
+
if by_pid is not None and get_model_spec(by_pid).family != BackendFamily.LEGACY:
|
|
492
|
+
return by_pid
|
|
493
|
+
|
|
494
|
+
upper_name = name.upper()
|
|
495
|
+
|
|
496
|
+
# 0x5A02 identifies the 5M family but not the specific model, so we still
|
|
497
|
+
# need the name to separate base from Pro.
|
|
498
|
+
if product_type == 0x5A02:
|
|
499
|
+
if upper_name == "AD5X":
|
|
500
|
+
return PrinterModelId.AD5X
|
|
501
|
+
if "PRO" in upper_name:
|
|
502
|
+
return PrinterModelId.ADVENTURER_5M_PRO
|
|
503
|
+
return PrinterModelId.ADVENTURER_5M
|
|
504
|
+
|
|
505
|
+
if upper_name == "AD5X":
|
|
506
|
+
return PrinterModelId.AD5X
|
|
507
|
+
|
|
508
|
+
if "ADVENTURER 5M" in upper_name or "AD5M" in upper_name:
|
|
509
|
+
if "PRO" in upper_name:
|
|
510
|
+
return PrinterModelId.ADVENTURER_5M_PRO
|
|
511
|
+
return PrinterModelId.ADVENTURER_5M
|
|
512
|
+
|
|
513
|
+
return PrinterModelId.UNKNOWN
|
|
514
|
+
|
|
515
|
+
def detect_legacy_model(self, name: str, product_id: int | None = None) -> PrinterModelId:
|
|
516
|
+
"""Detect a legacy printer model from discovery metadata."""
|
|
517
|
+
upper_name = name.upper()
|
|
518
|
+
|
|
519
|
+
if "ADVENTURER 4" in upper_name or "ADVENTURER4" in upper_name or "AD4" in upper_name:
|
|
520
|
+
return PrinterModelId.ADVENTURER_4
|
|
521
|
+
|
|
522
|
+
if "ADVENTURER 3" in upper_name or "ADVENTURER3" in upper_name or "AD3" in upper_name:
|
|
523
|
+
return PrinterModelId.ADVENTURER_3
|
|
524
|
+
|
|
525
|
+
by_pid = model_from_pid(product_id)
|
|
526
|
+
if by_pid is not None and get_model_spec(by_pid).family == BackendFamily.LEGACY:
|
|
527
|
+
return by_pid
|
|
528
|
+
|
|
529
|
+
return PrinterModelId.UNKNOWN
|
|
530
|
+
|
|
531
|
+
def map_status_code(self, status_code: int) -> PrinterStatus:
|
|
532
|
+
"""Map discovery status codes to the typed printer status enum."""
|
|
533
|
+
if status_code == 0:
|
|
534
|
+
return PrinterStatus.READY
|
|
535
|
+
if status_code == 1:
|
|
536
|
+
return PrinterStatus.BUSY
|
|
537
|
+
if status_code == 2:
|
|
538
|
+
return PrinterStatus.ERROR
|
|
539
|
+
return PrinterStatus.UNKNOWN
|
|
540
|
+
|
|
541
|
+
def get_broadcast_addresses(self) -> list[str]:
|
|
542
|
+
"""Retrieve broadcast addresses for all active IPv4 interfaces."""
|
|
543
|
+
broadcast_addresses: list[str] = []
|
|
544
|
+
|
|
545
|
+
try:
|
|
546
|
+
for adapter in ifaddr.get_adapters():
|
|
547
|
+
for ip in adapter.ips:
|
|
548
|
+
if not ip.is_IPv4:
|
|
549
|
+
continue
|
|
550
|
+
|
|
551
|
+
ip_addr = ip.ip
|
|
552
|
+
if not isinstance(ip_addr, str) or ip_addr.startswith("127."):
|
|
553
|
+
continue
|
|
554
|
+
|
|
555
|
+
prefix = ip.network_prefix
|
|
556
|
+
if not isinstance(prefix, int) or not 0 < prefix <= 32:
|
|
557
|
+
continue
|
|
558
|
+
|
|
559
|
+
mask_int = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
|
|
560
|
+
netmask = ".".join(str((mask_int >> (8 * (3 - i))) & 0xFF) for i in range(4))
|
|
561
|
+
|
|
562
|
+
broadcast = self.calculate_broadcast_address(ip_addr, netmask)
|
|
563
|
+
if broadcast and broadcast not in broadcast_addresses:
|
|
564
|
+
broadcast_addresses.append(broadcast)
|
|
565
|
+
except Exception as error:
|
|
566
|
+
logger.error("Error getting network interfaces: %s", error)
|
|
567
|
+
broadcast_addresses = ["255.255.255.255", "192.168.1.255", "192.168.0.255"]
|
|
568
|
+
|
|
569
|
+
if "255.255.255.255" not in broadcast_addresses:
|
|
570
|
+
broadcast_addresses.append("255.255.255.255")
|
|
571
|
+
|
|
572
|
+
return broadcast_addresses
|
|
573
|
+
|
|
574
|
+
def calculate_broadcast_address(self, ip_address: str, subnet_mask: str) -> str | None:
|
|
575
|
+
"""Calculate an IPv4 broadcast address from IP and subnet mask."""
|
|
576
|
+
try:
|
|
577
|
+
ip_parts = [int(part) for part in ip_address.split(".")]
|
|
578
|
+
mask_parts = [int(part) for part in subnet_mask.split(".")]
|
|
579
|
+
if len(ip_parts) != 4 or len(mask_parts) != 4:
|
|
580
|
+
return None
|
|
581
|
+
broadcast_parts = [ip_parts[index] | (~mask_parts[index] & 255) for index in range(4)]
|
|
582
|
+
return ".".join(str(part) for part in broadcast_parts)
|
|
583
|
+
except Exception:
|
|
584
|
+
return None
|
|
585
|
+
|
|
586
|
+
def _join_multicast_group(self, transport: asyncio.DatagramTransport) -> None:
|
|
587
|
+
"""Join the discovery multicast group if the OS/network stack allows it."""
|
|
588
|
+
get_extra_info = getattr(transport, "get_extra_info", None)
|
|
589
|
+
if get_extra_info is None:
|
|
590
|
+
return
|
|
591
|
+
|
|
592
|
+
raw_socket = get_extra_info("socket")
|
|
593
|
+
if raw_socket is None:
|
|
594
|
+
return
|
|
595
|
+
|
|
596
|
+
try:
|
|
597
|
+
membership = socket.inet_aton(MULTICAST_ADDRESS) + socket.inet_aton("0.0.0.0")
|
|
598
|
+
raw_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, membership)
|
|
599
|
+
except Exception as error:
|
|
600
|
+
logger.warning(
|
|
601
|
+
"Discovery: Failed to join multicast group %s - %s",
|
|
602
|
+
MULTICAST_ADDRESS,
|
|
603
|
+
error,
|
|
604
|
+
)
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
class FlashForgePrinterDiscovery:
|
|
608
|
+
"""Backward-compatible wrapper around the universal PrinterDiscovery API."""
|
|
609
|
+
|
|
610
|
+
DISCOVERY_PORT = 48899
|
|
611
|
+
LISTEN_PORT = 18007
|
|
612
|
+
|
|
613
|
+
def __init__(self) -> None:
|
|
614
|
+
self.discovery_port = self.DISCOVERY_PORT
|
|
615
|
+
self.listen_port = self.LISTEN_PORT
|
|
616
|
+
self.discovery_message = bytes(
|
|
617
|
+
[
|
|
618
|
+
0x77,
|
|
619
|
+
0x77,
|
|
620
|
+
0x77,
|
|
621
|
+
0x2E,
|
|
622
|
+
0x75,
|
|
623
|
+
0x73,
|
|
624
|
+
0x72,
|
|
625
|
+
0x22,
|
|
626
|
+
0x65,
|
|
627
|
+
0x36,
|
|
628
|
+
0xC0,
|
|
629
|
+
0x00,
|
|
630
|
+
0x00,
|
|
631
|
+
0x00,
|
|
632
|
+
0x00,
|
|
633
|
+
0x00,
|
|
634
|
+
0x00,
|
|
635
|
+
0x00,
|
|
636
|
+
0x00,
|
|
637
|
+
0x00,
|
|
638
|
+
]
|
|
639
|
+
)
|
|
640
|
+
self._discovery = PrinterDiscovery()
|
|
641
|
+
|
|
642
|
+
async def discover_printers_async(
|
|
643
|
+
self,
|
|
644
|
+
timeout_ms: int = 10000,
|
|
645
|
+
idle_timeout_ms: int = 1500,
|
|
646
|
+
max_retries: int = 3,
|
|
647
|
+
) -> list[FlashForgePrinter]:
|
|
648
|
+
"""Discover printers and return the legacy FlashForgePrinter wrapper objects."""
|
|
649
|
+
printers = await self._discovery.discover(
|
|
650
|
+
DiscoveryOptions(
|
|
651
|
+
timeout=timeout_ms,
|
|
652
|
+
idle_timeout=idle_timeout_ms,
|
|
653
|
+
max_retries=max_retries,
|
|
654
|
+
)
|
|
655
|
+
)
|
|
656
|
+
return [FlashForgePrinter.from_discovered_printer(printer) for printer in printers]
|
|
657
|
+
|
|
658
|
+
def _parse_printer_response(
|
|
659
|
+
self,
|
|
660
|
+
response: bytes | None,
|
|
661
|
+
ip_address: str,
|
|
662
|
+
) -> FlashForgePrinter | None:
|
|
663
|
+
if response is None:
|
|
664
|
+
return None
|
|
665
|
+
|
|
666
|
+
if LEGACY_PROTOCOL_SIZE <= len(response) < MODERN_PROTOCOL_SIZE and len(response) >= 0x92:
|
|
667
|
+
legacy_name = (
|
|
668
|
+
response[0:32].decode("utf-8", errors="ignore").split("\x00", 1)[0].strip()
|
|
669
|
+
)
|
|
670
|
+
legacy_serial = (
|
|
671
|
+
response[0x92 : 0x92 + 32]
|
|
672
|
+
.decode("utf-8", errors="ignore")
|
|
673
|
+
.split("\x00", 1)[0]
|
|
674
|
+
.strip()
|
|
675
|
+
)
|
|
676
|
+
if legacy_name or legacy_serial:
|
|
677
|
+
return FlashForgePrinter(
|
|
678
|
+
name=legacy_name,
|
|
679
|
+
serial_number=legacy_serial,
|
|
680
|
+
ip_address=ip_address,
|
|
681
|
+
)
|
|
682
|
+
|
|
683
|
+
printer = self._discovery.parse_discovery_response(response, ip_address)
|
|
684
|
+
if printer is None:
|
|
685
|
+
return None
|
|
686
|
+
if not printer.name and not printer.serial_number:
|
|
687
|
+
return None
|
|
688
|
+
return FlashForgePrinter.from_discovered_printer(printer)
|
|
689
|
+
|
|
690
|
+
def _get_broadcast_addresses(self) -> list[str]:
|
|
691
|
+
return self._discovery.get_broadcast_addresses()
|
|
692
|
+
|
|
693
|
+
def _calculate_broadcast_address(self, ip_address: str, subnet_mask: str) -> str | None:
|
|
694
|
+
return self._discovery.calculate_broadcast_address(ip_address, subnet_mask)
|
|
695
|
+
|
|
696
|
+
def print_debug_info(self, response: bytes, ip_address: str) -> None:
|
|
697
|
+
print(f"Received response from {ip_address}:")
|
|
698
|
+
print(f"Response length: {len(response)} bytes")
|
|
699
|
+
print("Hex dump:")
|
|
700
|
+
for index in range(0, len(response), 16):
|
|
701
|
+
parts = [f"{index:04x} "]
|
|
702
|
+
for offset in range(16):
|
|
703
|
+
if index + offset < len(response):
|
|
704
|
+
parts.append(f"{response[index + offset]:02x} ")
|
|
705
|
+
else:
|
|
706
|
+
parts.append(" ")
|
|
707
|
+
if offset == 7:
|
|
708
|
+
parts.append(" ")
|
|
709
|
+
|
|
710
|
+
parts.append(" ")
|
|
711
|
+
for offset in range(16):
|
|
712
|
+
if index + offset < len(response):
|
|
713
|
+
byte = response[index + offset]
|
|
714
|
+
parts.append(chr(byte) if 32 <= byte <= 126 else ".")
|
|
715
|
+
print("".join(parts))
|
|
716
|
+
|
|
717
|
+
print("ASCII dump:")
|
|
718
|
+
print(repr(response.decode("ascii", errors="replace")))
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
async def _async_main() -> None:
|
|
722
|
+
discovery = FlashForgePrinterDiscovery()
|
|
723
|
+
printers = await discovery.discover_printers_async()
|
|
724
|
+
for printer in printers:
|
|
725
|
+
print(printer)
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
def main() -> None:
|
|
729
|
+
"""CLI entry point for `flashforge-discover`."""
|
|
730
|
+
asyncio.run(_async_main())
|