forked from peterso/robotlearningblock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugClient.py
More file actions
85 lines (65 loc) · 3.05 KB
/
debugClient.py
File metadata and controls
85 lines (65 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#from Andrew Pasika at Kaa to debug OTA update messages
import itertools
import json
import queue
import random
import string
import sys
import time
import paho.mqtt.client as mqtt
KPC_HOST = "mqtt.cloud.kaaiot.com" # Kaa Cloud plain MQTT host
KPC_PORT = 1883 # Kaa Cloud plain MQTT port
CURRENT_SOFTWARE_VERSION = "0.0.2"
APPLICATION_VERSION = "c1v9jqmgul2l1s47m6bg-v0" # Paste your application version
ENDPOINT_TOKEN = "task-board-peter" # Paste your endpoint token
class SoftwareClient:
def __init__(self, client):
self.client = client
self.software_by_request_id = {}
self.global_request_id = itertools.count()
get_software_topic = f'kp1/{APPLICATION_VERSION}/cmx_ota/{ENDPOINT_TOKEN}/config/json/#'
self.client.message_callback_add(get_software_topic, self.handle_software)
def handle_software(self, client, userdata, message):
if message.topic.split('/')[-1] == 'status':
topic_part = message.topic.split('/')[-2]
if topic_part.isnumeric():
request_id = int(topic_part)
print(f'<--- Received software response on topic {message.topic}')
software_queue = self.software_by_request_id[request_id]
software_queue.put_nowait(message.payload)
else:
print(f'<--- Received software push on topic {message.topic}:\n{str(message.payload.decode("utf-8"))}')
else:
print(f'<--- Received bad software response on topic {message.topic}:\n{str(message.payload.decode("utf-8"))}')
def get_software(self):
request_id = next(self.global_request_id)
get_software_topic = f'kp1/{APPLICATION_VERSION}/cmx_ota/{ENDPOINT_TOKEN}/config/json/{request_id}'
software_queue = queue.Queue()
self.software_by_request_id[request_id] = software_queue
print(f'---> Requesting software by topic {get_software_topic}')
payload = {
"configId": CURRENT_SOFTWARE_VERSION
}
self.client.publish(topic=get_software_topic, payload=json.dumps(payload))
try:
software = software_queue.get(True, 5)
del self.software_by_request_id[request_id]
return str(software.decode("utf-8"))
except queue.Empty:
print('Timed out waiting for software response from server')
sys.exit()
def main():
# Initiate server connection
print(f'Connecting to Kaa server at {KPC_HOST}:{KPC_PORT} using application version {APPLICATION_VERSION} and endpoint token {ENDPOINT_TOKEN}')
client_id = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6))
client = mqtt.Client(client_id=client_id)
client.connect(KPC_HOST, KPC_PORT, 60)
client.loop_start()
software_client = SoftwareClient(client)
# Fetch current available software
retrieved_software = software_client.get_software()
print(f'Retrieved software from server: {retrieved_software}')
time.sleep(5)
client.disconnect()
if __name__ == '__main__':
main()