STAR-322: Extracting models to be able for converting themselves

This commit is contained in:
Daniel Schiavini 2018-12-05 14:08:40 +01:00
parent 163226f940
commit b693b9d98f
20 changed files with 324 additions and 254 deletions

View file

@ -0,0 +1,21 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from ...Models import BaseModel
## 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
self.is_online = None # type: bool
super().__init__(**kwargs)
# Validates the model, raising an exception if the model is invalid.
def validate(self) -> None:
super().validate()
if not self.cluster_id:
raise ValueError("cluster_id is required on CloudCluster")

View file

@ -0,0 +1,52 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from typing import List
from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
from .CloudClusterPrinterConfiguration import CloudClusterPrinterConfiguration
from .CloudClusterPrintJobConstraint import CloudClusterPrintJobConstraint
from ...Models import BaseModel
## Class representing a print job
from plugins.UM3NetworkPrinting.src.UM3PrintJobOutputModel import UM3PrintJobOutputModel
class CloudClusterPrintJob(BaseModel):
def __init__(self, **kwargs) -> None:
self.assigned_to = None # type: str
self.configuration = [] # type: List[CloudClusterPrinterConfiguration]
self.constraints = [] # type: List[CloudClusterPrintJobConstraint]
self.created_at = None # type: str
self.force = None # type: str
self.last_seen = None # type: str
self.machine_variant = None # type: str
self.name = None # type: str
self.network_error_count = None # type: int
self.owner = None # type: str
self.printer_uuid = None # type: str
self.started = None # type: str
self.status = None # type: str
self.time_elapsed = None # type: str
self.time_total = None # type: str
self.uuid = None # type: str
super().__init__(**kwargs)
self.printers = [CloudClusterPrinterConfiguration(**c) if isinstance(c, dict) else c
for c in self.configuration]
self.printers = [CloudClusterPrintJobConstraint(**p) if isinstance(p, dict) else p
for p in self.constraints]
## Creates an UM3 print job output model based on this cloud cluster print job.
# \param printer: The output model of the printer
def createOutputModel(self, printer: PrinterOutputModel) -> UM3PrintJobOutputModel:
model = UM3PrintJobOutputModel(printer.getController(), self.uuid, self.name)
model.updateAssignedPrinter(printer)
return model
## Updates an UM3 print job output model based on this cloud cluster print job.
# \param model: The model to update.
def updateOutputModel(self, model: UM3PrintJobOutputModel) -> None:
model.updateTimeTotal(self.time_total)
model.updateTimeElapsed(self.time_elapsed)
model.updateOwner(self.owner)
model.updateState(self.status)

View file

@ -0,0 +1,10 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from ...Models import BaseModel
## Class representing a cloud cluster print job constraint
class CloudClusterPrintJobConstraint(BaseModel):
def __init__(self, **kwargs) -> None:
self.require_printer_name = None # type: str
super().__init__(**kwargs)

View file

@ -0,0 +1,44 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from typing import List
from cura.PrinterOutput.PrinterOutputController import PrinterOutputController
from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
from .CloudClusterPrinterConfiguration import CloudClusterPrinterConfiguration
from ...Models import BaseModel
## Class representing a cluster printer
class CloudClusterPrinter(BaseModel):
def __init__(self, **kwargs) -> None:
self.configuration = [] # type: List[CloudClusterPrinterConfiguration]
self.enabled = None # type: str
self.firmware_version = None # type: str
self.friendly_name = None # type: str
self.ip_address = None # type: str
self.machine_variant = None # type: str
self.status = None # type: str
self.unique_name = None # type: str
self.uuid = None # type: str
super().__init__(**kwargs)
self.configuration = [CloudClusterPrinterConfiguration(**c)
if isinstance(c, dict) else c for c in self.configuration]
## Creates a new output model.
# \param controller - The controller of the model.
def createOutputModel(self, controller: PrinterOutputController) -> PrinterOutputModel:
model = PrinterOutputModel(controller, len(self.configuration), firmware_version = self.firmware_version)
self.updateOutputModel(model)
return model
## Updates the given output model.
# \param model - The output model to update.
def updateOutputModel(self, model: PrinterOutputModel) -> None:
model.updateKey(self.uuid)
model.updateName(self.friendly_name)
model.updateType(self.machine_variant)
model.updateState(self.status if self.enabled else "disabled")
for configuration, extruder in zip(self.configuration, model.extruders):
configuration.updateOutputModel(extruder)

View file

@ -0,0 +1,27 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from cura.PrinterOutput.ExtruderOutputModel import ExtruderOutputModel
from .CloudClusterPrinterConfigurationMaterial import CloudClusterPrinterConfigurationMaterial
from ...Models import BaseModel
## Class representing a cloud cluster printer configuration
class CloudClusterPrinterConfiguration(BaseModel):
def __init__(self, **kwargs) -> None:
self.extruder_index = None # type: str
self.material = None # type: CloudClusterPrinterConfigurationMaterial
self.nozzle_diameter = None # type: str
self.print_core_id = None # type: str
super().__init__(**kwargs)
if isinstance(self.material, dict):
self.material = CloudClusterPrinterConfigurationMaterial(**self.material)
## Updates the given output model.
# \param model - The output model to update.
def updateOutputModel(self, model: ExtruderOutputModel) -> None:
model.updateHotendID(self.print_core_id)
if model.activeMaterial is None or model.activeMaterial.guid != self.material.guid:
material = self.material.createOutputModel()
model.updateActiveMaterial(material)

