heartbeatserver/heartbeat/server.py
2018-10-30 23:13:45 +01:00

48 lines
1.4 KiB
Python

# 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
import json
def start(args):
basedir = os.path.dirname(os.path.realpath(__file__)) +'/../'
conn = sqlite3.connect(basedir + 'heartbeat.db')
with open(basedir+'token.json', 'r') as fp:
t = json.load(fp)
token = t['token']
print("VALIDATE WITH TOKEN: '{}'".format(token))
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,
recordedAt TIMESTAMP,
createdAt TIMESTAMP DEFAULT (datetime('now','localtime'))
);
""")
c.execute("CREATE INDEX IF NOT EXISTS beats_recordedAt ON beats (recordedAt);")
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}),
(r"/ws", WebSocketHandler, {"conn": conn, "token": token}),
(r"/(.*).csv", ExportHandler, {"conn": conn}),
(r"/assets/(.*)", tornado.web.StaticFileHandler, {"path": "assets/"}),
],debug=True)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()