def startPrint(self):
try:
self._send_gcode_start = time()
self._progress_message = Message(i18n_catalog.i18nc("@info:status", "Sending data to printer"), 0, False, -1)
self._progress_message.addAction("Abort", i18n_catalog.i18nc("@action:button", "Cancel"), None, "")
self._progress_message.actionTriggered.connect(self._progressMessageActionTrigger)
self._progress_message.show()
Logger.log("d", "Started sending g-code to remote printer.")
self._compressing_print = True
## Mash the data into single string
byte_array_file_data = b""
for line in self._gcode:
if not self._compressing_print:
self._progress_message.hide()
return # Stop trying to zip, abort was called.
if self._use_gzip:
byte_array_file_data += gzip.compress(line.encode("utf-8"))
QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
# Pretend that this is a response, as zipping might take a bit of time.
self._last_response_time = time()
else:
byte_array_file_data += line.encode("utf-8")
if self._use_gzip:
file_name = "%s.gcode.gz" % Application.getInstance().getPrintInformation().jobName
else:
file_name = "%s.gcode" % Application.getInstance().getPrintInformation().jobName
self._compressing_print = False
## Create multi_part request
self._post_multi_part = QHttpMultiPart(QHttpMultiPart.FormDataType)
## Create part (to be placed inside multipart)
self._post_part = QHttpPart()
self._post_part.setHeader(QNetworkRequest.ContentDispositionHeader,
"form-data; name=\"file\"; filename=\"%s\"" % file_name)
self._post_part.setBody(byte_array_file_data)
self._post_multi_part.append(self._post_part)
url = QUrl("http://" + self._address + self._api_prefix + "print_job")
## Create the QT request
self._post_request = QNetworkRequest(url)
## Post request + data
self._post_reply = self._manager.post(self._post_request, self._post_multi_part)
self._post_reply.uploadProgress.connect(self._onUploadProgress)
except IOError:
self._progress_message.hide()
self._error_message = Message(i18n_catalog.i18nc("@info:status", "Unable to send data to printer. Is another job still active?"))
self._error_message.show()
except Exception as e:
self._progress_message.hide()
Logger.log("e", "An exception occurred in network connection: %s" % str(e))
## Verify if we are authenticated to make requests.
NetworkPrinterOutputDevice.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录