UM3PrinterAction - refactor to property, remove discovery start from reset

This commit is contained in:
ChrisTerBeke 2019-08-15 14:30:11 +02:00
parent ab4fd3a7f9
commit 6833323845

View File

@ -18,7 +18,7 @@ I18N_CATALOG = i18nCatalog("cura")
## Machine action that allows to connect the active machine to a networked devices. ## Machine action that allows to connect the active machine to a networked devices.
# TODO: in the future this should be part of the new discovery workflow baked into Cura. # TODO: in the future this should be part of the new discovery workflow baked into Cura.
class UltimakerNetworkedPrinterAction(MachineAction): class UltimakerNetworkedPrinterAction(MachineAction):
# Signal emitted when discovered devices have changed. # Signal emitted when discovered devices have changed.
discoveredDevicesChanged = pyqtSignal() discoveredDevicesChanged = pyqtSignal()
@ -34,58 +34,54 @@ class UltimakerNetworkedPrinterAction(MachineAction):
## Start listening to network discovery events via the plugin. ## Start listening to network discovery events via the plugin.
@pyqtSlot(name = "startDiscovery") @pyqtSlot(name = "startDiscovery")
def startDiscovery(self) -> None: def startDiscovery(self) -> None:
network_plugin = self._getNetworkPlugin() self._networkPlugin.discoveredDevicesChanged.connect(self._onDeviceDiscoveryChanged)
network_plugin.discoveredDevicesChanged.connect(self._onDeviceDiscoveryChanged)
self.discoveredDevicesChanged.emit() # trigger at least once to populate the list self.discoveredDevicesChanged.emit() # trigger at least once to populate the list
## Reset the discovered devices. ## Reset the discovered devices.
@pyqtSlot(name = "reset") @pyqtSlot(name = "reset")
def reset(self) -> None: def reset(self) -> None:
self.restartDiscovery() self.discoveredDevicesChanged.emit() # trigger to reset the list
## Reset the discovered devices. ## Reset the discovered devices.
@pyqtSlot(name = "restartDiscovery") @pyqtSlot(name = "restartDiscovery")
def restartDiscovery(self) -> None: def restartDiscovery(self) -> None:
network_plugin = self._getNetworkPlugin() self._networkPlugin.startDiscovery()
network_plugin.startDiscovery()
self.discoveredDevicesChanged.emit() # trigger to reset the list self.discoveredDevicesChanged.emit() # trigger to reset the list
## Remove a manually added device. ## Remove a manually added device.
@pyqtSlot(str, str, name = "removeManualDevice") @pyqtSlot(str, str, name = "removeManualDevice")
def removeManualDevice(self, key: str, address: str) -> None: def removeManualDevice(self, key: str, address: str) -> None:
network_plugin = self._getNetworkPlugin() self._networkPlugin.removeManualDevice(key, address)
network_plugin.removeManualDevice(key, address)
## Add a new manual device. Can replace an existing one by key. ## Add a new manual device. Can replace an existing one by key.
@pyqtSlot(str, str, name = "setManualDevice") @pyqtSlot(str, str, name = "setManualDevice")
def setManualDevice(self, key: str, address: str) -> None: def setManualDevice(self, key: str, address: str) -> None:
network_plugin = self._getNetworkPlugin()
if key != "": if key != "":
network_plugin.removeManualDevice(key) self._networkPlugin.removeManualDevice(key)
if address != "": if address != "":
network_plugin.addManualDevice(address) self._networkPlugin.addManualDevice(address)
## Get the devices discovered in the local network sorted by name. ## Get the devices discovered in the local network sorted by name.
@pyqtProperty("QVariantList", notify = discoveredDevicesChanged) @pyqtProperty("QVariantList", notify = discoveredDevicesChanged)
def foundDevices(self): def foundDevices(self):
network_plugin = self._getNetworkPlugin() discovered_devices = list(self._networkPlugin.getDiscoveredDevices().values())
discovered_devices = list(network_plugin.getDiscoveredDevices().values())
discovered_devices.sort(key = lambda d: d.name) discovered_devices.sort(key = lambda d: d.name)
return discovered_devices return discovered_devices
## Connect a device selected in the list with the active machine. ## Connect a device selected in the list with the active machine.
@pyqtSlot(QObject, name = "associateActiveMachineWithPrinterDevice") @pyqtSlot(QObject, name = "associateActiveMachineWithPrinterDevice")
def associateActiveMachineWithPrinterDevice(self, device: LocalClusterOutputDevice) -> None: def associateActiveMachineWithPrinterDevice(self, device: LocalClusterOutputDevice) -> None:
network_plugin = self._getNetworkPlugin() self._networkPlugin.associateActiveMachineWithPrinterDevice(device)
network_plugin.associateActiveMachineWithPrinterDevice(device)
## Callback for when the list of discovered devices in the plugin was changed. ## Callback for when the list of discovered devices in the plugin was changed.
def _onDeviceDiscoveryChanged(self) -> None: def _onDeviceDiscoveryChanged(self) -> None:
self.discoveredDevicesChanged.emit() self.discoveredDevicesChanged.emit()
## Get the network manager from the plugin. ## Get the network manager from the plugin.
def _getNetworkPlugin(self) -> UM3OutputDevicePlugin: @property
def _networkPlugin(self) -> Optional[UM3OutputDevicePlugin]:
if not self._network_plugin: if not self._network_plugin:
plugin = CuraApplication.getInstance().getOutputDeviceManager().getOutputDevicePlugin("UM3NetworkPrinting") output_device_manager = CuraApplication.getInstance().getOutputDeviceManager()
self._network_plugin = cast(UM3OutputDevicePlugin, plugin) network_plugin = output_device_manager.getOutputDevicePlugin("UM3NetworkPrinting")
self._network_plugin = network_plugin
return self._network_plugin return self._network_plugin