heartbeatserver/heartbeat/server.py

49 lines
1.4 KiB
Python
Raw Normal View History

2018-10-23 20:07:28 +00:00
# import the libraries
import tornado.web
import tornado.ioloop
from .ws import WebSocketHandler
from .frontend import FrontendHandler
from .export import ExportHandler
import sqlite3
import os
2018-10-30 14:55:16 +00:00
import json
2018-10-23 20:07:28 +00:00
def start(args):
basedir = os.path.dirname(os.path.realpath(__file__)) +'/../'
conn = sqlite3.connect(basedir + 'heartbeat.db')
2018-10-30 14:55:16 +00:00
with open(basedir+'token.json', 'r') as fp:
t = json.load(fp)
token = t['token']
print("VALIDATE WITH TOKEN: '{}'".format(token))
2018-10-23 20:07:28 +00:00
conn.cursor()
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS beats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
bpm INTEGER,
beatcount INTEGER,
beattime REAL,
2018-10-30 14:55:16 +00:00
recordedAt TIMESTAMP,
2018-10-23 20:07:28 +00:00
createdAt TIMESTAMP DEFAULT (datetime('now','localtime'))
);
""")
2018-10-30 14:55:16 +00:00
c.execute("CREATE INDEX IF NOT EXISTS beats_recordedAt ON beats (recordedAt);")
2018-10-23 20:07:28 +00:00
conn.commit()
# start a new WebSocket Application
# use "/" as the root, and the
# WebSocketHandler as our handler
application = tornado.web.Application([
(r"/", FrontendHandler, {"conn": conn}),
2018-10-30 14:55:16 +00:00
(r"/ws", WebSocketHandler, {"conn": conn, "token": token}),
2018-10-23 20:07:28 +00:00
(r"/(.*).csv", ExportHandler, {"conn": conn}),
2018-10-30 22:13:45 +00:00
(r"/assets/(.*)", tornado.web.StaticFileHandler, {"path": "assets/"}),
2018-10-23 20:07:28 +00:00
],debug=True)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()