DiscoverUM3Action.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_zeroconf_event_time = time.time()
  21. self._zeroconf_change_grace_period = 0.25 # Time to wait after a zeroconf service change before allowing a zeroconf reset
  22. printersChanged = pyqtSignal()
  23. @pyqtSlot()
  24. def startDiscovery(self):
  25. if not self._network_plugin:
  26. Logger.log("d", "Starting printer discovery.")
  27. self._network_plugin = Application.getInstance().getOutputDeviceManager().getOutputDevicePlugin("UM3NetworkPrinting")
  28. self._network_plugin.printerListChanged.connect(self._onPrinterDiscoveryChanged)
  29. self.printersChanged.emit()
  30. ## Re-filters the list of printers.
  31. @pyqtSlot()
  32. def reset(self):
  33. Logger.log("d", "Reset the list of found printers.")
  34. self.printersChanged.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_zeroconf_event_time > self._zeroconf_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 removeManualPrinter(self, key, address):
  49. if not self._network_plugin:
  50. return
  51. self._network_plugin.removeManualPrinter(key, address)
  52. @pyqtSlot(str, str)
  53. def setManualPrinter(self, key, address):
  54. if key != "":
  55. # This manual printer replaces a current manual printer
  56. self._network_plugin.removeManualPrinter(key)
  57. if address != "":
  58. self._network_plugin.addManualPrinter(address)
  59. def _onPrinterDiscoveryChanged(self, *args):
  60. self._last_zeroconf_event_time = time.time()
  61. self.printersChanged.emit()
  62. @pyqtProperty("QVariantList", notify = printersChanged)
  63. def foundDevices(self):
  64. if self._network_plugin:
  65. if Application.getInstance().getGlobalContainerStack():
  66. global_printer_type = Application.getInstance().getGlobalContainerStack().getBottom().getId()
  67. else:
  68. global_printer_type = "unknown"
  69. printers = list(self._network_plugin.getPrinters().values())
  70. # TODO; There are still some testing printers that don't have a correct printer type, so don't filter out unkown ones just yet.
  71. printers = [printer for printer in printers if printer.printerType == global_printer_type or printer.printerType == "unknown"]
  72. printers.sort(key = lambda k: k.name)
  73. return printers
  74. else:
  75. return []
  76. @pyqtSlot(str)
  77. def setKey(self, key):
  78. Logger.log("d", "Attempting to set the network key of the active machine to %s", key)
  79. global_container_stack = Application.getInstance().getGlobalContainerStack()
  80. if global_container_stack:
  81. meta_data = global_container_stack.getMetaData()
  82. if "um_network_key" in meta_data:
  83. global_container_stack.setMetaDataEntry("um_network_key", key)
  84. # Delete old authentication data.
  85. Logger.log("d", "Removing old authentication id %s for device %s", global_container_stack.getMetaDataEntry("network_authentication_id", None), key)
  86. global_container_stack.removeMetaDataEntry("network_authentication_id")
  87. global_container_stack.removeMetaDataEntry("network_authentication_key")
  88. else:
  89. global_container_stack.addMetaDataEntry("um_network_key", key)
  90. if self._network_plugin:
  91. # Ensure that the connection states are refreshed.
  92. self._network_plugin.reCheckConnections()
  93. @pyqtSlot(result = str)
  94. def getStoredKey(self):
  95. global_container_stack = Application.getInstance().getGlobalContainerStack()
  96. if global_container_stack:
  97. meta_data = global_container_stack.getMetaData()
  98. if "um_network_key" in meta_data:
  99. return global_container_stack.getMetaDataEntry("um_network_key")
  100. return ""
  101. @pyqtSlot()
  102. def loadConfigurationFromPrinter(self):
  103. machine_manager = Application.getInstance().getMachineManager()
  104. hotend_ids = machine_manager.printerOutputDevices[0].hotendIds
  105. for index in range(len(hotend_ids)):
  106. machine_manager.printerOutputDevices[0].hotendIdChanged.emit(index, hotend_ids[index])
  107. material_ids = machine_manager.printerOutputDevices[0].materialIds
  108. for index in range(len(material_ids)):
  109. machine_manager.printerOutputDevices[0].materialIdChanged.emit(index, material_ids[index])
  110. def _createAdditionalComponentsView(self):
  111. Logger.log("d", "Creating additional ui components for UM3.")
  112. path = QUrl.fromLocalFile(os.path.join(PluginRegistry.getInstance().getPluginPath("UM3NetworkPrinting"), "UM3InfoComponents.qml"))
  113. self.__additional_component = QQmlComponent(Application.getInstance()._engine, path)
  114. # We need access to engine (although technically we can't)
  115. self.__additional_components_context = QQmlContext(Application.getInstance()._engine.rootContext())
  116. self.__additional_components_context.setContextProperty("manager", self)
  117. self.__additional_components_view = self.__additional_component.create(self.__additional_components_context)
  118. if not self.__additional_components_view:
  119. Logger.log("w", "Could not create ui components for UM3.")
  120. return
  121. Application.getInstance().addAdditionalComponent("monitorButtons", self.__additional_components_view.findChild(QObject, "networkPrinterConnectButton"))
  122. Application.getInstance().addAdditionalComponent("machinesDetailPane", self.__additional_components_view.findChild(QObject, "networkPrinterConnectionInfo"))