CloudOutputDevice.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from time import time
  5. from typing import Dict, List, Optional, Set, cast
  6. from PyQt5.QtCore import QObject, QUrl, pyqtProperty, pyqtSignal, pyqtSlot
  7. from UM import i18nCatalog
  8. from UM.Backend.Backend import BackendState
  9. from UM.FileHandler.FileHandler import FileHandler
  10. from UM.Logger import Logger
  11. from UM.Message import Message
  12. from UM.Qt.Duration import Duration, DurationFormat
  13. from UM.Scene.SceneNode import SceneNode
  14. from cura.CuraApplication import CuraApplication
  15. from cura.PrinterOutput.NetworkedPrinterOutputDevice import AuthState, NetworkedPrinterOutputDevice
  16. from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
  17. from cura.PrinterOutputDevice import ConnectionType
  18. from plugins.UM3NetworkPrinting.src.Cloud.CloudOutputController import CloudOutputController
  19. from ..MeshFormatHandler import MeshFormatHandler
  20. from ..UM3PrintJobOutputModel import UM3PrintJobOutputModel
  21. from .CloudProgressMessage import CloudProgressMessage
  22. from .CloudApiClient import CloudApiClient
  23. from .Models.CloudClusterResponse import CloudClusterResponse
  24. from .Models.CloudClusterStatus import CloudClusterStatus
  25. from .Models.CloudPrintJobUploadRequest import CloudPrintJobUploadRequest
  26. from .Models.CloudPrintResponse import CloudPrintResponse
  27. from .Models.CloudPrintJobResponse import CloudPrintJobResponse
  28. from .Models.CloudClusterPrinterStatus import CloudClusterPrinterStatus
  29. from .Models.CloudClusterPrintJobStatus import CloudClusterPrintJobStatus
  30. from .Utils import findChanges, formatDateCompleted, formatTimeCompleted
  31. ## Class that contains all the translations for this module.
  32. class T:
  33. # The translation catalog for this device.
  34. _I18N_CATALOG = i18nCatalog("cura")
  35. PRINT_VIA_CLOUD_BUTTON = _I18N_CATALOG.i18nc("@action:button", "Print via Cloud")
  36. PRINT_VIA_CLOUD_TOOLTIP = _I18N_CATALOG.i18nc("@properties:tooltip", "Print via Cloud")
  37. CONNECTED_VIA_CLOUD = _I18N_CATALOG.i18nc("@info:status", "Connected via Cloud")
  38. BLOCKED_UPLOADING = _I18N_CATALOG.i18nc("@info:status", "Sending new jobs (temporarily) blocked, still sending "
  39. "the previous print job.")
  40. COULD_NOT_EXPORT = _I18N_CATALOG.i18nc("@info:status", "Could not export print job.")
  41. ERROR = _I18N_CATALOG.i18nc("@info:title", "Error")
  42. UPLOAD_ERROR = _I18N_CATALOG.i18nc("@info:text", "Could not upload the data to the printer.")
  43. UPLOAD_SUCCESS_TITLE = _I18N_CATALOG.i18nc("@info:title", "Data Sent")
  44. UPLOAD_SUCCESS_TEXT = _I18N_CATALOG.i18nc("@info:status", "Print job was successfully sent to the printer.")
  45. JOB_COMPLETED_TITLE = _I18N_CATALOG.i18nc("@info:status", "Print finished")
  46. JOB_COMPLETED_PRINTER = _I18N_CATALOG.i18nc("@info:status",
  47. "Printer '{printer_name}' has finished printing '{job_name}'.")
  48. JOB_COMPLETED_NO_PRINTER = _I18N_CATALOG.i18nc("@info:status", "The print job '{job_name}' was finished.")
  49. ## The cloud output device is a network output device that works remotely but has limited functionality.
  50. # Currently it only supports viewing the printer and print job status and adding a new job to the queue.
  51. # As such, those methods have been implemented here.
  52. # Note that this device represents a single remote cluster, not a list of multiple clusters.
  53. class CloudOutputDevice(NetworkedPrinterOutputDevice):
  54. # The interval with which the remote clusters are checked
  55. CHECK_CLUSTER_INTERVAL = 50.0 # seconds
  56. # Signal triggered when the print jobs in the queue were changed.
  57. printJobsChanged = pyqtSignal()
  58. # Signal triggered when the selected printer in the UI should be changed.
  59. activePrinterChanged = pyqtSignal()
  60. # Notify can only use signals that are defined by the class that they are in, not inherited ones.
  61. # Therefore we create a private signal used to trigger the printersChanged signal.
  62. _clusterPrintersChanged = pyqtSignal()
  63. ## Creates a new cloud output device
  64. # \param api_client: The client that will run the API calls
  65. # \param cluster: The device response received from the cloud API.
  66. # \param parent: The optional parent of this output device.
  67. def __init__(self, api_client: CloudApiClient, cluster: CloudClusterResponse, parent: QObject = None) -> None:
  68. super().__init__(device_id = cluster.cluster_id, address = "",
  69. connection_type = ConnectionType.CloudConnection, properties = {}, parent = parent)
  70. self._api = api_client
  71. self._cluster = cluster
  72. self._setInterfaceElements()
  73. self._account = api_client.account
  74. # We use the Cura Connect monitor tab to get most functionality right away.
  75. self._monitor_view_qml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  76. "../../resources/qml/MonitorStage.qml")
  77. # Trigger the printersChanged signal when the private signal is triggered.
  78. self.printersChanged.connect(self._clusterPrintersChanged)
  79. # We keep track of which printer is visible in the monitor page.
  80. self._active_printer = None # type: Optional[PrinterOutputModel]
  81. # Properties to populate later on with received cloud data.
  82. self._print_jobs = [] # type: List[UM3PrintJobOutputModel]
  83. self._number_of_extruders = 2 # All networked printers are dual-extrusion Ultimaker machines.
  84. # We only allow a single upload at a time.
  85. self._progress = CloudProgressMessage()
  86. # Keep server string of the last generated time to avoid updating models more than once for the same response
  87. self._received_printers = None # type: Optional[List[CloudClusterPrinterStatus]]
  88. self._received_print_jobs = None # type: Optional[List[CloudClusterPrintJobStatus]]
  89. # A set of the user's job IDs that have finished
  90. self._finished_jobs = set() # type: Set[str]
  91. # Reference to the uploaded print job / mesh
  92. self._mesh = None # type: Optional[bytes]
  93. self._uploaded_print_job = None # type: Optional[CloudPrintJobResponse]
  94. ## Connects this device.
  95. def connect(self) -> None:
  96. super().connect()
  97. Logger.log("i", "Connected to cluster %s", self.key)
  98. CuraApplication.getInstance().getBackend().backendStateChange.connect(self._onBackendStateChange)
  99. ## Disconnects the device
  100. def disconnect(self) -> None:
  101. super().disconnect()
  102. Logger.log("i", "Disconnected to cluster %s", self.key)
  103. CuraApplication.getInstance().getBackend().backendStateChange.disconnect(self._onBackendStateChange)
  104. ## Resets the print job that was uploaded to force a new upload, runs whenever the user re-slices.
  105. def _onBackendStateChange(self, _: BackendState) -> None:
  106. self._mesh = None
  107. self._uploaded_print_job = None
  108. ## Gets the cluster response from which this device was created.
  109. @property
  110. def clusterData(self) -> CloudClusterResponse:
  111. return self._cluster
  112. ## Updates the cluster data from the cloud.
  113. @clusterData.setter
  114. def clusterData(self, value: CloudClusterResponse) -> None:
  115. self._cluster = value
  116. ## Checks whether the given network key is found in the cloud's host name
  117. def matchesNetworkKey(self, network_key: str) -> bool:
  118. # A network key looks like "ultimakersystem-aabbccdd0011._ultimaker._tcp.local."
  119. # the host name should then be "ultimakersystem-aabbccdd0011"
  120. return network_key.startswith(self.clusterData.host_name)
  121. ## Set all the interface elements and texts for this output device.
  122. def _setInterfaceElements(self) -> None:
  123. self.setPriority(2) # make sure we end up below the local networking and above 'save to file'
  124. self.setName(self._id)
  125. self.setShortDescription(T.PRINT_VIA_CLOUD_BUTTON)
  126. self.setDescription(T.PRINT_VIA_CLOUD_TOOLTIP)
  127. self.setConnectionText(T.CONNECTED_VIA_CLOUD)
  128. ## Called when Cura requests an output device to receive a (G-code) file.
  129. def requestWrite(self, nodes: List[SceneNode], file_name: Optional[str] = None, limit_mimetypes: bool = False,
  130. file_handler: Optional[FileHandler] = None, **kwargs: str) -> None:
  131. # Show an error message if we're already sending a job.
  132. if self._progress.visible:
  133. message = Message(text = T.BLOCKED_UPLOADING, title = T.ERROR, lifetime = 10)
  134. message.show()
  135. return
  136. if self._uploaded_print_job:
  137. # the mesh didn't change, let's not upload it again
  138. self._api.requestPrint(self.key, self._uploaded_print_job.job_id, self._onPrintRequested)
  139. return
  140. # Indicate we have started sending a job.
  141. self.writeStarted.emit(self)
  142. mesh_format = MeshFormatHandler(file_handler, self.firmwareVersion)
  143. if not mesh_format.is_valid:
  144. Logger.log("e", "Missing file or mesh writer!")
  145. return self._onUploadError(T.COULD_NOT_EXPORT)
  146. mesh = mesh_format.getBytes(nodes)
  147. self._mesh = mesh
  148. request = CloudPrintJobUploadRequest(
  149. job_name = file_name or mesh_format.file_extension,
  150. file_size = len(mesh),
  151. content_type = mesh_format.mime_type,
  152. )
  153. self._api.requestUpload(request, self._onPrintJobCreated)
  154. ## Called when the network data should be updated.
  155. def _update(self) -> None:
  156. super()._update()
  157. if self._last_request_time and time() - self._last_request_time < self.CHECK_CLUSTER_INTERVAL:
  158. return # avoid calling the cloud too often
  159. Logger.log("d", "Updating: %s - %s >= %s", time(), self._last_request_time, self.CHECK_CLUSTER_INTERVAL)
  160. if self._account.isLoggedIn:
  161. self.setAuthenticationState(AuthState.Authenticated)
  162. self._last_request_time = time()
  163. self._api.getClusterStatus(self.key, self._onStatusCallFinished)
  164. else:
  165. self.setAuthenticationState(AuthState.NotAuthenticated)
  166. ## Method called when HTTP request to status endpoint is finished.
  167. # Contains both printers and print jobs statuses in a single response.
  168. def _onStatusCallFinished(self, status: CloudClusterStatus) -> None:
  169. # Update all data from the cluster.
  170. self._last_response_time = time()
  171. if self._received_printers != status.printers:
  172. self._received_printers = status.printers
  173. self._updatePrinters(status.printers)
  174. if status.print_jobs != self._received_print_jobs:
  175. self._received_print_jobs = status.print_jobs
  176. self._updatePrintJobs(status.print_jobs)
  177. ## Updates the local list of printers with the list received from the cloud.
  178. # \param jobs: The printers received from the cloud.
  179. def _updatePrinters(self, printers: List[CloudClusterPrinterStatus]) -> None:
  180. previous = {p.key: p for p in self._printers} # type: Dict[str, PrinterOutputModel]
  181. received = {p.uuid: p for p in printers} # type: Dict[str, CloudClusterPrinterStatus]
  182. removed_printers, added_printers, updated_printers = findChanges(previous, received)
  183. for removed_printer in removed_printers:
  184. if self._active_printer == removed_printer:
  185. self.setActivePrinter(None)
  186. self._printers.remove(removed_printer)
  187. for added_printer in added_printers:
  188. self._printers.append(added_printer.createOutputModel(CloudOutputController(self)))
  189. for model, printer in updated_printers:
  190. printer.updateOutputModel(model)
  191. # Always have an active printer
  192. if self._printers and not self._active_printer:
  193. self.setActivePrinter(self._printers[0])
  194. self.printersChanged.emit() # TODO: Make this more efficient by not updating every request
  195. ## Updates the local list of print jobs with the list received from the cloud.
  196. # \param jobs: The print jobs received from the cloud.
  197. def _updatePrintJobs(self, jobs: List[CloudClusterPrintJobStatus]) -> None:
  198. received = {j.uuid: j for j in jobs} # type: Dict[str, CloudClusterPrintJobStatus]
  199. previous = {j.key: j for j in self._print_jobs} # type: Dict[str, UM3PrintJobOutputModel]
  200. removed_jobs, added_jobs, updated_jobs = findChanges(previous, received)
  201. for removed_job in removed_jobs:
  202. if removed_job.assignedPrinter:
  203. removed_job.assignedPrinter.updateActivePrintJob(None)
  204. removed_job.stateChanged.disconnect(self._onPrintJobStateChanged)
  205. self._print_jobs.remove(removed_job)
  206. for added_job in added_jobs:
  207. self._addPrintJob(added_job)
  208. for model, job in updated_jobs:
  209. job.updateOutputModel(model)
  210. if job.printer_uuid:
  211. self._updateAssignedPrinter(model, job.printer_uuid)
  212. # We only have to update when jobs are added or removed
  213. # updated jobs push their changes via their output model
  214. if added_jobs or removed_jobs or updated_jobs:
  215. self.printJobsChanged.emit()
  216. ## Registers a new print job received via the cloud API.
  217. # \param job: The print job received.
  218. def _addPrintJob(self, job: CloudClusterPrintJobStatus) -> None:
  219. model = job.createOutputModel(CloudOutputController(self))
  220. model.stateChanged.connect(self._onPrintJobStateChanged)
  221. if job.printer_uuid:
  222. self._updateAssignedPrinter(model, job.printer_uuid)
  223. self._print_jobs.append(model)
  224. ## Handles the event of a change in a print job state
  225. def _onPrintJobStateChanged(self) -> None:
  226. user_name = self._getUserName()
  227. for job in self._print_jobs:
  228. if job.state == "wait_cleanup" and job.key not in self._finished_jobs and job.owner == user_name:
  229. self._finished_jobs.add(job.key)
  230. Message(
  231. title = T.JOB_COMPLETED_TITLE,
  232. text = (T.JOB_COMPLETED_PRINTER.format(printer_name=job.assignedPrinter.name, job_name=job.name)
  233. if job.assignedPrinter else T.JOB_COMPLETED_NO_PRINTER.format(job_name=job.name)),
  234. ).show()
  235. # Ensure UI gets updated
  236. self.printJobsChanged.emit()
  237. ## Updates the printer assignment for the given print job model.
  238. def _updateAssignedPrinter(self, model: UM3PrintJobOutputModel, printer_uuid: str) -> None:
  239. printer = next((p for p in self._printers if printer_uuid == p.key), None)
  240. if not printer:
  241. return Logger.log("w", "Missing printer %s for job %s in %s", model.assignedPrinter, model.key,
  242. [p.key for p in self._printers])
  243. printer.updateActivePrintJob(model)
  244. model.updateAssignedPrinter(printer)
  245. ## Uploads the mesh when the print job was registered with the cloud API.
  246. # \param job_response: The response received from the cloud API.
  247. def _onPrintJobCreated(self, job_response: CloudPrintJobResponse) -> None:
  248. self._progress.show()
  249. self._uploaded_print_job = job_response
  250. mesh = cast(bytes, self._mesh)
  251. self._api.uploadMesh(job_response, mesh, self._onPrintJobUploaded, self._progress.update, self._onUploadError)
  252. ## Requests the print to be sent to the printer when we finished uploading the mesh.
  253. def _onPrintJobUploaded(self) -> None:
  254. self._progress.update(100)
  255. print_job = cast(CloudPrintJobResponse, self._uploaded_print_job)
  256. self._api.requestPrint(self.key, print_job.job_id, self._onPrintRequested)
  257. ## Displays the given message if uploading the mesh has failed
  258. # \param message: The message to display.
  259. def _onUploadError(self, message = None) -> None:
  260. self._progress.hide()
  261. self._uploaded_print_job = None
  262. Message(
  263. text = message or T.UPLOAD_ERROR,
  264. title = T.ERROR,
  265. lifetime = 10
  266. ).show()
  267. self.writeError.emit()
  268. ## Shows a message when the upload has succeeded
  269. # \param response: The response from the cloud API.
  270. def _onPrintRequested(self, response: CloudPrintResponse) -> None:
  271. Logger.log("d", "The cluster will be printing this print job with the ID %s", response.cluster_job_id)
  272. self._progress.hide()
  273. Message(
  274. text = T.UPLOAD_SUCCESS_TEXT,
  275. title = T.UPLOAD_SUCCESS_TITLE,
  276. lifetime = 5
  277. ).show()
  278. self.writeFinished.emit()
  279. ## Gets the remote printers.
  280. @pyqtProperty("QVariantList", notify=_clusterPrintersChanged)
  281. def printers(self) -> List[PrinterOutputModel]:
  282. return self._printers
  283. ## Get the active printer in the UI (monitor page).
  284. @pyqtProperty(QObject, notify = activePrinterChanged)
  285. def activePrinter(self) -> Optional[PrinterOutputModel]:
  286. return self._active_printer
  287. ## Set the active printer in the UI (monitor page).
  288. @pyqtSlot(QObject)
  289. def setActivePrinter(self, printer: Optional[PrinterOutputModel] = None) -> None:
  290. if printer != self._active_printer:
  291. self._active_printer = printer
  292. self.activePrinterChanged.emit()
  293. @pyqtProperty(int, notify = _clusterPrintersChanged)
  294. def clusterSize(self) -> int:
  295. return len(self._printers)
  296. ## Get remote print jobs.
  297. @pyqtProperty("QVariantList", notify = printJobsChanged)
  298. def printJobs(self) -> List[UM3PrintJobOutputModel]:
  299. return self._print_jobs
  300. ## Get remote print jobs that are still in the print queue.
  301. @pyqtProperty("QVariantList", notify = printJobsChanged)
  302. def queuedPrintJobs(self) -> List[UM3PrintJobOutputModel]:
  303. return [print_job for print_job in self._print_jobs
  304. if print_job.state == "queued" or print_job.state == "error"]
  305. ## Get remote print jobs that are assigned to a printer.
  306. @pyqtProperty("QVariantList", notify = printJobsChanged)
  307. def activePrintJobs(self) -> List[UM3PrintJobOutputModel]:
  308. return [print_job for print_job in self._print_jobs if
  309. print_job.assignedPrinter is not None and print_job.state != "queued"]
  310. @pyqtSlot(int, result = str)
  311. def formatDuration(self, seconds: int) -> str:
  312. return Duration(seconds).getDisplayString(DurationFormat.Format.Short)
  313. @pyqtSlot(int, result = str)
  314. def getTimeCompleted(self, time_remaining: int) -> str:
  315. return formatTimeCompleted(time_remaining)
  316. @pyqtSlot(int, result = str)
  317. def getDateCompleted(self, time_remaining: int) -> str:
  318. return formatDateCompleted(time_remaining)
  319. ## TODO: The following methods are required by the monitor page QML, but are not actually available using cloud.
  320. # TODO: We fake the methods here to not break the monitor page.
  321. @pyqtProperty(QUrl, notify = _clusterPrintersChanged)
  322. def activeCameraUrl(self) -> "QUrl":
  323. return QUrl()
  324. @pyqtSlot(QUrl)
  325. def setActiveCameraUrl(self, camera_url: "QUrl") -> None:
  326. pass
  327. @pyqtProperty(bool, notify = printJobsChanged)
  328. def receivedPrintJobs(self) -> bool:
  329. return bool(self._print_jobs)
  330. @pyqtSlot()
  331. def openPrintJobControlPanel(self) -> None:
  332. pass
  333. @pyqtSlot()
  334. def openPrinterControlPanel(self) -> None:
  335. pass
  336. @pyqtSlot(str)
  337. def sendJobToTop(self, print_job_uuid: str) -> None:
  338. pass
  339. @pyqtSlot(str)
  340. def deleteJobFromQueue(self, print_job_uuid: str) -> None:
  341. pass
  342. @pyqtSlot(str)
  343. def forceSendJob(self, print_job_uuid: str) -> None:
  344. pass
  345. @pyqtProperty("QVariantList", notify = _clusterPrintersChanged)
  346. def connectedPrintersTypeCount(self) -> List[Dict[str, str]]:
  347. return []