Add CloudOutputDeviceManager, test implementation

This commit is contained in:
ChrisTerBeke 2018-11-19 21:59:57 +01:00
parent 115936c46b
commit 228325eb89
No known key found for this signature in database
GPG key ID: A49F1AB9D7E0C263
5 changed files with 78 additions and 17 deletions

View file

@ -1,11 +1,12 @@
# Copyright (c) 2017 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from typing import TYPE_CHECKING
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from UM.Logger import Logger
from UM.Application import Application
from UM.Signal import Signal, signalemitter
from UM.Version import Version
from plugins.UM3NetworkPrinting.src.Cloud.CloudOutputDeviceManager import CloudOutputDeviceManager
from . import ClusterUM3OutputDevice, LegacyUM3OutputDevice
@ -19,6 +20,9 @@ from time import time
import json
if TYPE_CHECKING:
from cura.CuraApplication import CuraApplication
## This plugin handles the connection detection & creation of output device objects for the UM3 printer.
# Zero-Conf is used to detect printers, which are saved in a dict.
@ -29,8 +33,10 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
removeDeviceSignal = Signal()
discoveredDevicesChanged = Signal()
def __init__(self):
def __init__(self, application: "CuraApplication"):
super().__init__()
self._application = application
self._zero_conf = None
self._zero_conf_browser = None
@ -38,7 +44,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self.addDeviceSignal.connect(self._onAddDevice)
self.removeDeviceSignal.connect(self._onRemoveDevice)
Application.getInstance().globalContainerStackChanged.connect(self.reCheckConnections)
application.globalContainerStackChanged.connect(self.reCheckConnections)
self._discovered_devices = {}
@ -53,7 +59,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self._cluster_api_prefix = "/cluster-api/v" + self._cluster_api_version + "/"
# Get list of manual instances from preferences
self._preferences = Application.getInstance().getPreferences()
self._preferences = self._application.getPreferences()
self._preferences.addPreference("um3networkprinting/manual_instances",
"") # A comma-separated list of ip adresses or hostnames
@ -70,6 +76,9 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self._service_changed_request_event = Event()
self._service_changed_request_thread = Thread(target=self._handleOnServiceChangedRequests, daemon=True)
self._service_changed_request_thread.start()
# Create a cloud output device manager that abstract all cloud connection logic away.
self._cloud_output_device_manager = CloudOutputDeviceManager(self._application)
def getDiscoveredDevices(self):
return self._discovered_devices
@ -104,7 +113,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self.resetLastManualDevice()
def reCheckConnections(self):
active_machine = Application.getInstance().getGlobalContainerStack()
active_machine = self._application.getGlobalContainerStack()
if not active_machine:
return
@ -129,7 +138,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
return
if self._discovered_devices[key].isConnected():
# Sometimes the status changes after changing the global container and maybe the device doesn't belong to this machine
um_network_key = Application.getInstance().getGlobalContainerStack().getMetaDataEntry("um_network_key")
um_network_key = self._application.getGlobalContainerStack().getMetaDataEntry("um_network_key")
if key == um_network_key:
self.getOutputDeviceManager().addOutputDevice(self._discovered_devices[key])
else:
@ -281,7 +290,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self._discovered_devices[device.getId()] = device
self.discoveredDevicesChanged.emit()
global_container_stack = Application.getInstance().getGlobalContainerStack()
global_container_stack = self._application.getGlobalContainerStack()
if global_container_stack and device.getId() == global_container_stack.getMetaDataEntry("um_network_key"):
device.connect()
device.connectionStateChanged.connect(self._onDeviceConnectionStateChanged)
@ -299,7 +308,7 @@ class UM3OutputDevicePlugin(OutputDevicePlugin):
self._service_changed_request_event.wait(timeout = 5.0)
# Stop if the application is shutting down
if Application.getInstance().isShuttingDown():
if self._application.isShuttingDown():
return
self._service_changed_request_event.clear()