diff --git a/cura/API/Machines.py b/cura/API/Machines.py index f02bc1c463..4cd89420c7 100644 --- a/cura/API/Machines.py +++ b/cura/API/Machines.py @@ -25,6 +25,58 @@ class Machines(QObject): super().__init__(parent) self._application = application + @pyqtSlot(result=dict) + def getCurrentMachine(self) -> dict: + global_stack = self._application.getGlobalContainerStack() + if global_stack: + metadata = global_stack.getMetaData() + + # Since Cura doesn't have a machine class, we're going to make a fake one to make our + # lives a little bit easier. + fake_machine = { + "hostname": "", + "group_id": global_stack.getMetaDataEntry("group_id") if "group_id" in metadata else "", + "group_name": global_stack.getMetaDataEntry("group_name") if "group_name" in metadata else "", + "um_network_key": global_stack.getMetaDataEntry("um_network_key") if "um_network_key" in metadata else "", + "configuration": {} + } + return fake_machine + + ## Set the current machine's friendy name. + # This is the same as "group name" since we use "group" and "current machine" interchangeably. + # TODO: Maybe make this "friendly name" to distinguish from "hostname"? + @pyqtSlot(str) + def setCurrentMachineGroupName(self, group_name: str): + Logger.log("d", "Attempting to set the group name of the active machine to %s", group_name) + global_stack = self._application.getGlobalContainerStack() + if global_stack: + # Update a GlobalStacks in the same group with the new group name. + group_id = global_stack.getMetaDataEntry("group_id") + machine_manager = self._application.getMachineManager() + for machine in machine_manager.getMachinesInGroup(group_id): + machine.setMetaDataEntry("group_name", group_name) + + # Set the default value for "hidden", which is used when you have a group with multiple types of printers + global_stack.setMetaDataEntry("hidden", False) + + ## Set the current machine's configuration from an (optional) output device. + # If no output device is given, the first one available on the machine will be used. + # NOTE: Group and machine are used interchangeably. + @pyqtSlot(QObject) + def updateCurrentMachineConfiguration(self, output_device: Optional["PrinterOutputDevice"]) -> None: + + if output_device is None: + machine_manager = self._application.getMachineManager() + output_device = machine_manager.printerOutputDevices[0] + + hotend_ids = output_device.hotendIds + for index in range(len(hotend_ids)): + output_device.hotendIdChanged.emit(index, hotend_ids[index]) + + material_ids = output_device.materialIds + for index in range(len(material_ids)): + output_device.materialIdChanged.emit(index, material_ids[index]) + ## Add an output device to the current machine. # In practice, this means: # - Setting the output device's network key in the current machine's metadata @@ -32,33 +84,31 @@ class Machines(QObject): # types. # TODO: CHANGE TO HOSTNAME @pyqtSlot(QObject) - def addOutputDeviceToCurrentMachine(self, output_device): + def addOutputDeviceToCurrentMachine(self, output_device: "PrinterOutputDevice") -> None: if not output_device: return - Logger.log("d", "Attempting to set the network key of the active machine to %s", output_device.key) - - global_container_stack = self._application.getGlobalContainerStack() - if not global_container_stack: + global_stack = self._application.getGlobalContainerStack() + if not global_stack: return + metadata = global_stack.getMetaData() - metadata = global_container_stack.getMetaData() - - if "um_network_key" in metadata: # Global stack already had a connection, but it's changed. + # Global stack already had a connection, but it's changed. + if "um_network_key" in metadata: old_network_key = metadata["um_network_key"] + # Since we might have a bunch of hidden stacks, we also need to change it there. metadata_filter = {"um_network_key": old_network_key} containers = self._application.getContainerRegistry().findContainerStacks( type = "machine", **metadata_filter) - for container in containers: container.setMetaDataEntry("um_network_key", output_device.key) # Delete old authentication data. Logger.log("d", "Removing old authentication id %s for device %s", - global_container_stack.getMetaDataEntry("network_authentication_id", None), + global_stack.getMetaDataEntry("network_authentication_id", None), output_device.key) container.removeMetaDataEntry("network_authentication_id") @@ -69,8 +119,8 @@ class Machines(QObject): container.addConfiguredConnectionType(output_device.connectionType.value) else: # Global stack didn't have a connection yet, configure it. - global_container_stack.setMetaDataEntry("um_network_key", output_device.key) - global_container_stack.addConfiguredConnectionType(output_device.connectionType.value) + global_stack.setMetaDataEntry("um_network_key", output_device.key) + global_stack.addConfiguredConnectionType(output_device.connectionType.value) return None diff --git a/plugins/UM3NetworkPrinting/src/DiscoverUM3Action.py b/plugins/UM3NetworkPrinting/src/DiscoverUM3Action.py index 1ad3d1d96f..04f3c7f25a 100644 --- a/plugins/UM3NetworkPrinting/src/DiscoverUM3Action.py +++ b/plugins/UM3NetworkPrinting/src/DiscoverUM3Action.py @@ -108,63 +108,47 @@ class DiscoverUM3Action(MachineAction): else: return [] + # TODO: Should be able to just access the API from QML. @pyqtSlot(str) def setGroupName(self, group_name: str) -> None: - Logger.log("d", "Attempting to set the group name of the active machine to %s", group_name) - global_container_stack = self._application.getGlobalContainerStack() - if global_container_stack: - # Update a GlobalStacks in the same group with the new group name. - group_id = global_container_stack.getMetaDataEntry("group_id") - machine_manager = self._application.getMachineManager() - for machine in machine_manager.getMachinesInGroup(group_id): - machine.setMetaDataEntry("group_name", group_name) - - # Set the default value for "hidden", which is used when you have a group with multiple types of printers - global_container_stack.setMetaDataEntry("hidden", False) - + self._api.machines.setCurrentMachineGroupName(group_name) if self._network_plugin: - # Ensure that the connection states are refreshed. self._network_plugin.refreshConnections() - # Associates the currently active machine with the given printer device. The network connection information will be - # stored into the metadata of the currently active machine. + # TODO: Should be able to just access the API from QML. @pyqtSlot(QObject) def associateActiveMachineWithPrinterDevice(self, output_device: Optional["PrinterOutputDevice"]) -> None: self._api.machines.addOutputDeviceToCurrentMachine(output_device) if self._network_plugin: self._network_plugin.refreshConnections() + # TODO: Better naming needed. Stored where? This is current machine's key. + # TODO: CHANGE TO HOSTNAME + # TODO: Should be able to just access the API from QML. @pyqtSlot(result = str) def getStoredKey(self) -> str: - global_container_stack = self._application.getGlobalContainerStack() - if global_container_stack: - meta_data = global_container_stack.getMetaData() - if "um_network_key" in meta_data: - return global_container_stack.getMetaDataEntry("um_network_key") - - return "" + current_machine = self._api.machines.getCurrentMachine() + return current_machine["um_network_key"] + # TODO: CHANGE TO HOSTNAME @pyqtSlot(result = str) def getLastManualEntryKey(self) -> str: if self._network_plugin: return self._network_plugin.getLastManualDevice() return "" + # TODO: Better naming needed. Exists where? On the current machine? On all machines? + # TODO: CHANGE TO HOSTNAME @pyqtSlot(str, result = bool) def existsKey(self, key: str) -> bool: metadata_filter = {"um_network_key": key} containers = CuraContainerRegistry.getInstance().findContainerStacks(type="machine", **metadata_filter) return bool(containers) + # TODO: Should be able to just access the API from QML. @pyqtSlot() def loadConfigurationFromPrinter(self) -> None: - machine_manager = self._application.getMachineManager() - hotend_ids = machine_manager.printerOutputDevices[0].hotendIds - for index in range(len(hotend_ids)): - machine_manager.printerOutputDevices[0].hotendIdChanged.emit(index, hotend_ids[index]) - material_ids = machine_manager.printerOutputDevices[0].materialIds - for index in range(len(material_ids)): - machine_manager.printerOutputDevices[0].materialIdChanged.emit(index, material_ids[index]) + self._api.machines.updateCurrentMachineConfiguration() def _createAdditionalComponentsView(self) -> None: Logger.log("d", "Creating additional ui components for UM3.")