Cura/plugins/UM3NetworkPrinting/ClusterUM3OutputDevice.py
2017-11-20 17:00:02 +01:00

64 lines
2.8 KiB
Python

from UM.Logger import Logger
from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice
from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel
from cura.PrinterOutput.MaterialOutputModel import MaterialOutputModel
import json
from PyQt5.QtNetwork import QNetworkRequest
class ClusterUM3OutputDevice(NetworkedPrinterOutputDevice):
def __init__(self, device_id, address, properties, parent = None):
super().__init__(device_id = device_id, address = address, properties=properties, parent = parent)
self._api_prefix = "/cluster-api/v1/"
self._number_of_extruders = 2
def _update(self):
super()._update()
self._get("printers/", onFinished=self._onGetPrintersDataFinished)
def _onGetPrintersDataFinished(self, reply):
status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
if status_code == 200:
try:
result = json.loads(bytes(reply.readAll()).decode("utf-8"))
except json.decoder.JSONDecodeError:
Logger.log("w", "Received an invalid printer state message: Not valid JSON.")
return
for printer_data in result:
uuid = printer_data["uuid"]
printer = None
for device in self._printers:
if device.key == uuid:
printer = device
break
if printer is None:
printer = PrinterOutputModel(output_controller=None, number_of_extruders=self._number_of_extruders)
self._printers.append(printer)
printer.updateName(printer_data["friendly_name"])
printer.updateKey(uuid)
for index in range(0, self._number_of_extruders):
extruder = printer.extruders[index]
extruder_data = printer_data["configuration"][index]
try:
hotend_id = extruder_data["print_core_id"]
except KeyError:
hotend_id = ""
extruder.updateHotendID(hotend_id)
material_data = extruder_data["material"]
if extruder.activeMaterial is None or extruder.activeMaterial.guid != material_data["guid"]:
material = MaterialOutputModel(guid = material_data["guid"], type = material_data["material"], brand=material_data["brand"], color=material_data["color"])
extruder.updateActiveMaterial(material)
else:
Logger.log("w",
"Got status code {status_code} while trying to get printer data".format(status_code=status_code))