-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay.py
122 lines (98 loc) · 3.59 KB
/
Display.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Display.py
manages the server's connections to all the robots in the swarm
- creates a subscriber for every robot in the swarm
- tracks the latest updates from the robots
- maintains an internal dictonary that is updated whenever a new message is sent to the server from a robot
"""
from comms import Comms
import json
import sys
import threading
import Interface as inter
import time
from Service import Service
class Display(Service):
def init_config(self, service_conf):
"""
Attributes:
- state: dictonary
most up to date state of the robots
- comms: Comms
Message queue manager
Set up producer to server IP on port 3000
Set up consumer on port 3100 to accept sensor data
- g: Interface
the GUI
- config: dict
config information about the swarm
"""
self.g = inter.Interface()
# wait for the user to pick a file
while (len(self.g.get_config()) == 0):
self.g.refresh_gui()
self.config = self.g.get_config()
# Connect to analytics
for item in self.config['alist']:
self.comms.add_subscriber_port(item['ip'], item['port'], item['name'])
time.sleep(0.5)
def reload(self):
"""
dumps the subscribers and state and then goes through and re checks the config
"""
self.config = self.g.get_config()
del self.comms
self.comms = Comms()
self.state.clear()
# Connect to analytics
for item in self.config['alist']:
self.comms.add_subscriber_port(item['ip'], item['port'], item['name'])
time.sleep(0.5)
def update_options(self):
"""
Placeholder for when dispatcher becomes a thing
"""
pass
def transform(self):
"""
used to listen for messages from the subscribers and update the internal state dictonary with new data when new messages are received
"""
for topic in self.comms.subscriber_ports.keys():
msg_recv = self.comms.get(topic)
if msg_recv is not None:
if msg_recv.topic in self.state.keys():
self.state[msg_recv.topic].update(msg_recv.payload)
else:
self.state[msg_recv.topic] = msg_recv.payload
return None
def run(self):
"""
indefinitely checks for new messages and updates the GUI with the current status of the robots
Updates to GUI are sent even if no messages have been recieved since the last update
"""
while True:
# update state and transform data
try:
self.transform()
except KeyError:
pass
# Prepare states for GUI
bot_list = list()
for ip in self.state.keys():
ret_state = dict()
ret_state[ip] = dict()
for key in self.state[ip].keys():
#ret_state[ip][key] = key + " : " + str(self.state[ip][key])
ret_state[ip][key] = str(self.state[ip][key])
bot_list.append(ret_state)
# Update GUI
self.g.refresh_gui()
for bot in bot_list:
self.g.update_display(bot)
# Refresh Display if GUI has reloadd
if self.g.get_reload() == True:
self.reload()
self.g.inform_reload()
if __name__ == "__main__":
display = Display(sys.argv[1])
display.run()