From 6a43d10982e41e175c54338383435ca91d666199 Mon Sep 17 00:00:00 2001 From: ChrisTerBeke Date: Mon, 26 Nov 2018 14:51:24 +0100 Subject: [PATCH] Use BaseModel for CloudCluster, some fixes --- plugins/UM3NetworkPrinting/__init__.py | 2 +- .../src/Cloud/CloudOutputDeviceManager.py | 21 ++++++++--------- .../UM3NetworkPrinting/src/Cloud/Models.py | 23 ++++++++++++------- .../src/UM3OutputDevicePlugin.py | 23 ++++++++----------- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/plugins/UM3NetworkPrinting/__init__.py b/plugins/UM3NetworkPrinting/__init__.py index 23262aed94..3da7795589 100644 --- a/plugins/UM3NetworkPrinting/__init__.py +++ b/plugins/UM3NetworkPrinting/__init__.py @@ -10,6 +10,6 @@ def getMetaData(): def register(app): return { - "output_device": UM3OutputDevicePlugin.UM3OutputDevicePlugin(app), + "output_device": UM3OutputDevicePlugin.UM3OutputDevicePlugin(), "machine_action": DiscoverUM3Action.DiscoverUM3Action() } diff --git a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py index 17e82417ef..e48c06dbe9 100644 --- a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py +++ b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py @@ -9,7 +9,7 @@ from UM.Logger import Logger from cura.CuraApplication import CuraApplication from cura.NetworkClient import NetworkClient from plugins.UM3NetworkPrinting.src.Cloud.CloudOutputDevice import CloudOutputDevice -from .Models import Cluster +from .Models import CloudCluster ## The cloud output device manager is responsible for using the Ultimaker Cloud APIs to manage remote clusters. @@ -78,23 +78,20 @@ class CloudOutputDeviceManager(NetworkClient): @staticmethod def _parseStatusResponse(reply: QNetworkReply) -> Optional[Cluster]: try: - return [Cluster(**c) for c in json.loads(reply.readAll().data().decode("utf-8"))] + return [CloudCluster(**c) for c in json.loads(reply.readAll().data().decode("utf-8"))] + except UnicodeDecodeError: + Logger.log("w", "Unable to read server response") except json.decoder.JSONDecodeError: Logger.logException("w", "Unable to decode JSON from reply.") - return None - except UnicodeDecodeError: - Logger.log("e", "Unable to read server response") - except json.JSONDecodeError: - Logger.logException("w", "Unable to decode JSON from reply.") - + except ValueError: + Logger.logException("w", "Response was missing values.") return None ## Adds a CloudOutputDevice for each entry in the remote cluster list from the API. - def _addCloudOutputDevice(self, cluster: Cluster): - print("cluster_data====", cluster) - device = CloudOutputDevice(cluster["cluster_id"]) + def _addCloudOutputDevice(self, cluster: CloudCluster): + device = CloudOutputDevice(cluster.cluster_id) self._output_device_manager.addOutputDevice(device) - self._remote_clusters[cluster["cluster_id"]] = device + self._remote_clusters[cluster.cluster_id] = device ## Callback for when the active machine was changed by the user. def _activeMachineChanged(self): diff --git a/plugins/UM3NetworkPrinting/src/Cloud/Models.py b/plugins/UM3NetworkPrinting/src/Cloud/Models.py index b118f3e61c..3cbfecadfb 100644 --- a/plugins/UM3NetworkPrinting/src/Cloud/Models.py +++ b/plugins/UM3NetworkPrinting/src/Cloud/Models.py @@ -1,11 +1,18 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from collections import namedtuple +from plugins.UM3NetworkPrinting.src.Models import BaseModel -Cluster = namedtuple("Cluster", [ - "cluster_id", # Type: str - "host_guid", # Type: str - "host_name", # Type: str - "host_version", # Type: str - "status", # Type: str -]) + +## Class representing a cloud connected cluster. +class CloudCluster(BaseModel): + def __init__(self, **kwargs): + self.cluster_id = None # type: str + self.host_guid = None # type: str + self.host_name = None # type: str + self.host_version = None # type: str + self.status = None # type: str + super().__init__(**kwargs) + + def validate(self): + if not self.cluster_id: + raise ValueError("cluster_id is required on CloudCluster") diff --git a/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py b/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py index 47c3482aa5..086bca03e2 100644 --- a/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py +++ b/plugins/UM3NetworkPrinting/src/UM3OutputDevicePlugin.py @@ -1,7 +1,6 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from typing import TYPE_CHECKING - +from UM.Application import Application from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin from UM.Logger import Logger from UM.Signal import Signal, signalemitter @@ -20,9 +19,6 @@ from time import time import json -if TYPE_CHECKING: - from cura.CuraApplication import CuraApplication - ## This plugin handles the connection detection & creation of output device objects for the UM3 printer. # Zero-Conf is used to detect printers, which are saved in a dict. @@ -33,21 +29,20 @@ class UM3OutputDevicePlugin(OutputDevicePlugin): removeDeviceSignal = Signal() discoveredDevicesChanged = Signal() - def __init__(self, application: "CuraApplication"): + def __init__(self): super().__init__() - self._application = application self._zero_conf = None self._zero_conf_browser = None # Create a cloud output device manager that abstract all cloud connection logic away. - # self._cloud_output_device_manager = CloudOutputDeviceManager() + self._cloud_output_device_manager = CloudOutputDeviceManager() # Because the model needs to be created in the same thread as the QMLEngine, we use a signal. self.addDeviceSignal.connect(self._onAddDevice) self.removeDeviceSignal.connect(self._onRemoveDevice) - application.globalContainerStackChanged.connect(self.reCheckConnections) + Application.getInstance().globalContainerStackChanged.connect(self.reCheckConnections) self._discovered_devices = {} @@ -62,7 +57,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin): self._cluster_api_prefix = "/cluster-api/v" + self._cluster_api_version + "/" # Get list of manual instances from preferences - self._preferences = self._application.getPreferences() + self._preferences = Application.getInstance().getPreferences() self._preferences.addPreference("um3networkprinting/manual_instances", "") # A comma-separated list of ip adresses or hostnames @@ -113,7 +108,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin): self.resetLastManualDevice() def reCheckConnections(self): - active_machine = self._application.getGlobalContainerStack() + active_machine = Application.getInstance().getGlobalContainerStack() if not active_machine: return @@ -138,7 +133,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin): return if self._discovered_devices[key].isConnected(): # Sometimes the status changes after changing the global container and maybe the device doesn't belong to this machine - um_network_key = self._application.getGlobalContainerStack().getMetaDataEntry("um_network_key") + um_network_key = Application.getInstance().getGlobalContainerStack().getMetaDataEntry("um_network_key") if key == um_network_key: self.getOutputDeviceManager().addOutputDevice(self._discovered_devices[key]) else: @@ -290,7 +285,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin): self._discovered_devices[device.getId()] = device self.discoveredDevicesChanged.emit() - global_container_stack = self._application.getGlobalContainerStack() + global_container_stack = Application.getInstance().getGlobalContainerStack() if global_container_stack and device.getId() == global_container_stack.getMetaDataEntry("um_network_key"): device.connect() device.connectionStateChanged.connect(self._onDeviceConnectionStateChanged) @@ -308,7 +303,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin): self._service_changed_request_event.wait(timeout = 5.0) # Stop if the application is shutting down - if self._application.isShuttingDown(): + if Application.getInstance().isShuttingDown(): return self._service_changed_request_event.clear()