Skip to content

Commit

Permalink
fix crash with HTTP finished signal being called also in error states
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriechi committed May 17, 2020
1 parent 39344d2 commit 1eb0738
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog of Cura-DuetRRFPlugin

## v1.0.10: 2020-05-XX
* fix Cura crashes when using Duet3 with SBC

## v1.0.9: 2020-05-02
* bump compatibility for Cura 4.6 / API 7.2
* fix simulation result reply for RRF HTTP API
Expand Down
70 changes: 43 additions & 27 deletions DuetRRFOutputDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, name, url, duet_password, http_user, http_password, device_ty
self.setPriority(priority)

self._stage = OutputStage.ready
self._stream = None
self._name = name
self._name_id = name_id
self._device_type = device_type
Expand All @@ -79,9 +80,6 @@ def __init__(self, name, url, duet_password, http_user, http_password, device_ty
Logger.log("d", self._name_id + " | HTTP Basic Auth user: " + ("set." if self._http_user else "empty."))
Logger.log("d", self._name_id + " | HTTP Basic Auth password: " + ("set." if self._http_password else "empty."))

self._qnam = QtNetwork.QNetworkAccessManager()

self._stream = None
self._cleanupRequest()

if hasattr(self, '_message'):
Expand All @@ -101,26 +99,27 @@ def _send(self, command, query=None, next_stage=None, data=None, on_error=None,
if enc_query:
url += '?' + enc_query

self._request = QtNetwork.QNetworkRequest(QUrl(url))
self._request.setRawHeader(b'User-Agent', b'Cura Plugin DuetRRF')
self._request.setRawHeader(b'Accept', b'application/json, text/javascript')
self._request.setRawHeader(b'Connection', b'keep-alive')
request = QtNetwork.QNetworkRequest(QUrl(url))
request.setRawHeader(b'User-Agent', b'Cura Plugin DuetRRF')
request.setRawHeader(b'Accept', b'application/json, text/javascript')
request.setRawHeader(b'Connection', b'keep-alive')

if self._http_user and self._http_password:
self._request.setRawHeader(b'Authorization', b'Basic ' + base64.b64encode("{}:{}".format(self._http_user, self._http_password).encode()))
request.setRawHeader(b'Authorization', b'Basic ' + base64.b64encode("{}:{}".format(self._http_user, self._http_password).encode()))

self._qnam = QtNetwork.QNetworkAccessManager()
if next_stage:
self._qnam.finished.connect(next_stage)

if data:
self._request.setRawHeader(b'Content-Type', b'application/octet-stream')
request.setRawHeader(b'Content-Type', b'application/octet-stream')
if method == 'PUT':
self._reply = self._qnam.put(self._request, data)
self._reply = self._qnam.put(request, data)
else:
self._reply = self._qnam.post(self._request, data)
self._reply = self._qnam.post(request, data)
self._reply.uploadProgress.connect(self._onUploadProgress)
else:
self._reply = self._qnam.get(self._request)

if next_stage:
self._reply.finished.connect(next_stage)
self._reply = self._qnam.get(request)

if on_error:
self._reply.error.connect(on_error)
Expand Down Expand Up @@ -210,16 +209,15 @@ def _check_duet3_sbc(self, errorCode):
else:
self._onNetworkError(errorCode)

def onUploadReady(self):
def onUploadReady(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NoError:
Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error())
return

Logger.log("d", self._name_id + " | Uploading...")

if not self._stream:
Logger.log("d", self._name_id + " | Upload failed because stream is already None - THIS SHOULD NOT HAPPEN!")
return

self._stream.seek(0)
self._postData = QByteArray()
self._postData.append(self._stream.getvalue().encode())
Expand All @@ -237,9 +235,12 @@ def onUploadReady(self):
method='PUT',
)

def onUploadDone(self):
def onUploadDone(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NoError:
Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error())
return

Logger.log("d", self._name_id + " | Upload done")

Expand Down Expand Up @@ -300,9 +301,12 @@ def onReadyToPrint(self):
next_stage=self.onPrintStarted,
)

def onPrintStarted(self):
def onPrintStarted(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NoError:
Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error())
return

Logger.log("d", self._name_id + " | Print started")

Expand All @@ -319,9 +323,12 @@ def onPrintStarted(self):
self.writeSuccess.emit(self)
self._cleanupRequest()

def onSimulationPrintStarted(self):
def onSimulationPrintStarted(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NoError:
Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error())
return

Logger.log("d", self._name_id + " | Simulation print started for file " + self._fileName)

Expand All @@ -344,9 +351,12 @@ def onCheckStatus(self):
next_stage=self.onStatusReceived,
)

def onStatusReceived(self):
def onStatusReceived(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NoError:
Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error())
return

Logger.log("d", self._name_id + " | Status received - decoding...")
reply_body = bytes(self._reply.readAll()).decode()
Expand Down Expand Up @@ -380,9 +390,12 @@ def onStatusReceived(self):
data=gcode.encode(),
next_stage=self.onReported,
)
def onM37Reported(self):
def onM37Reported(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NoError:
Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error())
return

Logger.log("d", self._name_id + " | M37 finished - let's get it's reply...")
reply_body = bytes(self._reply.readAll()).decode().strip()
Expand All @@ -392,9 +405,12 @@ def onM37Reported(self):
next_stage=self.onReported,
)

def onReported(self):
def onReported(self, reply):
if self._stage != OutputStage.writing:
return
if reply.error() != QNetworkReply.NoError:
Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error())
return

Logger.log("d", self._name_id + " | Simulation status received - decoding...")
reply_body = bytes(self._reply.readAll()).decode().strip()
Expand All @@ -421,8 +437,8 @@ def _onProgress(self, progress):

def _cleanupRequest(self):
Logger.log("e", "_cleanupRequest called")
self._qnam = None
self._reply = None
self._request = None
if self._stream:
self._stream.close()
self._stream = None
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "DuetRRF",
"author": "Thomas Kriechbaumer",
"description": "Upload and Print to Duet 2 Wifi / Duet 2 Ethernet / Duet 2 Maestro / Duet 3 with RepRapFirmware.",
"version": "1.0.9",
"version": "1.0.10",
"supported_sdk_versions": ["7.2.0"]
}

0 comments on commit 1eb0738

Please sign in to comment.