-
Notifications
You must be signed in to change notification settings - Fork 10
/
DuetRRFOutputDevice.py
551 lines (463 loc) · 19.7 KB
/
DuetRRFOutputDevice.py
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
import sys
import os.path
import datetime
import urllib
import json
import base64
from enum import Enum
try: # Cura 5
from PyQt6.QtNetwork import QNetworkReply
from PyQt6.QtCore import QUrl, QObject, QByteArray, QTimer
from PyQt6.QtGui import QDesktopServices
except: # Cura 4
from PyQt5.QtNetwork import QNetworkReply
from PyQt5.QtCore import QUrl, QObject, QByteArray, QTimer
from PyQt5.QtGui import QDesktopServices
from cura.CuraApplication import CuraApplication
from cura.Utils.Threading import call_on_qt_thread
from UM.Application import Application
from UM.Logger import Logger
from UM.Message import Message
from UM.OutputDevice.OutputDevice import OutputDevice
from UM.OutputDevice import OutputDeviceError
from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura")
from .helpers import serializing_scene_to_gcode
class OutputStage(Enum):
ready = 0
writing = 1
class DuetRRFDeviceType(Enum):
print = 0
simulate = 1
upload = 2
class DuetRRFConfigureOutputDevice(OutputDevice):
def __init__(self) -> None:
super().__init__("duetrrf-configure")
self.setShortDescription("DuetRRF Plugin")
self.setDescription("Configure Duet RepRapFirmware...")
self.setPriority(0)
def requestWrite(self, node, fileName=None, *args, **kwargs):
msg = (
"To configure your Duet RepRapFirmware printer go to:\n"
"→ Cura Preferences\n"
"→ Printers\n"
"→ activate and select your printer\n"
"→ click on 'Connect Duet RepRapFirmware'\n"
)
message = Message(
msg,
lifetime=0,
title="Configure DuetRRF in Cura Preferences!",
)
message.show()
self.writeSuccess.emit(self)
class DuetRRFOutputDevice(OutputDevice):
def __init__(self, config, device_type):
self._name_id = "duetrrf-{}".format(device_type.name)
super().__init__(self._name_id)
self._url = config["url"]
self._duet_password = config["duet_password"]
self._http_user = config["http_user"]
self._http_password = config["http_password"]
self.application = CuraApplication.getInstance()
global_container_stack = self.application.getGlobalContainerStack()
self._name = global_container_stack.getName()
self._device_type = device_type
if device_type == DuetRRFDeviceType.print:
description = catalog.i18nc("@action:button", "Print on {0}").format(self._name)
priority = 30
elif device_type == DuetRRFDeviceType.simulate:
description = catalog.i18nc("@action:button", "Simulate on {0}").format(self._name)
priority = 20
elif device_type == DuetRRFDeviceType.upload:
description = catalog.i18nc("@action:button", "Upload to {0}").format(self._name)
priority = 10
else:
assert False
self.setShortDescription(description)
self.setDescription(description)
self.setPriority(priority)
self._stage = OutputStage.ready
self._device_type = device_type
self._stream = None
self._message = None
self._use_rrf_http_api = True # by default we try to connect to the RRF HTTP API via rr_connect
Logger.log("d",
"New {} DuetRRFOutputDevice created | URL: {} | Duet password: {} | HTTP Basic Auth: user:{}, password:{}".format(
self._name_id,
self._url,
"set" if self._duet_password else "<empty>",
self._http_user if self._http_user else "<empty>",
"set" if self._http_password else "<empty>",
))
self._resetState()
def _timestamp(self):
return ("time", datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'))
def _send(self, command, query=None, next_stage=None, data=None, on_error=None, method='POST'):
url = self._url + command
if not query:
query = dict()
enc_query = urllib.parse.urlencode(query, quote_via=urllib.parse.quote)
if enc_query:
url += '?' + enc_query
headers = {
'User-Agent': 'Cura Plugin DuetRRF',
'Accept': 'application/json, text/javascript',
'Connection': 'keep-alive',
}
if self._http_user and self._http_password:
auth = "{}:{}".format(self._http_user, self._http_password).encode()
headers['Authorization'] = 'Basic ' + base64.b64encode(auth).decode()
if data:
headers['Content-Type'] = 'application/octet-stream'
if method == 'PUT':
self.application.getHttpRequestManager().put(
url,
headers,
data,
callback=next_stage,
error_callback=on_error if on_error else self._onNetworkError,
upload_progress_callback=self._onUploadProgress,
)
else:
self.application.getHttpRequestManager().post(
url,
headers,
data,
callback=next_stage,
error_callback=on_error if on_error else self._onNetworkError,
upload_progress_callback=self._onUploadProgress,
)
else:
self.application.getHttpRequestManager().get(
url,
headers,
callback=next_stage,
error_callback=on_error if on_error else self._onNetworkError,
)
# call on qt thread to get OpenGL to render the snapshot
@call_on_qt_thread
def requestWrite(self, node, fileName=None, *args, **kwargs):
if self._stage != OutputStage.ready:
raise OutputDeviceError.DeviceBusyError()
if fileName:
fileName = os.path.splitext(fileName)[0] + '.gcode'
else:
fileName = "%s.gcode" % Application.getInstance().getPrintInformation().jobName
self._fileName = fileName
extra_path = ""
if "PyQt5" in sys.modules: # Cura 4
extra_path = "legacy"
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', 'qml', extra_path, 'UploadFilename.qml')
self._dialog = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
self._dialog.textChanged.connect(self._onFilenameChanged)
self._dialog.accepted.connect(self._onFilenameAccepted)
self._dialog.show()
self._dialog.findChild(QObject, "nameField").setProperty('text', self._fileName)
self._dialog.findChild(QObject, "nameField").select(0, len(self._fileName) - len(".gcode"))
self._dialog.findChild(QObject, "nameField").setProperty('focus', True)
def _onFilenameChanged(self):
fileName = self._dialog.findChild(QObject, "nameField").property('text').strip()
forbidden_characters = "\"'´`<>()[]?*\,;:&%#$!"
for forbidden_character in forbidden_characters:
if forbidden_character in fileName:
self._dialog.setProperty('validName', False)
self._dialog.setProperty('validationError', 'Filename cannot contain {}'.format(forbidden_characters))
return
if fileName == '.' or fileName == '..':
self._dialog.setProperty('validName', False)
self._dialog.setProperty('validationError', 'Filename cannot be "." or ".."')
return
self._dialog.setProperty('validName', len(fileName) > 0)
self._dialog.setProperty('validationError', 'Filename too short')
def _onFilenameAccepted(self):
self._fileName = self._dialog.findChild(QObject, "nameField").property('text').strip()
if not self._fileName.endswith('.gcode') and '.' not in self._fileName:
self._fileName += '.gcode'
Logger.log("d", "Filename set to: " + self._fileName)
self._dialog.deleteLater()
self._stage = OutputStage.writing
self.writeStarted.emit(self)
# show a progress message
self._message = Message(
"Serializing gcode...",
lifetime=0,
dismissable=False,
progress=-1,
title="DuetRRF: " + self._name,
)
self._message.show()
self._stream = serializing_scene_to_gcode()
# start upload workflow
self._message.setText("Uploading {} ...".format(self._fileName))
Logger.log("d", "Connecting...")
self._send('rr_connect',
query=[("password", self._duet_password), self._timestamp()],
next_stage=self._onUploadReady,
on_error=self._check_duet3_sbc,
)
def _check_duet3_sbc(self, reply, error):
Logger.log("d", "rr_connect failed with error " + str(error))
if error == QNetworkReply.NetworkError.ContentNotFoundError:
Logger.log("d", "error indicates Duet3+SBC - let's try the DuetSoftwareFramework API instead...")
self._use_rrf_http_api = False # let's try the newer DuetSoftwareFramework for Duet3+SBC API instead
self._send('machine/status',
next_stage=self._onUploadReady
)
else:
self._onNetworkError(reply, error)
def _onUploadReady(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NetworkError.NoError:
Logger.log("d", "Stopping due to reply error: " + reply.error())
return
Logger.log("d", "Uploading...")
self._postData = QByteArray()
self._postData.append(self._stream.getvalue().encode())
if self._use_rrf_http_api:
self._send('rr_upload',
query=[("name", "0:/gcodes/" + self._fileName), self._timestamp()],
next_stage=self._onUploadDone,
data=self._postData,
)
else:
self._send('machine/file/gcodes/' + self._fileName,
next_stage=self._onUploadDone,
data=self._postData,
method='PUT',
)
def _onUploadDone(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NetworkError.NoError:
Logger.log("d", "Stopping due to reply error: " + reply.error())
return
Logger.log("d", "Upload done")
self._stream.close()
self._stream = None
if self._device_type == DuetRRFDeviceType.simulate:
Logger.log("d", "Simulating...")
if self._message:
self._message.hide()
self._message = None
self._message = Message(
"Simulating print {}...\nPlease close DWC and DO NOT interact with the printer!".format(self._fileName),
lifetime=0,
dismissable=False,
progress=-1,
title="DuetRRF: " + self._name,
)
self._message.show()
gcode='M37 P"0:/gcodes/' + self._fileName + '"'
Logger.log("d", "Sending gcode: " + gcode)
if self._use_rrf_http_api:
self._send('rr_gcode',
query=[("gcode", gcode)],
next_stage=self._onSimulationPrintStarted,
)
else:
self._send('machine/code',
data=gcode.encode(),
next_stage=self._onSimulationPrintStarted,
)
elif self._device_type == DuetRRFDeviceType.print:
self._onReadyToPrint()
elif self._device_type == DuetRRFDeviceType.upload:
if self._use_rrf_http_api:
self._send('rr_disconnect')
if self._message:
self._message.hide()
self._message = None
self._message = Message(
"Uploaded file: {}".format(self._fileName),
lifetime=15,
title="DuetRRF: " + self._name,
)
self._message.addAction("open_browser", catalog.i18nc("@action:button", "Open Browser"), "globe", catalog.i18nc("@info:tooltip", "Open browser to DuetWebControl."))
self._message.actionTriggered.connect(self._onMessageActionTriggered)
self._message.show()
self.writeSuccess.emit(self)
self._resetState()
def _onReadyToPrint(self):
if self._stage != OutputStage.writing:
return
Logger.log("d", "Ready to print")
gcode = 'M32 "0:/gcodes/' + self._fileName + '"'
Logger.log("d", "Sending gcode: " + gcode)
if self._use_rrf_http_api:
self._send('rr_gcode',
query=[("gcode", gcode)],
next_stage=self._onPrintStarted,
)
else:
self._send('machine/code',
data=gcode.encode(),
next_stage=self._onPrintStarted,
)
def _onPrintStarted(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NetworkError.NoError:
Logger.log("d", "Stopping due to reply error: " + reply.error())
return
Logger.log("d", "Print started")
if self._use_rrf_http_api:
self._send('rr_disconnect')
if self._message:
self._message.hide()
self._message = None
self._message = Message(
"Print started: {}".format(self._fileName),
lifetime=15,
title="DuetRRF: " + self._name,
)
self._message.addAction("open_browser", catalog.i18nc("@action:button", "Open Browser"), "globe", catalog.i18nc("@info:tooltip", "Open browser to DuetWebControl."))
self._message.actionTriggered.connect(self._onMessageActionTriggered)
self._message.show()
self.writeSuccess.emit(self)
self._resetState()
def _onSimulationPrintStarted(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NetworkError.NoError:
Logger.log("d", "Stopping due to reply error: " + reply.error())
return
Logger.log("d", "Simulation print started for file " + self._fileName)
# give it some to start the simulation
QTimer.singleShot(2000, self._onCheckStatus)
def _onCheckStatus(self):
if self._stage != OutputStage.writing:
return
Logger.log("d", "Checking status...")
if self._use_rrf_http_api:
self._send('rr_status',
query=[("type", "3")],
next_stage=self._onStatusReceived,
)
else:
self._send('machine/status',
next_stage=self._onStatusReceived,
)
def _onStatusReceived(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NetworkError.NoError:
Logger.log("d", "Stopping due to reply error: " + reply.error())
return
Logger.log("d", "Status received - decoding...")
reply_body = bytes(reply.readAll()).decode()
# Logger.log("d", "Status: " + reply_body)
status = json.loads(reply_body)
if self._use_rrf_http_api:
# RRF 1.21RC2 and earlier used P while simulating
# RRF 1.21RC3 and later uses M while simulating
busy = status["status"] in ['P', 'M']
else:
s = status.get("state", {}).get("status", None)
if not s:
# we might not have received a full status report, assume we are still simulating and busy
busy = True
else:
busy = s == 'simulating'
progress = 0.0
try:
if "fractionPrinted" in status:
progress = float(status["fractionPrinted"])
else:
file_size = status.get("job", {}).get("file", {}).get("size", None)
file_position = status.get("job", {}).get("filePosition", 0)
progress = int(file_position) / int(file_size) * 100.0
except:
pass
if busy:
# still simulating
if self._message:
self._message.setProgress(progress)
QTimer.singleShot(2000, self._onCheckStatus)
else:
Logger.log("d", "Simulation print finished")
gcode='M37'
Logger.log("d", "Sending gcode:" + gcode)
if self._use_rrf_http_api:
self._send('rr_gcode',
query=[("gcode", gcode)],
next_stage=self._onM37Reported,
)
else:
self._send('machine/code',
data=gcode.encode(),
next_stage=self._onReported,
)
def _onM37Reported(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NetworkError.NoError:
Logger.log("d", "Stopping due to reply error: " + reply.error())
return
Logger.log("d", "M37 finished - let's get it's reply...")
reply_body = bytes(reply.readAll()).decode().strip()
Logger.log("d", "M37 gcode reply | " + reply_body)
self._send('rr_reply',
next_stage=self._onReported,
)
def _onReported(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NetworkError.NoError:
Logger.log("d", "Stopping due to reply error: " + reply.error())
return
Logger.log("d", "Simulation status received - decoding...")
reply_body = bytes(reply.readAll()).decode().strip()
Logger.log("d", "Reported | " + reply_body)
if self._message:
self._message.hide()
self._message = None
self._message = Message(
"Simulation finished!\n\n{}".format(reply_body),
lifetime=0,
title="DuetRRF: " + self._name,
)
self._message.addAction("open_browser", catalog.i18nc("@action:button", "Open Browser"), "globe", catalog.i18nc("@info:tooltip", "Open browser to DuetWebControl."))
self._message.actionTriggered.connect(self._onMessageActionTriggered)
self._message.show()
if self._use_rrf_http_api:
self._send('rr_disconnect')
self.writeSuccess.emit(self)
self._resetState()
def _resetState(self):
Logger.log("d", "called")
if self._stream:
self._stream.close()
self._stream = None
self._stage = OutputStage.ready
self._fileName = None
def _onMessageActionTriggered(self, message, action):
if action == "open_browser":
QDesktopServices.openUrl(QUrl(self._url))
if self._message:
self._message.hide()
self._message = None
def _onUploadProgress(self, bytesSent, bytesTotal):
if bytesTotal > 0:
progress = int(bytesSent * 100 / bytesTotal)
if self._message:
self._message.setProgress(progress)
self.writeProgress.emit(self, progress)
def _onNetworkError(self, reply, error):
# https://doc.qt.io/qt-6/qnetworkreply.html#NetworkError-enum
Logger.log("e", repr(error))
if self._message:
self._message.hide()
self._message = None
errorString = ''
if reply:
errorString = reply.errorString()
message = Message(
"There was a network error: {} {}".format(error, errorString),
lifetime=0,
title="DuetRRF: " + self._name,
)
message.show()
self.writeError.emit(self)
self._resetState()