def _createMaterialOutputModel( self, material_data: Dict[str, Any]) -> "MaterialOutputModel": material_manager = self._application.getMaterialManager() material_group_list = None # Avoid crashing if there is no "guid" field in the metadata material_guid = material_data.get("guid") if material_guid: material_group_list = material_manager.getMaterialGroupListByGUID( material_guid) # This can happen if the connected machine has no material in one or more extruders (if GUID is empty), or the # material is unknown to Cura, so we should return an "empty" or "unknown" material model. if material_group_list is None: material_name = i18n_catalog.i18nc("@label:material", "Empty") if len(material_data.get("guid", "")) == 0 \ else i18n_catalog.i18nc("@label:material", "Unknown") return MaterialOutputModel(guid=material_data.get("guid", ""), type=material_data.get("material", ""), color=material_data.get("color", ""), brand=material_data.get("brand", ""), name=material_data.get( "name", material_name)) # 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=material_data["guid"])) color = material_data["color"] brand = material_data["brand"] material_type = material_data["material"] name = i18n_catalog.i18nc("@label:material", "Empty") if material_data["material"] == "empty" \ else i18n_catalog.i18nc("@label:material", "Unknown") return MaterialOutputModel(guid=material_data["guid"], type=material_type, brand=brand, color=color, name=name)
def _updatePrinter(self, printer: PrinterOutputModel, data: Dict[str, Any]) -> None: # For some unknown reason the cluster wants UUID for everything, except for sending a job directly to a printer. # Then we suddenly need the unique name. So in order to not have to mess up all the other code, we save a mapping. self._printer_uuid_to_unique_name_mapping[data["uuid"]] = data["unique_name"] definitions = ContainerRegistry.getInstance().findDefinitionContainers(name = data["machine_variant"]) if not definitions: Logger.log("w", "Unable to find definition for machine variant %s", data["machine_variant"]) return machine_definition = definitions[0] printer.updateName(data["friendly_name"]) printer.updateKey(data["uuid"]) printer.updateType(data["machine_variant"]) # Do not store the build plate information that comes from connect if the current printer has not build plate information if "build_plate" in data and machine_definition.getMetaDataEntry("has_variant_buildplates", False): printer.updateBuildplateName(data["build_plate"]["type"]) if not data["enabled"]: printer.updateState("disabled") else: printer.updateState(data["status"]) for index in range(0, self._number_of_extruders): extruder = printer.extruders[index] try: extruder_data = data["configuration"][index] except IndexError: break extruder.updateHotendID(extruder_data.get("print_core_id", "")) material_data = extruder_data["material"] if extruder.activeMaterial is None or extruder.activeMaterial.guid != material_data["guid"]: containers = ContainerRegistry.getInstance().findInstanceContainers(type="material", GUID=material_data["guid"]) if containers: color = containers[0].getMetaDataEntry("color_code") brand = containers[0].getMetaDataEntry("brand") material_type = containers[0].getMetaDataEntry("material") name = containers[0].getName() else: Logger.log("w", "Unable to find material with guid {guid}. Using data as provided by cluster".format( guid=material_data["guid"])) color = material_data["color"] brand = material_data["brand"] material_type = material_data["material"] name = "Empty" if material_data["material"] == "empty" else "Unknown" material = MaterialOutputModel(guid=material_data["guid"], type=material_type, brand=brand, color=color, name=name) extruder.updateActiveMaterial(material)
def _updatePrinter(self, printer, data): # For some unknown reason the cluster wants UUID for everything, except for sending a job directly to a printer. # Then we suddenly need the unique name. So in order to not have to mess up all the other code, we save a mapping. self._printer_uuid_to_unique_name_mapping[ data["uuid"]] = data["unique_name"] printer.updateName(data["friendly_name"]) printer.updateKey(data["uuid"]) printer.updateType(data["machine_variant"]) if not data["enabled"]: printer.updateState("disabled") else: printer.updateState(data["status"]) for index in range(0, self._number_of_extruders): extruder = printer.extruders[index] try: extruder_data = data["configuration"][index] except IndexError: break extruder.updateHotendID(extruder_data.get("print_core_id", "")) material_data = extruder_data["material"] if extruder.activeMaterial is None or extruder.activeMaterial.guid != material_data[ "guid"]: containers = ContainerRegistry.getInstance( ).findInstanceContainers(type="material", GUID=material_data["guid"]) if containers: color = containers[0].getMetaDataEntry("color_code") brand = containers[0].getMetaDataEntry("brand") material_type = containers[0].getMetaDataEntry("material") name = containers[0].getName() else: Logger.log( "w", "Unable to find material with guid {guid}. Using data as provided by cluster" .format(guid=material_data["guid"])) color = material_data["color"] brand = material_data["brand"] material_type = material_data["material"] name = "Unknown" material = MaterialOutputModel(guid=material_data["guid"], type=material_type, brand=brand, color=color, name=name) extruder.updateActiveMaterial(material)
def _createMaterialOutputModel( self, material_data: Dict[str, Any]) -> "MaterialOutputModel": material_manager = CuraApplication.getInstance().getMaterialManager() material_group_list = material_manager.getMaterialGroupListByGUID( material_data["guid"]) # 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=material_data["guid"])) color = material_data["color"] brand = material_data["brand"] material_type = material_data["material"] name = "Empty" if material_data[ "material"] == "empty" else "Unknown" return MaterialOutputModel(guid=material_data["guid"], type=material_type, brand=brand, color=color, name=name)
def _createMaterialOutputModel(self, material_data) -> MaterialOutputModel: containers = ContainerRegistry.getInstance().findInstanceContainers( type="material", GUID=material_data["guid"]) if containers: color = containers[0].getMetaDataEntry("color_code") brand = containers[0].getMetaDataEntry("brand") material_type = containers[0].getMetaDataEntry("material") name = containers[0].getName() else: Logger.log( "w", "Unable to find material with guid {guid}. Using data as provided by cluster" .format(guid=material_data["guid"])) color = material_data["color"] brand = material_data["brand"] material_type = material_data["material"] name = "Empty" if material_data[ "material"] == "empty" else "Unknown" return MaterialOutputModel(guid=material_data["guid"], type=material_type, brand=brand, color=color, name=name)
def _onGetPrinterDataFinished(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 if not self._printers: # Quickest way to get the firmware version is to grab it from the zeroconf. firmware_version = self._properties.get(b"firmware_version", b"").decode("utf-8") self._printers = [PrinterOutputModel(output_controller=self._output_controller, number_of_extruders=self._number_of_extruders, firmware_version=firmware_version)] self._printers[0].setCamera(NetworkCamera("http://" + self._address + ":8080/?action=stream")) for extruder in self._printers[0].extruders: extruder.activeMaterialChanged.connect(self.materialIdChanged) extruder.hotendIDChanged.connect(self.hotendIdChanged) self.printersChanged.emit() # LegacyUM3 always has a single printer. printer = self._printers[0] printer.updateBedTemperature(result["bed"]["temperature"]["current"]) printer.updateTargetBedTemperature(result["bed"]["temperature"]["target"]) printer.updateState(result["status"]) try: # If we're still handling the request, we should ignore remote for a bit. if not printer.getController().isPreheatRequestInProgress(): printer.updateIsPreheating(result["bed"]["pre_heat"]["active"]) except KeyError: # Older firmwares don't support preheating, so we need to fake it. pass head_position = result["heads"][0]["position"] printer.updateHeadPosition(head_position["x"], head_position["y"], head_position["z"]) for index in range(0, self._number_of_extruders): temperatures = result["heads"][0]["extruders"][index]["hotend"]["temperature"] extruder = printer.extruders[index] extruder.updateTargetHotendTemperature(temperatures["target"]) extruder.updateHotendTemperature(temperatures["current"]) material_guid = result["heads"][0]["extruders"][index]["active_material"]["guid"] if extruder.activeMaterial is None or extruder.activeMaterial.guid != material_guid: # Find matching material (as we need to set brand, type & color) containers = ContainerRegistry.getInstance().findInstanceContainers(type="material", GUID=material_guid) if containers: color = containers[0].getMetaDataEntry("color_code") brand = containers[0].getMetaDataEntry("brand") material_type = containers[0].getMetaDataEntry("material") name = containers[0].getName() else: # Unknown material. color = "#00000000" brand = "Unknown" material_type = "Unknown" name = "Unknown" material = MaterialOutputModel(guid=material_guid, type=material_type, brand=brand, color=color, name = name) extruder.updateActiveMaterial(material) try: hotend_id = result["heads"][0]["extruders"][index]["hotend"]["id"] except KeyError: hotend_id = "" printer.extruders[index].updateHotendID(hotend_id) else: Logger.log("w", "Got status code {status_code} while trying to get printer data".format(status_code = status_code))