Put the firmware-update meta-data in the 'normal' printer definitions and make the code handle that.

This commit is contained in:
Remco Burema 2018-10-13 21:55:33 +02:00
parent 931143ceaa
commit 2e3abbc904
8 changed files with 73 additions and 113 deletions

View file

@ -9,7 +9,7 @@ from UM.Version import Version
import urllib.request
from urllib.error import URLError
from typing import Dict
from typing import Dict, Optional
from .FirmwareUpdateCheckerLookup import FirmwareUpdateCheckerLookup, getSettingsKeyForMachine
from .FirmwareUpdateCheckerMessage import FirmwareUpdateCheckerMessage
@ -25,13 +25,15 @@ class FirmwareUpdateCheckerJob(Job):
ZERO_VERSION = Version(STRING_ZERO_VERSION)
EPSILON_VERSION = Version(STRING_EPSILON_VERSION)
def __init__(self, container, silent, lookups: FirmwareUpdateCheckerLookup, callback) -> None:
def __init__(self, container, silent, machine_name, metadata, callback) -> None:
super().__init__()
self._container = container
self.silent = silent
self._callback = callback
self._lookups = lookups
self._machine_name = machine_name
self._metadata = metadata
self._lookups = None # type:Optional[FirmwareUpdateCheckerLookup]
self._headers = {} # type:Dict[str, str] # Don't set headers yet.
def getUrlResponse(self, url: str) -> str:
@ -50,10 +52,12 @@ class FirmwareUpdateCheckerJob(Job):
raw_str = response.split("\n", 1)[0].rstrip()
return Version(raw_str)
def getCurrentVersionForMachine(self, machine_id: int) -> Version:
def getCurrentVersion(self) -> Version:
max_version = self.ZERO_VERSION
if self._lookups is None:
return max_version
machine_urls = self._lookups.getCheckUrlsFor(machine_id)
machine_urls = self._lookups.getCheckUrls()
if machine_urls is not None:
for url in machine_urls:
version = self.parseVersionResponse(self.getUrlResponse(url))
@ -61,16 +65,20 @@ class FirmwareUpdateCheckerJob(Job):
max_version = version
if max_version < self.EPSILON_VERSION:
Logger.log("w", "MachineID {0} not handled!".format(repr(machine_id)))
Logger.log("w", "MachineID {0} not handled!".format(self._lookups.getMachineName()))
return max_version
def run(self):
if self._lookups is None:
Logger.log("e", "Can not check for a new release. URL not set!")
return
self._lookups = FirmwareUpdateCheckerLookup(self._machine_name, self._metadata)
try:
# Initialize a Preference that stores the last version checked for this printer.
Application.getInstance().getPreferences().addPreference(
getSettingsKeyForMachine(self._lookups.getMachineId()), "")
# Get headers
application_name = Application.getInstance().getApplicationName()
application_version = Application.getInstance().getVersion()
self._headers = {"User-Agent": "%s - %s" % (application_name, application_version)}
@ -79,11 +87,11 @@ class FirmwareUpdateCheckerJob(Job):
machine_name = self._container.definition.getName()
# If it is not None, then we compare between the checked_version and the current_version
machine_id = self._lookups.getMachineByName(machine_name.lower())
machine_id = self._lookups.getMachineId()
if machine_id is not None:
Logger.log("i", "You have a(n) {0} in the printer list. Let's check the firmware!".format(machine_name))
current_version = self.getCurrentVersionForMachine(machine_id)
current_version = self.getCurrentVersion()
# If it is the first time the version is checked, the checked_version is ""
setting_key_str = getSettingsKeyForMachine(machine_id)
@ -99,7 +107,7 @@ class FirmwareUpdateCheckerJob(Job):
# notify the user when no new firmware version is available.
if (checked_version != "") and (checked_version != current_version):
Logger.log("i", "SHOWING FIRMWARE UPDATE MESSAGE")
message = FirmwareUpdateCheckerMessage(machine_id, machine_name)
message = FirmwareUpdateCheckerMessage(machine_id, machine_name, self._lookups.getRedirectUserUrl())
message.actionTriggered.connect(self._callback)
message.show()
else: