# @name UpdateCheck # @desc Appears only when a newer AWTRIX NG release exists # @author Blueforcer # @version 1.0 import string class UpdateCheck var installed, latest, ticks, busy def init() self.installed = nil self.latest = nil self.ticks = 0 self.busy = false end # "1.0.13" -> 1000013, so 1.0.9 sorts below 1.0.13. A "-dev" suffix is dropped. def rank(v) var d = string.find(v, "-") if d >= 0 v = v[0 .. d - 1] end var p = string.split(v, ".") var n = 0 for i : 0 .. 2 n = n * 1000 + (i < size(p) ? int(p[i]) : 0) end return n end # Keeps the first run of digits and dots, so "1.0.13\n" and {"version":"1.0.13"} # both reduce to the bare number. def digits(s) var v = "" for i : 0 .. size(s) - 1 var c = s[i] if (c >= "0" && c <= "9") || c == "." v += c elif v != "" break end end return v end def newer() if self.installed == nil || self.latest == nil return false end return self.rank(self.latest) > self.rank(self.installed) end # The whole point: no update, no turn in the rotation. def should_show() return self.newer() end def on_self(body, status) self.busy = false log("uc: self st=" + str(status) + " body=" + (body == nil ? "nil" : body)) if body == nil return end var v = self.digits(body) if v == "" return end self.installed = v end def on_github(body, status) self.busy = false self.ticks = 300 # retry in 5 min unless we parsed something if body == nil return end var v = self.digits(body) if v == "" return end self.latest = v self.ticks = 21600 # 6 h only once it actually worked log("uc: latest=" + v + " newer=" + str(self.newer())) end def loop() if !self.busy && self.ticks <= 0 self.busy = true if self.installed == nil self.ticks = 10 http.get("http://localhost/api/v1/version", / b, st -> self.on_self(b, st)) else self.ticks = 21600 http.get("https://raw.githubusercontent.com/Blueforcer/awtrix-ng/main/version", / b, st -> self.on_github(b, st)) end end self.ticks -= 1 end # A download arrow into a tray, 8x8 at the left edge. def arrow(col) rect_fill(3, 0, 2, 3, col) line(1, 3, 6, 3, col) line(2, 4, 5, 4, col) line(3, 5, 4, 5, col) line(1, 7, 6, 7, col) end def draw() clear() if !self.newer() text(1, 6, self.installed == nil ? "..." : self.installed, 0x404040) return end var ph = int(now_ms() / 12) % 360 self.arrow(hsv(ph, 100, 100)) var s = "UPDATE" var x = 9 for i : 0 .. size(s) - 1 x += text(x, 6, s[i], hsv((ph + i * 40) % 360, 100, 100)) end end end return UpdateCheck()