DiscoverUM3Action.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. if self._network_plugin:
  35. self._network_plugin.resetLastManualDevice()
  36. self.discoveredDevicesChanged.emit()
  37. @pyqtSlot()
  38. def restartDiscovery(self):
  39. # Ensure that there is a bit of time after a printer has been discovered.
  40. # 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.
  41. # It's most likely that the QML engine is still creating delegates, where the python side already deleted or
  42. # garbage collected the data.
  43. # Whatever the case, waiting a bit ensures that it doesn't crash.
  44. if time.time() - self._last_zero_conf_event_time > self._zero_conf_change_grace_period:
  45. if not self._network_plugin:
  46. self.startDiscovery()
  47. else:
  48. self._network_plugin.startDiscovery()
  49. @pyqtSlot(str, str)
  50. def removeManualDevice(self, key, address):
  51. if not self._network_plugin:
  52. return
  53. self._network_plugin.removeManualDevice(key, address)
  54. @pyqtSlot(str, str)
  55. def setManualDevice(self, key, address):
  56. if key != "":
  57. # This manual printer replaces a current manual printer
  58. self._network_plugin.removeManualDevice(key)
  59. if address != "":
  60. self._network_plugin.addManualDevice(address)
  61. def _onDeviceDiscoveryChanged(self, *args):
  62. self._last_zero_conf_event_time = time.time()
  63. self.discoveredDevicesChanged.emit()
  64. @pyqtProperty("QVariantList", notify = discoveredDevicesChanged)
  65. def foundDevices(self):
  66. if self._network_plugin:
  67. printers = list(self._network_plugin.getDiscoveredDevices().values())
  68. printers.sort(key = lambda k: k.name)
  69. return printers
  70. else:
  71. return []
  72. @pyqtSlot(str)
  73. def setGroupName(self, group_name):
  74. Logger.log("d", "Attempting to set the group name of the active machine to %s", group_name)
  75. global_container_stack = Application.getInstance().getGlobalContainerStack()
  76. if global_container_stack:
  77. meta_data = global_container_stack.getMetaData()
  78. if "connect_group_name" in meta_data:
  79. previous_connect_group_name = meta_data["connect_group_name"]
  80. global_container_stack.setMetaDataEntry("connect_group_name", group_name)
  81. # Find all the places where there is the same group name and change it accordingly
  82. Application.getInstance().getMachineManager().replaceContainersMetadata(key = "connect_group_name", value = previous_connect_group_name, new_value = group_name)
  83. else:
  84. global_container_stack.addMetaDataEntry("connect_group_name", group_name)
  85. global_container_stack.addMetaDataEntry("hidden", False)
  86. if self._network_plugin:
  87. # Ensure that the connection states are refreshed.
  88. self._network_plugin.reCheckConnections()
  89. @pyqtSlot(str)
  90. def setKey(self, key):
  91. Logger.log("d", "Attempting to set the network key of the active machine to %s", key)
  92. global_container_stack = Application.getInstance().getGlobalContainerStack()
  93. if global_container_stack:
  94. meta_data = global_container_stack.getMetaData()
  95. if "um_network_key" in meta_data:
  96. previous_network_key= meta_data["um_network_key"]
  97. global_container_stack.setMetaDataEntry("um_network_key", key)
  98. # Delete old authentication data.
  99. Logger.log("d", "Removing old authentication id %s for device %s", global_container_stack.getMetaDataEntry("network_authentication_id", None), key)
  100. global_container_stack.removeMetaDataEntry("network_authentication_id")
  101. global_container_stack.removeMetaDataEntry("network_authentication_key")
  102. Application.getInstance().getMachineManager().replaceContainersMetadata(key = "um_network_key", value = previous_network_key, new_value = key)
  103. else:
  104. global_container_stack.addMetaDataEntry("um_network_key", key)
  105. if self._network_plugin:
  106. # Ensure that the connection states are refreshed.
  107. self._network_plugin.reCheckConnections()
  108. @pyqtSlot(result = str)
  109. def getStoredKey(self) -> str:
  110. global_container_stack = Application.getInstance().getGlobalContainerStack()
  111. if global_container_stack:
  112. meta_data = global_container_stack.getMetaData()
  113. if "um_network_key" in meta_data:
  114. return global_container_stack.getMetaDataEntry("um_network_key")
  115. return ""
  116. @pyqtSlot(result = str)
  117. def getLastManualEntryKey(self) -> str:
  118. if self._network_plugin:
  119. return self._network_plugin.getLastManualDevice()
  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"))