-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.py
93 lines (73 loc) · 2.69 KB
/
web_server.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
import os
import sys
from datetime import datetime
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.gen
from multiprocessing.connection import Client
AUDIO_SERVER_ADDRESS = ('localhost', 6000)
WEB_SERVER_ADDRESS = ('0.0.0.0', 8090)
# The highest (practical) volume for the microphone, which is used to normalize the signal
# This depends on: microphone sensitivity, distance to crib, amount of smoothing
UPPER_LIMIT = 7000
# After the signal has been normalized to the range [0, 1], volumes higher than this will be
# classified as noise.
# Vary based on: background noise, how loud the baby is, etc.
NOISE_THRESHOLD = 0.25
# seconds of quiet before transition mode from "noise" to "quiet"
MIN_QUIET_TIME = 30
# seconds of noise before transition mode from "quiet" to "noise"
MIN_NOISE_TIME = 5
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
clients = []
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
print("New connection")
clients.append(self)
def on_close(self):
print("Connection closed")
clients.remove(self)
def broadcast_mic_data():
# get the latest data from the audio server
parameters = {"upper_limit": UPPER_LIMIT,
"noise_threshold": NOISE_THRESHOLD,
"min_quiet_time": MIN_QUIET_TIME,
"min_noise_time": MIN_NOISE_TIME}
try:
conn = Client(AUDIO_SERVER_ADDRESS)
except ConnectionRefusedError as e:
print("No connection to audio server: {}".format(e))
sys.exit(1)
conn.send(parameters)
results = conn.recv()
conn.close()
# send results to all clients
now = datetime.now()
results['date_current'] = '{dt:%A} {dt:%B} {dt.day}, {dt.year}'.format(dt=now)
results['time_current'] = now.strftime("%H:%M:%S").lstrip('0')
results['audio_plot'] = results['audio_plot'].tolist()
for c in clients:
c.write_message(results)
def main():
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
app = tornado.web.Application(
handlers=[
(r"/", IndexHandler),
(r"/ws", WebSocketHandler),
], **settings
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(WEB_SERVER_ADDRESS[1], WEB_SERVER_ADDRESS[0])
print('Listening on port: {}'.format(WEB_SERVER_ADDRESS[1]))
main_loop = tornado.ioloop.IOLoop.instance()
scheduler = tornado.ioloop.PeriodicCallback(broadcast_mic_data, 1000) #, io_loop=main_loop)
scheduler.start()
main_loop.start()
if __name__ == "__main__":
main()