From 63c5b779595cb1488c44094eb431694430161625 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Fri, 7 Dec 2018 16:43:53 +0100 Subject: [PATCH] STAR-322: Fixing finished jobs --- .../src/Cloud/CloudOutputDevice.py | 54 +++++++++---------- .../src/Cloud/CloudOutputDeviceManager.py | 9 ++-- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDevice.py b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDevice.py index 321c40bc74..2800bc1c8c 100644 --- a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDevice.py +++ b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDevice.py @@ -1,10 +1,9 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. import os -from datetime import datetime from time import time -from typing import Dict, List, Optional +from typing import Dict, List, Optional, Set from PyQt5.QtCore import QObject, QUrl, pyqtProperty, pyqtSignal, pyqtSlot @@ -92,7 +91,7 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): self._host_name = host_name self._setInterfaceElements() - + self._device_id = device_id self._account = CuraApplication.getInstance().getCuraAPI().account @@ -111,7 +110,7 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): # Properties to populate later on with received cloud data. self._print_jobs = [] # type: List[UM3PrintJobOutputModel] self._number_of_extruders = 2 # All networked printers are dual-extrusion Ultimaker machines. - + # We only allow a single upload at a time. self._sending_job = False # TODO: handle progress messages in another class. @@ -121,6 +120,9 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): self._received_printers = None # type: Optional[List[CloudClusterPrinter]] self._received_print_jobs = None # type: Optional[List[CloudClusterPrintJob]] + # A set of the user's job IDs that have finished + self._finished_jobs = set() # type: Set[str] + ## Gets the host name of this device @property def host_name(self) -> str: @@ -144,16 +146,16 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): self.setShortDescription(T.PRINT_VIA_CLOUD_BUTTON) self.setDescription(T.PRINT_VIA_CLOUD_TOOLTIP) self.setConnectionText(T.CONNECTED_VIA_CLOUD) - + ## Called when Cura requests an output device to receive a (G-code) file. def requestWrite(self, nodes: List[SceneNode], file_name: Optional[str] = None, limit_mime_types: bool = False, file_handler: Optional[FileHandler] = None, **kwargs: str) -> None: - + # Show an error message if we're already sending a job. if self._sending_job: self._onUploadError(T.BLOCKED_UPLOADING) return - + # Indicate we have started sending a job. self._sending_job = True self.writeStarted.emit(self) @@ -165,9 +167,8 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): mesh_bytes = mesh_format.getBytes(nodes) - # TODO: Remove extension from the file name, since we are using content types now request = CloudJobUploadRequest( - job_name = file_name, ## + "." + mesh_format.file_extension, + job_name = file_name, file_size = len(mesh_bytes), content_type = mesh_format.mime_type, ) @@ -261,18 +262,15 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): ## Handles the event of a change in a print job state def _onPrintJobStateChanged(self) -> None: - username = self._account.userName - finished_jobs = [job for job in self._print_jobs if job.state == "wait_cleanup"] - - newly_finished_jobs = [job for job in finished_jobs if job not in self._finished_jobs and job.owner == username] - for job in newly_finished_jobs: - if job.assignedPrinter: - job_completed_text = T.JOB_COMPLETED_PRINTER.format(printer_name=job.assignedPrinter.name, - job_name=job.name) - else: - job_completed_text = T.JOB_COMPLETED_NO_PRINTER.format(job_name=job.name) - job_completed_message = Message(text=job_completed_text, title = T.JOB_COMPLETED_TITLE) - job_completed_message.show() + user_name = self._getUserName() + for job in self._print_jobs: + if job.state == "wait_cleanup" and job.key not in self._finished_jobs and job.owner == user_name: + self._finished_jobs.add(job.key) + Message( + title = T.JOB_COMPLETED_TITLE, + text = (T.JOB_COMPLETED_PRINTER.format(printer_name=job.assignedPrinter.name, job_name=job.name) + if job.assignedPrinter else T.JOB_COMPLETED_NO_PRINTER.format(job_name=job.name)), + ).show() # Ensure UI gets updated self.printJobsChanged.emit() @@ -326,13 +324,12 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): def _onUploadError(self, message: str = None) -> None: self._resetUploadProgress() if message: - message = Message( + Message( text = message, title = T.ERROR, lifetime = 10, dismissable = True - ) - message.show() + ).show() self._sending_job = False # the upload has finished so we're not sending a job anymore self.writeError.emit() @@ -341,18 +338,17 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): def _onUploadSuccess(self, response: CloudPrintResponse) -> None: Logger.log("i", "The cluster will be printing this print job with the ID %s", response.cluster_job_id) self._resetUploadProgress() - message = Message( + Message( text = T.UPLOAD_SUCCESS_TEXT, title = T.UPLOAD_SUCCESS_TITLE, lifetime = 5, dismissable = True, - ) - message.show() + ).show() self._sending_job = False # the upload has finished so we're not sending a job anymore self.writeFinished.emit() ## Gets the remote printers. - @pyqtProperty("QVariantList", notify = _clusterPrintersChanged) + @pyqtProperty("QVariantList", notify=_clusterPrintersChanged) def printers(self) -> List[PrinterOutputModel]: return self._printers @@ -374,7 +370,7 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice): ## Get remote print jobs. @pyqtProperty("QVariantList", notify = printJobsChanged) - def printJobs(self)-> List[UM3PrintJobOutputModel]: + def printJobs(self) -> List[UM3PrintJobOutputModel]: return self._print_jobs ## Get remote print jobs that are still in the print queue. diff --git a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py index bc410a4a1d..f06bbb305e 100644 --- a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py +++ b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py @@ -7,6 +7,7 @@ from PyQt5.QtCore import QTimer from UM import i18nCatalog from UM.Logger import Logger from UM.Message import Message +from cura.API import Account from cura.CuraApplication import CuraApplication from cura.Settings.GlobalStack import GlobalStack from .CloudApiClient import CloudApiClient @@ -40,7 +41,7 @@ class CloudOutputDeviceManager: application = CuraApplication.getInstance() self._output_device_manager = application.getOutputDeviceManager() - self._account = application.getCuraAPI().account + self._account = application.getCuraAPI().account # type: Account self._account.loginStateChanged.connect(self._onLoginStateChanged) self._api = CloudApiClient(self._account, self._onApiError) @@ -54,11 +55,11 @@ class CloudOutputDeviceManager: self._update_timer.timeout.connect(self._getRemoteClusters) # Make sure the timer is started in case we missed the loginChanged signal - self._onLoginStateChanged() + self._onLoginStateChanged(self._account.isLoggedIn) # Called when the uses logs in or out - def _onLoginStateChanged(self) -> None: - if self._account.isLoggedIn: + def _onLoginStateChanged(self, is_logged_in: bool) -> None: + if is_logged_in: if not self._update_timer.isActive(): self._update_timer.start() else: