FirmwareUpdaterMachineAction.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from cura.CuraApplication import CuraApplication
  4. from UM.Settings.DefinitionContainer import DefinitionContainer
  5. from cura.MachineAction import MachineAction
  6. from UM.i18n import i18nCatalog
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from cura.PrinterOutput.FirmwareUpdater import FirmwareUpdateState
  9. from PyQt6.QtCore import pyqtSignal, pyqtProperty, QObject
  10. from typing import Optional
  11. MYPY = False
  12. if MYPY:
  13. from cura.PrinterOutput.FirmwareUpdater import FirmwareUpdater
  14. from cura.PrinterOutput.PrinterOutputDevice import PrinterOutputDevice
  15. from UM.Settings.ContainerInterface import ContainerInterface
  16. catalog = i18nCatalog("cura")
  17. class FirmwareUpdaterMachineAction(MachineAction):
  18. """Upgrade the firmware of a machine by USB with this action."""
  19. def __init__(self) -> None:
  20. super().__init__("UpgradeFirmware", catalog.i18nc("@action", "Update Firmware"))
  21. self._qml_url = "FirmwareUpdaterMachineAction.qml"
  22. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)
  23. self._active_output_device = None # type: Optional[PrinterOutputDevice]
  24. self._active_firmware_updater = None # type: Optional[FirmwareUpdater]
  25. CuraApplication.getInstance().engineCreatedSignal.connect(self._onEngineCreated)
  26. def _onEngineCreated(self) -> None:
  27. CuraApplication.getInstance().getMachineManager().outputDevicesChanged.connect(self._onOutputDevicesChanged)
  28. def _onContainerAdded(self, container: "ContainerInterface") -> None:
  29. # Add this action as a supported action to all machine definitions if they support USB connection
  30. if isinstance(container, DefinitionContainer) and container.getMetaDataEntry("type") == "machine" and container.getMetaDataEntry("supports_usb_connection"):
  31. CuraApplication.getInstance().getMachineActionManager().addSupportedAction(container.getId(), self.getKey())
  32. def _onOutputDevicesChanged(self) -> None:
  33. if self._active_output_device and self._active_output_device.activePrinter:
  34. self._active_output_device.activePrinter.getController().canUpdateFirmwareChanged.disconnect(self._onControllerCanUpdateFirmwareChanged)
  35. output_devices = CuraApplication.getInstance().getMachineManager().printerOutputDevices
  36. self._active_output_device = output_devices[0] if output_devices else None
  37. if self._active_output_device and self._active_output_device.activePrinter:
  38. self._active_output_device.activePrinter.getController().canUpdateFirmwareChanged.connect(self._onControllerCanUpdateFirmwareChanged)
  39. self.outputDeviceCanUpdateFirmwareChanged.emit()
  40. def _onControllerCanUpdateFirmwareChanged(self) -> None:
  41. self.outputDeviceCanUpdateFirmwareChanged.emit()
  42. outputDeviceCanUpdateFirmwareChanged = pyqtSignal()
  43. @pyqtProperty(QObject, notify = outputDeviceCanUpdateFirmwareChanged)
  44. def firmwareUpdater(self) -> Optional["FirmwareUpdater"]:
  45. if self._active_output_device and self._active_output_device.activePrinter and self._active_output_device.activePrinter.getController() is not None and self._active_output_device.activePrinter.getController().can_update_firmware:
  46. self._active_firmware_updater = self._active_output_device.getFirmwareUpdater()
  47. return self._active_firmware_updater
  48. elif self._active_firmware_updater and self._active_firmware_updater.firmwareUpdateState not in [FirmwareUpdateState.idle, FirmwareUpdateState.completed]:
  49. # During a firmware update, the PrinterOutputDevice is disconnected but the FirmwareUpdater is still there
  50. return self._active_firmware_updater
  51. self._active_firmware_updater = None
  52. return None