Add more endpoints to machines API

- getCurrentMachine
- setCurrentMachineGroupName
- updateCurrentMachineConfiguration

Also shortened `global_container_stack` to `global_stack` since every stack is a container stack.

Contributes to CL-1331
This commit is contained in:
Ian Paschal 2019-05-09 16:01:44 +02:00
parent 20a9b65ea5
commit 26ab359a93
2 changed files with 75 additions and 41 deletions

View file

@ -25,6 +25,58 @@ class Machines(QObject):
super().__init__(parent) super().__init__(parent)
self._application = application 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. ## Add an output device to the current machine.
# In practice, this means: # In practice, this means:
# - Setting the output device's network key in the current machine's metadata # - Setting the output device's network key in the current machine's metadata
@ -32,33 +84,31 @@ class Machines(QObject):
# types. # types.
# TODO: CHANGE TO HOSTNAME # TODO: CHANGE TO HOSTNAME
@pyqtSlot(QObject) @pyqtSlot(QObject)
def addOutputDeviceToCurrentMachine(self, output_device): def addOutputDeviceToCurrentMachine(self, output_device: "PrinterOutputDevice") -> None:
if not output_device: if not output_device:
return return
Logger.log("d", Logger.log("d",
"Attempting to set the network key of the active machine to %s", "Attempting to set the network key of the active machine to %s",
output_device.key) output_device.key)
global_stack = self._application.getGlobalContainerStack()
global_container_stack = self._application.getGlobalContainerStack() if not global_stack:
if not global_container_stack:
return return
metadata = global_stack.getMetaData()
metadata = global_container_stack.getMetaData() # Global stack already had a connection, but it's changed.
if "um_network_key" in metadata:
if "um_network_key" in metadata: # Global stack already had a connection, but it's changed.
old_network_key = metadata["um_network_key"] old_network_key = metadata["um_network_key"]
# Since we might have a bunch of hidden stacks, we also need to change it there. # Since we might have a bunch of hidden stacks, we also need to change it there.
metadata_filter = {"um_network_key": old_network_key} metadata_filter = {"um_network_key": old_network_key}
containers = self._application.getContainerRegistry().findContainerStacks( containers = self._application.getContainerRegistry().findContainerStacks(
type = "machine", **metadata_filter) type = "machine", **metadata_filter)
for container in containers: for container in containers:
container.setMetaDataEntry("um_network_key", output_device.key) container.setMetaDataEntry("um_network_key", output_device.key)
# Delete old authentication data. # Delete old authentication data.
Logger.log("d", "Removing old authentication id %s for device %s", 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) output_device.key)
container.removeMetaDataEntry("network_authentication_id") container.removeMetaDataEntry("network_authentication_id")
@ -69,8 +119,8 @@ class Machines(QObject):
container.addConfiguredConnectionType(output_device.connectionType.value) container.addConfiguredConnectionType(output_device.connectionType.value)
else: # Global stack didn't have a connection yet, configure it. else: # Global stack didn't have a connection yet, configure it.
global_container_stack.setMetaDataEntry("um_network_key", output_device.key) global_stack.setMetaDataEntry("um_network_key", output_device.key)
global_container_stack.addConfiguredConnectionType(output_device.connectionType.value) global_stack.addConfiguredConnectionType(output_device.connectionType.value)
return None return None

View file

@ -108,63 +108,47 @@ class DiscoverUM3Action(MachineAction):
else: else:
return [] return []
# TODO: Should be able to just access the API from QML.
@pyqtSlot(str) @pyqtSlot(str)
def setGroupName(self, group_name: str) -> None: def setGroupName(self, group_name: str) -> None:
Logger.log("d", "Attempting to set the group name of the active machine to %s", group_name) self._api.machines.setCurrentMachineGroupName(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)
if self._network_plugin: if self._network_plugin:
# Ensure that the connection states are refreshed.
self._network_plugin.refreshConnections() self._network_plugin.refreshConnections()
# Associates the currently active machine with the given printer device. The network connection information will be # TODO: Should be able to just access the API from QML.
# stored into the metadata of the currently active machine.
@pyqtSlot(QObject) @pyqtSlot(QObject)
def associateActiveMachineWithPrinterDevice(self, output_device: Optional["PrinterOutputDevice"]) -> None: def associateActiveMachineWithPrinterDevice(self, output_device: Optional["PrinterOutputDevice"]) -> None:
self._api.machines.addOutputDeviceToCurrentMachine(output_device) self._api.machines.addOutputDeviceToCurrentMachine(output_device)
if self._network_plugin: if self._network_plugin:
self._network_plugin.refreshConnections() 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) @pyqtSlot(result = str)
def getStoredKey(self) -> str: def getStoredKey(self) -> str:
global_container_stack = self._application.getGlobalContainerStack() current_machine = self._api.machines.getCurrentMachine()
if global_container_stack: return current_machine["um_network_key"]
meta_data = global_container_stack.getMetaData()
if "um_network_key" in meta_data:
return global_container_stack.getMetaDataEntry("um_network_key")
return ""
# TODO: CHANGE TO HOSTNAME
@pyqtSlot(result = str) @pyqtSlot(result = str)
def getLastManualEntryKey(self) -> str: def getLastManualEntryKey(self) -> str:
if self._network_plugin: if self._network_plugin:
return self._network_plugin.getLastManualDevice() return self._network_plugin.getLastManualDevice()
return "" return ""
# TODO: Better naming needed. Exists where? On the current machine? On all machines?
# TODO: CHANGE TO HOSTNAME
@pyqtSlot(str, result = bool) @pyqtSlot(str, result = bool)
def existsKey(self, key: str) -> bool: def existsKey(self, key: str) -> bool:
metadata_filter = {"um_network_key": key} metadata_filter = {"um_network_key": key}
containers = CuraContainerRegistry.getInstance().findContainerStacks(type="machine", **metadata_filter) containers = CuraContainerRegistry.getInstance().findContainerStacks(type="machine", **metadata_filter)
return bool(containers) return bool(containers)
# TODO: Should be able to just access the API from QML.
@pyqtSlot() @pyqtSlot()
def loadConfigurationFromPrinter(self) -> None: def loadConfigurationFromPrinter(self) -> None:
machine_manager = self._application.getMachineManager() self._api.machines.updateCurrentMachineConfiguration()
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])
def _createAdditionalComponentsView(self) -> None: def _createAdditionalComponentsView(self) -> None:
Logger.log("d", "Creating additional ui components for UM3.") Logger.log("d", "Creating additional ui components for UM3.")