diff --git a/cura/API/Machines.py b/cura/API/Machines.py new file mode 100644 index 0000000000..f02bc1c463 --- /dev/null +++ b/cura/API/Machines.py @@ -0,0 +1,76 @@ +# Copyright (c) 2019 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +from typing import Optional, Dict, TYPE_CHECKING +from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty +from UM.i18n import i18nCatalog +from UM.Logger import Logger +if TYPE_CHECKING: + from cura.CuraApplication import CuraApplication + +i18n_catalog = i18nCatalog("cura") + +## The account API provides a version-proof bridge to use Ultimaker Accounts +# +# Usage: +# ``` +# from cura.API import CuraAPI +# api = CuraAPI() +# api.machines.addOutputDeviceToCurrentMachine() +# ``` +# +class Machines(QObject): + + def __init__(self, application: "CuraApplication", parent = None) -> None: + super().__init__(parent) + self._application = application + + ## 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 + # - Adding the output device's connection type to the current machine's configured connection + # types. + # TODO: CHANGE TO HOSTNAME + @pyqtSlot(QObject) + def addOutputDeviceToCurrentMachine(self, output_device): + 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: + return + + metadata = global_container_stack.getMetaData() + + if "um_network_key" in metadata: # Global stack already had a connection, but it's changed. + 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), + output_device.key) + + container.removeMetaDataEntry("network_authentication_id") + container.removeMetaDataEntry("network_authentication_key") + + # Ensure that these containers do know that they are configured for the given + # connection type (can be more than one type; e.g. LAN & Cloud) + 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) + + return None + diff --git a/cura/API/__init__.py b/cura/API/__init__.py index b3e702263a..8e73680dfe 100644 --- a/cura/API/__init__.py +++ b/cura/API/__init__.py @@ -6,6 +6,7 @@ from PyQt5.QtCore import QObject, pyqtProperty from cura.API.Backups import Backups from cura.API.Interface import Interface +from cura.API.Machines import Machines from cura.API.Account import Account if TYPE_CHECKING: @@ -44,6 +45,9 @@ class CuraAPI(QObject): # Backups API self._backups = Backups(self._application) + # Machines API + self._machines = Machines(self._application) + # Interface API self._interface = Interface(self._application) @@ -58,6 +62,10 @@ class CuraAPI(QObject): def backups(self) -> "Backups": return self._backups + @property + def machines(self) -> "Machines": + return self._machines + @property def interface(self) -> "Interface": return self._interface