ClusterUM3OutputDevice.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, cast, Optional, Set, Tuple, Union
  4. from UM.FileHandler.FileHandler import FileHandler
  5. from UM.FileHandler.FileWriter import FileWriter # To choose based on the output file mode (text vs. binary).
  6. from UM.FileHandler.WriteFileJob import WriteFileJob # To call the file writer asynchronously.
  7. from UM.Logger import Logger
  8. from UM.Settings.ContainerRegistry import ContainerRegistry
  9. from UM.i18n import i18nCatalog
  10. from UM.Message import Message
  11. from UM.Qt.Duration import Duration, DurationFormat
  12. from UM.OutputDevice import OutputDeviceError # To show that something went wrong when writing.
  13. from UM.Scene.SceneNode import SceneNode # For typing.
  14. from UM.Version import Version # To check against firmware versions for support.
  15. from cura.CuraApplication import CuraApplication
  16. from cura.PrinterOutput.ConfigurationModel import ConfigurationModel
  17. from cura.PrinterOutput.ExtruderConfigurationModel import ExtruderConfigurationModel
  18. from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
  19. from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
  20. from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel
  21. from cura.PrinterOutput.MaterialOutputModel import MaterialOutputModel
  22. from cura.PrinterOutput.NetworkCamera import NetworkCamera
  23. from .ClusterUM3PrinterOutputController import ClusterUM3PrinterOutputController
  24. from .SendMaterialJob import SendMaterialJob
  25. from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
  26. from PyQt5.QtGui import QDesktopServices, QImage
  27. from PyQt5.QtCore import pyqtSlot, QUrl, pyqtSignal, pyqtProperty, QObject
  28. from time import time
  29. from datetime import datetime
  30. from typing import Optional, Dict, List
  31. import io # To create the correct buffers for sending data to the printer.
  32. import json
  33. import os
  34. i18n_catalog = i18nCatalog("cura")
  35. class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice):
  36. printJobsChanged = pyqtSignal()
  37. activePrinterChanged = pyqtSignal()
  38. # This is a bit of a hack, as the notify can only use signals that are defined by the class that they are in.
  39. # Inheritance doesn't seem to work. Tying them together does work, but i'm open for better suggestions.
  40. clusterPrintersChanged = pyqtSignal()
  41. def __init__(self, device_id, address, properties, parent = None) -> None:
  42. super().__init__(device_id = device_id, address = address, properties=properties, parent = parent)
  43. self._api_prefix = "/cluster-api/v1/"
  44. self._number_of_extruders = 2
  45. self._dummy_lambdas = ("", {}, io.BytesIO()) #type: Tuple[str, Dict, Union[io.StringIO, io.BytesIO]]
  46. self._print_jobs = [] # type: List[PrintJobOutputModel]
  47. self._monitor_view_qml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ClusterMonitorItem.qml")
  48. self._control_view_qml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ClusterControlItem.qml")
  49. # See comments about this hack with the clusterPrintersChanged signal
  50. self.printersChanged.connect(self.clusterPrintersChanged)
  51. self._accepts_commands = True # type: bool
  52. # Cluster does not have authentication, so default to authenticated
  53. self._authentication_state = AuthState.Authenticated
  54. self._error_message = None # type: Optional[Message]
  55. self._write_job_progress_message = None # type: Optional[Message]
  56. self._progress_message = None # type: Optional[Message]
  57. self._active_printer = None # type: Optional[PrinterOutputModel]
  58. self._printer_selection_dialog = None # type: QObject
  59. self.setPriority(3) # Make sure the output device gets selected above local file output
  60. self.setName(self._id)
  61. self.setShortDescription(i18n_catalog.i18nc("@action:button Preceded by 'Ready to'.", "Print over network"))
  62. self.setDescription(i18n_catalog.i18nc("@properties:tooltip", "Print over network"))
  63. self.setConnectionText(i18n_catalog.i18nc("@info:status", "Connected over the network"))
  64. self._printer_uuid_to_unique_name_mapping = {} # type: Dict[str, str]
  65. self._finished_jobs = [] # type: List[PrintJobOutputModel]
  66. self._cluster_size = int(properties.get(b"cluster_size", 0)) # type: int
  67. self._latest_reply_handler = None # type: Optional[QNetworkReply]
  68. self._sending_job = None
  69. def requestWrite(self, nodes: List[SceneNode], file_name: Optional[str] = None, limit_mimetypes: bool = False, file_handler: Optional[FileHandler] = None, **kwargs: str) -> None:
  70. self.writeStarted.emit(self)
  71. self.sendMaterialProfiles()
  72. # Formats supported by this application (file types that we can actually write).
  73. if file_handler:
  74. file_formats = file_handler.getSupportedFileTypesWrite()
  75. else:
  76. file_formats = CuraApplication.getInstance().getMeshFileHandler().getSupportedFileTypesWrite()
  77. global_stack = CuraApplication.getInstance().getGlobalContainerStack()
  78. # Create a list from the supported file formats string.
  79. if not global_stack:
  80. Logger.log("e", "Missing global stack!")
  81. return
  82. machine_file_formats = global_stack.getMetaDataEntry("file_formats").split(";")
  83. machine_file_formats = [file_type.strip() for file_type in machine_file_formats]
  84. # Exception for UM3 firmware version >=4.4: UFP is now supported and should be the preferred file format.
  85. if "application/x-ufp" not in machine_file_formats and Version(self.firmwareVersion) >= Version("4.4"):
  86. machine_file_formats = ["application/x-ufp"] + machine_file_formats
  87. # Take the intersection between file_formats and machine_file_formats.
  88. format_by_mimetype = {format["mime_type"]: format for format in file_formats}
  89. file_formats = [format_by_mimetype[mimetype] for mimetype in machine_file_formats] #Keep them ordered according to the preference in machine_file_formats.
  90. if len(file_formats) == 0:
  91. Logger.log("e", "There are no file formats available to write with!")
  92. raise OutputDeviceError.WriteRequestFailedError(i18n_catalog.i18nc("@info:status", "There are no file formats available to write with!"))
  93. preferred_format = file_formats[0]
  94. #J ust take the first file format available.
  95. if file_handler is not None:
  96. writer = file_handler.getWriterByMimeType(cast(str, preferred_format["mime_type"]))
  97. else:
  98. writer = CuraApplication.getInstance().getMeshFileHandler().getWriterByMimeType(cast(str, preferred_format["mime_type"]))
  99. if not writer:
  100. Logger.log("e", "Unexpected error when trying to get the FileWriter")
  101. return
  102. # This function pauses with the yield, waiting on instructions on which printer it needs to print with.
  103. if not writer:
  104. Logger.log("e", "Missing file or mesh writer!")
  105. return
  106. self._sending_job = self._sendPrintJob(writer, preferred_format, nodes)
  107. self._sending_job.send(None) # Start the generator.
  108. if len(self._printers) > 1: # We need to ask the user.
  109. self._spawnPrinterSelectionDialog()
  110. is_job_sent = True
  111. else: # Just immediately continue.
  112. self._sending_job.send("") # No specifically selected printer.
  113. is_job_sent = self._sending_job.send(None)
  114. def _spawnPrinterSelectionDialog(self):
  115. if self._printer_selection_dialog is None:
  116. path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "PrintWindow.qml")
  117. self._printer_selection_dialog = CuraApplication.getInstance().createQmlComponent(path, {"OutputDevice": self})
  118. if self._printer_selection_dialog is not None:
  119. self._printer_selection_dialog.show()
  120. @pyqtProperty(int, constant=True)
  121. def clusterSize(self) -> int:
  122. return self._cluster_size
  123. ## Allows the user to choose a printer to print with from the printer
  124. # selection dialogue.
  125. # \param target_printer The name of the printer to target.
  126. @pyqtSlot(str)
  127. def selectPrinter(self, target_printer: str = "") -> None:
  128. self._sending_job.send(target_printer)
  129. @pyqtSlot()
  130. def cancelPrintSelection(self) -> None:
  131. self._sending_gcode = False
  132. ## Greenlet to send a job to the printer over the network.
  133. #
  134. # This greenlet gets called asynchronously in requestWrite. It is a
  135. # greenlet in order to optionally wait for selectPrinter() to select a
  136. # printer.
  137. # The greenlet yields exactly three times: First time None,
  138. # \param writer The file writer to use to create the data.
  139. # \param preferred_format A dictionary containing some information about
  140. # what format to write to. This is necessary to create the correct buffer
  141. # types and file extension and such.
  142. def _sendPrintJob(self, writer: FileWriter, preferred_format: Dict, nodes: List[SceneNode]):
  143. Logger.log("i", "Sending print job to printer.")
  144. if self._sending_gcode:
  145. self._error_message = Message(
  146. i18n_catalog.i18nc("@info:status",
  147. "Sending new jobs (temporarily) blocked, still sending the previous print job."))
  148. self._error_message.show()
  149. yield #Wait on the user to select a target printer.
  150. yield #Wait for the write job to be finished.
  151. yield False #Return whether this was a success or not.
  152. yield #Prevent StopIteration.
  153. self._sending_gcode = True
  154. target_printer = yield #Potentially wait on the user to select a target printer.
  155. # Using buffering greatly reduces the write time for many lines of gcode
  156. stream = io.BytesIO() # type: Union[io.BytesIO, io.StringIO]# Binary mode.
  157. if preferred_format["mode"] == FileWriter.OutputMode.TextMode:
  158. stream = io.StringIO()
  159. job = WriteFileJob(writer, stream, nodes, preferred_format["mode"])
  160. self._write_job_progress_message = Message(i18n_catalog.i18nc("@info:status", "Sending data to printer"), lifetime = 0, dismissable = False, progress = -1,
  161. title = i18n_catalog.i18nc("@info:title", "Sending Data"), use_inactivity_timer = False)
  162. self._write_job_progress_message.show()
  163. self._dummy_lambdas = (target_printer, preferred_format, stream)
  164. job.finished.connect(self._sendPrintJobWaitOnWriteJobFinished)
  165. job.start()
  166. yield True # Return that we had success!
  167. yield # To prevent having to catch the StopIteration exception.
  168. def _sendPrintJobWaitOnWriteJobFinished(self, job: WriteFileJob) -> None:
  169. if self._write_job_progress_message:
  170. self._write_job_progress_message.hide()
  171. self._progress_message = Message(i18n_catalog.i18nc("@info:status", "Sending data to printer"), lifetime = 0, dismissable = False, progress = -1,
  172. title = i18n_catalog.i18nc("@info:title", "Sending Data"))
  173. self._progress_message.addAction("Abort", i18n_catalog.i18nc("@action:button", "Cancel"), icon = None, description = "")
  174. self._progress_message.actionTriggered.connect(self._progressMessageActionTriggered)
  175. self._progress_message.show()
  176. parts = []
  177. target_printer, preferred_format, stream = self._dummy_lambdas
  178. # If a specific printer was selected, it should be printed with that machine.
  179. if target_printer:
  180. target_printer = self._printer_uuid_to_unique_name_mapping[target_printer]
  181. parts.append(self._createFormPart("name=require_printer_name", bytes(target_printer, "utf-8"), "text/plain"))
  182. # Add user name to the print_job
  183. parts.append(self._createFormPart("name=owner", bytes(self._getUserName(), "utf-8"), "text/plain"))
  184. file_name = CuraApplication.getInstance().getPrintInformation().jobName + "." + preferred_format["extension"]
  185. output = stream.getvalue() # Either str or bytes depending on the output mode.
  186. if isinstance(stream, io.StringIO):
  187. output = cast(str, output).encode("utf-8")
  188. output = cast(bytes, output)
  189. parts.append(self._createFormPart("name=\"file\"; filename=\"%s\"" % file_name, output))
  190. self._latest_reply_handler = self.postFormWithParts("print_jobs/", parts, on_finished = self._onPostPrintJobFinished, on_progress = self._onUploadPrintJobProgress)
  191. @pyqtProperty(QObject, notify = activePrinterChanged)
  192. def activePrinter(self) -> Optional[PrinterOutputModel]:
  193. return self._active_printer
  194. @pyqtSlot(QObject)
  195. def setActivePrinter(self, printer: Optional[PrinterOutputModel]) -> None:
  196. if self._active_printer != printer:
  197. if self._active_printer and self._active_printer.camera:
  198. self._active_printer.camera.stop()
  199. self._active_printer = printer
  200. self.activePrinterChanged.emit()
  201. def _onPostPrintJobFinished(self, reply: QNetworkReply) -> None:
  202. if self._progress_message:
  203. self._progress_message.hide()
  204. self._compressing_gcode = False
  205. self._sending_gcode = False
  206. def _onUploadPrintJobProgress(self, bytes_sent: int, bytes_total: int) -> None:
  207. if bytes_total > 0:
  208. new_progress = bytes_sent / bytes_total * 100
  209. # Treat upload progress as response. Uploading can take more than 10 seconds, so if we don't, we can get
  210. # timeout responses if this happens.
  211. self._last_response_time = time()
  212. if self._progress_message and new_progress > self._progress_message.getProgress():
  213. self._progress_message.show() # Ensure that the message is visible.
  214. self._progress_message.setProgress(bytes_sent / bytes_total * 100)
  215. # If successfully sent:
  216. if bytes_sent == bytes_total:
  217. # Show a confirmation to the user so they know the job was sucessful and provide the option to switch to
  218. # the monitor tab.
  219. self._success_message = Message(
  220. i18n_catalog.i18nc("@info:status", "Print job was successfully sent to the printer."),
  221. lifetime=5, dismissable=True,
  222. title=i18n_catalog.i18nc("@info:title", "Data Sent"))
  223. self._success_message.addAction("View", i18n_catalog.i18nc("@action:button", "View in Monitor"), icon=None,
  224. description="")
  225. self._success_message.actionTriggered.connect(self._successMessageActionTriggered)
  226. self._success_message.show()
  227. else:
  228. if self._progress_message is not None:
  229. self._progress_message.setProgress(0)
  230. self._progress_message.hide()
  231. def _progressMessageActionTriggered(self, message_id: Optional[str] = None, action_id: Optional[str] = None) -> None:
  232. if action_id == "Abort":
  233. Logger.log("d", "User aborted sending print to remote.")
  234. if self._progress_message is not None:
  235. self._progress_message.hide()
  236. self._compressing_gcode = False
  237. self._sending_gcode = False
  238. CuraApplication.getInstance().getController().setActiveStage("PrepareStage")
  239. # After compressing the sliced model Cura sends data to printer, to stop receiving updates from the request
  240. # the "reply" should be disconnected
  241. if self._latest_reply_handler:
  242. self._latest_reply_handler.disconnect()
  243. self._latest_reply_handler = None
  244. def _successMessageActionTriggered(self, message_id: Optional[str] = None, action_id: Optional[str] = None) -> None:
  245. if action_id == "View":
  246. CuraApplication.getInstance().getController().setActiveStage("MonitorStage")
  247. @pyqtSlot()
  248. def openPrintJobControlPanel(self) -> None:
  249. Logger.log("d", "Opening print job control panel...")
  250. QDesktopServices.openUrl(QUrl("http://" + self._address + "/print_jobs"))
  251. @pyqtSlot()
  252. def openPrinterControlPanel(self) -> None:
  253. Logger.log("d", "Opening printer control panel...")
  254. QDesktopServices.openUrl(QUrl("http://" + self._address + "/printers"))
  255. @pyqtProperty("QVariantList", notify = printJobsChanged)
  256. def printJobs(self)-> List[PrintJobOutputModel]:
  257. return self._print_jobs
  258. @pyqtProperty("QVariantList", notify = printJobsChanged)
  259. def queuedPrintJobs(self) -> List[PrintJobOutputModel]:
  260. return [print_job for print_job in self._print_jobs if print_job.state == "queued"]
  261. @pyqtProperty("QVariantList", notify = printJobsChanged)
  262. def activePrintJobs(self) -> List[PrintJobOutputModel]:
  263. return [print_job for print_job in self._print_jobs if print_job.assignedPrinter is not None and print_job.state != "queued"]
  264. @pyqtProperty("QVariantList", notify = clusterPrintersChanged)
  265. def connectedPrintersTypeCount(self) -> List[Dict[str, str]]:
  266. printer_count = {} # type: Dict[str, int]
  267. for printer in self._printers:
  268. if printer.type in printer_count:
  269. printer_count[printer.type] += 1
  270. else:
  271. printer_count[printer.type] = 1
  272. result = []
  273. for machine_type in printer_count:
  274. result.append({"machine_type": machine_type, "count": str(printer_count[machine_type])})
  275. return result
  276. @pyqtProperty("QVariantList", notify=clusterPrintersChanged)
  277. def printers(self):
  278. return self._printers
  279. @pyqtSlot(int, result = str)
  280. def formatDuration(self, seconds: int) -> str:
  281. return Duration(seconds).getDisplayString(DurationFormat.Format.Short)
  282. @pyqtSlot(int, result = str)
  283. def getTimeCompleted(self, time_remaining: int) -> str:
  284. current_time = time()
  285. datetime_completed = datetime.fromtimestamp(current_time + time_remaining)
  286. return "{hour:02d}:{minute:02d}".format(hour=datetime_completed.hour, minute=datetime_completed.minute)
  287. @pyqtSlot(int, result = str)
  288. def getDateCompleted(self, time_remaining: int) -> str:
  289. current_time = time()
  290. datetime_completed = datetime.fromtimestamp(current_time + time_remaining)
  291. return (datetime_completed.strftime("%a %b ") + "{day}".format(day=datetime_completed.day)).upper()
  292. @pyqtSlot(str)
  293. def sendJobToTop(self, print_job_uuid: str) -> None:
  294. # This function is part of the output device (and not of the printjob output model) as this type of operation
  295. # is a modification of the cluster queue and not of the actual job.
  296. data = "{\"to_position\": 0}"
  297. self.put("print_jobs/{uuid}/move_to_position".format(uuid = print_job_uuid), data, on_finished=None)
  298. @pyqtSlot(str)
  299. def deleteJobFromQueue(self, print_job_uuid: str) -> None:
  300. # This function is part of the output device (and not of the printjob output model) as this type of operation
  301. # is a modification of the cluster queue and not of the actual job.
  302. self.delete("print_jobs/{uuid}".format(uuid = print_job_uuid), on_finished=None)
  303. def _printJobStateChanged(self) -> None:
  304. username = self._getUserName()
  305. if username is None:
  306. return # We only want to show notifications if username is set.
  307. finished_jobs = [job for job in self._print_jobs if job.state == "wait_cleanup"]
  308. newly_finished_jobs = [job for job in finished_jobs if job not in self._finished_jobs and job.owner == username]
  309. for job in newly_finished_jobs:
  310. if job.assignedPrinter:
  311. job_completed_text = i18n_catalog.i18nc("@info:status", "Printer '{printer_name}' has finished printing '{job_name}'.".format(printer_name=job.assignedPrinter.name, job_name = job.name))
  312. else:
  313. job_completed_text = i18n_catalog.i18nc("@info:status", "The print job '{job_name}' was finished.".format(job_name = job.name))
  314. job_completed_message = Message(text=job_completed_text, title = i18n_catalog.i18nc("@info:status", "Print finished"))
  315. job_completed_message.show()
  316. # Ensure UI gets updated
  317. self.printJobsChanged.emit()
  318. # Keep a list of all completed jobs so we know if something changed next time.
  319. self._finished_jobs = finished_jobs
  320. ## Called when the connection to the cluster changes.
  321. def connect(self) -> None:
  322. super().connect()
  323. self.sendMaterialProfiles()
  324. def _onGetPreviewImageFinished(self, reply: QNetworkReply) -> None:
  325. reply_url = reply.url().toString()
  326. uuid = reply_url[reply_url.find("print_jobs/")+len("print_jobs/"):reply_url.rfind("/preview_image")]
  327. print_job = findByKey(self._print_jobs, uuid)
  328. if print_job:
  329. image = QImage()
  330. image.loadFromData(reply.readAll())
  331. print_job.updatePreviewImage(image)
  332. def _update(self) -> None:
  333. super()._update()
  334. self.get("printers/", on_finished = self._onGetPrintersDataFinished)
  335. self.get("print_jobs/", on_finished = self._onGetPrintJobsFinished)
  336. for print_job in self._print_jobs:
  337. if print_job.getPreviewImage() is None:
  338. self.get("print_jobs/{uuid}/preview_image".format(uuid=print_job.key), on_finished=self._onGetPreviewImageFinished)
  339. def _onGetPrintJobsFinished(self, reply: QNetworkReply) -> None:
  340. if not checkValidGetReply(reply):
  341. return
  342. result = loadJsonFromReply(reply)
  343. if result is None:
  344. return
  345. print_jobs_seen = []
  346. job_list_changed = False
  347. for idx, print_job_data in enumerate(result):
  348. print_job = findByKey(self._print_jobs, print_job_data["uuid"])
  349. if print_job is None:
  350. print_job = self._createPrintJobModel(print_job_data)
  351. job_list_changed = True
  352. elif not job_list_changed:
  353. # Check if the order of the jobs has changed since the last check
  354. if self._print_jobs.index(print_job) != idx:
  355. job_list_changed = True
  356. self._updatePrintJob(print_job, print_job_data)
  357. if print_job.state != "queued": # Print job should be assigned to a printer.
  358. if print_job.state in ["failed", "finished", "aborted", "none"]:
  359. # Print job was already completed, so don't attach it to a printer.
  360. printer = None
  361. else:
  362. printer = self._getPrinterByKey(print_job_data["printer_uuid"])
  363. else: # The job can "reserve" a printer if some changes are required.
  364. printer = self._getPrinterByKey(print_job_data["assigned_to"])
  365. if printer:
  366. printer.updateActivePrintJob(print_job)
  367. print_jobs_seen.append(print_job)
  368. # Check what jobs need to be removed.
  369. removed_jobs = [print_job for print_job in self._print_jobs if print_job not in print_jobs_seen]
  370. for removed_job in removed_jobs:
  371. job_list_changed = job_list_changed or self._removeJob(removed_job)
  372. if job_list_changed:
  373. # Override the old list with the new list (either because jobs were removed / added or order changed)
  374. self._print_jobs = print_jobs_seen
  375. self.printJobsChanged.emit() # Do a single emit for all print job changes.
  376. def _onGetPrintersDataFinished(self, reply: QNetworkReply) -> None:
  377. if not checkValidGetReply(reply):
  378. return
  379. result = loadJsonFromReply(reply)
  380. if result is None:
  381. return
  382. printer_list_changed = False
  383. printers_seen = []
  384. for printer_data in result:
  385. printer = findByKey(self._printers, printer_data["uuid"])
  386. if printer is None:
  387. printer = self._createPrinterModel(printer_data)
  388. printer_list_changed = True
  389. printers_seen.append(printer)
  390. self._updatePrinter(printer, printer_data)
  391. removed_printers = [printer for printer in self._printers if printer not in printers_seen]
  392. for printer in removed_printers:
  393. self._removePrinter(printer)
  394. if removed_printers or printer_list_changed:
  395. self.printersChanged.emit()
  396. def _createPrinterModel(self, data: Dict[str, Any]) -> PrinterOutputModel:
  397. printer = PrinterOutputModel(output_controller = ClusterUM3PrinterOutputController(self),
  398. number_of_extruders = self._number_of_extruders)
  399. printer.setCamera(NetworkCamera("http://" + data["ip_address"] + ":8080/?action=stream"))
  400. self._printers.append(printer)
  401. return printer
  402. def _createPrintJobModel(self, data: Dict[str, Any]) -> PrintJobOutputModel:
  403. print_job = PrintJobOutputModel(output_controller=ClusterUM3PrinterOutputController(self),
  404. key=data["uuid"], name= data["name"])
  405. configuration = ConfigurationModel()
  406. extruders = [ExtruderConfigurationModel(position = idx) for idx in range(0, self._number_of_extruders)]
  407. for index in range(0, self._number_of_extruders):
  408. try:
  409. extruder_data = data["configuration"][index]
  410. except IndexError:
  411. continue
  412. extruder = extruders[int(data["configuration"][index]["extruder_index"])]
  413. extruder.setHotendID(extruder_data.get("print_core_id", ""))
  414. extruder.setMaterial(self._createMaterialOutputModel(extruder_data.get("material", {})))
  415. configuration.setExtruderConfigurations(extruders)
  416. print_job.updateConfiguration(configuration)
  417. print_job.stateChanged.connect(self._printJobStateChanged)
  418. return print_job
  419. def _updatePrintJob(self, print_job: PrintJobOutputModel, data: Dict[str, Any]) -> None:
  420. print_job.updateTimeTotal(data["time_total"])
  421. print_job.updateTimeElapsed(data["time_elapsed"])
  422. print_job.updateState(data["status"])
  423. print_job.updateOwner(data["owner"])
  424. def _createMaterialOutputModel(self, material_data) -> MaterialOutputModel:
  425. containers = ContainerRegistry.getInstance().findInstanceContainers(type="material", GUID=material_data["guid"])
  426. if containers:
  427. color = containers[0].getMetaDataEntry("color_code")
  428. brand = containers[0].getMetaDataEntry("brand")
  429. material_type = containers[0].getMetaDataEntry("material")
  430. name = containers[0].getName()
  431. else:
  432. Logger.log("w",
  433. "Unable to find material with guid {guid}. Using data as provided by cluster".format(
  434. guid=material_data["guid"]))
  435. color = material_data["color"]
  436. brand = material_data["brand"]
  437. material_type = material_data["material"]
  438. name = "Empty" if material_data["material"] == "empty" else "Unknown"
  439. return MaterialOutputModel(guid=material_data["guid"], type=material_type,
  440. brand=brand, color=color, name=name)
  441. def _updatePrinter(self, printer: PrinterOutputModel, data: Dict[str, Any]) -> None:
  442. # For some unknown reason the cluster wants UUID for everything, except for sending a job directly to a printer.
  443. # Then we suddenly need the unique name. So in order to not have to mess up all the other code, we save a mapping.
  444. self._printer_uuid_to_unique_name_mapping[data["uuid"]] = data["unique_name"]
  445. definitions = ContainerRegistry.getInstance().findDefinitionContainers(name = data["machine_variant"])
  446. if not definitions:
  447. Logger.log("w", "Unable to find definition for machine variant %s", data["machine_variant"])
  448. return
  449. machine_definition = definitions[0]
  450. printer.updateName(data["friendly_name"])
  451. printer.updateKey(data["uuid"])
  452. printer.updateType(data["machine_variant"])
  453. # Do not store the build plate information that comes from connect if the current printer has not build plate information
  454. if "build_plate" in data and machine_definition.getMetaDataEntry("has_variant_buildplates", False):
  455. printer.updateBuildplateName(data["build_plate"]["type"])
  456. if not data["enabled"]:
  457. printer.updateState("disabled")
  458. else:
  459. printer.updateState(data["status"])
  460. for index in range(0, self._number_of_extruders):
  461. extruder = printer.extruders[index]
  462. try:
  463. extruder_data = data["configuration"][index]
  464. except IndexError:
  465. break
  466. extruder.updateHotendID(extruder_data.get("print_core_id", ""))
  467. material_data = extruder_data["material"]
  468. if extruder.activeMaterial is None or extruder.activeMaterial.guid != material_data["guid"]:
  469. material = self._createMaterialOutputModel(material_data)
  470. extruder.updateActiveMaterial(material)
  471. def _removeJob(self, job: PrintJobOutputModel) -> bool:
  472. if job not in self._print_jobs:
  473. return False
  474. if job.assignedPrinter:
  475. job.assignedPrinter.updateActivePrintJob(None)
  476. job.stateChanged.disconnect(self._printJobStateChanged)
  477. self._print_jobs.remove(job)
  478. return True
  479. def _removePrinter(self, printer: PrinterOutputModel) -> None:
  480. self._printers.remove(printer)
  481. if self._active_printer == printer:
  482. self._active_printer = None
  483. self.activePrinterChanged.emit()
  484. ## Sync the material profiles in Cura with the printer.
  485. #
  486. # This gets called when connecting to a printer as well as when sending a
  487. # print.
  488. def sendMaterialProfiles(self) -> None:
  489. job = SendMaterialJob(device = self)
  490. job.run()
  491. def loadJsonFromReply(reply: QNetworkReply) -> Optional[List[Dict[str, Any]]]:
  492. try:
  493. result = json.loads(bytes(reply.readAll()).decode("utf-8"))
  494. except json.decoder.JSONDecodeError:
  495. Logger.logException("w", "Unable to decode JSON from reply.")
  496. return None
  497. return result
  498. def checkValidGetReply(reply: QNetworkReply) -> bool:
  499. status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
  500. if status_code != 200:
  501. Logger.log("w", "Got status code {status_code} while trying to get data".format(status_code=status_code))
  502. return False
  503. return True
  504. def findByKey(lst: List[Union[PrintJobOutputModel, PrinterOutputModel]], key: str) -> Optional[PrintJobOutputModel]:
  505. for item in lst:
  506. if item.key == key:
  507. return item
  508. return None