Skip to content

Commit 0ef86ae

Browse files
Updated firmware update example
1 parent b87f793 commit 0ef86ae

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

examples/device/firmware_update.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@
1414

1515
import time
1616
import logging
17-
from tb_device_mqtt import TBDeviceMqttClient, FW_STATE_ATTR
17+
from random import randint
18+
from tb_device_mqtt import TBDeviceMqttClient, TBFirmwareState, FW_STATE_ATTR, FW_TITLE_ATTR
1819

1920
logging.basicConfig(level=logging.INFO)
2021

22+
def on_firmware_received_example(client, firmware_data, version_to):
23+
client.update_firmware_info(state = TBFirmwareState.UPDATING)
24+
time.sleep(1)
25+
26+
with open(client.firmware_info.get(FW_TITLE_ATTR), "wb") as firmware_file:
27+
firmware_file.write(firmware_data)
28+
29+
random_value = randint(0, 5)
30+
if random_value > 3:
31+
logging.error('Dummy fail! Do not panic, just restart and try again the chance of this fail is ~20%')
32+
client.update_firmware_info(state = TBFirmwareState.FAILED, error = "Dummy fail! Do not panic, just restart and try again the chance of this fail is ~20%")
33+
else:
34+
logging.info("Successfully updated!")
35+
client.update_firmware_info(version = version_to, state = TBFirmwareState.UPDATED)
36+
2137

2238
def main():
2339
client = TBDeviceMqttClient("127.0.0.1", username="A2_TEST_TOKEN")
@@ -26,7 +42,7 @@ def main():
2642
client.get_firmware_update()
2743

2844
# Waiting for firmware to be delivered
29-
while not client.current_firmware_info[FW_STATE_ATTR] == 'UPDATED':
45+
while not client.current_firmware_info[FW_STATE_ATTR] == TBFirmwareState.UPDATED.value:
3046
time.sleep(1)
3147

3248
client.disconnect()

0 commit comments

Comments
 (0)