DiscoverUM3Action.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from cura.MachineAction import MachineAction
  2. from UM.Application import Application
  3. from UM.PluginRegistry import PluginRegistry
  4. from UM.Logger import Logger
  5. from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QUrl, QObject
  6. from PyQt5.QtQml import QQmlComponent, QQmlContext
  7. import os.path
  8. import time
  9. from UM.i18n import i18nCatalog
  10. catalog = i18nCatalog("cura")
  11. class DiscoverUM3Action(MachineAction):
  12. def __init__(self):
  13. super().__init__("DiscoverUM3Action", catalog.i18nc("@action","Connect via Network"))
  14. self._qml_url = "DiscoverUM3Action.qml"
  15. self._network_plugin = None
  16. self.__additional_components_context = None
  17. self.__additional_component = None
  18. self.__additional_components_view = None
  19. Application.getInstance().engineCreatedSignal.connect(self._createAdditionalComponentsView)
  20. self._last_zero_conf_event_time = time.time()
  21. # Time to wait after a zero-conf service change before allowing a zeroconf reset
  22. self._zero_conf_change_grace_period = 0.25
  23. discoveredDevicesChanged = pyqtSignal()
  24. @pyqtSlot()
  25. def startDiscovery(self):
  26. if not self._network_plugin:
  27. Logger.log("d", "Starting device discovery.")
  28. self._network_plugin = Application.getInstance().getOutputDeviceManager().getOutputDevicePlugin("UM3NetworkPrinting")
  29. self._network_plugin.discoveredDevicesChanged.connect(self._onDeviceDiscoveryChanged)
  30. self.discoveredDevicesChanged.emit()
  31. ## Re-filters the list of devices.
  32. @pyqtSlot()
  33. def reset(self):
  34. Logger.log("d", "Reset the list of found devices.")
  35. self.discoveredDevicesChanged.emit()
  36. @pyqtSlot()
  37. def restartDiscovery(self):
  38. # Ensure that there is a bit of time after a printer has been discovered.
  39. # This is a work around for an issue with Qt 5.5.1 up to Qt 5.7 which can segfault if we do this too often.
  40. # It's most likely that the QML engine is still creating delegates, where the python side already deleted or
  41. # garbage collected the data.
  42. # Whatever the case, waiting a bit ensures that it doesn't crash.
  43. if time.time() - self._last_zero_conf_event_time > self._zero_conf_change_grace_period:
  44. if not self._network_plugin:
  45. self.startDiscovery()
  46. else:
  47. self._network_plugin.startDiscovery()
  48. @pyqtSlot(str, str)
  49. def removeManualDevice(self, key, address):
  50. if not self._network_plugin:
  51. return
  52. self._network_plugin.removeManualDevice(key, address)
  53. @pyqtSlot(str, str)
  54. def setManualDevice(self, key, address):
  55. if key != "":
  56. # This manual printer replaces a current manual printer
  57. self._network_plugin.removeManualDevice(key)
  58. if address != "":
  59. self._network_plugin.addManualPrinter(address)
  60. def _onDeviceDiscoveryChanged(self, *args):
  61. self._last_zero_conf_event_time = time.time()
  62. self.discoveredDevicesChanged.emit()
  63. @pyqtProperty("QVariantList", notify = discoveredDevicesChanged)
  64. def foundDevices(self):
  65. if self._network_plugin:
  66. # TODO: Check if this needs to stay.
  67. if Application.getInstance().getGlobalContainerStack():
  68. global_printer_type = Application.getInstance().getGlobalContainerStack().getBottom().getId()
  69. else:
  70. global_printer_type = "unknown"
  71. printers = list(self._network_plugin.getDiscoveredDevices().values())
  72. # TODO; There are still some testing printers that don't have a correct printer type, so don't filter out unkown ones just yet.
  73. #printers = [printer for printer in printers if printer.printerType == global_printer_type or printer.printerType == "unknown"]
  74. printers.sort(key = lambda k: k.name)
  75. return printers
  76. else:
  77. return []
  78. @pyqtSlot(str)
  79. def setKey(self, key):
  80. Logger.log("d", "Attempting to set the network key of the active machine to %s", key)
  81. global_container_stack = Application.getInstance().getGlobalContainerStack()
  82. if global_container_stack:
  83. meta_data = global_container_stack.getMetaData()
  84. if "um_network_key" in meta_data:
  85. global_container_stack.setMetaDataEntry("um_network_key", key)
  86. # Delete old authentication data.
  87. Logger.log("d", "Removing old authentication id %s for device %s", global_container_stack.getMetaDataEntry("network_authentication_id", None), key)
  88. global_container_stack.removeMetaDataEntry("network_authentication_id")
  89. global_container_stack.removeMetaDataEntry("network_authentication_key")
  90. else:
  91. global_container_stack.addMetaDataEntry("um_network_key", key)
  92. if self._network_plugin:
  93. # Ensure that the connection states are refreshed.
  94. self._network_plugin.reCheckConnections()
  95. @pyqtSlot(result = str)
  96. def getStoredKey(self):
  97. global_container_stack = Application.getInstance().getGlobalContainerStack()
  98. if global_container_stack:
  99. meta_data = global_container_stack.getMetaData()
  100. if "um_network_key" in meta_data:
  101. return global_container_stack.getMetaDataEntry("um_network_key")
  102. return ""
  103. @pyqtSlot()
  104. def loadConfigurationFromPrinter(self):
  105. machine_manager = Application.getInstance().getMachineManager()
  106. hotend_ids = machine_manager.printerOutputDevices[0].hotendIds
  107. for index in range(len(hotend_ids)):
  108. machine_manager.printerOutputDevices[0].hotendIdChanged.emit(index, hotend_ids[index])
  109. material_ids = machine_manager.printerOutputDevices[0].materialIds
  110. for index in range(len(material_ids)):
  111. machine_manager.printerOutputDevices[0].materialIdChanged.emit(index, material_ids[index])
  112. def _createAdditionalComponentsView(self):
  113. Logger.log("d", "Creating additional ui components for UM3.")
  114. path = QUrl.fromLocalFile(os.path.join(PluginRegistry.getInstance().getPluginPath("UM3NetworkPrinting"), "UM3InfoComponents.qml"))
  115. self.__additional_component = QQmlComponent(Application.getInstance()._engine, path)
  116. # We need access to engine (although technically we can't)
  117. self.__additional_components_context = QQmlContext(Application.getInstance()._engine.rootContext())
  118. self.__additional_components_context.setContextProperty("manager", self)
  119. self.__additional_components_view = self.__additional_component.create(self.__additional_components_context)
  120. if not self.__additional_components_view:
  121. Logger.log("w", "Could not create ui components for UM3.")
  122. return
  123. Application.getInstance().addAdditionalComponent("monitorButtons", self.__additional_components_view.findChild(QObject, "networkPrinterConnectButton"))
  124. Application.getInstance().addAdditionalComponent("machinesDetailPane", self.__additional_components_view.findChild(QObject, "networkPrinterConnectionInfo"))