Simplify output device cleanup code

This commit is contained in:
ChrisTerBeke 2019-07-30 19:29:35 +02:00
parent c1b5cce064
commit 72ac8b5f4c
No known key found for this signature in database
GPG key ID: A49F1AB9D7E0C263
2 changed files with 30 additions and 45 deletions

View file

@ -25,6 +25,7 @@ from ..Models.Http.CloudError import CloudError
class CloudOutputDeviceManager: class CloudOutputDeviceManager:
META_CLUSTER_ID = "um_cloud_cluster_id" META_CLUSTER_ID = "um_cloud_cluster_id"
META_NETWORK_KEY = "um_network_key"
# The interval with which the remote clusters are checked # The interval with which the remote clusters are checked
CHECK_CLUSTER_INTERVAL = 30.0 # seconds CHECK_CLUSTER_INTERVAL = 30.0 # seconds
@ -125,9 +126,8 @@ class CloudOutputDeviceManager:
self._connectToActiveMachine() self._connectToActiveMachine()
def _createMachineFromDiscoveredDevice(self, key: str) -> None: def _createMachineFromDiscoveredDevice(self, key: str) -> None:
device = self._remote_clusters[key] # type: CloudOutputDevice device = self._remote_clusters[key]
if not device: if not device:
Logger.log("e", "Could not find discovered device with key [%s]", key)
return return
# The newly added machine is automatically activated. # The newly added machine is automatically activated.
@ -145,32 +145,19 @@ class CloudOutputDeviceManager:
if not active_machine: if not active_machine:
return return
# Remove all output devices that we have registered.
# This is needed because when we switch we can only leave output devices that are meant for that machine.
for device_id in self._remote_clusters:
CuraApplication.getInstance().getOutputDeviceManager().removeOutputDevice(device_id)
# Check if the stored cluster_id for the active machine is in our list of remote clusters.
stored_cluster_id = active_machine.getMetaDataEntry(self.META_CLUSTER_ID) stored_cluster_id = active_machine.getMetaDataEntry(self.META_CLUSTER_ID)
if stored_cluster_id in self._remote_clusters: local_network_key = active_machine.getMetaDataEntry(self.META_NETWORK_KEY)
device = self._remote_clusters[stored_cluster_id] for device in self._remote_clusters.values():
self._connectToOutputDevice(device, active_machine) if device.key == stored_cluster_id:
Logger.log("d", "Device connected by metadata cluster ID %s", stored_cluster_id) # Connect to it if the stored ID matches.
else: self._connectToOutputDevice(device, active_machine)
self._connectByNetworkKey(active_machine) elif local_network_key and device.matchesNetworkKey(local_network_key):
# Connect to it if we can match the local network key that was already present.
## Tries to match the local network key to the cloud cluster host name. active_machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key)
def _connectByNetworkKey(self, active_machine: GlobalStack) -> None: self._connectToOutputDevice(device, active_machine)
local_network_key = active_machine.getMetaDataEntry("um_network_key") else:
if not local_network_key: # Remove device if it is not meant for the active machine.
return CuraApplication.getInstance().getOutputDeviceManager().removeOutputDevice(device.key)
device = next((c for c in self._remote_clusters.values() if c.matchesNetworkKey(local_network_key)), None)
if not device:
return
Logger.log("i", "Found cluster %s with network key %s", device, local_network_key)
# TODO: fix this
# active_machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key)
# self._connectToOutputDevice(device, active_machine)
## Connects to an output device and makes sure it is registered in the output device manager. ## Connects to an output device and makes sure it is registered in the output device manager.
@staticmethod @staticmethod

View file

