DiscoverUM3Action.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import os.path
  2. import time
  3. from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QObject
  4. from UM.Application import Application
  5. from UM.PluginRegistry import PluginRegistry
  6. from UM.Logger import Logger
  7. from UM.i18n import i18nCatalog
  8. from cura.MachineAction import MachineAction
  9. catalog = i18nCatalog("cura")
  10. class DiscoverUM3Action(MachineAction):
  11. discoveredDevicesChanged = pyqtSignal()
  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. @pyqtSlot()
  24. def startDiscovery(self):
  25. if not self._network_plugin:
  26. Logger.log("d", "Starting device discovery.")
  27. self._network_plugin = Application.getInstance().getOutputDeviceManager().getOutputDevicePlugin("UM3NetworkPrinting")
  28. self._network_plugin.discoveredDevicesChanged.connect(self._onDeviceDiscoveryChanged)
  29. self.discoveredDevicesChanged.emit()
  30. ## Re-filters the list of devices.
  31. @pyqtSlot()
  32. def reset(self):
  33. Logger.log("d", "Reset the list of found devices.")
  34. self.discoveredDevicesChanged.emit()
  35. @pyqtSlot()
  36. def restartDiscovery(self):
  37. # Ensure that there is a bit of time after a printer has been discovered.
  38. # 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.
  39. # It's most likely that the QML engine is still creating delegates, where the python side already deleted or
  40. # garbage collected the data.
  41. # Whatever the case, waiting a bit ensures that it doesn't crash.
  42. if time.time() - self._last_zero_conf_event_time > self._zero_conf_change_grace_period:
  43. if not self._network_plugin:
  44. self.startDiscovery()
  45. else:
  46. self._network_plugin.startDiscovery()
  47. @pyqtSlot(str, str)
  48. def removeManualDevice(self, key, address):
  49. if not self._network_plugin:
  50. return
  51. self._network_plugin.removeManualDevice(key, address)
  52. @pyqtSlot(str, str)
  53. def setManualDevice(self, key, address):
  54. if key != "":
  55. # This manual printer replaces a current manual printer
  56. self._network_plugin.removeManualDevice(key)
  57. if address != "":
  58. self._network_plugin.addManualDevice(address)
  59. def _onDeviceDiscoveryChanged(self, *args):
  60. self._last_zero_conf_event_time = time.time()
  61. self.discoveredDevicesChanged.emit()
  62. @pyqtProperty("QVariantList", notify = discoveredDevicesChanged)
  63. def foundDevices(self):
  64. if self._network_plugin:
  65. # TODO: Check if this needs to stay.
  66. if Application.getInstance().getGlobalContainerStack():
  67. global_printer_type = Application.getInstance().getGlobalContainerStack().getBottom().getId()
  68. else:
  69. global_printer_type = "unknown"
  70. printers = list(self._network_plugin.getDiscoveredDevices().values())
  71. # TODO; There are still some testing printers that don't have a correct printer type, so don't filter out unkown ones just yet.
  72. #printers = [printer for printer in printers if printer.printerType == global_printer_type or printer.printerType == "unknown"]
  73. printers.sort(key = lambda k: k.name)
  74. return printers
  75. else:
  76. return []
  77. @pyqtSlot(str)
  78. def setGroupName(self, group_name):
  79. Logger.log("d", "Attempting to set the group name of the active machine to %s", group_name)
  80. global_container_stack = Application.getInstance().getGlobalContainerStack()
  81. if global_container_stack:
  82. meta_data = global_container_stack.getMetaData()
  83. if "connect_group_name" in meta_data:
  84. previous_connect_group_name = meta_data["connect_group_name"]
  85. global_container_stack.setMetaDataEntry("connect_group_name", group_name)
  86. # Find all the places where there is the same group name and change it accordingly
  87. Application.getInstance().getMachineManager().replaceContainersMetadata(key = "connect_group_name", value = previous_connect_group_name, new_value = group_name)
  88. else:
  89. global_container_stack.addMetaDataEntry("connect_group_name", group_name)
  90. global_container_stack.addMetaDataEntry("hidden", False)
  91. if self._network_plugin:
  92. # Ensure that the connection states are refreshed.
  93. self._network_plugin.reCheckConnections()
  94. @pyqtSlot(str)
  95. def setKey(self, key):
  96. Logger.log("d", "Attempting to set the network key of the active machine to %s", key)
  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. previous_network_key= meta_data["um_network_key"]
  102. global_container_stack.setMetaDataEntry("um_network_key", key)
  103. # Delete old authentication data.
  104. Logger.log("d", "Removing old authentication id %s for device %s", global_container_stack.getMetaDataEntry("network_authentication_id", None), key)
  105. global_container_stack.removeMetaDataEntry("network_authentication_id")
  106. global_container_stack.removeMetaDataEntry("network_authentication_key")
  107. Application.getInstance().getMachineManager().replaceContainersMetadata(key = "um_network_key", value = previous_network_key, new_value = key)
  108. else:
  109. global_container_stack.addMetaDataEntry("um_network_key", key)
  110. if self._network_plugin:
  111. # Ensure that the connection states are refreshed.
  112. self._network_plugin.reCheckConnections()
  113. @pyqtSlot(result = str)
  114. def getStoredKey(self):
  115. global_container_stack = Application.getInstance().getGlobalContainerStack()
  116. if global_container_stack:
  117. meta_data = global_container_stack.getMetaData()
  118. if "um_network_key" in meta_data:
  119. return global_container_stack.getMetaDataEntry("um_network_key")
  120. return ""
  121. @pyqtSlot(str, result = bool)
  122. def existsKey(self, key) -> bool:
  123. return Application.getInstance().getMachineManager().existNetworkInstances(network_key = key)
  124. @pyqtSlot()
  125. def loadConfigurationFromPrinter(self):
  126. machine_manager = Application.getInstance().getMachineManager()
  127. hotend_ids = machine_manager.printerOutputDevices[0].hotendIds
  128. for index in range(len(hotend_ids)):
  129. machine_manager.printerOutputDevices[0].hotendIdChanged.emit(index, hotend_ids[index])
  130. material_ids = machine_manager.printerOutputDevices[0].materialIds
  131. for index in range(len(material_ids)):
  132. machine_manager.printerOutputDevices[0].materialIdChanged.emit(index, material_ids[index])
  133. def _createAdditionalComponentsView(self):
  134. Logger.log("d", "Creating additional ui components for UM3.")
  135. # Create networking dialog
  136. path = os.path.join(PluginRegistry.getInstance().getPluginPath("UM3NetworkPrinting"), "UM3InfoComponents.qml")
  137. self.__additional_components_view = Application.getInstance().createQmlComponent(path, {"manager": self})
  138. if not self.__additional_components_view:
  139. Logger.log("w", "Could not create ui components for UM3.")
  140. return
  141. # Create extra components
  142. Application.getInstance().addAdditionalComponent("monitorButtons", self.__additional_components_view.findChild(QObject, "networkPrinterConnectButton"))
  143. Application.getInstance().addAdditionalComponent("machinesDetailPane", self.__additional_components_view.findChild(QObject, "networkPrinterConnectionInfo"))