mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-10 08:17:49 -06:00
Refactor the connection status to be informed by the autosync
The autosync, which happens every 30 seconds, will now also inform the connection status when the get request succeeds or fails, which is an indicator of whether the internet is reachable. CURA-7492
This commit is contained in:
parent
e5a7ad2eca
commit
ddf312eca5
2 changed files with 17 additions and 40 deletions
|
@ -1,22 +1,16 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from PyQt5.QtCore import QObject, pyqtSignal, QTimer, pyqtProperty
|
from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty
|
||||||
from PyQt5.QtNetwork import QNetworkReply
|
|
||||||
|
|
||||||
from UM.TaskManagement.HttpRequestManager import HttpRequestManager
|
from UM.Logger import Logger
|
||||||
from cura.UltimakerCloud import UltimakerCloudConstants
|
|
||||||
|
|
||||||
|
|
||||||
class ConnectionStatus(QObject):
|
class ConnectionStatus(QObject):
|
||||||
"""Status info for some web services"""
|
"""Status info for some web services"""
|
||||||
|
|
||||||
UPDATE_INTERVAL = 10.0 # seconds
|
|
||||||
ULTIMAKER_CLOUD_STATUS_URL = UltimakerCloudConstants.CuraCloudAPIRoot + "/connect/v1/"
|
|
||||||
|
|
||||||
__instance = None # type: Optional[ConnectionStatus]
|
__instance = None # type: Optional[ConnectionStatus]
|
||||||
|
|
||||||
internetReachableChanged = pyqtSignal()
|
internetReachableChanged = pyqtSignal()
|
||||||
umCloudReachableChanged = pyqtSignal()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def getInstance(cls, *args, **kwargs) -> "ConnectionStatus":
|
def getInstance(cls, *args, **kwargs) -> "ConnectionStatus":
|
||||||
|
@ -27,38 +21,16 @@ class ConnectionStatus(QObject):
|
||||||
def __init__(self, parent: Optional["QObject"] = None):
|
def __init__(self, parent: Optional["QObject"] = None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
|
||||||
self._http = HttpRequestManager.getInstance()
|
self._is_internet_reachable: bool = True
|
||||||
self._statuses = {
|
|
||||||
self.ULTIMAKER_CLOUD_STATUS_URL: True,
|
|
||||||
"http://example.com": True
|
|
||||||
}
|
|
||||||
|
|
||||||
# Create a timer for automatic updates
|
|
||||||
self._update_timer = QTimer()
|
|
||||||
self._update_timer.setInterval(int(self.UPDATE_INTERVAL * 1000))
|
|
||||||
# The timer is restarted automatically
|
|
||||||
self._update_timer.setSingleShot(False)
|
|
||||||
self._update_timer.timeout.connect(self._update)
|
|
||||||
self._update_timer.start()
|
|
||||||
|
|
||||||
@pyqtProperty(bool, notify = internetReachableChanged)
|
@pyqtProperty(bool, notify = internetReachableChanged)
|
||||||
def isInternetReachable(self) -> bool:
|
def isInternetReachable(self) -> bool:
|
||||||
# Is any of the test urls reachable?
|
return self._is_internet_reachable
|
||||||
return any(self._statuses.values())
|
|
||||||
|
|
||||||
def _update(self):
|
def setOnlineStatus(self, new_status: bool):
|
||||||
for url in self._statuses.keys():
|
old_status = self._is_internet_reachable
|
||||||
self._http.get(
|
self._is_internet_reachable = new_status
|
||||||
url = url,
|
if old_status != new_status:
|
||||||
callback = self._statusCallback,
|
Logger.debug(
|
||||||
error_callback = self._statusCallback,
|
"Connection status has been set to {}".format("online" if self._is_internet_reachable else "offline"))
|
||||||
timeout = 5
|
|
||||||
)
|
|
||||||
|
|
||||||
def _statusCallback(self, reply: QNetworkReply, error: QNetworkReply.NetworkError = None):
|
|
||||||
url = reply.request().url().toString()
|
|
||||||
prev_statuses = self._statuses.copy()
|
|
||||||
self._statuses[url] = HttpRequestManager.replyIndicatesSuccess(reply, error)
|
|
||||||
|
|
||||||
if any(self._statuses.values()) != any(prev_statuses.values()):
|
|
||||||
self.internetReachableChanged.emit()
|
self.internetReachableChanged.emit()
|
||||||
|
|
|
@ -104,8 +104,10 @@ class CloudOutputDeviceManager:
|
||||||
self._api.getClusters(self._onGetRemoteClustersFinished, self._onGetRemoteClusterFailed)
|
self._api.getClusters(self._onGetRemoteClustersFinished, self._onGetRemoteClusterFailed)
|
||||||
|
|
||||||
def _onGetRemoteClustersFinished(self, clusters: List[CloudClusterResponse]) -> None:
|
def _onGetRemoteClustersFinished(self, clusters: List[CloudClusterResponse]) -> None:
|
||||||
"""Callback for when the request for getting the clusters is finished."""
|
"""Callback for when the request for getting the clusters is successful and finished."""
|
||||||
|
|
||||||
|
# Remote clusters were successfully retrieved, which means that the computer is online
|
||||||
|
CuraApplication.getInstance().getCuraAPI().connectionStatus.setOnlineStatus(True)
|
||||||
self._um_cloud_printers = {m.getMetaDataEntry(self.META_CLUSTER_ID): m for m in
|
self._um_cloud_printers = {m.getMetaDataEntry(self.META_CLUSTER_ID): m for m in
|
||||||
CuraApplication.getInstance().getContainerRegistry().findContainerStacks(
|
CuraApplication.getInstance().getContainerRegistry().findContainerStacks(
|
||||||
type = "machine") if m.getMetaDataEntry(self.META_CLUSTER_ID, None)}
|
type = "machine") if m.getMetaDataEntry(self.META_CLUSTER_ID, None)}
|
||||||
|
@ -150,6 +152,9 @@ class CloudOutputDeviceManager:
|
||||||
def _onGetRemoteClusterFailed(self, reply: QNetworkReply, error: QNetworkReply.NetworkError) -> None:
|
def _onGetRemoteClusterFailed(self, reply: QNetworkReply, error: QNetworkReply.NetworkError) -> None:
|
||||||
self._syncing = False
|
self._syncing = False
|
||||||
self._account.setSyncState(self.SYNC_SERVICE_NAME, SyncState.ERROR)
|
self._account.setSyncState(self.SYNC_SERVICE_NAME, SyncState.ERROR)
|
||||||
|
# If getting the remote clusters fails, then the cloud printers are unreachable, so we need to inform the
|
||||||
|
# connection status
|
||||||
|
CuraApplication.getInstance().getCuraAPI().connectionStatus.setOnlineStatus(False)
|
||||||
|
|
||||||
def _onDevicesDiscovered(self, clusters: List[CloudClusterResponse]) -> None:
|
def _onDevicesDiscovered(self, clusters: List[CloudClusterResponse]) -> None:
|
||||||
"""**Synchronously** create machines for discovered devices
|
"""**Synchronously** create machines for discovered devices
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue