mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-12 09:17:50 -06:00
Fix syncing materials via API, show nice message
This commit is contained in:
parent
8dd6dd6573
commit
47237cda5f
12 changed files with 198 additions and 314 deletions
|
@ -0,0 +1,39 @@
|
|||
# Copyright (c) 2019 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from UM import i18nCatalog
|
||||
from UM.Message import Message
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..UltimakerNetworkedPrinterOutputDevice import UltimakerNetworkedPrinterOutputDevice
|
||||
|
||||
|
||||
I18N_CATALOG = i18nCatalog("cura")
|
||||
|
||||
|
||||
## Message shown when sending material files to cluster host.
|
||||
class MaterialSyncMessage(Message):
|
||||
|
||||
# Singleton used to prevent duplicate messages of this type at the same time.
|
||||
__is_visible = False
|
||||
|
||||
def __init__(self, device: "UltimakerNetworkedPrinterOutputDevice") -> None:
|
||||
super().__init__(
|
||||
text = I18N_CATALOG.i18nc("@info:status", "Cura has detected material profiles that were not yet installed "
|
||||
"on the host printer of group {0}.", device.name),
|
||||
title = I18N_CATALOG.i18nc("@info:title", "Sending materials to printer"),
|
||||
lifetime = 10,
|
||||
dismissable = True
|
||||
)
|
||||
|
||||
def show(self) -> None:
|
||||
if MaterialSyncMessage.__is_visible:
|
||||
return
|
||||
super().show()
|
||||
MaterialSyncMessage.__is_visible = True
|
||||
|
||||
def hide(self, send_signal = True) -> None:
|
||||
super().hide(send_signal)
|
||||
MaterialSyncMessage.__is_visible = False
|
|
@ -1,6 +1,6 @@
|
|||
# Copyright (c) 2019 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
from .BaseModel import BaseModel
|
||||
from ..BaseModel import BaseModel
|
||||
|
||||
|
||||
class ClusterMaterial(BaseModel):
|
0
plugins/UM3NetworkPrinting/src/Models/Http/__init__.py
Normal file
0
plugins/UM3NetworkPrinting/src/Models/Http/__init__.py
Normal file
|
@ -13,6 +13,7 @@ from ..Models.BaseModel import BaseModel
|
|||
from ..Models.Http.ClusterPrintJobStatus import ClusterPrintJobStatus
|
||||
from ..Models.Http.ClusterPrinterStatus import ClusterPrinterStatus
|
||||
from ..Models.Http.PrinterSystemStatus import PrinterSystemStatus
|
||||
from ..Models.Http.ClusterMaterial import ClusterMaterial
|
||||
|
||||
|
||||
## The generic type variable used to document the methods below.
|
||||
|
@ -44,6 +45,13 @@ class ClusterApiClient:
|
|||
reply = self._manager.get(self._createEmptyRequest(url))
|
||||
self._addCallback(reply, on_finished, PrinterSystemStatus)
|
||||
|
||||
## Get the installed materials on the printer.
|
||||
# \param on_finished: The callback in case the response is successful.
|
||||
def getMaterials(self, on_finished: Callable[[List[ClusterMaterial]], Any]) -> None:
|
||||
url = "{}/materials".format(self.CLUSTER_API_PREFIX)
|
||||
reply = self._manager.get(self._createEmptyRequest(url))
|
||||
self._addCallback(reply, on_finished, ClusterMaterial)
|
||||
|
||||
## Get the printers in the cluster.
|
||||
# \param on_finished: The callback in case the response is successful.
|
||||
def getPrinters(self, on_finished: Callable[[List[ClusterPrinterStatus]], Any]) -> None:
|
||||
|
@ -62,7 +70,7 @@ class ClusterApiClient:
|
|||
def movePrintJobToTop(self, print_job_uuid: str) -> None:
|
||||
url = "{}/print_jobs/{}/action/move".format(self.CLUSTER_API_PREFIX, print_job_uuid)
|
||||
self._manager.post(self._createEmptyRequest(url), json.dumps({"to_position": 0, "list": "queued"}).encode())
|
||||
|
||||
|
||||
## Override print job configuration and force it to be printed.
|
||||
def forcePrintJob(self, print_job_uuid: str) -> None:
|
||||
url = "{}/print_jobs/{}".format(self.CLUSTER_API_PREFIX, print_job_uuid)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Copyright (c) 2019 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
from typing import Optional, Dict, List
|
||||
from typing import Optional, Dict, List, Callable, Any
|
||||
|
||||
from PyQt5.QtGui import QDesktopServices
|
||||
from PyQt5.QtCore import pyqtSlot, QUrl, pyqtSignal, pyqtProperty
|
||||
|
@ -13,12 +13,13 @@ from cura.PrinterOutput.NetworkedPrinterOutputDevice import AuthState
|
|||
from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
|
||||
|
||||
from .ClusterApiClient import ClusterApiClient
|
||||
from .SendMaterialJob import SendMaterialJob
|
||||
from ..ExportFileJob import ExportFileJob
|
||||
from ..SendMaterialJob import SendMaterialJob
|
||||
from ..UltimakerNetworkedPrinterOutputDevice import UltimakerNetworkedPrinterOutputDevice
|
||||
from ..Messages.PrintJobUploadBlockedMessage import PrintJobUploadBlockedMessage
|
||||
from ..Messages.PrintJobUploadErrorMessage import PrintJobUploadErrorMessage
|
||||
from ..Messages.PrintJobUploadSuccessMessage import PrintJobUploadSuccessMessage
|
||||
from ..Models.Http.ClusterMaterial import ClusterMaterial
|
||||
|
||||
|
||||
I18N_CATALOG = i18nCatalog("cura")
|
||||
|
@ -100,10 +101,14 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
|
|||
self._getApiClient().getPrintJobs(self._updatePrintJobs)
|
||||
self._updatePrintJobPreviewImages()
|
||||
|
||||
## Get a list of materials that are installed on the cluster host.
|
||||
def getMaterials(self, on_finished: Callable[[List[ClusterMaterial]], Any]) -> None:
|
||||
self._getApiClient().getMaterials(on_finished = on_finished)
|
||||
|
||||
## Sync the material profiles in Cura with the printer.
|
||||
# This gets called when connecting to a printer as well as when sending a print.
|
||||
def sendMaterialProfiles(self) -> None:
|
||||
job = SendMaterialJob(device=self)
|
||||
job = SendMaterialJob(device = self)
|
||||
job.run()
|
||||
|
||||
## Send a print job to the cluster.
|
||||
|
@ -133,6 +138,7 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
|
|||
self._createFormPart("name=owner", bytes(self._getUserName(), "utf-8"), "text/plain"),
|
||||
self._createFormPart("name=\"file\"; filename=\"%s\"" % job.getFileName(), job.getOutput())
|
||||
]
|
||||
# FIXME: move form posting to API client
|
||||
self.postFormWithParts("/cluster-api/v1/print_jobs/", parts, on_finished=self._onPrintUploadCompleted,
|
||||
on_progress=self._onPrintJobUploadProgress)
|
||||
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
# Copyright (c) 2019 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
import json
|
||||
import os
|
||||
from typing import Dict, TYPE_CHECKING, Set, Optional
|
||||
from typing import Dict, TYPE_CHECKING, Set, List
|
||||
from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest
|
||||
|
||||
from UM.Job import Job
|
||||
from UM.Logger import Logger
|
||||
from cura.CuraApplication import CuraApplication
|
||||
|
||||
from .Models.ClusterMaterial import ClusterMaterial
|
||||
from .Models.LocalMaterial import LocalMaterial
|
||||
from ..Models.Http.ClusterMaterial import ClusterMaterial
|
||||
from ..Models.LocalMaterial import LocalMaterial
|
||||
from ..Messages.MaterialSyncMessage import MaterialSyncMessage
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .Network.LocalClusterOutputDevice import LocalClusterOutputDevice
|
||||
from .LocalClusterOutputDevice import LocalClusterOutputDevice
|
||||
|
||||
|
||||
## Asynchronous job to send material profiles to the printer.
|
||||
|
@ -27,64 +27,49 @@ class SendMaterialJob(Job):
|
|||
|
||||
## Send the request to the printer and register a callback
|
||||
def run(self) -> None:
|
||||
self.device.get("materials/", on_finished = self._onGetRemoteMaterials)
|
||||
self.device.getMaterials(on_finished = self._onGetMaterials)
|
||||
|
||||
## Process the materials reply from the printer.
|
||||
#
|
||||
# \param reply The reply from the printer, a json file.
|
||||
def _onGetRemoteMaterials(self, reply: QNetworkReply) -> None:
|
||||
# Got an error from the HTTP request. If we did not receive a 200 something happened.
|
||||
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) != 200:
|
||||
Logger.log("e", "Error fetching materials from printer: %s", reply.errorString())
|
||||
return
|
||||
|
||||
# Collect materials from the printer's reply and send the missing ones if needed.
|
||||
remote_materials_by_guid = self._parseReply(reply)
|
||||
if remote_materials_by_guid:
|
||||
self._sendMissingMaterials(remote_materials_by_guid)
|
||||
## Callback for when the remote materials were returned.
|
||||
def _onGetMaterials(self, materials: List[ClusterMaterial]) -> None:
|
||||
remote_materials_by_guid = {material.guid: material for material in materials}
|
||||
self._sendMissingMaterials(remote_materials_by_guid)
|
||||
|
||||
## Determine which materials should be updated and send them to the printer.
|
||||
#
|
||||
# \param remote_materials_by_guid The remote materials by GUID.
|
||||
def _sendMissingMaterials(self, remote_materials_by_guid: Dict[str, ClusterMaterial]) -> None:
|
||||
# Collect local materials
|
||||
local_materials_by_guid = self._getLocalMaterials()
|
||||
if len(local_materials_by_guid) == 0:
|
||||
Logger.log("d", "There are no local materials to synchronize with the printer.")
|
||||
return
|
||||
|
||||
# Find out what materials are new or updated and must be sent to the printer
|
||||
material_ids_to_send = self._determineMaterialsToSend(local_materials_by_guid, remote_materials_by_guid)
|
||||
if len(material_ids_to_send) == 0:
|
||||
Logger.log("d", "There are no remote materials to update.")
|
||||
return
|
||||
|
||||
# Send materials to the printer
|
||||
self._sendMaterials(material_ids_to_send)
|
||||
|
||||
## From the local and remote materials, determine which ones should be synchronized.
|
||||
#
|
||||
# Makes a Set of id's containing only the id's of the materials that are not on the printer yet or the ones that
|
||||
# are newer in Cura.
|
||||
#
|
||||
# \param local_materials The local materials by GUID.
|
||||
# \param remote_materials The remote materials by GUID.
|
||||
@staticmethod
|
||||
def _determineMaterialsToSend(local_materials: Dict[str, LocalMaterial],
|
||||
remote_materials: Dict[str, ClusterMaterial]) -> Set[str]:
|
||||
return {
|
||||
material.id
|
||||
for guid, material in local_materials.items()
|
||||
if guid not in remote_materials or material.version > remote_materials[guid].version
|
||||
local_material.id
|
||||
for guid, local_material in local_materials.items()
|
||||
if guid not in remote_materials.keys() or local_material.version > remote_materials[guid].version
|
||||
}
|
||||
|
||||
## Send the materials to the printer.
|
||||
#
|
||||
# The given materials will be loaded from disk en sent to to printer.
|
||||
# The given id's will be matched with filenames of the locally stored materials.
|
||||
#
|
||||
# \param materials_to_send A set with id's of materials that must be sent.
|
||||
def _sendMaterials(self, materials_to_send: Set[str]) -> None:
|
||||
|
||||
# Inform the user of this process.
|
||||
MaterialSyncMessage(self.device).show()
|
||||
|
||||
container_registry = CuraApplication.getInstance().getContainerRegistry()
|
||||
material_manager = CuraApplication.getInstance().getMaterialManager()
|
||||
material_group_dict = material_manager.getAllMaterialGroups()
|
||||
|
@ -103,9 +88,7 @@ class SendMaterialJob(Job):
|
|||
self._sendMaterialFile(file_path, file_name, root_material_id)
|
||||
|
||||
## Send a single material file to the printer.
|
||||
#
|
||||
# Also add the material signature file if that is available.
|
||||
#
|
||||
# \param file_path The path of the material file.
|
||||
# \param file_name The name of the material file.
|
||||
# \param material_id The ID of the material in the file.
|
||||
|
@ -125,48 +108,27 @@ class SendMaterialJob(Job):
|
|||
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 %s with cluster.", material_id)
|
||||
# FIXME: move form posting to API client
|
||||
self.device.postFormWithParts(target = "/cluster-api/v1/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
|
||||
def sendingFinished(reply: QNetworkReply) -> None:
|
||||
def _sendingFinished(reply: QNetworkReply) -> None:
|
||||
if reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) != 200:
|
||||
Logger.log("e", "Received error code from printer when syncing material: {code}, {text}".format(
|
||||
code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute),
|
||||
text = reply.errorString()
|
||||
))
|
||||
|
||||
## Parse the reply from the printer
|
||||
#
|
||||
# Parses the reply to a "/materials" request to the printer
|
||||
#
|
||||
# \return a dictionary of ClusterMaterial objects by GUID
|
||||
# \throw KeyError Raised when on of the materials does not include a valid guid
|
||||
@classmethod
|
||||
def _parseReply(cls, reply: QNetworkReply) -> Optional[Dict[str, ClusterMaterial]]:
|
||||
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 ValueError:
|
||||
Logger.log("e", "Request material storage on printer: Printer's answer had an incorrect value.")
|
||||
except TypeError:
|
||||
Logger.log("e", "Request material storage on printer: Printer's answer was missing a required value.")
|
||||
return None
|
||||
|
||||
## Retrieves a list of local materials
|
||||
#
|
||||
# Only the new newest version of the local materials is returned
|
||||
#
|
||||
# \return a dictionary of LocalMaterial objects by GUID
|
||||
def _getLocalMaterials(self) -> Dict[str, LocalMaterial]:
|
||||
@staticmethod
|
||||
def _getLocalMaterials() -> Dict[str, LocalMaterial]:
|
||||
result = {} # type: Dict[str, LocalMaterial]
|
||||
material_manager = CuraApplication.getInstance().getMaterialManager()
|
||||
|
||||
material_group_dict = material_manager.getAllMaterialGroups()
|
||||
|
||||
# Find the latest version of all material containers in the registry.
|
Loading…
Add table
Add a link
Reference in a new issue