Use BaseModel for CloudCluster, some fixes

This commit is contained in:
ChrisTerBeke 2018-11-26 14:51:24 +01:00
parent 579522857a
commit 6a43d10982
4 changed files with 34 additions and 35 deletions

View file

@ -10,6 +10,6 @@ def getMetaData():
def register(app): def register(app):
return { return {
"output_device": UM3OutputDevicePlugin.UM3OutputDevicePlugin(app), "output_device": UM3OutputDevicePlugin.UM3OutputDevicePlugin(),
"machine_action": DiscoverUM3Action.DiscoverUM3Action() "machine_action": DiscoverUM3Action.DiscoverUM3Action()
} }

View file

@ -9,7 +9,7 @@ from UM.Logger import Logger
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
from cura.NetworkClient import NetworkClient from cura.NetworkClient import NetworkClient
from plugins.UM3NetworkPrinting.src.Cloud.CloudOutputDevice import CloudOutputDevice 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. ## 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 @staticmethod
def _parseStatusResponse(reply: QNetworkReply) -> Optional[Cluster]: def _parseStatusResponse(reply: QNetworkReply) -> Optional[Cluster]:
try: 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: except json.decoder.JSONDecodeError:
Logger.logException("w", "Unable to decode JSON from reply.") Logger.logException("w", "Unable to decode JSON from reply.")
return None except ValueError:
except UnicodeDecodeError: Logger.logException("w", "Response was missing values.")
Logger.log("e", "Unable to read server response")
except json.JSONDecodeError:
Logger.logException("w", "Unable to decode JSON from reply.")
return None return None
## Adds a CloudOutputDevice for each entry in the remote cluster list from the API. ## Adds a CloudOutputDevice for each entry in the remote cluster list from the API.
def _addCloudOutputDevice(self, cluster: Cluster): def _addCloudOutputDevice(self, cluster: CloudCluster):
print("cluster_data====", cluster) device = CloudOutputDevice(cluster.cluster_id)
device = CloudOutputDevice(cluster["cluster_id"])
self._output_device_manager.addOutputDevice(device) 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. ## Callback for when the active machine was changed by the user.
def _activeMachineChanged(self): def _activeMachineChanged(self):

View file

@ -1,11 +1,18 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # 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 ## Class representing a cloud connected cluster.
"host_guid", # Type: str class CloudCluster(BaseModel):
"host_name", # Type: str def __init__(self, **kwargs):
"host_version", # Type: str self.cluster_id = None # type: str
"status", # 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")

View file

@ -1,7 +1,6 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # 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.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from UM.Logger import Logger from UM.Logger import Logger
from UM.Signal import Signal, signalemitter from UM.Signal import Signal, signalemitter
@ -20,9 +19,6 @@ from time import time
import json 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. ## 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. # Zero-Conf is used to detect printers, which are saved in a dict.
@ -33,21 +29,20 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
removeDeviceSignal = Signal() removeDeviceSignal = Signal()
discoveredDevicesChanged = Signal() discoveredDevicesChanged = Signal()
def __init__(self, application: "CuraApplication"): def __init__(self):
super().__init__() super().__init__()
self._application = application
self._zero_conf = None self._zero_conf = None
self._zero_conf_browser = None self._zero_conf_browser = None
# Create a cloud output device manager that abstract all cloud connection logic away. # 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. # Because the model needs to be created in the same thread as the QMLEngine, we use a signal.
self.addDeviceSignal.connect(self._onAddDevice) self.addDeviceSignal.connect(self._onAddDevice)
self.removeDeviceSignal.connect(self._onRemoveDevice) self.removeDeviceSignal.connect(self._onRemoveDevice)
application.globalContainerStackChanged.connect(self.reCheckConnections) Application.getInstance().globalContainerStackChanged.connect(self.reCheckConnections)
self._discovered_devices = {} self._discovered_devices = {}
@ -62,7 +57,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self._cluster_api_prefix = "/cluster-api/v" + self._cluster_api_version + "/" self._cluster_api_prefix = "/cluster-api/v" + self._cluster_api_version + "/"
# Get list of manual instances from preferences # Get list of manual instances from preferences
self._preferences = self._application.getPreferences() self._preferences = Application.getInstance().getPreferences()
self._preferences.addPreference("um3networkprinting/manual_instances", self._preferences.addPreference("um3networkprinting/manual_instances",
"") # A comma-separated list of ip adresses or hostnames "") # A comma-separated list of ip adresses or hostnames
@ -113,7 +108,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self.resetLastManualDevice() self.resetLastManualDevice()
def reCheckConnections(self): def reCheckConnections(self):
active_machine = self._application.getGlobalContainerStack() active_machine = Application.getInstance().getGlobalContainerStack()
if not active_machine: if not active_machine:
return return
@ -138,7 +133,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
return return
if self._discovered_devices[key].isConnected(): 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 # 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: if key == um_network_key:
self.getOutputDeviceManager().addOutputDevice(self._discovered_devices[key]) self.getOutputDeviceManager().addOutputDevice(self._discovered_devices[key])
else: else:
@ -290,7 +285,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self._discovered_devices[device.getId()] = device self._discovered_devices[device.getId()] = device
self.discoveredDevicesChanged.emit() 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"): if global_container_stack and device.getId() == global_container_stack.getMetaDataEntry("um_network_key"):
device.connect() device.connect()
device.connectionStateChanged.connect(self._onDeviceConnectionStateChanged) device.connectionStateChanged.connect(self._onDeviceConnectionStateChanged)
@ -308,7 +303,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self._service_changed_request_event.wait(timeout = 5.0) self._service_changed_request_event.wait(timeout = 5.0)
# Stop if the application is shutting down # Stop if the application is shutting down
if self._application.isShuttingDown(): if Application.getInstance().isShuttingDown():
return return
self._service_changed_request_event.clear() self._service_changed_request_event.clear()