From 894c69685a740641b62ab7431b338cd985e11009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijn=20De=C3=A9?= Date: Tue, 4 Dec 2018 13:06:27 +0100 Subject: [PATCH 1/2] Periodically update the remote clusters and printjobs --- .../src/Cloud/CloudOutputDeviceManager.py | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py index 06beb8bce4..e50cd6540c 100644 --- a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py +++ b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py @@ -8,6 +8,7 @@ from typing import Dict, Optional from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply from UM.Logger import Logger +from UM.Signal import Signal from cura.CuraApplication import CuraApplication from cura.NetworkClient import NetworkClient @@ -42,10 +43,14 @@ class CloudOutputDeviceManager(NetworkClient): # When switching machines we check if we have to activate a remote cluster. application.globalContainerStackChanged.connect(self._connectToActiveMachine) - # TODO: fix this # Periodically check all remote clusters for the authenticated user. - # self._update_clusters_thread = Thread(target=self._updateClusters, daemon=True) - # self._update_clusters_thread.start() + # This is done by emitting to _on_cluster_received by _update_clusters_thread + # The thread is only started after the user is authenticated, otherwise the api call results in + # an authentication error + self._on_cluster_received = Signal() + self._on_cluster_received.connect(self._getRemoteClusters) + self._update_clusters_thread = Thread(target=self._updateClusters, daemon=True) + ## Override _createEmptyRequest to add the needed authentication header for talking to the Ultimaker Cloud API. def _createEmptyRequest(self, path: str, content_type: Optional[str] = "application/json") -> QNetworkRequest: @@ -59,13 +64,25 @@ class CloudOutputDeviceManager(NetworkClient): ## Update the clusters def _updateClusters(self) -> None: while True: - self._getRemoteClusters() - sleep(self.CHECK_CLUSTER_INTERVAL) - + + # Stop if the application is shutting down + if CuraApplication.getInstance().isShuttingDown(): + return + + self._on_cluster_received.emit() + sleep(5) + ## Gets all remote clusters from the API. def _getRemoteClusters(self) -> None: Logger.log("i", "Retrieving remote clusters") - self.get("/clusters", on_finished = self._onGetRemoteClustersFinished) + if self._account.isLoggedIn: + self.get("/clusters", on_finished = self._onGetRemoteClustersFinished) + + # Only start the polling thread after the user is authenticated + # The first call to _getRemoteClusters comes from self._account.loginStateChanged + if not self._update_clusters_thread.is_alive(): + self._update_clusters_thread.start() + ## Callback for when the request for getting the clusters. is finished. def _onGetRemoteClustersFinished(self, reply: QNetworkReply) -> None: From 02963eb9bfdeac576786889e5568203ee11ab764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijn=20De=C3=A9?= Date: Tue, 4 Dec 2018 13:19:12 +0100 Subject: [PATCH 2/2] Use a timer for the periodic update of the remote clusters and printjobs --- .../src/Cloud/CloudOutputDeviceManager.py | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py index 37198fd7c6..85e734f7a3 100644 --- a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py +++ b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py @@ -2,7 +2,7 @@ # Cura is released under the terms of the LGPLv3 or higher. import json from time import sleep -from threading import Thread +from threading import Timer from typing import Dict, Optional from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply @@ -43,13 +43,8 @@ class CloudOutputDeviceManager(NetworkClient): # When switching machines we check if we have to activate a remote cluster. application.globalContainerStackChanged.connect(self._connectToActiveMachine) - # Periodically check all remote clusters for the authenticated user. - # This is done by emitting to _on_cluster_received by _update_clusters_thread - # The thread is only started after the user is authenticated, otherwise the api call results in - # an authentication error self._on_cluster_received = Signal() self._on_cluster_received.connect(self._getRemoteClusters) - self._update_clusters_thread = Thread(target=self._updateClusters, daemon=True) ## Override _createEmptyRequest to add the needed authentication header for talking to the Ultimaker Cloud API. @@ -61,17 +56,6 @@ class CloudOutputDeviceManager(NetworkClient): request.setRawHeader(b"Authorization", "Bearer {}".format(self._account.accessToken).encode()) return request - ## Update the clusters - def _updateClusters(self) -> None: - while True: - - # Stop if the application is shutting down - if CuraApplication.getInstance().isShuttingDown(): - return - - self._on_cluster_received.emit() - sleep(5) - ## Gets all remote clusters from the API. def _getRemoteClusters(self) -> None: Logger.log("i", "Retrieving remote clusters") @@ -80,8 +64,8 @@ class CloudOutputDeviceManager(NetworkClient): # Only start the polling thread after the user is authenticated # The first call to _getRemoteClusters comes from self._account.loginStateChanged - if not self._update_clusters_thread.is_alive(): - self._update_clusters_thread.start() + timer = Timer(5.0, self._on_cluster_received.emit) + timer.start() ## Callback for when the request for getting the clusters. is finished.