DiscoverUM3Action.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. def __init__(self):
  12. super().__init__("DiscoverUM3Action", catalog.i18nc("@action","Connect via Network"))
  13. self._qml_url = "DiscoverUM3Action.qml"
  14. self._network_plugin = None
  15. self.__additional_components_context = None
  16. self.__additional_component = None
  17. self.__additional_components_view = None
  18. Application.getInstance().engineCreatedSignal.connect(self._createAdditionalComponentsView)
  19. self._last_zeroconf_event_time = time.time()
  20. self._zeroconf_change_grace_period = 0.25 # Time to wait after a zeroconf service change before allowing a zeroconf reset
  21. printersChanged = pyqtSignal()
  22. @pyqtSlot()
  23. def startDiscovery(self):
  24. if not self._network_plugin:
  25. Logger.log("d", "Starting printer discovery.")
  26. self._network_plugin = Application.getInstance().getOutputDeviceManager().getOutputDevicePlugin("UM3NetworkPrinting")
  27. self._network_plugin.printerListChanged.connect(self._onPrinterDiscoveryChanged)
  28. self.printersChanged.emit()
  29. ## Re-filters the list of printers.
  30. @pyqtSlot()
  31. def reset(self):
  32. Logger.log("d", "Reset the list of found printers.")
  33. self.printersChanged.emit()
  34. @pyqtSlot()
  35. def restartDiscovery(self):
  36. # Ensure that there is a bit of time after a printer has been discovered.
  37. # 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.
  38. # It's most likely that the QML engine is still creating delegates, where the python side already deleted or
  39. # garbage collected the data.
  40. # Whatever the case, waiting a bit ensures that it doesn't crash.
  41. if time.time() - self._last_zeroconf_event_time > self._zeroconf_change_grace_period:
  42. if not self._network_plugin:
  43. self.startDiscovery()
  44. else:
  45. self._network_plugin.startDiscovery()
  46. @pyqtSlot(str, str)
  47. def removeManualPrinter(self, key, address):
  48. if not self._network_plugin:
  49. return
  50. self._network_plugin.removeManualPrinter(key, address)
  51. @pyqtSlot(str, str)
  52. def setManualPrinter(self, key, address):
  53. if key != "":
  54. # This manual printer replaces a current manual printer
  55. self._network_plugin.removeManualPrinter(key)
  56. if address != "":
  57. self._network_plugin.addManualPrinter(address)
  58. def _onPrinterDiscoveryChanged(self, *args):
  59. self._last_zeroconf_event_time = time.time()
  60. self.printersChanged.emit()
  61. @pyqtProperty("QVariantList", notify = printersChanged)
  62. def foundDevices(self):
  63. if self._network_plugin:
  64. if Application.getInstance().getGlobalContainerStack():
  65. global_printer_type = Application.getInstance().getGlobalContainerStack().getBottom().getId()
  66. else:
  67. global_printer_type = "unknown"
  68. printers = list(self._network_plugin.getPrinters().values())
  69. # TODO; There are still some testing printers that don't have a correct printer type, so don't filter out unkown ones just yet.
  70. printers = [printer for printer in printers if printer.printerType == global_printer_type or printer.printerType == "unknown"]
  71. printers.sort(key = lambda k: k.name)
  72. return printers
  73. else:
  74. return []
  75. @pyqtSlot(str)
  76. def setKey(self, key):
  77. Logger.log("d", "Attempting to set the network key of the active machine to %s", key)
  78. global_container_stack = Application.getInstance().getGlobalContainerStack()
  79. if global_container_stack:
  80. meta_data = global_container_stack.getMetaData()
  81. if "um_network_key" in meta_data:
  82. global_container_stack.setMetaDataEntry("um_network_key", key)
  83. # Delete old authentication data.
  84. Logger.log("d", "Removing old authentication id %s for device %s", global_container_stack.getMetaDataEntry("network_authentication_id", None), key)
  85. global_container_stack.removeMetaDataEntry("network_authentication_id")
  86. global_container_stack.removeMetaDataEntry("network_authentication_key")
  87. else:
  88. global_container_stack.addMetaDataEntry("um_network_key", key)
  89. if self._network_plugin:
  90. # Ensure that the connection states are refreshed.
  91. self._network_plugin.reCheckConnections()
  92. @pyqtSlot(result = str)
  93. def getStoredKey(self):
  94. global_container_stack = Application.getInstance().getGlobalContainerStack()
  95. if global_container_stack:
  96. meta_data = global_container_stack.getMetaData()
  97. if "um_network_key" in meta_data:
  98. return global_container_stack.getMetaDataEntry("um_network_key")
  99. return ""
  100. @pyqtSlot()
  101. def loadConfigurationFromPrinter(self):
  102. machine_manager = Application.getInstance().getMachineManager()
  103. hotend_ids = machine_manager.printerOutputDevices[0].hotendIds
  104. for index in range(len(hotend_ids)):
  105. machine_manager.printerOutputDevices[0].hotendIdChanged.emit(index, hotend_ids[index])
  106. material_ids = machine_manager.printerOutputDevices[0].materialIds
  107. for index in range(len(material_ids)):
  108. machine_manager.printerOutputDevices[0].materialIdChanged.emit(index, material_ids[index])
  109. def _createAdditionalComponentsView(self):
  110. Logger.log("d", "Creating additional ui components for UM3.")
  111. # Create networking dialog
  112. path = os.path.join(PluginRegistry.getInstance().getPluginPath("UM3NetworkPrinting"), "UM3InfoComponents.qml")
  113. self.__additional_components_view = Application.getInstance().createQmlComponent(path, {"manager": self})
  114. if not self.__additional_components_view:
  115. Logger.log("w", "Could not create ui components for UM3.")
  116. return
  117. # Create extra components
  118. Application.getInstance().addAdditionalComponent("monitorButtons", self.__additional_components_view.findChild(QObject, "networkPrinterConnectButton"))
  119. Application.getInstance().addAdditionalComponent("machinesDetailPane", self.__additional_components_view.findChild(QObject, "networkPrinterConnectionInfo"))