diff --git a/db.py b/db.py deleted file mode 100644 index 4f6421c..0000000 --- a/db.py +++ /dev/null @@ -1,65 +0,0 @@ -import os -import psycopg2 -from psycopg2.extras import RealDictCursor -from urllib.parse import urlparse - - -class DataBase(): - def __init__(self, app): - db_url = urlparse(os.environ['DATABASE_URL']) - - self.conn = psycopg2.connect( - database=db_url.path[1:], - user=db_url.username, - password=db_url.password, - host=db_url.hostname, - port=db_url.port - ) - - def execute(self, query, args=(), one=False, commit=False): - cur = self.conn.cursor(cursor_factory=RealDictCursor) - try: - cur.execute(query, args) - except: - self.conn.rollback() - raise Exception('Rolling back transaction...') - - rv = None - if cur.description is not None: - rv = cur.fetchall() - if commit: - try: - self.conn.commit() - except: - self.conn.rollback() - cur.close() - - return (rv[0] if rv else None) if one else rv - - def insert_cadence(self, rpm, device_id): - self.execute('INSERT INTO cadence (rpm, device_id) VALUES (%s, %s)', - [rpm, device_id], commit=True) - - def get_all_cadences(self, device_id): - return self.execute("""SELECT LOGGED_AT, RPM FROM cadence WHERE device_id = %s AND (NOW() < (LOGGED_AT + (INTERVAL '3 hour')))""", [device_id]) - - def get_devices(self): - return self.execute(""" - SELECT DEVICE.ID, - DEVICE.NAME, - MAX(CADENCE.LOGGED_AT) AS LAST_LOGGED_AT, - CASE WHEN(NOW() < (MAX(CADENCE.LOGGED_AT) + (INTERVAL '5 min'))) THEN 'yes' - ELSE 'no' - END AS IS_ACTIVE - FROM DEVICE - LEFT JOIN CADENCE ON DEVICE.ID = CADENCE.DEVICE_ID - GROUP BY DEVICE.ID - ORDER BY LAST_LOGGED_AT - """) - - def get_device(self, device_id): - return self.execute('SELECT * FROM device WHERE id = %s', [device_id], one=True) - - def insert_cadence(self, rpm, device_id): - self.execute('INSERT INTO cadence (rpm, device_id) VALUES (%s, %s)', - [rpm, device_id], commit=True) diff --git a/static/js/BLE.js b/static/js/BLE.js deleted file mode 100644 index 090768a..0000000 --- a/static/js/BLE.js +++ /dev/null @@ -1,118 +0,0 @@ -let characteristic = null; -let prevRes = null; - -let btn = document.querySelector("#ble-connect"); - -function delay(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - -async function connect(props) { - const device = await navigator.bluetooth.requestDevice({ - filters: [{ services: ["cycling_speed_and_cadence"] }], - acceptAllDevices: false, - }); - console.log(`%c\nš©š¼āāļø`, "font-size: 82px;", "Starting CSC...\n\n"); - const server = await device.gatt.connect(); - await delay(500); - console.log("Getting Service..."); - const service = await server.getPrimaryService("cycling_speed_and_cadence"); - console.log("Getting Characteristic..."); - characteristic = await service.getCharacteristic("csc_measurement"); - await characteristic.startNotifications(); - console.log("> Notifications started"); - characteristic.addEventListener("characteristicvaluechanged", props.onChange); - console.log("> Characteristic value changed event listener added"); - - btn.classList.remove("bg-blue-600"); - btn.classList.add("bg-green-600"); -} - -async function disconnect() { - if (characteristic) { - try { - await characteristic.stopNotifications(); - log("> Notifications stopped"); - characteristic.removeEventListener( - "characteristicvaluechanged", - handleNotifications - ); - btn.classList.remove("bg-green-600"); - btn.classList.add("bg-blue-600"); - } catch (error) { - console.log("Argh! " + error); - swal("Oops", error, "error"); - } - } -} - -function parseCSC(e) { - const value = e.target.value; - const flags = value.getUint8(0, true); - const hasWheel = !!(flags & 0x01); - const hasCrank = !!(flags & 0x02); - let index = 1; - const res = { - wheelRevs: null, - wheelTime: null, - crankRevs: null, - crankTime: null, - }; - if (hasWheel) { - res.wheelRevs = value.getUint32(index, true); - index += 4; - res.wheelTime = value.getUint16(index, true); - index += 2; - } - if (hasCrank) { - res.crankRevs = value.getUint16(index, true); - index += 2; - res.crankTime = value.getUint16(index, true); - index += 2; - } - console.log("CSC", res); - if (prevRes) { - let rpm = revsToRPM(prevRes, res); - if (rpm > 0) - fetch("/cadence", { - method: "POST", - headers: { - Accept: "application/json, text/plain, */*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ rpm: parseFloat(rpm.toFixed(2)), id: 1 }), - }); - } - prevRes = res; - return res; -} - -function revsToRPM(prevRes, res) { - const deltaRevs = res.crankRevs - prevRes.crankRevs; - if (deltaRevs === 0) { - // no rotation - return 0; - } - - let deltaTime = (res.crankTime - prevRes.crankTime) / 1024; - if (deltaTime < 0) { - // time counter wraparound - deltaTime += Math.pow(2, 16) / 1024; - } - deltaTime /= 60; // seconds to minutes - - const rpm = deltaRevs / deltaTime; - return rpm; -} - -btn.addEventListener("click", () => { - if (!characteristic) { - connect({ - onChange: parseCSC, - }).catch((err) => { - swal("Oops", err, "error"); - }); - } else { - disconnect(); - } -}); diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index a9e0f72..0000000 --- a/templates/base.html +++ /dev/null @@ -1,72 +0,0 @@ - - - -
- - - -- •
-{{d['name']}}
-0
-| - TimeStamp - | -- RPM - | -
|---|---|
| - {{c['logged_at']}} - | -- {{c['rpm']}} - | -