Skip to content

Commit

Permalink
added some error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmcaleer committed Aug 19, 2023
1 parent 663e475 commit e54d887
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions ota.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ def connect_wifi(self):
sleep(0.25)
print(f'Connected to WiFi, IP is: {sta_if.ifconfig()[0]}')

def fetch_latest_code(self):
""" Fetch the latest code from the repo."""
def fetch_latest_code(self)->bool:
""" Fetch the latest code from the repo, returns False if not found."""

# Fetch the latest code from the repo.
response = urequests.get(self.firmware_url)

print(f'Fetched latest firmware code, status: {response.status_code}, - {response.text}')
if response.status_code == 200:
print(f'Fetched latest firmware code, status: {response.status_code}, - {response.text}')

# Save the fetched code to memory
self.latest_code = response.text
return True

# Save the fetched code to memory
self.latest_code = response.text
elif response.status_code == 404:
print('Firmware not found.')
return False

def update_no_reset(self):
""" Update the code without resetting the device."""
Expand Down Expand Up @@ -75,7 +80,7 @@ def update_and_reset(self):
print('Updating device...', end='')

# Overwrite the old code.
os.rename('latest_code.py', 'main.py')
os.rename('latest_code.py', self.filename)

# Restart the device to run the new code.
print('Restarting device...')
Expand Down Expand Up @@ -107,8 +112,8 @@ def check_for_updates(self):
def download_and_install_update_if_available(self):
""" Check for updates, download and install them."""
if self.check_for_updates():
self.fetch_latest_code()
self.update_no_reset()
self.update_and_reset()
if self.fetch_latest_code():
self.update_no_reset()
self.update_and_reset()
else:
print('No new updates available.')
2 changes: 1 addition & 1 deletion test_ota.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

firmware_url = "https://raw.githubusercontent.com/kevinmcaleer/ota_test/"

ota_updater = OTAUpdater(SSID, PASSWORD, firmware_url, "test.py")
ota_updater = OTAUpdater(SSID, PASSWORD, firmware_url, "main.py")
# if ota_updater.check_for_updates():
# ota_updater.fetch_latest_code()
# ota_updater.save_code()
Expand Down

0 comments on commit e54d887

Please sign in to comment.