WIP: Implement add machine from network device

This commit is contained in:
Lipu Fei 2019-03-13 12:04:52 +01:00
parent f7f5123fea
commit de9f6f47bd
5 changed files with 80 additions and 8 deletions

View file

@ -1,3 +1,5 @@
from typing import Callable, List, Optional, TYPE_CHECKING
from PyQt5.QtCore import pyqtSlot, pyqtProperty, pyqtSignal, QObject
@ -13,6 +15,7 @@ class DiscoveredPrinter(QObject):
def __init__(self, ip_address: str, key: str, name: str, create_callback: Callable[[str], None], machine_type: str,
device, parent = None) -> None:
super().__init__(parent)
self._ip_address = ip_address
self._key = key
self._name = name
@ -40,10 +43,11 @@ class DiscoveredPrinter(QObject):
def machine_type(self) -> str:
return self._machine_type
def setMachineType(self, machine_type: str) -> None:
if self._machine_type != machine_type:
self._machine_type = machine_type
self.machineTypeChanged.emit()
# Machine type string with underscores "_" replaced with spaces " "
@pyqtProperty(str, notify = machineTypeChanged)
def machine_type_with_spaces(self) -> str:
from cura.CuraApplication import CuraApplication
return CuraApplication.getInstance().getMachineManager().getMachineTypeNameFromId(self._machine_type)
@pyqtProperty(QObject, constant = True)
def device(self):

View file

@ -1657,3 +1657,47 @@ class MachineManager(QObject):
abbr_machine += stripped_word
return abbr_machine
def getMachineTypeNameFromId(self, machine_type_id: str) -> str:
machine_type_name = ""
results = self._container_registry.findDefinitionContainersMetadata(id = machine_type_id)
if results:
machine_type_name = results[0]["name"]
return machine_type_name
@pyqtSlot(QObject)
def associateActiveMachineWithPrinterDevice(self, printer_device: Optional["PrinterOutputDevice"]) -> None:
if not printer_device:
return
Logger.log("d", "Attempting to set the network key of the active machine to %s", printer_device.key)
global_stack = self._global_container_stack
if not global_stack:
return
meta_data = global_stack.getMetaData()
if "um_network_key" in meta_data: # Global stack already had a connection, but it's changed.
old_network_key = meta_data["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._container_registry.findContainerStacks(type = "machine", **metadata_filter)
for container in containers:
container.setMetaDataEntry("um_network_key", printer_device.key)
# Delete old authentication data.
Logger.log("d", "Removing old authentication id %s for device %s",
global_stack.getMetaDataEntry("network_authentication_id", None),
printer_device.key)
container.removeMetaDataEntry("network_authentication_id")
container.removeMetaDataEntry("network_authentication_key")
# Ensure that these containers do know that they are configured for network connection
container.addConfiguredConnectionType(printer_device.connectionType.value)
else: # Global stack didn't have a connection yet, configure it.
global_stack.setMetaDataEntry("um_network_key", printer_device.key)
global_stack.addConfiguredConnectionType(printer_device.connectionType.value)