Merge branch 'tests-for-um3networkplugin' into cloud-output-device

This commit is contained in:
ChrisTerBeke 2018-11-23 14:08:55 +01:00
commit 8ee39c0489
3 changed files with 98 additions and 97 deletions

View file

@ -2,32 +2,32 @@
# Cura is released under the terms of the LGPLv3 or higher.
from collections import namedtuple
ClusterMaterial = namedtuple('ClusterMaterial', [
'guid',
'material',
'brand',
'version',
'color',
'density'
ClusterMaterial = namedtuple("ClusterMaterial", [
"guid", # Type: str
"material", # Type: str
"brand", # Type: str
"version", # Type: int
"color", # Type: str
"density" # Type: str
])
LocalMaterial = namedtuple('LocalMaterial', [
'GUID',
'id',
'type',
'status',
'base_file',
'setting_version',
'version',
'name',
'brand',
'material',
'color_name',
'color_code',
'description',
'adhesion_info',
'approximate_diameter',
'properties',
'definition',
'compatible'
LocalMaterial = namedtuple("LocalMaterial", [
"GUID", # Type: str
"id", # Type: str
"type", # Type: str
"status", # Type: str
"base_file", # Type: str
"setting_version", # Type: int
"version", # Type: int
"name", # Type: str
"brand", # Type: str
"material", # Type: str
"color_name", # Type: str
"color_code", # Type: str
"description", # Type: str
"adhesion_info", # Type: str
"approximate_diameter", # Type: str
"properties", # Type: str
"definition", # Type: str
"compatible" # Type: str
])

View file

@ -2,24 +2,24 @@
# Cura is released under the terms of the LGPLv3 or higher.
import json
import os
import re
import urllib.parse
from typing import Dict, TYPE_CHECKING, Set
from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest
from UM.Application import Application
from UM.Job import Job
from UM.Logger import Logger
from UM.MimeTypeDatabase import MimeTypeDatabase
from UM.Resources import Resources
from cura.CuraApplication import CuraApplication
# Absolute imports don't work in plugins
from .Models import ClusterMaterial, LocalMaterial
if TYPE_CHECKING:
from .ClusterUM3OutputDevice import ClusterUM3OutputDevice
## Asynchronous job to send material profiles to the printer.
#
# This way it won't freeze up the interface while sending those materials.
@ -28,7 +28,6 @@ class SendMaterialJob(Job):
def __init__(self, device: "ClusterUM3OutputDevice") -> None:
super().__init__()
self.device = device # type: ClusterUM3OutputDevice
self._application = CuraApplication.getInstance() # type: CuraApplication
## Send the request to the printer and register a callback
def run(self) -> None:
@ -45,13 +44,9 @@ class SendMaterialJob(Job):
return
# Collect materials from the printer's reply and send the missing ones if needed.
try:
remote_materials_by_guid = self._parseReply(reply)
remote_materials_by_guid = self._parseReply(reply)
if remote_materials_by_guid:
self._sendMissingMaterials(remote_materials_by_guid)
except json.JSONDecodeError:
Logger.logException("w", "Error parsing materials from printer")
except TypeError:
Logger.logException("w", "Error parsing materials from printer")
## Determine which materials should be updated and send them to the printer.
#
@ -86,7 +81,7 @@ class SendMaterialJob(Job):
return {
material.id
for guid, material in local_materials.items()
if guid not in remote_materials or int(material.version) > remote_materials[guid].version
if guid not in remote_materials or material.version > remote_materials[guid].version
}
## Send the materials to the printer.
@ -122,23 +117,23 @@ class SendMaterialJob(Job):
# \param material_id The ID of the material in the file.
def _sendMaterialFile(self, file_path: str, file_name: str, material_id: str) -> None:
parts = []
parts = []
# Add the material file.
with open(file_path, "rb") as f:
parts.append(self.device.createFormPart("name=\"file\"; filename=\"{file_name}\""
.format(file_name = file_name), f.read()))
# Add the material file.
with open(file_path, "rb") as f:
parts.append(self.device.createFormPart("name=\"file\"; filename=\"{file_name}\""
.format(file_name = file_name), f.read()))
# Add the material signature file if needed.
signature_file_path = "{}.sig".format(file_path)
if os.path.exists(signature_file_path):
signature_file_name = os.path.basename(signature_file_path)
with open(signature_file_path, "rb") as f:
parts.append(self.device.createFormPart("name=\"signature_file\"; filename=\"{file_name}\""
.format(file_name = signature_file_name), f.read()))
# Add the material signature file if needed.
signature_file_path = "{}.sig".format(file_path)
if os.path.exists(signature_file_path):
signature_file_name = os.path.basename(signature_file_path)
with open(signature_file_path, "rb") as f:
parts.append(self.device.createFormPart("name=\"signature_file\"; filename=\"{file_name}\""
.format(file_name = signature_file_name), f.read()))
Logger.log("d", "Syncing material {material_id} with cluster.".format(material_id = material_id))
self.device.postFormWithParts(target = "materials/", parts = parts, on_finished = self.sendingFinished)
Logger.log("d", "Syncing material {material_id} with cluster.".format(material_id = material_id))
self.device.postFormWithParts(target = "materials/", parts = parts, on_finished = self.sendingFinished)
## Check a reply from an upload to the printer and log an error when the call failed
@staticmethod
@ -154,12 +149,18 @@ class SendMaterialJob(Job):
# Parses the reply to a "/materials" request to the printer
#
# \return a dictionary of ClusterMaterial objects by GUID
# \throw json.JSONDecodeError Raised when the reply does not contain a valid json string
# \throw KeyError Raised when on of the materials does not include a valid guid
@classmethod
def _parseReply(cls, reply: QNetworkReply) -> Dict[str, ClusterMaterial]:
remote_materials = json.loads(reply.readAll().data().decode("utf-8"))
return {material["guid"]: ClusterMaterial(**material) for material in remote_materials}
try:
remote_materials = json.loads(reply.readAll().data().decode("utf-8"))
return {material["guid"]: ClusterMaterial(**material) for material in remote_materials}
except UnicodeDecodeError:
Logger.log("e", "Request material storage on printer: I didn't understand the printer's answer.")
except json.JSONDecodeError:
Logger.log("e", "Request material storage on printer: I didn't understand the printer's answer.")
except TypeError:
Logger.log("e", "Request material storage on printer: Printer's answer was missing GUIDs.")
## Retrieves a list of local materials
#
@ -168,22 +169,24 @@ class SendMaterialJob(Job):
# \return a dictionary of LocalMaterial objects by GUID
def _getLocalMaterials(self) -> Dict[str, LocalMaterial]:
result = {} # type: Dict[str, LocalMaterial]
container_registry = self._application.getContainerRegistry()
container_registry = Application.getInstance().getContainerRegistry()
material_containers = container_registry.findContainersMetadata(type = "material")
# Find the latest version of all material containers in the registry.
for material in material_containers:
try:
material = LocalMaterial(**material)
# material version must be an int
if not re.match("\d+", material.version):
Logger.logException("w", "Local material {} has invalid version '{}'."
.format(material["id"], material.version))
continue
material["version"] = int(material["version"])
if material.GUID not in result or material.version > result.get(material.GUID).version:
result[material.GUID] = material
# Create a new local material
local_material = LocalMaterial(**material)
if local_material.GUID not in result or \
local_material.version > result.get(local_material.GUID).version:
result[local_material.GUID] = local_material
except KeyError:
Logger.logException("w", "Local material {} has missing values.".format(material["id"]))
except ValueError:
Logger.logException("w", "Local material {} has invalid values.".format(material["id"]))