Skip to content

Commit

Permalink
Improve OTA app
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewWilkes committed May 28, 2024
1 parent 7e34d3c commit 08a8fcf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
2 changes: 0 additions & 2 deletions modules/app_components/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,13 @@ 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

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:
Expand Down
86 changes: 46 additions & 40 deletions modules/system/ota/ota.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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:")
Expand All @@ -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
6 changes: 3 additions & 3 deletions modules/wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 08a8fcf

Please sign in to comment.