View file

@ -0,0 +1,46 @@
from UM.Logger import Logger
from cura.CuraApplication import CuraApplication
from cura.PrinterOutput.MaterialOutputModel import MaterialOutputModel
from ...Models import BaseModel
## Class representing a cloud cluster printer configuration
class CloudClusterPrinterConfigurationMaterial(BaseModel):
def __init__(self, **kwargs) -> None:
self.guid = None # type: str
self.brand = None # type: str
self.color = None # type: str
self.material = None # type: str
super().__init__(**kwargs)
## Creates a material output model based on this cloud printer material.
def createOutputModel(self) -> MaterialOutputModel:
material_manager = CuraApplication.getInstance().getMaterialManager()
material_group_list = material_manager.getMaterialGroupListByGUID(self.guid) or []
# Sort the material groups by "is_read_only = True" first, and then the name alphabetically.
read_only_material_group_list = list(filter(lambda x: x.is_read_only, material_group_list))
non_read_only_material_group_list = list(filter(lambda x: not x.is_read_only, material_group_list))
material_group = None
if read_only_material_group_list:
read_only_material_group_list = sorted(read_only_material_group_list, key = lambda x: x.name)
material_group = read_only_material_group_list[0]
elif non_read_only_material_group_list:
non_read_only_material_group_list = sorted(non_read_only_material_group_list, key = lambda x: x.name)
material_group = non_read_only_material_group_list[0]
if material_group:
container = material_group.root_material_node.getContainer()
color = container.getMetaDataEntry("color_code")
brand = container.getMetaDataEntry("brand")
material_type = container.getMetaDataEntry("material")
name = container.getName()
else:
Logger.log("w", "Unable to find material with guid {guid}. Using data as provided by cluster"
.format(guid = self.guid))
color = self.color
brand = self.brand
material_type = self.material
name = "Empty" if self.material == "empty" else "Unknown"
return MaterialOutputModel(guid = self.guid, type = material_type, brand = brand, color = color, name = name)

View file

@ -0,0 +1,22 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from typing import List
from .CloudClusterPrinter import CloudClusterPrinter
from .CloudClusterPrintJob import CloudClusterPrintJob
from ...Models import BaseModel
# Model that represents the status of the cluster for the cloud
class CloudClusterStatus(BaseModel):
def __init__(self, **kwargs) -> None:
# a list of the printers
self.printers = [] # type: List[CloudClusterPrinter]
# a list of the print jobs
self.print_jobs = [] # type: List[CloudClusterPrintJob]
super().__init__(**kwargs)
# converting any dictionaries into models
self.printers = [CloudClusterPrinter(**p) if isinstance(p, dict) else p for p in self.printers]
self.print_jobs = [CloudClusterPrintJob(**j) if isinstance(j, dict) else j for j in self.print_jobs]

View file

@ -0,0 +1,17 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from typing import Dict
from ...Models import BaseModel
## Class representing errors generated by the cloud servers, according to the json-api standard.
class CloudErrorObject(BaseModel):
def __init__(self, **kwargs) -> None:
self.id = None # type: str
self.code = None # type: str
self.http_status = None # type: str
self.title = None # type: str
self.detail = None # type: str
self.meta = None # type: Dict[str, any]
super().__init__(**kwargs)

View file

@ -0,0 +1,16 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from ...Models import BaseModel
# Model that represents the response received from the cloud after requesting to upload a print job
class CloudJobResponse(BaseModel):
def __init__(self, **kwargs) -> None:
self.download_url = None # type: str
self.job_id = None # type: str
self.job_name = None # type: str
self.slicing_details = None # type: str
self.status = None # type: str
self.upload_url = None # type: str
self.content_type = None # type: str
super().__init__(**kwargs)

View file

@ -0,0 +1,12 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from ...Models import BaseModel
# Model that represents the request to upload a print job to the cloud
class CloudJobUploadRequest(BaseModel):
def __init__(self, **kwargs) -> None:
self.file_size = None # type: int
self.job_name = None # type: str
self.content_type = None # type: str
super().__init__(**kwargs)

View file

@ -0,0 +1,12 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from ...Models import BaseModel
# Model that represents the responses received from the cloud after requesting a job to be printed.
class CloudPrintResponse(BaseModel):
def __init__(self, **kwargs) -> None:
self.cluster_job_id = None # type: str
self.job_id = None # type: str
self.status = None # type: str
super().__init__(**kwargs)

View file

@ -0,0 +1,2 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.