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:
META_CLUSTER_ID = "um_cloud_cluster_id"
META_NETWORK_KEY = "um_network_key"
# The interval with which the remote clusters are checked
CHECK_CLUSTER_INTERVAL = 30.0 # seconds
@ -125,9 +126,8 @@ class CloudOutputDeviceManager:
self._connectToActiveMachine()
def _createMachineFromDiscoveredDevice(self, key: str) -> None:
device = self._remote_clusters[key] # type: CloudOutputDevice
device = self._remote_clusters[key]
if not device:
Logger.log("e", "Could not find discovered device with key [%s]", key)
return
# The newly added machine is automatically activated.
@ -145,32 +145,19 @@ class CloudOutputDeviceManager:
if not active_machine:
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)
if stored_cluster_id in self._remote_clusters:
device = self._remote_clusters[stored_cluster_id]
local_network_key = active_machine.getMetaDataEntry(self.META_NETWORK_KEY)
for device in self._remote_clusters.values():
if device.key == stored_cluster_id:
# Connect to it if the stored ID matches.
self._connectToOutputDevice(device, 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.
active_machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key)
self._connectToOutputDevice(device, active_machine)
Logger.log("d", "Device connected by metadata cluster ID %s", stored_cluster_id)
else:
self._connectByNetworkKey(active_machine)
## Tries to match the local network key to the cloud cluster host name.
def _connectByNetworkKey(self, active_machine: GlobalStack) -> None:
local_network_key = active_machine.getMetaDataEntry("um_network_key")
if not local_network_key:
return
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)
# Remove device if it is not meant for the active machine.
CuraApplication.getInstance().getOutputDeviceManager().removeOutputDevice(device.key)
## Connects to an output device and makes sure it is registered in the output device manager.
@staticmethod

View file

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