forked from UrLab/hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.py
239 lines (202 loc) · 7.66 KB
/
daemon.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from ambianceduino import Ambianceduino
from urllib import urlopen
from datetime import datetime
import json
import puka
from math import log, sqrt
from time import sleep
from threading import Thread
from logbook import Logger, FileHandler
import traceback
import sys
import os
from contextlib import contextmanager
from config import *
# @contextmanager
# def nbopen(name, flag):
# f = os.open(name, flag)
# fo = os.fdopen(f)
# yield fo
# os.close(f)
def getMsbFromAudio(audio_source):
msb = ord(audio_source.read(1))
audio_source.read(1)
return (msb & 0x7f) - (msb & 0x80) # 2's complement
def avg_for_key(list_of_dictionaries, key):
"""
[{'a': 1}, {'a': 5}], 'a' # => 3
"""
return sum([x[key] for x in list_of_dictionaries]) / len(list_of_dictionaries)
def convertThermistor(analogValue, resistance, beta):
vout = 5 * analogValue / 1023.0
r = resistance * (5 - vout) / vout
return (beta / (log(r / resistance) + beta / 298.15)) - 273.15
class AmbianceDaemon(Ambianceduino):
def __init__(self, *args, **kwargs):
self.logger = Logger('Daemon')
super(AmbianceDaemon, self).__init__(*args, **kwargs)
self.current_delay = None
self.powered = None
self.meteo = []
self.__init_puka_client()
self.anims_uploaded = 0
def delay_red(self, milliseconds):
self.delay('R', milliseconds)
def delay_blue(self, milliseconds):
self.delay('B', milliseconds)
def __spacestatus(self):
try:
statuspage = urlopen(STATUS_URL)
payload = json.loads(statuspage.read())
if 'state' in payload:
if payload['state'] == 'open' and not self.powered:
self.on()
elif payload['state'] == 'closed' and self.powered:
self.off()
except Exception as err:
self.logger.error("%s: %s" % (err.__class__.__name__, err.message))
def __peoplecount(self):
try:
pamelapage = urlopen('http://pamela.urlab.be/mac.json')
payload = json.loads(pamelapage.read())
people = len(payload['color']) + len(payload['grey'])
d = int(25 / log(people + 2))
if d != self.current_delay:
self.delay_red(d)
except Exception as err:
self.logger.error("%s: %s" % (err.__class__.__name__, err.message))
def __init_puka_client(self):
try:
self.puka_client = puka.Client(AMQ_SERVER)
promise = self.puka_client.connect()
self.puka_client.wait(promise)
self.logger.info("Connected AMQ to %s" % (AMQ_SERVER))
promise = self.puka_client.queue_declare(queue=METEO_QUEUE)
self.puka_client.wait(promise)
self.logger.info("Got queue %s" % (METEO_QUEUE))
promise = self.puka_client.queue_declare(queue=EVENTS_QUEUE)
self.puka_client.wait(promise)
self.logger.info("Got queue %s" % (EVENTS_QUEUE))
except:
self.logger.error("Unable to connect to %s#%s" % (AMQ_SERVER, METEO_QUEUE))
self.puka_client = None
def __send_message(self, queue, json_dumpable):
if self.puka_client:
msg = json.dumps(json_dumpable)
promise = self.puka_client.basic_publish(
exchange='',
routing_key=queue,
body=msg
)
self.puka_client.wait(promise)
self.logger.info('Sent [%s] << %s' % (queue, msg[:50]))
def __http_requests(self):
i = 0
while self.running:
self.analogs()
if i == 0:
self.__spacestatus()
self.__peoplecount()
i = (i + 1) % 20
sleep(1)
def stop(self):
with open("/tmp/mpd.fifo", "w") as fifo:
fifo.write(chr(0) * (80200 / 14))
super(Ambianceduino, self).stop()
def run(self):
super(AmbianceDaemon, self).run()
self.requestsThread = Thread(target=self.__http_requests, name="http-request")
self.threads.append(self.requestsThread)
self.requestsThread.start()
self.mpdThread = Thread(target=self.__mpd_loop, name="mpd")
self.threads.append(self.mpdThread)
self.mpdThread.start()
while True:
for t in self.threads:
if not t.is_alive():
self.logger.error("One of my children is dead, suicide.")
self.stop()
exit()
sleep(0.5)
def default_handler(self, *args):
self.logger.info(' '.join(map(str, args)))
def when_on(self):
self.powered = True
self.logger.info('Powered on')
def when_off(self):
self.powered = False
self.logger.info('Powered off')
def when_delay(self, output, delay):
if output == 'R':
self.current_delay = delay
self.logger.info('Delay RED set to %d' % (delay))
elif output == 'B':
self.logger.info('Delay BLUE set to %d' % (delay))
def when_bell(self):
self.__send_message(EVENTS_QUEUE, {
'trigger': 'bell', 'time': str(datetime.now())
})
def when_door(self):
if not self.powered:
self.__send_message(EVENTS_QUEUE, {
'trigger': 'door', 'time': str(datetime.now())
})
def when_radiator(self):
self.__send_message(EVENTS_QUEUE, {
'trigger': 'radiator', 'time': str(datetime.now())
})
def when_anim(self, anim_name, anim_length):
self.anims_uploaded += 1
def when_analogs(self, analogs):
self.meteo.append(analogs)
if len(self.meteo) == METEO_LEN:
self.__send_message(METEO_QUEUE, {
'time': str(datetime.now()),
'light': {
'inside': avg_for_key(self.meteo, 'light_in') / 10.23,
'outside': avg_for_key(self.meteo, 'light_out') / 10.23
},
'temperature': {
'radiator': convertThermistor(avg_for_key(self.meteo, 'temp_radia'), 4700, 3950),
'ambiant': convertThermistor(avg_for_key(self.meteo, 'temp_amb'), 2200, 3950)
}
})
self.meteo = []
def __mpd_loop(self):
SAMPLES_PER_FRAME = 25
AUDIO_SAMPLE_RATE = 44100
FRAMES_PER_SECOND = 14
SAMPLES_TO_DROP = (AUDIO_SAMPLE_RATE / FRAMES_PER_SECOND) - SAMPLES_PER_FRAME
self.delay('B', int(1000 / FRAMES_PER_SECOND))
#with nbopen("/tmp/mpd.fifo", os.O_RDONLY | os.O_NONBLOCK) as sound:
with open("/tmp/mpd.fifo", "rb") as sound:
while self.running:
if not self.powered:
sleep(1)
continue
sound.read(SAMPLES_TO_DROP * 2)
s = 0
for i in range(SAMPLES_PER_FRAME):
s += getMsbFromAudio(sound) ** 2
x = sqrt(s / SAMPLES_PER_FRAME)
self.upload_anim('B', [int(1.0446300614125956 ** x) - 1])
if __name__ == "__main__":
# log_handler = FileHandler('daemon.log')
# log_handler.push_application()
logger = Logger('Main')
logger.info('Starting')
try:
a = AmbianceDaemon(logger=logger, boot_time=2)
logger.info('Got Ambianceduino version ' + a.version)
a.run()
except KeyboardInterrupt:
a.stop()
logger.info('Stopping')
exit()
except Exception as err:
logger.error("%s: %s" % (err.__class__.__name__, err.message))
print traceback.format_exc()
try:
a.stop()
except NameError:
pass