-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·120 lines (90 loc) · 3.05 KB
/
app.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
import os
import sys
import paho.mqtt.client as mqtt
import tornado.ioloop
import tornado.web
import tornado.template
import tornado.gen
from tornado.queues import Queue
import settings
from display import displayText, clock, dimmer
from standby import stabdby_mode
brokerIP = settings.read('settings', 'mqtt', 'brokerIP')
brokerPort = settings.read('settings', 'mqtt', 'brokerPort')
httpPort = settings.read('settings', 'http', 'port')
standbyFrom = settings.read('settings', 'standby', 'from')
standbyTo = settings.read('settings', 'standby', 'to')
q = Queue(maxsize=2)
class HomePage(tornado.web.RequestHandler):
def get(self):
self.render('app.tpl')
class MessagePage(tornado.web.RequestHandler):
def get(self):
self.render('message.tpl')
def post(self):
msg = self.get_argument('msg')
text_color = self.get_argument('text_color')
bg_color = self.get_argument('bg_color')
delay = self.get_argument('delay')
if msg:
displayText(5, str(msg), text_color, bg_color, delay)
else:
print 'err'
self.render('message.tpl')
def on_connect(client, obj, flags, rc):
print("rc: " + str(rc))
@tornado.gen.coroutine
def on_message(client, obj, msg):
# print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
item = str(msg.payload)
yield q.put(item)
# print('Put %s' % item)
def mqtt_run():
feeds = settings.read('feeds')
print("Follow %s topics") % (len(feeds))
feedsTopics = [(f.get('topic'),f.get('priority')) for f in feeds]
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(brokerIP, brokerPort, 60)
client.subscribe(feedsTopics)
client.loop_start()
class SettingsPage(tornado.web.RequestHandler):
def get(self):
self.render('settings.tpl')
def make_app():
# run MQTT loop in background
mqtt_run()
return tornado.web.Application([
(r'/', HomePage),
(r'/message', MessagePage),
(r'/settings', SettingsPage),
(r'/favicon.ico', tornado.web.StaticFileHandler, {'path': os.path.join(os.path.dirname(__file__), "static")}),
],
template_path=os.path.join(os.path.dirname(__file__), "views"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True)
@tornado.gen.coroutine
def update():
standby = stabdby_mode(standbyFrom, standbyTo)
if not standby:
dimmer()
# if items in queue - display them
if q.qsize():
item = yield q.get()
try:
# print('Doing work on %s' % item)
displayText(2, item, 'text_green', 'bg_black', 5)
yield tornado.gen.sleep(0.01)
finally:
q.task_done()
yield q.join()
clock()
if __name__ == "__main__":
app = make_app()
app.listen(httpPort)
mainLoop = tornado.ioloop.IOLoop.current()
scheduler = tornado.ioloop.PeriodicCallback(
update, 500, io_loop=mainLoop)
scheduler.start()
mainLoop.start()