FirmwareUpdateCheckerJob.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Application import Application
  4. from UM.Message import Message
  5. from UM.Logger import Logger
  6. from UM.Job import Job
  7. from UM.Version import Version
  8. import urllib.request
  9. from urllib.error import URLError
  10. from typing import Dict, Optional
  11. from .FirmwareUpdateCheckerLookup import FirmwareUpdateCheckerLookup, getSettingsKeyForMachine
  12. from .FirmwareUpdateCheckerMessage import FirmwareUpdateCheckerMessage
  13. from UM.i18n import i18nCatalog
  14. i18n_catalog = i18nCatalog("cura")
  15. ## This job checks if there is an update available on the provided URL.
  16. class FirmwareUpdateCheckerJob(Job):
  17. STRING_ZERO_VERSION = "0.0.0"
  18. STRING_EPSILON_VERSION = "0.0.1"
  19. ZERO_VERSION = Version(STRING_ZERO_VERSION)
  20. EPSILON_VERSION = Version(STRING_EPSILON_VERSION)
  21. def __init__(self, silent, machine_name, metadata, callback) -> None:
  22. super().__init__()
  23. self.silent = silent
  24. self._callback = callback
  25. self._machine_name = machine_name
  26. self._metadata = metadata
  27. self._lookups = FirmwareUpdateCheckerLookup(self._machine_name, self._metadata)
  28. self._headers = {} # type:Dict[str, str] # Don't set headers yet.
  29. def getUrlResponse(self, url: str) -> str:
  30. result = self.STRING_ZERO_VERSION
  31. try:
  32. request = urllib.request.Request(url, headers = self._headers)
  33. response = urllib.request.urlopen(request)
  34. result = response.read().decode("utf-8")
  35. except URLError:
  36. Logger.log("w", "Could not reach '{0}', if this URL is old, consider removal.".format(url))
  37. return result
  38. def parseVersionResponse(self, response: str) -> Version:
  39. raw_str = response.split("\n", 1)[0].rstrip()
  40. return Version(raw_str)
  41. def getCurrentVersion(self) -> Version:
  42. max_version = self.ZERO_VERSION
  43. if self._lookups is None:
  44. return max_version
  45. machine_urls = self._lookups.getCheckUrls()
  46. if machine_urls is not None:
  47. for url in machine_urls:
  48. version = self.parseVersionResponse(self.getUrlResponse(url))
  49. if version > max_version:
  50. max_version = version
  51. if max_version < self.EPSILON_VERSION:
  52. Logger.log("w", "MachineID {0} not handled!".format(self._lookups.getMachineName()))
  53. return max_version
  54. def run(self):
  55. try:
  56. # Initialize a Preference that stores the last version checked for this printer.
  57. Application.getInstance().getPreferences().addPreference(
  58. getSettingsKeyForMachine(self._lookups.getMachineId()), "")
  59. # Get headers
  60. application_name = Application.getInstance().getApplicationName()
  61. application_version = Application.getInstance().getVersion()
  62. self._headers = {"User-Agent": "%s - %s" % (application_name, application_version)}
  63. # If it is not None, then we compare between the checked_version and the current_version
  64. machine_id = self._lookups.getMachineId()
  65. if machine_id is not None:
  66. Logger.log("i", "You have a(n) {0} in the printer list. Do firmware-check.".format(self._machine_name))
  67. current_version = self.getCurrentVersion()
  68. # This case indicates that was an error checking the version.
  69. # It happens for instance when not connected to internet.
  70. if current_version == self.ZERO_VERSION:
  71. return
  72. # If it is the first time the version is checked, the checked_version is ""
  73. setting_key_str = getSettingsKeyForMachine(machine_id)
  74. checked_version = Version(Application.getInstance().getPreferences().getValue(setting_key_str))
  75. # If the checked_version is "", it's because is the first time we check firmware and in this case
  76. # we will not show the notification, but we will store it for the next time
  77. Application.getInstance().getPreferences().setValue(setting_key_str, current_version)
  78. Logger.log("i", "Reading firmware version of %s: checked = %s - latest = %s",
  79. self._machine_name, checked_version, current_version)
  80. # The first time we want to store the current version, the notification will not be shown,
  81. # because the new version of Cura will be release before the firmware and we don't want to
  82. # notify the user when no new firmware version is available.
  83. if (checked_version != "") and (checked_version != current_version):
  84. Logger.log("i", "SHOWING FIRMWARE UPDATE MESSAGE")
  85. message = FirmwareUpdateCheckerMessage(machine_id, self._machine_name,
  86. self._lookups.getRedirectUserUrl())
  87. message.actionTriggered.connect(self._callback)
  88. message.show()
  89. else:
  90. Logger.log("i", "No machine with name {0} in list of firmware to check.".format(self._machine_name))
  91. except Exception as e:
  92. Logger.log("w", "Failed to check for new version: %s", e)
  93. if not self.silent:
  94. Message(i18n_catalog.i18nc("@info", "Could not access update information.")).show()
  95. return