UltimakerNetworkedPrinterAction.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, cast
  4. from PyQt6.QtCore import pyqtSlot, pyqtSignal, pyqtProperty, QObject
  5. from UM import i18nCatalog
  6. from cura.CuraApplication import CuraApplication
  7. from cura.MachineAction import MachineAction
  8. from .UM3OutputDevicePlugin import UM3OutputDevicePlugin
  9. from .Network.LocalClusterOutputDevice import LocalClusterOutputDevice
  10. I18N_CATALOG = i18nCatalog("cura")
  11. class UltimakerNetworkedPrinterAction(MachineAction):
  12. """Machine action that allows to connect the active machine to a networked devices.
  13. TODO: in the future this should be part of the new discovery workflow baked into Cura.
  14. """
  15. # Signal emitted when discovered devices have changed.
  16. discoveredDevicesChanged = pyqtSignal()
  17. def __init__(self) -> None:
  18. super().__init__("DiscoverUM3Action", I18N_CATALOG.i18nc("@action", "Connect via Network"))
  19. self._qml_url = "resources/qml/DiscoverUM3Action.qml"
  20. self._network_plugin = None # type: Optional[UM3OutputDevicePlugin]
  21. def needsUserInteraction(self) -> bool:
  22. """Override the default value."""
  23. return False
  24. @pyqtSlot(name = "startDiscovery")
  25. def startDiscovery(self) -> None:
  26. """Start listening to network discovery events via the plugin."""
  27. self._networkPlugin.discoveredDevicesChanged.connect(self._onDeviceDiscoveryChanged)
  28. self.discoveredDevicesChanged.emit() # trigger at least once to populate the list
  29. @pyqtSlot(name = "reset")
  30. def reset(self) -> None:
  31. """Reset the discovered devices."""
  32. self.discoveredDevicesChanged.emit() # trigger to reset the list
  33. @pyqtSlot(name = "restartDiscovery")
  34. def restartDiscovery(self) -> None:
  35. """Reset the discovered devices."""
  36. self._networkPlugin.startDiscovery()
  37. self.discoveredDevicesChanged.emit() # trigger to reset the list
  38. @pyqtSlot(str, str, name = "removeManualDevice")
  39. def removeManualDevice(self, key: str, address: str) -> None:
  40. """Remove a manually added device."""
  41. self._networkPlugin.removeManualDevice(key, address)
  42. @pyqtSlot(str, str, name = "setManualDevice")
  43. def setManualDevice(self, key: str, address: str) -> None:
  44. """Add a new manual device. Can replace an existing one by key."""
  45. if key != "":
  46. self._networkPlugin.removeManualDevice(key)
  47. if address != "":
  48. self._networkPlugin.addManualDevice(address)
  49. @pyqtProperty("QVariantList", notify = discoveredDevicesChanged)
  50. def foundDevices(self):
  51. """Get the devices discovered in the local network sorted by name."""
  52. discovered_devices = list(self._networkPlugin.getDiscoveredDevices().values())
  53. discovered_devices.sort(key = lambda d: d.name)
  54. return discovered_devices
  55. @pyqtSlot(QObject, name = "associateActiveMachineWithPrinterDevice")
  56. def associateActiveMachineWithPrinterDevice(self, device: LocalClusterOutputDevice) -> None:
  57. """Connect a device selected in the list with the active machine."""
  58. self._networkPlugin.associateActiveMachineWithPrinterDevice(device)
  59. def _onDeviceDiscoveryChanged(self) -> None:
  60. """Callback for when the list of discovered devices in the plugin was changed."""
  61. self.discoveredDevicesChanged.emit()
  62. @property
  63. def _networkPlugin(self) -> UM3OutputDevicePlugin:
  64. """Get the network manager from the plugin."""
  65. if not self._network_plugin:
  66. output_device_manager = CuraApplication.getInstance().getOutputDeviceManager()
  67. network_plugin = output_device_manager.getOutputDevicePlugin("UM3NetworkPrinting")
  68. self._network_plugin = cast(UM3OutputDevicePlugin, network_plugin)
  69. return self._network_plugin