-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserv.py
executable file
·76 lines (59 loc) · 2.09 KB
/
webserv.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
#!/usr/bin/env python
import time
import serial
import tornado.ioloop
import tornado.web
import tornado.websocket
from weatherdata import WeatherData
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("static/index.html")
class WeatherData24H(tornado.web.RequestHandler):
def get(self):
self.write({"24h": weather_data.last_day_data()})
class WeatherData7D(tornado.web.RequestHandler):
def get(self):
self.write({"7d": weather_data.last_week_data()})
class WeatherWebSocket(tornado.websocket.WebSocketHandler):
sessions = []
def open(self):
self.__class__.sessions.append(self)
last_hour = weather_data.last_hour_data()
if len(last_hour):
self.write_message({"initial": last_hour})
def on_message(self, message):
print "Received:", message
def on_close(self):
self.__class__.sessions.remove(self)
@classmethod
def broadcast(cls, msg):
for each_session in cls.sessions:
each_session.write_message(msg)
class SerialReader(object):
def __init__(self):
self.readbuf = ""
self.serial = serial.Serial("/dev/ttyACM0", 9600)
tornado.ioloop.IOLoop().current().add_handler(self.serial.fileno(),
self.read_event, tornado.ioloop.IOLoop.READ)
def parse_readbuf(self):
while self.readbuf.find("\n") != -1:
splitted = self.readbuf.split("\n", 1)
weather_data.new_input(splitted[0])
if len(splitted) > 1:
self.readbuf = splitted[1]
else:
self.readbuf = ""
def read_event(self, fd, events):
self.readbuf += self.serial.read(self.serial.inWaiting())
self.parse_readbuf()
application = tornado.web.Application([
# (r"/", IndexHandler),
(r"/arduino", WeatherWebSocket),
(r"/weather-24h", WeatherData24H),
(r"/weather-7d", WeatherData7D),
])
weather_data = WeatherData(WeatherWebSocket)
if __name__ == "__main__":
serial_reader = SerialReader()
application.listen(8000)
tornado.ioloop.IOLoop.instance().start()