Added a model to represent a cluster

This commit is contained in:
Marijn Deé 2018-11-26 10:47:53 +01:00
parent d8232caec0
commit 908628e2aa
2 changed files with 29 additions and 14 deletions

View file

@ -8,6 +8,7 @@ from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
from UM.Logger import Logger from UM.Logger import Logger
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
## 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.
@ -60,35 +61,38 @@ class CloudOutputDeviceManager(NetworkClient):
return return
# Parse the response (returns the "data" field from the body). # Parse the response (returns the "data" field from the body).
clusters_data = self._parseStatusResponse(reply) clusters = self._parseStatusResponse(reply)
if not clusters_data: if not clusters:
return return
# Add an output device for each remote cluster. # Add an output device for each remote cluster.
# The clusters are an array of objects in a field called "data". # The clusters are an array of objects in a field called "data".
for cluster in clusters_data: for cluster in clusters:
self._addCloudOutputDevice(cluster) self._addCloudOutputDevice(cluster)
# # For testing we add a dummy device: # # For testing we add a dummy device:
# self._addCloudOutputDevice({ "cluster_id": "LJ0tciiuZZjarrXAvFLEZ6ox4Cvx8FvtXUlQv4vIhV6w" }) # self._addCloudOutputDevice({ "cluster_id": "LJ0tciiuZZjarrXAvFLEZ6ox4Cvx8FvtXUlQv4vIhV6w" })
@staticmethod @staticmethod
def _parseStatusResponse(reply: QNetworkReply) -> Optional[dict]: def _parseStatusResponse(reply: QNetworkReply) -> Optional[Cluster]:
try: try:
result = json.loads(bytes(reply.readAll()).decode("utf-8")) return [Cluster(**c) for c in json.loads(reply.readAll().data().decode("utf-8"))]
# TODO: use model or named tuple here.
return result.data
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 return None
except UnicodeDecodeError:
Logger.log("e", "Unable to read server response")
except json.JSONDecodeError:
Logger.logException("w", "Unable to decode JSON from reply.")
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_data: Dict[str, any]): def _addCloudOutputDevice(self, cluster: Cluster):
# TODO: use model or named tuple for cluster_data print("cluster_data====", cluster)
print("cluster_data====", cluster_data) device = CloudOutputDevice(cluster["cluster_id"])
device = CloudOutputDevice(cluster_data["cluster_id"])
self._output_device_manager.addOutputDevice(device) self._output_device_manager.addOutputDevice(device)
self._remote_clusters[cluster_data["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

@ -0,0 +1,11 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from collections import namedtuple
Cluster = namedtuple("Cluster", [
"cluster_id", # Type: str
"host_guid", # Type: str
"host_name", # Type: str
"host_version", # Type: str
"status", # Type: str
])