From 8abaf69873bfc8b1d285b26fd803481356070465 Mon Sep 17 00:00:00 2001 From: Kostas Karmas Date: Wed, 17 Jun 2020 16:11:18 +0200 Subject: Update CloudOutputDevice of the machine when it is rediscovered Even though the metadata of the machine was updated when it was rediscovered, the new cluster id was not updated in all its references (such as in the _remote_clusters and in the _um_cloud_printers dicts). This commit fixes that issue by properly updating all the entries that depend on the machine's cluster id. CURA-7505 --- .../src/Cloud/CloudOutputDeviceManager.py | 32 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'plugins/UM3NetworkPrinting') diff --git a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py index e473ae7cc3..bde324df24 100644 --- a/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py +++ b/plugins/UM3NetworkPrinting/src/Cloud/CloudOutputDeviceManager.py @@ -174,12 +174,13 @@ class CloudOutputDeviceManager: for cluster_data in clusters: device = CloudOutputDevice(self._api, cluster_data) + # If the machine already existed before, it will be present in the host_guid_map if cluster_data.host_guid in host_guid_map: machine = machine_manager.getMachine(device.printerType, {self.META_HOST_GUID: cluster_data.host_guid}) - # Update the META_CLUSTER_ID of the machine in case it has been changed (e.g. if the printer was - # removed and re-added to the account). if machine and machine.getMetaDataEntry(self.META_CLUSTER_ID) != device.key: - machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key) + # If the retrieved device has a different cluster_id than the existing machine, bring the existing + # machine up-to-date. + self._updateOutdatedMachine(outdated_machine = machine, new_cloud_output_device = device) # Create a machine if we don't already have it. Do not make it the active machine. # We only need to add it if it wasn't already added by "local" network or by cloud. @@ -266,6 +267,31 @@ class CloudOutputDeviceManager: ) message.setText(message_text) + def _updateOutdatedMachine(self, outdated_machine: GlobalStack, new_cloud_output_device: CloudOutputDevice) -> None: + """ + Update the cloud metadata of a pre-existing machine that is rediscovered (e.g. if the printer was removed and + re-added to the account) and delete the old CloudOutputDevice related to this machine. + + :param outdated_machine: The cloud machine that needs to be brought up-to-date with the new data received from + the account + :param new_cloud_output_device: The new CloudOutputDevice that should be linked to the pre-existing machine + :return: None + """ + old_cluster_id = outdated_machine.getMetaDataEntry(self.META_CLUSTER_ID) + outdated_machine.setMetaDataEntry(self.META_CLUSTER_ID, new_cloud_output_device.key) + outdated_machine.setMetaDataEntry(META_UM_LINKED_TO_ACCOUNT, True) + # Cleanup the remainings of the old CloudOutputDevice(old_cluster_id) + self._um_cloud_printers[new_cloud_output_device.key] = self._um_cloud_printers.pop(old_cluster_id) + output_device_manager = CuraApplication.getInstance().getOutputDeviceManager() + if old_cluster_id in output_device_manager.getOutputDeviceIds(): + output_device_manager.removeOutputDevice(old_cluster_id) + if old_cluster_id in self._remote_clusters: + # We need to close the device so that it stops checking for its status + self._remote_clusters[old_cluster_id].close() + del self._remote_clusters[old_cluster_id] + self._remote_clusters[new_cloud_output_device.key] = new_cloud_output_device + + def _devicesRemovedFromAccount(self, removed_device_ids: Set[str]) -> None: """ Removes the CloudOutputDevice from the received device ids and marks the specific printers as "removed from -- cgit v1.2.3