-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
78 lines (67 loc) · 1.9 KB
/
api.py
File metadata and controls
78 lines (67 loc) · 1.9 KB
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
import asyncio
import json
import os
import sys
import time
import tornado.gen
import tornado.ioloop
import tornado.platform
import tornado.web
from tools.iremitter import IREmitter
SIGNAL_ATTEMPTS = 10
OUTLET_NAME = "outlet_a";
RESET_DELAY_SECS = 10
is_debug = "--debug" in sys.argv
class APIHandlerBase(tornado.web.RequestHandler):
def prepare(self):
self.emitter = IREmitter(SIGNAL_ATTEMPTS, is_debug)
class OffHandler(APIHandlerBase):
async def post(self):
self.write(json.dumps({"status": "success"}))
self.finish()
self.emitter.emit_off(OUTLET_NAME)
class OnHandler(APIHandlerBase):
async def post(self):
self.write(json.dumps({"status": "success"}))
self.finish()
self.emitter.emit_on(OUTLET_NAME)
class ResetHandler(APIHandlerBase):
async def post(self):
self.write(json.dumps({"status": "success"}))
self.finish()
self.emitter.emit_off(OUTLET_NAME)
await asyncio.sleep(RESET_DELAY_SECS)
self.emitter.emit_on(OUTLET_NAME)
class PingHandler(tornado.web.RequestHandler):
async def get(self):
self.write(json.dumps({"status": "success"}))
self.finish()
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.redirect("/static/index.html")
def make_app():
return tornado.web.Application(
[
(r"/", MainHandler),
(r"/off", OffHandler),
(r"/on", OnHandler),
(r"/reset", ResetHandler),
(r"/ping", PingHandler),
(
r"/static/(.*)",
tornado.web.StaticFileHandler,
{"path": os.path.join(os.path.dirname(__file__), "frontend/static")}
),
(
r"/out/(.*)",
tornado.web.StaticFileHandler,
{"path": os.path.join(os.path.dirname(__file__), "frontend/out")}
),
],
debug = is_debug
)
if __name__ == "__main__":
tornado.platform.asyncio.AsyncIOMainLoop().install()
app = make_app()
app.listen(8000)
asyncio.get_event_loop().run_forever()