From ca79e398c88524dd35ec1e2557ff4971c4025d3c Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 30 Jun 2016 14:25:54 +0200 Subject: [PATCH 1/2] All known materials are now send upon connection Due to garbage collection issues, the (multi) part requests are now cached. CURA-334 --- NetworkPrinterOutputDevice.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/NetworkPrinterOutputDevice.py b/NetworkPrinterOutputDevice.py index 0c8805e026..f82da80d4c 100644 --- a/NetworkPrinterOutputDevice.py +++ b/NetworkPrinterOutputDevice.py @@ -5,6 +5,8 @@ from UM.Signal import signalemitter from UM.Message import Message +import UM.Settings + from cura.PrinterOutputDevice import PrinterOutputDevice, ConnectionState from PyQt5.QtNetwork import QHttpMultiPart, QHttpPart, QNetworkRequest, QNetworkAccessManager @@ -63,6 +65,10 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): self._post_multi_part = None self._post_part = None + + self._material_multi_part = None + self._material_part = None + self._progress_message = None self._error_message = None @@ -93,6 +99,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): self._authentication_requested_message = Message(i18n_catalog.i18nc("@info:status", "Requested access. Please aprove the request on the printer"), lifetime = 0, dismissable = False, progress = 0) self._camera_image = QImage() + self._material_post_objects = {} + def _onAuthenticationTimer(self): self._authentication_counter += 1 self._authentication_requested_message.setProgress(self._authentication_counter / self._max_authentication_counter * 100) @@ -145,6 +153,9 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): self._authentication_requested_message.hide() authentication_succeeded_message = Message(i18n_catalog.i18nc("@info:status", "Printer was successfully paired with Cura")) authentication_succeeded_message.show() + # Once we are authenticated we need to send all material profiles. + # + self.sendMaterialProfiles() elif auth_state == AuthState.AuthenticationDenied: self._authentication_requested_message.hide() authentication_failed_message = Message(i18n_catalog.i18nc("@info:status", "Pairing request failed. This can be either due to a timeout or the printer refused the request.")) @@ -326,6 +337,32 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): self.setAuthenticationState(AuthState.AuthenticationRequested) self._manager.post(request, json.dumps({"application": "Cura-" + Application.getInstance().getVersion(), "user": self._getUserName()}).encode()) + ## Send all material profiles to the printer. + def sendMaterialProfiles(self): + for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(type = "material"): + try: + xml_data = container.serialize() + if xml_data == "": + continue + material_multi_part = QHttpMultiPart(QHttpMultiPart.FormDataType) + + material_part = QHttpPart() + file_name = "none.xml" + material_part.setHeader(QNetworkRequest.ContentDispositionHeader, "form-data; name=\"file\";filename=\"%s\"" % file_name) + material_part.setBody(xml_data.encode()) + material_multi_part.append(material_part) + url = QUrl("http://" + self._address + self._api_prefix + "materials") + material_post_request = QNetworkRequest(url) + + self._manager.post(material_post_request, material_multi_part) + + # Keep reference to material_part and material_multi_part so the garbage collector won't touch them. + self._material_post_objects[container.getId()] = (material_part, material_multi_part) + except NotImplementedError: + # If the material container is not the most "generic" one it can't be serialized an will raise a + # NotImplementedError. We can simply ignore these. + pass + ## Handler for all requests that have finished. def _onFinished(self, reply): if reply.operation() == QNetworkAccessManager.GetOperation: @@ -403,6 +440,9 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): # Check if the authentication is accepted. self._checkAuthentication() + elif "materials" in reply.url().toString(): + # TODO: Remove cached post request items. + pass else: reply.uploadProgress.disconnect(self._onUploadProgress) self._progress_message.hide() From bfc7a7e3c956d9df7df11aa82f9aaf8cf27d47ea Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 30 Jun 2016 14:56:36 +0200 Subject: [PATCH 2/2] Multipart data objects used for posting new materials are now deleted upon completion CURA-344 --- NetworkPrinterOutputDevice.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/NetworkPrinterOutputDevice.py b/NetworkPrinterOutputDevice.py index f82da80d4c..3fc22ad4d7 100644 --- a/NetworkPrinterOutputDevice.py +++ b/NetworkPrinterOutputDevice.py @@ -353,11 +353,10 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): material_multi_part.append(material_part) url = QUrl("http://" + self._address + self._api_prefix + "materials") material_post_request = QNetworkRequest(url) + reply = self._manager.post(material_post_request, material_multi_part) - self._manager.post(material_post_request, material_multi_part) - - # Keep reference to material_part and material_multi_part so the garbage collector won't touch them. - self._material_post_objects[container.getId()] = (material_part, material_multi_part) + # Keep reference to material_part, material_multi_part and reply so the garbage collector won't touch them. + self._material_post_objects[id(reply)] = (material_part, material_multi_part, reply) except NotImplementedError: # If the material container is not the most "generic" one it can't be serialized an will raise a # NotImplementedError. We can simply ignore these. @@ -441,8 +440,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice): # Check if the authentication is accepted. self._checkAuthentication() elif "materials" in reply.url().toString(): - # TODO: Remove cached post request items. - pass + # Remove cached post request items. + del self._material_post_objects[id(reply)] else: reply.uploadProgress.disconnect(self._onUploadProgress) self._progress_message.hide()