It's now possible to send print jobs to cluster again

CL-541
This commit is contained in:
Jaime van Kessel 2017-11-28 12:59:54 +01:00
parent cfc6a3ad48
commit 9084dfd6bd
3 changed files with 59 additions and 17 deletions

View file

@ -7,14 +7,14 @@ from UM.Logger import Logger
from cura.PrinterOutputDevice import PrinterOutputDevice, ConnectionState
from PyQt5.QtNetwork import QHttpMultiPart, QHttpPart, QNetworkRequest, QNetworkAccessManager, QNetworkReply
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QTimer, pyqtSignal, QUrl
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, pyqtSignal, QUrl, QCoreApplication
from time import time
from typing import Callable, Any, Optional
from enum import IntEnum
from typing import List
import os
import os # To get the username
import gzip
class AuthState(IntEnum):
NotAuthenticated = 1
@ -63,6 +63,16 @@ class NetworkedPrinterOutputDevice(PrinterOutputDevice):
def authenticationState(self):
return self._authentication_state
def _compressDataAndNotifyQt(self, data_to_append):
compressed_data = gzip.compress(data_to_append.encode("utf-8"))
self._progress_message.setProgress(-1) # Tickle the message so that it's clear that it's still being used.
QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
# Pretend that this is a response, as zipping might take a bit of time.
# If we don't do this, the device might trigger a timeout.
self._last_response_time = time()
return compressed_data
def _compressGCode(self):
self._compressing_gcode = True
@ -81,12 +91,12 @@ class NetworkedPrinterOutputDevice(PrinterOutputDevice):
# Compressing line by line in this case is extremely slow, so we need to batch them.
if len(batched_line) < max_chars_per_line:
continue
byte_array_file_data += self.__compressDataAndNotifyQt(batched_line)
byte_array_file_data += self._compressDataAndNotifyQt(batched_line)
batched_line = ""
# Don't miss the last batch (If any)
if batched_line:
byte_array_file_data += self.__compressDataAndNotifyQt(batched_line)
byte_array_file_data += self._compressDataAndNotifyQt(batched_line)
self._compressing_gcode = False
return byte_array_file_data

View file

@ -16,6 +16,8 @@ from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import pyqtSlot, QUrl, pyqtSignal, pyqtProperty
from time import time
import json
import os
@ -61,6 +63,9 @@ class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice):
# Unable to find g-code. Nothing to send
return
# TODO; DEBUG
self.sendPrintJob()
@pyqtSlot()
def sendPrintJob(self):
Logger.log("i", "Sending print job to printer.")
@ -83,8 +88,46 @@ class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice):
# Abort was called.
return
parts = []
# If a specific printer was selected, it should be printed with that machine.
require_printer_name = "" # Todo; actually needs to be set
if require_printer_name:
parts.append(self._createFormPart("name=require_printer_name", bytes(require_printer_name, "utf-8"), "text/plain"))
# Add user name to the print_job
parts.append(self._createFormPart("name=owner", bytes(self._getUserName(), "utf-8"), "text/plain"))
file_name = "%s.gcode.gz" % Application.getInstance().getPrintInformation().jobName
parts.append(self._createFormPart("name=\"file\"; filename=\"%s\"" % file_name, compressed_gcode))
self.postFormWithParts("print_jobs/", parts, onFinished=self._onPostPrintJobFinished, onProgress=self._onUploadPrintJobProgress)
def _onPostPrintJobFinished(self, reply):
print("POST PRINTJOB DONE! YAY!", reply.readAll())
pass
def _onUploadPrintJobProgress(self, bytes_sent, bytes_total):
if bytes_total > 0:
new_progress = bytes_sent / bytes_total * 100
# Treat upload progress as response. Uploading can take more than 10 seconds, so if we don't, we can get
# timeout responses if this happens.
self._last_response_time = time()
if new_progress > self._progress_message.getProgress():
self._progress_message.show() # Ensure that the message is visible.
self._progress_message.setProgress(bytes_sent / bytes_total * 100)
else:
self._progress_message.setProgress(0)
self._progress_message.hide()
def _progressMessageActionTriggered(self, message_id=None, action_id=None):
if action_id == "Abort":
Logger.log("d", "User aborted sending print to remote.")
self._progress_message.hide()
self._compressing_gcode = False
self._sending_gcode = False
Application.getInstance().showPrintMonitor.emit(False)
@pyqtSlot()
def openPrintJobControlPanel(self):

View file

@ -21,8 +21,7 @@ from .LegacyUM3PrinterOutputController import LegacyUM3PrinterOutputController
from time import time
import json
import os # To get the username
import gzip
i18n_catalog = i18nCatalog("cura")
@ -259,16 +258,6 @@ class LegacyUM3OutputDevice(NetworkedPrinterOutputDevice):
self._progress_message.hide()
self._sending_gcode = False
def __compressDataAndNotifyQt(self, data_to_append):
compressed_data = gzip.compress(data_to_append.encode("utf-8"))
self._progress_message.setProgress(-1) # Tickle the message so that it's clear that it's still being used.
QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
# Pretend that this is a response, as zipping might take a bit of time.
# If we don't do this, the device might trigger a timeout.
self._last_response_time = time()
return compressed_data
def _onUploadPrintJobProgress(self, bytes_sent, bytes_total):
if bytes_total > 0:
new_progress = bytes_sent / bytes_total * 100