5
5
import base64 , sys , math
6
6
from hashlib import md5
7
7
8
+ # Global variable for total bytes to transfer
9
+ total = 0
10
+
8
11
# The callback for when the client receives a CONNACK response from the server.
9
12
def on_connect (client , userdata , flags , rc ):
10
13
if rc != 0 :
@@ -19,27 +22,36 @@ def on_connect(client, userdata, flags, rc):
19
22
20
23
print ("Waiting for device to come online..." )
21
24
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 ()
22
34
23
35
# The callback for when a PUBLISH message is received from the server.
24
36
def on_message (client , userdata , msg ):
37
+ global total
25
38
# decode string for python2/3 compatiblity
26
39
msg .payload = msg .payload .decode ()
27
40
28
41
if msg .topic .endswith ('$implementation/ota/status' ):
29
42
status = int (msg .payload .split ()[0 ])
30
43
31
44
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 :
33
50
print ("Checksum accepted" )
34
51
elif status == 206 : # in progress
35
52
# state in progress, print progress bar
36
53
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 )
43
55
elif status == 304 : # not modified
44
56
print ("Device firmware already up to date with md5 checksum: {}" .format (userdata .get ('md5' )))
45
57
client .disconnect ()
0 commit comments