FirmwareUpdater.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, QUrl, pyqtSignal, pyqtProperty
  4. from enum import IntEnum
  5. from threading import Thread
  6. from typing import Union
  7. MYPY = False
  8. if MYPY:
  9. from cura.PrinterOutput.PrinterOutputDevice import PrinterOutputDevice
  10. class FirmwareUpdater(QObject):
  11. firmwareProgressChanged = pyqtSignal()
  12. firmwareUpdateStateChanged = pyqtSignal()
  13. def __init__(self, output_device: "PrinterOutputDevice") -> None:
  14. super().__init__()
  15. self._output_device = output_device
  16. self._update_firmware_thread = Thread(target=self._updateFirmware, daemon=True, name = "FirmwareUpdateThread")
  17. self._firmware_file = ""
  18. self._firmware_progress = 0
  19. self._firmware_update_state = FirmwareUpdateState.idle
  20. def updateFirmware(self, firmware_file: Union[str, QUrl]) -> None:
  21. # the file path could be url-encoded.
  22. if firmware_file.startswith("file://"):
  23. self._firmware_file = QUrl(firmware_file).toLocalFile()
  24. else:
  25. self._firmware_file = firmware_file
  26. if self._firmware_file == "":
  27. self._setFirmwareUpdateState(FirmwareUpdateState.firmware_not_found_error)
  28. return
  29. self._setFirmwareUpdateState(FirmwareUpdateState.updating)
  30. self._update_firmware_thread.start()
  31. def _updateFirmware(self) -> None:
  32. raise NotImplementedError("_updateFirmware needs to be implemented")
  33. def _cleanupAfterUpdate(self) -> None:
  34. """Cleanup after a succesful update"""
  35. # Clean up for next attempt.
  36. self._update_firmware_thread = Thread(target=self._updateFirmware, daemon=True, name = "FirmwareUpdateThread")
  37. self._firmware_file = ""
  38. self._onFirmwareProgress(100)
  39. self._setFirmwareUpdateState(FirmwareUpdateState.completed)
  40. @pyqtProperty(int, notify = firmwareProgressChanged)
  41. def firmwareProgress(self) -> int:
  42. return self._firmware_progress
  43. @pyqtProperty(int, notify=firmwareUpdateStateChanged)
  44. def firmwareUpdateState(self) -> "FirmwareUpdateState":
  45. return self._firmware_update_state
  46. def _setFirmwareUpdateState(self, state: "FirmwareUpdateState") -> None:
  47. if self._firmware_update_state != state:
  48. self._firmware_update_state = state
  49. self.firmwareUpdateStateChanged.emit()
  50. # Callback function for firmware update progress.
  51. def _onFirmwareProgress(self, progress: int, max_progress: int = 100) -> None:
  52. self._firmware_progress = int(progress * 100 / max_progress) # Convert to scale of 0-100
  53. self.firmwareProgressChanged.emit()
  54. class FirmwareUpdateState(IntEnum):
  55. idle = 0
  56. updating = 1
  57. completed = 2
  58. unknown_error = 3
  59. communication_error = 4
  60. io_error = 5
  61. firmware_not_found_error = 6