NetworkedPrinterOutputDevice.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.FileHandler.FileHandler import FileHandler #For typing.
  4. from UM.Logger import Logger
  5. from UM.Scene.SceneNode import SceneNode #For typing.
  6. from cura.API import Account
  7. from cura.CuraApplication import CuraApplication
  8. from cura.PrinterOutput.PrinterOutputDevice import PrinterOutputDevice, ConnectionState, ConnectionType
  9. from PyQt6.QtNetwork import QHttpMultiPart, QHttpPart, QNetworkRequest, QNetworkAccessManager, QNetworkReply, QAuthenticator
  10. from PyQt6.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl, QCoreApplication
  11. from time import time
  12. from typing import Callable, Dict, List, Optional, Union
  13. from enum import IntEnum
  14. import os # To get the username
  15. import gzip
  16. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  17. class AuthState(IntEnum):
  18. NotAuthenticated = 1
  19. AuthenticationRequested = 2
  20. Authenticated = 3
  21. AuthenticationDenied = 4
  22. AuthenticationReceived = 5
  23. class NetworkedPrinterOutputDevice(PrinterOutputDevice):
  24. authenticationStateChanged = pyqtSignal()
  25. def __init__(self, device_id, address: str, properties: Dict[bytes, bytes], connection_type: ConnectionType = ConnectionType.NetworkConnection, parent: QObject = None) -> None:
  26. super().__init__(device_id = device_id, connection_type = connection_type, parent = parent)
  27. self._manager = None # type: Optional[QNetworkAccessManager]
  28. self._timeout_time = 10 # After how many seconds of no response should a timeout occur?
  29. self._last_response_time = None # type: Optional[float]
  30. self._last_request_time = None # type: Optional[float]
  31. self._api_prefix = ""
  32. self._address = address
  33. self._properties = properties
  34. self._user_agent = "%s/%s " % (CuraApplication.getInstance().getApplicationName(),
  35. CuraApplication.getInstance().getVersion())
  36. self._onFinishedCallbacks = {} # type: Dict[str, Callable[[QNetworkReply], None]]
  37. self._authentication_state = AuthState.NotAuthenticated
  38. # QHttpMultiPart objects need to be kept alive and not garbage collected during the
  39. # HTTP which uses them. We hold references to these QHttpMultiPart objects here.
  40. self._kept_alive_multiparts = {} # type: Dict[QNetworkReply, QHttpMultiPart]
  41. self._sending_gcode = False
  42. self._compressing_gcode = False
  43. self._gcode = [] # type: List[str]
  44. self._connection_state_before_timeout = None # type: Optional[ConnectionState]
  45. def requestWrite(self, nodes: List["SceneNode"], file_name: Optional[str] = None, limit_mimetypes: bool = False,
  46. file_handler: Optional["FileHandler"] = None, filter_by_machine: bool = False, **kwargs) -> None:
  47. raise NotImplementedError("requestWrite needs to be implemented")
  48. def setAuthenticationState(self, authentication_state: AuthState) -> None:
  49. if self._authentication_state != authentication_state:
  50. self._authentication_state = authentication_state
  51. self.authenticationStateChanged.emit()
  52. @pyqtProperty(int, notify = authenticationStateChanged)
  53. def authenticationState(self) -> AuthState:
  54. return self._authentication_state
  55. def _compressDataAndNotifyQt(self, data_to_append: str) -> bytes:
  56. compressed_data = gzip.compress(data_to_append.encode("utf-8"))
  57. self._progress_message.setProgress(-1) # Tickle the message so that it's clear that it's still being used.
  58. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  59. # Pretend that this is a response, as zipping might take a bit of time.
  60. # If we don't do this, the device might trigger a timeout.
  61. self._last_response_time = time()
  62. return compressed_data
  63. def _compressGCode(self) -> Optional[bytes]:
  64. self._compressing_gcode = True
  65. max_chars_per_line = int(1024 * 1024 / 4) # 1/4 MB per line.
  66. """Mash the data into single string"""
  67. file_data_bytes_list = []
  68. batched_lines = []
  69. batched_lines_count = 0
  70. for line in self._gcode:
  71. if not self._compressing_gcode:
  72. self._progress_message.hide()
  73. # Stop trying to zip / send as abort was called.
  74. return None
  75. # if the gcode was read from a gcode file, self._gcode will be a list of all lines in that file.
  76. # Compressing line by line in this case is extremely slow, so we need to batch them.
  77. batched_lines.append(line)
  78. batched_lines_count += len(line)
  79. if batched_lines_count >= max_chars_per_line:
  80. file_data_bytes_list.append(self._compressDataAndNotifyQt("".join(batched_lines)))
  81. batched_lines = []
  82. batched_lines_count = 0
  83. # Don't miss the last batch (If any)
  84. if len(batched_lines) != 0:
  85. file_data_bytes_list.append(self._compressDataAndNotifyQt("".join(batched_lines)))
  86. self._compressing_gcode = False
  87. return b"".join(file_data_bytes_list)
  88. def _update(self) -> None:
  89. """
  90. Update the connection state of this device.
  91. This is called on regular intervals.
  92. """
  93. if self._last_response_time:
  94. time_since_last_response = time() - self._last_response_time
  95. else:
  96. time_since_last_response = 0
  97. if self._last_request_time:
  98. time_since_last_request = time() - self._last_request_time
  99. else:
  100. time_since_last_request = float("inf") # An irrelevantly large number of seconds
  101. if time_since_last_response > self._timeout_time >= time_since_last_request:
  102. # Go (or stay) into timeout.
  103. if self._connection_state_before_timeout is None:
  104. self._connection_state_before_timeout = self.connectionState
  105. self.setConnectionState(ConnectionState.Closed)
  106. elif self.connectionState == ConnectionState.Closed:
  107. # Go out of timeout.
  108. if self._connection_state_before_timeout is not None: # sanity check, but it should never be None here
  109. self.setConnectionState(self._connection_state_before_timeout)
  110. self._connection_state_before_timeout = None
  111. def _createEmptyRequest(self, target: str, content_type: Optional[str] = "application/json") -> QNetworkRequest:
  112. url = QUrl("http://" + self._address + self._api_prefix + target)
  113. request = QNetworkRequest(url)
  114. if content_type is not None:
  115. request.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, content_type)
  116. request.setHeader(QNetworkRequest.KnownHeaders.UserAgentHeader, self._user_agent)
  117. return request
  118. def createFormPart(self, content_header: str, data: bytes, content_type: Optional[str] = None) -> QHttpPart:
  119. """This method was only available privately before, but it was actually called from SendMaterialJob.py.
  120. We now have a public equivalent as well. We did not remove the private one as plugins might be using that.
  121. """
  122. return self._createFormPart(content_header, data, content_type)
  123. def _createFormPart(self, content_header: str, data: bytes, content_type: Optional[str] = None) -> QHttpPart:
  124. part = QHttpPart()
  125. if not content_header.startswith("form-data;"):
  126. content_header = "form-data; " + content_header
  127. part.setHeader(QNetworkRequest.KnownHeaders.ContentDispositionHeader, content_header)
  128. if content_type is not None:
  129. part.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, content_type)
  130. part.setBody(data)
  131. return part
  132. def _getUserName(self) -> str:
  133. """Convenience function to get the username, either from the cloud or from the OS."""
  134. # check first if we are logged in with the Ultimaker Account
  135. account = CuraApplication.getInstance().getCuraAPI().account # type: Account
  136. if account and account.isLoggedIn:
  137. return account.userName
  138. # Otherwise get the username from the US
  139. # The code below was copied from the getpass module, as we try to use as little dependencies as possible.
  140. for name in ("LOGNAME", "USER", "LNAME", "USERNAME"):
  141. user = os.environ.get(name)
  142. if user:
  143. return user
  144. return "Unknown User" # Couldn't find out username.
  145. def _clearCachedMultiPart(self, reply: QNetworkReply) -> None:
  146. if reply in self._kept_alive_multiparts:
  147. del self._kept_alive_multiparts[reply]
  148. def _validateManager(self) -> None:
  149. if self._manager is None:
  150. self._createNetworkManager()
  151. assert (self._manager is not None)
  152. def put(self, url: str, data: Union[str, bytes], content_type: Optional[str] = "application/json",
  153. on_finished: Optional[Callable[[QNetworkReply], None]] = None,
  154. on_progress: Optional[Callable[[int, int], None]] = None) -> None:
  155. """Sends a put request to the given path.
  156. :param url: The path after the API prefix.
  157. :param data: The data to be sent in the body
  158. :param content_type: The content type of the body data.
  159. :param on_finished: The function to call when the response is received.
  160. :param on_progress: The function to call when the progress changes. Parameters are bytes_sent / bytes_total.
  161. """
  162. self._validateManager()
  163. request = self._createEmptyRequest(url, content_type = content_type)
  164. self._last_request_time = time()
  165. if not self._manager:
  166. Logger.log("e", "No network manager was created to execute the PUT call with.")
  167. return
  168. body = data if isinstance(data, bytes) else data.encode() # type: bytes
  169. reply = self._manager.put(request, body)
  170. self._registerOnFinishedCallback(reply, on_finished)
  171. if on_progress is not None:
  172. reply.uploadProgress.connect(on_progress)
  173. def delete(self, url: str, on_finished: Optional[Callable[[QNetworkReply], None]]) -> None:
  174. """Sends a delete request to the given path.
  175. :param url: The path after the API prefix.
  176. :param on_finished: The function to be call when the response is received.
  177. """
  178. self._validateManager()
  179. request = self._createEmptyRequest(url)
  180. self._last_request_time = time()
  181. if not self._manager:
  182. Logger.log("e", "No network manager was created to execute the DELETE call with.")
  183. return
  184. reply = self._manager.deleteResource(request)
  185. self._registerOnFinishedCallback(reply, on_finished)
  186. def get(self, url: str, on_finished: Optional[Callable[[QNetworkReply], None]]) -> None:
  187. """Sends a get request to the given path.
  188. :param url: The path after the API prefix.
  189. :param on_finished: The function to be call when the response is received.
  190. """
  191. self._validateManager()
  192. request = self._createEmptyRequest(url)
  193. self._last_request_time = time()
  194. if not self._manager:
  195. Logger.log("e", "No network manager was created to execute the GET call with.")
  196. return
  197. reply = self._manager.get(request)
  198. self._registerOnFinishedCallback(reply, on_finished)
  199. def post(self, url: str, data: Union[str, bytes],
  200. on_finished: Optional[Callable[[QNetworkReply], None]],
  201. on_progress: Optional[Callable[[int, int], None]] = None) -> None:
  202. """Sends a post request to the given path.
  203. :param url: The path after the API prefix.
  204. :param data: The data to be sent in the body
  205. :param on_finished: The function to call when the response is received.
  206. :param on_progress: The function to call when the progress changes. Parameters are bytes_sent / bytes_total.
  207. """
  208. self._validateManager()
  209. request = self._createEmptyRequest(url)
  210. self._last_request_time = time()
  211. if not self._manager:
  212. Logger.log("e", "Could not find manager.")
  213. return
  214. body = data if isinstance(data, bytes) else data.encode() # type: bytes
  215. reply = self._manager.post(request, body)
  216. if on_progress is not None:
  217. reply.uploadProgress.connect(on_progress)
  218. self._registerOnFinishedCallback(reply, on_finished)
  219. def postFormWithParts(self, target: str, parts: List[QHttpPart],
  220. on_finished: Optional[Callable[[QNetworkReply], None]],
  221. on_progress: Optional[Callable[[int, int], None]] = None) -> QNetworkReply:
  222. self._validateManager()
  223. request = self._createEmptyRequest(target, content_type=None)
  224. multi_post_part = QHttpMultiPart(QHttpMultiPart.ContentType.FormDataType)
  225. for part in parts:
  226. multi_post_part.append(part)
  227. self._last_request_time = time()
  228. if self._manager is not None:
  229. reply = self._manager.post(request, multi_post_part)
  230. self._kept_alive_multiparts[reply] = multi_post_part
  231. if on_progress is not None:
  232. reply.uploadProgress.connect(on_progress)
  233. self._registerOnFinishedCallback(reply, on_finished)
  234. return reply
  235. else:
  236. Logger.log("e", "Could not find manager.")
  237. def postForm(self, target: str, header_data: str, body_data: bytes, on_finished: Optional[Callable[[QNetworkReply], None]], on_progress: Callable = None) -> None:
  238. post_part = QHttpPart()
  239. post_part.setHeader(QNetworkRequest.KnownHeaders.ContentDispositionHeader, header_data)
  240. post_part.setBody(body_data)
  241. self.postFormWithParts(target, [post_part], on_finished, on_progress)
  242. def _onAuthenticationRequired(self, reply: QNetworkReply, authenticator: QAuthenticator) -> None:
  243. Logger.log("w", "Request to {url} required authentication, which was not implemented".format(url = reply.url().toString()))
  244. def _createNetworkManager(self) -> None:
  245. Logger.log("d", "Creating network manager")
  246. if self._manager:
  247. self._manager.finished.disconnect(self._handleOnFinished)
  248. self._manager.authenticationRequired.disconnect(self._onAuthenticationRequired)
  249. self._manager = QNetworkAccessManager()
  250. self._manager.finished.connect(self._handleOnFinished)
  251. self._manager.authenticationRequired.connect(self._onAuthenticationRequired)
  252. if self._properties.get(b"temporary", b"false") != b"true":
  253. self._checkCorrectGroupName(self.getId(), self.name)
  254. def _registerOnFinishedCallback(self, reply: QNetworkReply, on_finished: Optional[Callable[[QNetworkReply], None]]) -> None:
  255. if on_finished is not None:
  256. self._onFinishedCallbacks[reply.url().toString() + str(reply.operation())] = on_finished
  257. def _checkCorrectGroupName(self, device_id: str, group_name: str) -> None:
  258. """This method checks if the name of the group stored in the definition container is correct.
  259. After updating from 3.2 to 3.3 some group names may be temporary. If there is a mismatch in the name of the group
  260. then all the container stacks are updated, both the current and the hidden ones.
  261. """
  262. global_container_stack = CuraApplication.getInstance().getGlobalContainerStack()
  263. active_machine_network_name = CuraApplication.getInstance().getMachineManager().activeMachineNetworkKey()
  264. if global_container_stack and device_id == active_machine_network_name:
  265. # Check if the group_name is correct. If not, update all the containers connected to the same printer
  266. if CuraApplication.getInstance().getMachineManager().activeMachineNetworkGroupName != group_name:
  267. metadata_filter = {"um_network_key": active_machine_network_name}
  268. containers = CuraContainerRegistry.getInstance().findContainerStacks(type="machine",
  269. **metadata_filter)
  270. for container in containers:
  271. container.setMetaDataEntry("group_name", group_name)
  272. def _handleOnFinished(self, reply: QNetworkReply) -> None:
  273. # Due to garbage collection, we need to cache certain bits of post operations.
  274. # As we don't want to keep them around forever, delete them if we get a reply.
  275. if reply.operation() == QNetworkAccessManager.Operation.PostOperation:
  276. self._clearCachedMultiPart(reply)
  277. if reply.attribute(QNetworkRequest.Attribute.HttpStatusCodeAttribute) is None:
  278. # No status code means it never even reached remote.
  279. return
  280. self._last_response_time = time()
  281. if self.connectionState == ConnectionState.Connecting:
  282. self.setConnectionState(ConnectionState.Connected)
  283. callback_key = reply.url().toString() + str(reply.operation())
  284. try:
  285. if callback_key in self._onFinishedCallbacks:
  286. self._onFinishedCallbacks[callback_key](reply)
  287. except Exception:
  288. Logger.logException("w", "something went wrong with callback")
  289. @pyqtSlot(str, result=str)
  290. def getProperty(self, key: str) -> str:
  291. bytes_key = key.encode("utf-8")
  292. if bytes_key in self._properties:
  293. return self._properties.get(bytes_key, b"").decode("utf-8")
  294. else:
  295. return ""
  296. def getProperties(self):
  297. return self._properties
  298. @pyqtProperty(str, constant = True)
  299. def key(self) -> str:
  300. """Get the unique key of this machine
  301. :return: key String containing the key of the machine.
  302. """
  303. return self._id
  304. @pyqtProperty(str, constant = True)
  305. def address(self) -> str:
  306. """The IP address of the printer."""
  307. return self._properties.get(b"address", b"").decode("utf-8")
  308. @pyqtProperty(str, constant = True)
  309. def name(self) -> str:
  310. """Name of the printer (as returned from the ZeroConf properties)"""
  311. return self._properties.get(b"name", b"").decode("utf-8")
  312. @pyqtProperty(str, constant = True)
  313. def firmwareVersion(self) -> str:
  314. """Firmware version (as returned from the ZeroConf properties)"""
  315. return self._properties.get(b"firmware_version", b"").decode("utf-8")
  316. @pyqtProperty(str, constant = True)
  317. def printerType(self) -> str:
  318. return NetworkedPrinterOutputDevice.applyPrinterTypeMapping(self._properties.get(b"printer_type", b"Unknown").decode("utf-8"))
  319. @staticmethod
  320. def applyPrinterTypeMapping(printer_type):
  321. _PRINTER_TYPE_NAME = {
  322. "fire_e": "ultimaker_method",
  323. "lava_f": "ultimaker_methodx",
  324. "magma_10": "ultimaker_methodxl",
  325. "sketch": "ultimaker_sketch"
  326. }
  327. if printer_type in _PRINTER_TYPE_NAME:
  328. return _PRINTER_TYPE_NAME[printer_type]
  329. return printer_type
  330. @pyqtProperty(str, constant = True)
  331. def ipAddress(self) -> str:
  332. """IP address of this printer"""
  333. return self._address