@ -18,6 +18,8 @@ from .NetworkOutputDevice import NetworkOutputDevice
## The NetworkOutputDeviceManager is responsible for discovering and managing local networked clusters. ## The NetworkOutputDeviceManager is responsible for discovering and managing local networked clusters.
class NetworkOutputDeviceManager: class NetworkOutputDeviceManager:
META_NETWORK_KEY = "um_network_key"
MANUAL_DEVICES_PREFERENCE_KEY = "um3networkprinting/manual_instances" MANUAL_DEVICES_PREFERENCE_KEY = "um3networkprinting/manual_instances"
MIN_SUPPORTED_CLUSTER_VERSION = Version("4.0.0") MIN_SUPPORTED_CLUSTER_VERSION = Version("4.0.0")
@ -72,9 +74,8 @@ class NetworkOutputDeviceManager:
b"incomplete": b"true", b"incomplete": b"true",
b"temporary": b"true" b"temporary": b"true"
}) })
self._checkManualDevice(address, lambda status_code, response: self._onCheckManualDeviceResponse(
response_callback = lambda status_code, response: self._onCheckManualDeviceResponse(status_code, address) status_code, address))
self._checkManualDevice(address, response_callback)
## Remove a manually added networked printer. ## Remove a manually added networked printer.
def removeManualDevice(self, device_id: str, address: Optional[str] = None) -> None: def removeManualDevice(self, device_id: str, address: Optional[str] = None) -> None:
@ -102,17 +103,14 @@ class NetworkOutputDeviceManager:
if not active_machine: if not active_machine:
return return
# Remove all output devices that we have registered. stored_device_id = active_machine.getMetaDataEntry(self.META_NETWORK_KEY)
# This is needed because when we switch we can only leave output devices that are meant for that machine. for device in self._discovered_devices.values():
for device_id in self._discovered_devices: if device.key == stored_device_id:
CuraApplication.getInstance().getOutputDeviceManager().removeOutputDevice(device_id) # Connect to it if the stored key matches.
self._connectToOutputDevice(device, active_machine)
# Check if the stored network key for the active machine is in our list of discovered devices. else:
stored_network_key = active_machine.getMetaDataEntry("um_network_key") # Remove device if it is not meant for the active machine.
if stored_network_key in self._discovered_devices: CuraApplication.getInstance().getOutputDeviceManager().removeOutputDevice(device.key)
device = self._discovered_devices[stored_network_key]
self._connectToOutputDevice(device, active_machine)
Logger.log("d", "Device connected by metadata network key %s", stored_network_key)
## Add a device to the current active machine. ## Add a device to the current active machine.
@staticmethod @staticmethod
@ -191,16 +189,16 @@ class NetworkOutputDeviceManager:
def _createMachineFromDiscoveredDevice(self, device_id: str) -> None: def _createMachineFromDiscoveredDevice(self, device_id: str) -> None:
device = self._discovered_devices.get(device_id) device = self._discovered_devices.get(device_id)
if device is None: if device is None:
Logger.log("e", "Could not find discovered device with device_id [%s]", device_id)
return return
# The newly added machine is automatically activated. # The newly added machine is automatically activated.
CuraApplication.getInstance().getMachineManager().addMachine(device.printerType, device.name) CuraApplication.getInstance().getMachineManager().addMachine(device.printerType, device.name)
active_machine = CuraApplication.getInstance().getGlobalContainerStack() active_machine = CuraApplication.getInstance().getGlobalContainerStack()
active_machine.setMetaDataEntry("um_network_key", device.key) if not active_machine:
return
active_machine.setMetaDataEntry(self.META_NETWORK_KEY, device.key)
active_machine.setMetaDataEntry("group_name", device.name) active_machine.setMetaDataEntry("group_name", device.name)
if active_machine: self._connectToOutputDevice(device, active_machine)
self._connectToOutputDevice(device, active_machine)
## Load the user-configured manual devices from Cura preferences. ## Load the user-configured manual devices from Cura preferences.
def _getStoredManualInstances(self) -> Dict[str, Optional[Callable]]: def _getStoredManualInstances(self) -> Dict[str, Optional[Callable]]: