Move updateFirmware to PrinterOutputDevice...

along with codestyle and typing fixes
This commit is contained in:
fieldOfView 2018-10-09 16:26:45 +02:00
parent 04bca109ba
commit a36deea651
7 changed files with 34 additions and 34 deletions

View file

@ -22,16 +22,16 @@ class FirmwareUpdater(QObject):
self._update_firmware_thread = Thread(target=self._updateFirmware, daemon=True) self._update_firmware_thread = Thread(target=self._updateFirmware, daemon=True)
self._firmware_location = "" self._firmware_file = ""
self._firmware_progress = 0 self._firmware_progress = 0
self._firmware_update_state = FirmwareUpdateState.idle self._firmware_update_state = FirmwareUpdateState.idle
def updateFirmware(self, file: Union[str, QUrl]) -> None: def updateFirmware(self, firmware_file: Union[str, QUrl]) -> None:
# the file path could be url-encoded. # the file path could be url-encoded.
if file.startswith("file://"): if firmware_file.startswith("file://"):
self._firmware_location = QUrl(file).toLocalFile() self._firmware_file = QUrl(firmware_file).toLocalFile()
else: else:
self._firmware_location = file self._firmware_file = firmware_file
self._setFirmwareUpdateState(FirmwareUpdateState.updating) self._setFirmwareUpdateState(FirmwareUpdateState.updating)
@ -44,26 +44,26 @@ class FirmwareUpdater(QObject):
def _cleanupAfterUpdate(self) -> None: def _cleanupAfterUpdate(self) -> None:
# Clean up for next attempt. # Clean up for next attempt.
self._update_firmware_thread = Thread(target=self._updateFirmware, daemon=True) self._update_firmware_thread = Thread(target=self._updateFirmware, daemon=True)
self._firmware_location = "" self._firmware_file = ""
self._onFirmwareProgress(100) self._onFirmwareProgress(100)
self._setFirmwareUpdateState(FirmwareUpdateState.completed) self._setFirmwareUpdateState(FirmwareUpdateState.completed)
@pyqtProperty(float, notify = firmwareProgressChanged) @pyqtProperty(int, notify = firmwareProgressChanged)
def firmwareProgress(self) -> float: def firmwareProgress(self) -> int:
return self._firmware_progress return self._firmware_progress
@pyqtProperty(int, notify=firmwareUpdateStateChanged) @pyqtProperty(int, notify=firmwareUpdateStateChanged)
def firmwareUpdateState(self) -> "FirmwareUpdateState": def firmwareUpdateState(self) -> "FirmwareUpdateState":
return self._firmware_update_state return self._firmware_update_state
def _setFirmwareUpdateState(self, state) -> None: def _setFirmwareUpdateState(self, state: "FirmwareUpdateState") -> None:
if self._firmware_update_state != state: if self._firmware_update_state != state:
self._firmware_update_state = state self._firmware_update_state = state
self.firmwareUpdateStateChanged.emit() self.firmwareUpdateStateChanged.emit()
# Callback function for firmware update progress. # Callback function for firmware update progress.
def _onFirmwareProgress(self, progress, max_progress = 100) -> None: def _onFirmwareProgress(self, progress: int, max_progress: int = 100) -> None:
self._firmware_progress = (progress / max_progress) * 100 # Convert to scale of 0-100 self._firmware_progress = int(progress * 100 / max_progress) # Convert to scale of 0-100
self.firmwareProgressChanged.emit() self.firmwareProgressChanged.emit()

View file

@ -20,15 +20,15 @@ class GenericOutputController(PrinterOutputController):
self._preheat_bed_timer = QTimer() self._preheat_bed_timer = QTimer()
self._preheat_bed_timer.setSingleShot(True) self._preheat_bed_timer.setSingleShot(True)
self._preheat_bed_timer.timeout.connect(self._onPreheatBedTimerFinished) self._preheat_bed_timer.timeout.connect(self._onPreheatBedTimerFinished)
self._preheat_printer = None #type: Optional[PrinterOutputModel] self._preheat_printer = None # type: Optional[PrinterOutputModel]
self._preheat_hotends_timer = QTimer() self._preheat_hotends_timer = QTimer()
self._preheat_hotends_timer.setSingleShot(True) self._preheat_hotends_timer.setSingleShot(True)
self._preheat_hotends_timer.timeout.connect(self._onPreheatHotendsTimerFinished) self._preheat_hotends_timer.timeout.connect(self._onPreheatHotendsTimerFinished)
self._preheat_hotends = set() #type: Set[ExtruderOutputModel] self._preheat_hotends = set() # type: Set[ExtruderOutputModel]
self._output_device.printersChanged.connect(self._onPrintersChanged) self._output_device.printersChanged.connect(self._onPrintersChanged)
self._active_printer = None #type: Optional[PrinterOutputModel] self._active_printer = None # type: Optional[PrinterOutputModel]
def _onPrintersChanged(self) -> None: def _onPrintersChanged(self) -> None:
if self._active_printer: if self._active_printer:
@ -54,7 +54,7 @@ class GenericOutputController(PrinterOutputController):
self._preheat_hotends_timer.stop() self._preheat_hotends_timer.stop()
for extruder in self._preheat_hotends: for extruder in self._preheat_hotends:
extruder.updateIsPreheating(False) extruder.updateIsPreheating(False)
self._preheat_hotends = set() #type: Set[ExtruderOutputModel] self._preheat_hotends = set() # type: Set[ExtruderOutputModel]
def moveHead(self, printer: "PrinterOutputModel", x, y, z, speed) -> None: def moveHead(self, printer: "PrinterOutputModel", x, y, z, speed) -> None:
self._output_device.sendCommand("G91") self._output_device.sendCommand("G91")

View file

@ -91,7 +91,7 @@ class PrintJobOutputModel(QObject):
def assignedPrinter(self): def assignedPrinter(self):
return self._assigned_printer return self._assigned_printer
def updateAssignedPrinter(self, assigned_printer: Optional["PrinterOutputModel"]): def updateAssignedPrinter(self, assigned_printer: Optional["PrinterOutputModel"]) -> None:
if self._assigned_printer != assigned_printer: if self._assigned_printer != assigned_printer:
old_printer = self._assigned_printer old_printer = self._assigned_printer
self._assigned_printer = assigned_printer self._assigned_printer = assigned_printer

View file

@ -4,7 +4,7 @@
from UM.Decorators import deprecated from UM.Decorators import deprecated
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
from UM.OutputDevice.OutputDevice import OutputDevice from UM.OutputDevice.OutputDevice import OutputDevice
from PyQt5.QtCore import pyqtProperty, QObject, QTimer, pyqtSignal from PyQt5.QtCore import pyqtProperty, pyqtSignal, QObject, QTimer, QUrl
from PyQt5.QtWidgets import QMessageBox from PyQt5.QtWidgets import QMessageBox
from UM.Logger import Logger from UM.Logger import Logger
@ -12,9 +12,10 @@ from UM.FileHandler.FileHandler import FileHandler #For typing.
from UM.Scene.SceneNode import SceneNode #For typing. from UM.Scene.SceneNode import SceneNode #For typing.
from UM.Signal import signalemitter from UM.Signal import signalemitter
from UM.Qt.QtApplication import QtApplication from UM.Qt.QtApplication import QtApplication
from UM.FlameProfiler import pyqtSlot
from enum import IntEnum # For the connection state tracking. from enum import IntEnum # For the connection state tracking.
from typing import Callable, List, Optional from typing import Callable, List, Optional, Union
MYPY = False MYPY = False
if MYPY: if MYPY:
@ -230,4 +231,11 @@ class PrinterOutputDevice(QObject, OutputDevice):
return self._firmware_name return self._firmware_name
def getFirmwareUpdater(self) -> Optional["FirmwareUpdater"]: def getFirmwareUpdater(self) -> Optional["FirmwareUpdater"]:
return self._firmware_updater return self._firmware_updater
@pyqtSlot(str)
def updateFirmware(self, firmware_file: Union[str, QUrl]) -> None:
if not self._firmware_updater:
return
self._firmware_updater.updateFirmware(firmware_file)

View file

