From 08a8fcff8b491531adac2b3076cc01387bedc12f Mon Sep 17 00:00:00 2001 From: Matthew Wilkes Date: Wed, 29 May 2024 00:26:33 +0100 Subject: [PATCH] Improve OTA app --- modules/app_components/layout.py | 2 - modules/system/ota/ota.py | 86 +++++++++++++++++--------------- modules/wifi.py | 6 +-- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/modules/app_components/layout.py b/modules/app_components/layout.py index 1b3e2f0..5fab83c 100644 --- a/modules/app_components/layout.py +++ b/modules/app_components/layout.py @@ -143,7 +143,6 @@ def centred_component(self): cumulative_height = 0 for item in self.items: cumulative_height += item.height * self.scale_factor - print(f"Y: {self.y_offset}, cumulative: {cumulative_height}") if round(cumulative_height) > round(120 - self.y_offset): return item return item @@ -151,7 +150,6 @@ def centred_component(self): async def button_event(self, event) -> bool: focused = self.centred_component() to_jump = min(focused.height * self.scale_factor, 60) - print(f"Y: {self.y_offset}, Jump: {to_jump}") if BUTTON_TYPES["UP"] in event.button: self.y_offset += to_jump if self.y_offset > 120: diff --git a/modules/system/ota/ota.py b/modules/system/ota/ota.py index e6a4050..feda1ec 100644 --- a/modules/system/ota/ota.py +++ b/modules/system/ota/ota.py @@ -1,22 +1,36 @@ +import asyncio import async_helpers from app import App from esp32 import Partition import machine from app_components.dialog import YesNoDialog +from app_components import layout, tokens import network import ota import ntptime import requests import wifi +from system.eventbus import eventbus +from system.scheduler.events import RequestStopAppEvent class OtaUpdate(App): def __init__(self): self.status = None super().__init__() + self.status = layout.DefinitionDisplay("Status", "") + self.old_version = layout.DefinitionDisplay("Current", "") + self.new_version = layout.DefinitionDisplay("New", "") + self.layout = layout.LinearLayout( + [self.status, self.old_version, self.new_version] + ) + + def minimise(self): + # Close this app each time + eventbus.emit(RequestStopAppEvent(self)) async def run(self, render_update): - self.status = "Checking version" + self.status.value = "Checking version" try: Partition(Partition.RUNNING) @@ -37,21 +51,13 @@ async def run(self, render_update): version = ota.get_version() if version == "HEAD-HASH-NOTFOUND": version = "Custom" - # window.println("Boot: " + current.info()[4]) - # window.println("Next: " + nxt.info()[4]) - # window.println("Version: " + version) - # window.println() - # line = window.get_next_line() - dialog = YesNoDialog([f"Current: {version}", "Check for updates?"], self) - # Wait for an answer from the dialogue - if not await dialog.run(render_update): - self.minimise() - return - else: - self.status = "Connecting" - await self.connect(render_update) - self.status = "Connected" - await self.otaupdate(render_update) + self.old_version.value = version + + await render_update() + + await self.connect(render_update) + await self.otaupdate(render_update) + self.minimise() async def connect(self, render_update): # window = self.window @@ -64,19 +70,18 @@ async def connect(self, render_update): if not wifi.status(): wifi.connect() while True: - print("Connecting to") - print(f"{ssid}...") + self.status.value = f"Connecting to {ssid}" + await render_update() + if wifi.wait(): # Returning true means connected break - # window.println("WiFi timed out", line) - # window.println("[A] to retry", line + 1) dialog = YesNoDialog("Retry connection?", self) self.overlays = [dialog] + # Wait for an answer from the dialogue if await dialog.run(render_update): - self.overlays = [] if wifi.get_sta_status() == network.STAT_CONNECTING: pass # go round loop and keep waiting else: @@ -93,10 +98,11 @@ async def otaupdate(self, render_update): self.confirmed = False retry = True + self.status.value = "Searching for OTA" + await render_update() + while retry: # window.clear_from_line(line) - self.status = "Updating..." - response = await async_helpers.unblock( requests.head, render_update, @@ -110,7 +116,7 @@ async def otaupdate(self, render_update): result = await async_helpers.unblock( ota.update, render_update, - lambda version, val: self.progress(version, val), + self.progress, url, ) retry = False @@ -130,20 +136,24 @@ async def otaupdate(self, render_update): # window.println("Press [A] to") # window.println("reboot and") # window.println("finish update.") - dialog = YesNoDialog("Reboot", self) - # Wait for an answer from the dialogue - if await dialog.run(render_update): - machine.reset() + self.status.value = "Rebooting" + await render_update() + await asyncio.sleep(5) + machine.reset() else: + await render_update() + await asyncio.sleep(5) + self.minimise() print("Update cancelled") def progress(self, version, val): - # window = self.window + self.new_version.value = version + if not self.confirmed: if len(version) > 0: - if version == ota.get_version(): - self.status = "No new version" - # window.println("available.") + self.new_version.value = version + if version <= ota.get_version(): + self.status.value = "No update needed" return False print("New version:") @@ -162,16 +172,12 @@ def progress(self, version, val): # window.println("Updating...") # window.println() - self.version = version self.progress_pct = val - self.status = f"{val} %" - print(version, val) - # window.progress_bar(window.get_next_line(), val) + self.status.value = f"Downloading ({val} %)" return True def draw(self, ctx): # print("draw") - ctx.rgb(1, 0, 0).rectangle(-120, -120, 240, 240).fill() - if self.status: - ctx.rgb(1, 1, 1).move_to(-50, 0).text(str(self.status)) - self.draw_overlays(ctx) + tokens.clear_background(ctx) + self.layout.draw(ctx) + self.layout.y_offset = 70 diff --git a/modules/wifi.py b/modules/wifi.py index 3bb5f08..b9722f6 100644 --- a/modules/wifi.py +++ b/modules/wifi.py @@ -9,9 +9,9 @@ DEFAULT_CONNECT_TIMEOUT = 20 DEFAULT_TX_POWER = 20 -DEFAULT_SSID = "emfcamp" -DEFAULT_USERNAME = "badge" -DEFAULT_PASSWORD = "badge" +DEFAULT_SSID = "emf2024-open" +DEFAULT_USERNAME = None +DEFAULT_PASSWORD = None WIFI_AUTH_OPEN = 0 WIFI_AUTH_WEP = 1