This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynapse.py
180 lines (153 loc) · 5.92 KB
/
synapse.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import sys
import time
import requests
from multiprocessing import Process, Value
import subprocess
import signal
import attention
import heartrate
import proximity
import lights
import button
import camera
DATA_URL = "http://192.168.42.1/data/"
ENABLE_PUBLISH = False
class Synapse:
STATE_HEART_MIND = 0
STATE_PROXIMITY = 1
STATES = [STATE_HEART_MIND, STATE_PROXIMITY]
def __init__(self):
self.attention = attention.Attention()
self.heartrate = heartrate.HeartBeat()
self.proximity = proximity.Proxemic()
self.button = button.Button()
self.state_value = Value('i', Synapse.STATES[0])
self.attention_value = Value('d', 0)
self.heartrate_value = Value('i', 60)
self.proximity_space_value = Value('i', -1)
self.proximity_value = Value('d', 0)
self.lights = lights.SynapseLights()
self.lights.set_heart(self.heartrate_value)
self.lights.set_mind(self.attention_value)
self.lights.set_space(self.proximity_space_value)
self.lights.set_space_enable(self.state_value)
self.copying_files = Value('b', False)
self.camera_process = Process(target=self.handle_camera)
def start(self):
self.button.monitor_button(self.on_button)
self.proximity.monitor_space(self.update_proximity_space, self.update_proximity)
self.heartrate.monitor_heartrate(self.update_heartrate)
self.attention.monitor_attention(self.update_attention)
self.camera_process.start()
self.lights.reset()
self.lights.start()
def stop(self):
self.lights.stop()
def join(self):
try:
while True:
if self.lights.join(0.500):
break
except KeyboardInterrupt:
self.on_interrupt()
def on_interrupt(self):
print >>sys.stderr, "\nInterrupt caught"
self.stop()
sys.exit(0)
def on_button(self):
self.state_value.value = Synapse.STATES[(self.state_value.value + 1) % len(Synapse.STATES)]
print >>sys.stderr, "BUTTON: state=" + str(self.state_value.value)
return True
def update_heartrate(self, value):
if self.state_value.value == Synapse.STATE_HEART_MIND:
print >>sys.stderr, "EKG:", value
self.heartrate_value.value = value
self.publish_value(self.heartrate, value)
return True
def update_attention(self, value):
if self.state_value.value == Synapse.STATE_HEART_MIND:
print >>sys.stderr, "Attention:", value
self.attention_value.value = value
self.publish_value(self.attention, value)
return True
def update_proximity_space(self, value):
if self.state_value.value == Synapse.STATE_PROXIMITY:
print >>sys.stderr, "SPACE:", value
self.proximity_space_value.value = value
return True
def update_proximity(self, value):
if self.state_value.value == Synapse.STATE_PROXIMITY:
print >>sys.stderr, "PRX:", value
self.proximity_value.value = value
self.publish_value(self.proximity, value)
return True
def publish_value(self, source, value):
if not ENABLE_PUBLISH:
return
#timestamp = time.now()
if source is self.attention:
url_id = "attention"
elif source is self.heartrate:
url_id = "heartrate"
elif source is self.proximity:
url_id = "proximity"
elif source is camera:
if '.JPG' in value:
url_id = 'image'
elif '.mp4' in value:
url_id = 'video'
else:
raise ValueError, "invalid data source"
print url_id
else:
raise ValueError, "invalid data source"
do_post = lambda: requests.post(DATA_URL + url_id, {"value" : str(value)})
task = Process(target=do_post)
#task.setDaemon(True)
task.start()
def handle_camera(self):
recording = False
while True:
time.sleep(0.5)
if self.attention_value.value >= 80:
if not recording:
camera.take_picture()
print 'sleeping 2 seconds'
time.sleep(2)
if self.attention_value.value >= 80:
camera.start_recording()
recording = True
print 'sleeping 2 seconds'
time.sleep(2)
else:
if recording:
camera.stop_recording()
recording = False
if not self.copying_files.value:
Process(target=self.copy_camera_files).start()
print 'sleeping 1 second'
time.sleep(1)
def copy_camera_files(self):
self.copying_files.value = True
files = camera.get_latest_files(None)
if len(files) > 0:
for file in files:
print 'copying latest pictures & videos to web server'
child = subprocess.Popen(['wget', '-nc', file, '-P', '/home/root/synapse/web_server/images/'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
streamdata = child.communicate()
stdout = streamdata[0]
stderr = streamdata[1]
print streamdata
if 'already there' not in stderr:
print 'publishing!'
value = file.replace('http://' + camera.IP + '/DCIM/100DRIFT/', '')
self.publish_value(camera, value)
self.copying_files.value = False
def main(args):
synapse = Synapse()
synapse.start()
synapse.join()
if __name__ == '__main__':
main(sys.argv[1:])