@ -15,6 +15,7 @@ MYPY = False
if MYPY: if MYPY:
from cura.PrinterOutput.FirmwareUpdater import FirmwareUpdater from cura.PrinterOutput.FirmwareUpdater import FirmwareUpdater
from cura.PrinterOutput.PrinterOutputDevice import PrinterOutputDevice from cura.PrinterOutput.PrinterOutputDevice import PrinterOutputDevice
from UM.Settings.ContainerInterface import ContainerInterface
catalog = i18nCatalog("cura") catalog = i18nCatalog("cura")
@ -25,15 +26,15 @@ class FirmwareUpdaterMachineAction(MachineAction):
self._qml_url = "FirmwareUpdaterMachineAction.qml" self._qml_url = "FirmwareUpdaterMachineAction.qml"
ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded) ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)
self._active_output_device = None #type: Optional[PrinterOutputDevice] self._active_output_device = None # type: Optional[PrinterOutputDevice]
self._active_firmware_updater = None #type: Optional[FirmwareUpdater] self._active_firmware_updater = None # type: Optional[FirmwareUpdater]
CuraApplication.getInstance().engineCreatedSignal.connect(self._onEngineCreated) CuraApplication.getInstance().engineCreatedSignal.connect(self._onEngineCreated)
def _onEngineCreated(self) -> None: def _onEngineCreated(self) -> None:
CuraApplication.getInstance().getMachineManager().outputDevicesChanged.connect(self._onOutputDevicesChanged) CuraApplication.getInstance().getMachineManager().outputDevicesChanged.connect(self._onOutputDevicesChanged)
def _onContainerAdded(self, container) -> None: def _onContainerAdded(self, container: "ContainerInterface") -> None:
# Add this action as a supported action to all machine definitions if they support USB connection # Add this action as a supported action to all machine definitions if they support USB connection
if isinstance(container, DefinitionContainer) and container.getMetaDataEntry("type") == "machine" and container.getMetaDataEntry("supports_usb_connection"): if isinstance(container, DefinitionContainer) and container.getMetaDataEntry("type") == "machine" and container.getMetaDataEntry("supports_usb_connection"):
CuraApplication.getInstance().getMachineActionManager().addSupportedAction(container.getId(), self.getKey()) CuraApplication.getInstance().getMachineActionManager().addSupportedAction(container.getId(), self.getKey())

View file

@ -16,7 +16,7 @@ Cura.MachineAction
anchors.fill: parent; anchors.fill: parent;
property bool printerConnected: Cura.MachineManager.printerConnected property bool printerConnected: Cura.MachineManager.printerConnected
property var activeOutputDevice: printerConnected ? Cura.MachineManager.printerOutputDevices[0] : null property var activeOutputDevice: printerConnected ? Cura.MachineManager.printerOutputDevices[0] : null
property var canUpdateFirmware: activeOutputDevice ? activeOutputDevice.activePrinter.canUpdateFirmware : false property bool canUpdateFirmware: activeOutputDevice ? activeOutputDevice.activePrinter.canUpdateFirmware : false
Column Column
{ {
@ -51,7 +51,7 @@ Cura.MachineAction
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
width: childrenRect.width width: childrenRect.width
spacing: UM.Theme.getSize("default_margin").width spacing: UM.Theme.getSize("default_margin").width
property var firmwareName: Cura.MachineManager.activeMachine.getDefaultFirmwareName() property string firmwareName: Cura.MachineManager.activeMachine.getDefaultFirmwareName()
Button Button
{ {
id: autoUpgradeButton id: autoUpgradeButton

View file

@ -15,8 +15,6 @@ from cura.PrinterOutput.GenericOutputController import GenericOutputController
from .AutoDetectBaudJob import AutoDetectBaudJob from .AutoDetectBaudJob import AutoDetectBaudJob
from .AvrFirmwareUpdater import AvrFirmwareUpdater from .AvrFirmwareUpdater import AvrFirmwareUpdater
from PyQt5.QtCore import pyqtSlot, pyqtSignal, pyqtProperty, QUrl
from serial import Serial, SerialException, SerialTimeoutException from serial import Serial, SerialException, SerialTimeoutException
from threading import Thread, Event from threading import Thread, Event
from time import time, sleep from time import time, sleep
@ -98,13 +96,6 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
application = CuraApplication.getInstance() application = CuraApplication.getInstance()
application.triggerNextExitCheck() application.triggerNextExitCheck()
@pyqtSlot(str)
def updateFirmware(self, file: Union[str, QUrl]) -> None:
if not self._firmware_updater:
return
self._firmware_updater.updateFirmware(file)
## Reset USB device settings ## Reset USB device settings
# #
def resetDeviceSettings(self) -> None: def resetDeviceSettings(self) -> None:
@ -169,7 +160,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
self._baud_rate = baud_rate self._baud_rate = baud_rate
def connect(self): def connect(self):
self._firmware_name = None # after each connection ensure that the firmware name is removed self._firmware_name = None # after each connection ensure that the firmware name is removed
if self._baud_rate is None: if self._baud_rate is None:
if self._use_auto_detect: if self._use_auto_detect: