UM3OutputDevicePlugin.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, Callable, Dict
  4. from UM.Signal import Signal
  5. from cura.CuraApplication import CuraApplication
  6. from UM.OutputDevice.OutputDeviceManager import ManualDeviceAdditionAttempt
  7. from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
  8. from .Network.LocalClusterOutputDevice import LocalClusterOutputDevice
  9. from .Network.LocalClusterOutputDeviceManager import LocalClusterOutputDeviceManager
  10. from .Cloud.CloudOutputDeviceManager import CloudOutputDeviceManager
  11. class UM3OutputDevicePlugin(OutputDevicePlugin):
  12. """This plugin handles the discovery and networking for Ultimaker 3D printers"""
  13. discoveredDevicesChanged = Signal()
  14. """Signal emitted when the list of discovered devices changed. Used by printer action in this plugin."""
  15. def __init__(self) -> None:
  16. super().__init__()
  17. # Create a network output device manager that abstracts all network connection logic away.
  18. self._network_output_device_manager = LocalClusterOutputDeviceManager()
  19. self._network_output_device_manager.discoveredDevicesChanged.connect(self.discoveredDevicesChanged)
  20. # Create a cloud output device manager that abstracts all cloud connection logic away.
  21. self._cloud_output_device_manager = CloudOutputDeviceManager()
  22. # Refresh network connections when another machine was selected in Cura.
  23. # This ensures no output devices are still connected that do not belong to the new active machine.
  24. CuraApplication.getInstance().globalContainerStackChanged.connect(self.refreshConnections)
  25. def start(self):
  26. """Start looking for devices in the network and cloud."""
  27. self._network_output_device_manager.start()
  28. self._cloud_output_device_manager.start()
  29. # Stop network and cloud discovery.
  30. def stop(self) -> None:
  31. self._network_output_device_manager.stop()
  32. self._cloud_output_device_manager.stop()
  33. def startDiscovery(self) -> None:
  34. """Restart network discovery."""
  35. self._network_output_device_manager.startDiscovery()
  36. def refreshConnections(self) -> None:
  37. """Force refreshing the network connections."""
  38. self._network_output_device_manager.refreshConnections()
  39. self._cloud_output_device_manager.refreshConnections()
  40. def canAddManualDevice(self, address: str = "") -> ManualDeviceAdditionAttempt:
  41. """Indicate that this plugin supports adding networked printers manually."""
  42. return ManualDeviceAdditionAttempt.PRIORITY
  43. def addManualDevice(self, address: str, callback: Optional[Callable[[bool, str], None]] = None) -> None:
  44. """Add a networked printer manually based on its network address."""
  45. self._network_output_device_manager.addManualDevice(address, callback)
  46. def removeManualDevice(self, key: str, address: Optional[str] = None) -> None:
  47. """Remove a manually connected networked printer."""
  48. self._network_output_device_manager.removeManualDevice(key, address)
  49. def getDiscoveredDevices(self) -> Dict[str, LocalClusterOutputDevice]:
  50. """Get the discovered devices from the local network."""
  51. return self._network_output_device_manager.getDiscoveredDevices()
  52. def associateActiveMachineWithPrinterDevice(self, device: LocalClusterOutputDevice) -> None:
  53. """Connect the active machine to a device."""
  54. self._network_output_device_manager.associateActiveMachineWithPrinterDevice(device)