Skip to content

Commit a1b4c7b

Browse files
committed
Fix progress bar not reaching 100%
1 parent a4806e8 commit a1b4c7b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

scripts/ota_updater/ota_updater.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import base64, sys, math
66
from hashlib import md5
77

8+
# Global variable for total bytes to transfer
9+
total = 0
10+
811
# The callback for when the client receives a CONNACK response from the server.
912
def on_connect(client, userdata, flags, rc):
1013
if rc != 0:
@@ -19,27 +22,36 @@ def on_connect(client, userdata, flags, rc):
1922

2023
print("Waiting for device to come online...")
2124

25+
# Called from on_message to print a progress bar
26+
def on_progress(progress, total):
27+
g_total = total
28+
bar_width = 30
29+
bar = int(bar_width*(progress/total))
30+
print("\r[", '+'*bar, ' '*(bar_width-bar), "] ", progress, end='', sep='')
31+
if (progress == total):
32+
print()
33+
sys.stdout.flush()
2234

2335
# The callback for when a PUBLISH message is received from the server.
2436
def on_message(client, userdata, msg):
37+
global total
2538
# decode string for python2/3 compatiblity
2639
msg.payload = msg.payload.decode()
2740

2841
if msg.topic.endswith('$implementation/ota/status'):
2942
status = int(msg.payload.split()[0])
3043

3144
if userdata.get("published"):
32-
if status == 202:
45+
if status == 200:
46+
on_progress(total, total)
47+
print("Firmware uploaded successfully. Waiting for device to come back online.")
48+
sys.stdout.flush()
49+
elif status == 202:
3350
print("Checksum accepted")
3451
elif status == 206: # in progress
3552
# state in progress, print progress bar
3653
progress, total = [int(x) for x in msg.payload.split()[1].split('/')]
37-
bar_width = 30
38-
bar = int(bar_width*(progress/total))
39-
print("\r[", '+'*bar, ' '*(bar_width-bar), "] ", msg.payload.split()[1], end='', sep='')
40-
if (progress == total):
41-
print()
42-
sys.stdout.flush()
54+
on_progress(progress, total)
4355
elif status == 304: # not modified
4456
print("Device firmware already up to date with md5 checksum: {}".format(userdata.get('md5')))
4557
client.disconnect()

0 commit comments

Comments
 (0)