-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspaceteam.py
executable file
·110 lines (85 loc) · 2.69 KB
/
spaceteam.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
#!/usr/bin/env python2.7
import spaceteam
from spaceteam import Client
from spaceteam import peripherals
from spaceteam import state
import time
import sdnotify
SERVER_IP = '10.110.0.1'
def updates(prev, new):
diffs = {}
for k in prev:
if prev[k] != new[k]:
diffs[k] = new[k]
return diffs
def main(args):
# load and initialize our peripherals
peripherals.reset_all()
# begin looping over them, reading their state
client = None
try:
# start communication with peripherals
peripherals.read_all()
# initialize an announce message
announce = state.announce()
# initialize client connection
if '--local' in args:
print "Acting in local mode!"
else:
client = Client(SERVER_IP)
client.start(announce)
# initialize systemd notifications
notifier = sdnotify.SystemdNotifier()
notifier.notify("READY=1")
# loop, generating new state each time
prev_state = state.generate()
while True:
# check that everything is still working
if client and not client.running():
raise RuntimeError("The client has stopped!")
notifier.notify("WATCHDOG=1")
# read any peripherals
peripherals.read_all()
# first, deal with any state updates
new_state = state.generate()
for id, val in updates(prev_state, new_state).items():
if client:
client.update(id, val)
else:
i = [i for i in state.INPUTS if i['id'] == id]
if len(i) > 1:
print 'there are two controls with id %s!' % id
else:
i = i[0]
try:
act = i['actions'][str(val)]
print 'just did action %s' % act
except KeyError:
print 'changed %s to %s but no associated action' % (id, val)
time.sleep(0.001)
# update the state
prev_state = new_state
# deal with any messages from the server
if not client:
time.sleep(0.001)
continue
inst = client.get_instruction()
while inst is not None:
if inst['type'] == 'display':
peripherals.DISPLAY.message = inst['message']
elif inst['type'] == 'progress':
peripherals.RED_BAR.update_value(inst['message'] / 10)
peripherals.ORANGE_BAR.update_value(inst['message'] / 10)
elif inst['type'] == 'status':
peripherals.DISPLAY.status = inst['message']
elif inst['type'] == 'integrity':
peripherals.INTEGRITY.update(inst['message'])
# get next instruction
inst = client.get_instruction()
finally:
if client:
client.stop()
# run spaceteam!
import sys
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))