diff options
author | Matt Kohls <mattkohls13@gmail.com> | 2023-09-04 15:36:33 -0400 |
---|---|---|
committer | Matt Kohls <mattkohls13@gmail.com> | 2023-09-04 15:36:33 -0400 |
commit | c8ef8843aaaf28bc38b544ae8ac72accf233aead (patch) | |
tree | 53e846d7480814040bbebe11dd6e290f5d366589 /snag/data.py | |
download | sensor-aggregator-c8ef8843aaaf28bc38b544ae8ac72accf233aead.tar.gz sensor-aggregator-c8ef8843aaaf28bc38b544ae8ac72accf233aead.tar.bz2 sensor-aggregator-c8ef8843aaaf28bc38b544ae8ac72accf233aead.zip |
Revamped project init
New repo for larger structural changes from Sensor-Server. Currently
doing most all the same stuff but hopefully better. Still have to clean
out some of the older templates for the new interface
Diffstat (limited to 'snag/data.py')
-rw-r--r-- | snag/data.py | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/snag/data.py b/snag/data.py new file mode 100644 index 0000000..3bf8df3 --- /dev/null +++ b/snag/data.py @@ -0,0 +1,116 @@ +# snag +# Matt Kohls +# (c) 2023 + +from flask import ( + Blueprint, flash, g, redirect, render_template, request, url_for, json +) +from werkzeug.exceptions import abort +from datetime import datetime +from snag.db import get_db + +bp = Blueprint('data', __name__, url_prefix='/data') + +# Process json data +# +# Expects something like this: +# { +# "scheme": "1", +# "deviceId": 1, +# "readings": [ +# { +# "env": "outdoor", +# "temp": 123 +# }, +# { +# "env": "enclosed", +# "temp": 456 +# } +# ], +# "status": [ +# { +# "heater": "off", +# "battery": 10 +# } +# ] +# } +def unpack_json_scheme_1(req): + try: + deviceId = req['deviceId'] + db = get_db() + + # Status only has one object in it, and is optional + if 'status' in req: + if 'timeStamp' in req['status'][0]: + now = req['status'][0]['timeStamp'] + else: + now = datetime.now() + battery = req['status'][0]['battery'] + heater = req['status'][0]['heater'] + db.execute('INSERT INTO device_status (date, deviceId, batteryCharge, heaterRunning) VALUES (?, ?, ?, ?)', [now, deviceId, battery, heater]) + + for environment in req['readings']: + env = environment['environment'] + temp = environment['temperature'] + humidity = environment['humidity'] + pressure = environment['pressure'] + now = environment['timeStamp'] + if env == 'outdoor': + lux = environment['lux'] + uv = environment['uv'] + db.execute('INSERT INTO env_outdoor (date, deviceId, temperature, humidity, pressure, lux, uv_intensity) VALUES (?, ?, ?, ?, ?, ?, ?)', [now, deviceId, temp, humidity, pressure, lux, uv]) + elif env == 'indoor': + db.execute('INSERT INTO env_indoor (date, deviceId, temperature, humidity, pressure) VALUES (?, ?, ?, ?, ?)', [now, deviceId, temp, humidity, pressure]) + elif env == 'enclosed': + light = environment['light'] + db.execute('INSERT INTO env_enclosed (date, deviceId, temperature, humidity, pressure, light) VALUES (?, ?, ?, ?, ?, ?)', [now, deviceId, temp, humidity, pressure, light]) + else: + raise 'Bad reading environment' + + db.commit() + + except Exception as err: + print(err) + return "Bad JSON", 400 + + return "Data received", 200 + +@bp.route('/json', methods=['POST']) +def add_json_data(): + if request.is_json: + req = request.get_json() + if 'scheme' in req: + if req['scheme'] == '1': + return unpack_json_scheme_1(req) + else: + abort(400, "Unknown scheme") + else: + abort(400, "Unknown JSON") + + else: + abort(400, "Unknown payload type") + +@bp.route('/devices', methods=['GET']) +def get_devices(): + db = get_db() + + out = [] + devices = db.execute('SELECT * FROM devices ORDER BY deviceId').fetchall() + for device in devices: + envList = [] + envs = db.execute('SELECT * FROM device_env WHERE deviceId = ?', [device['deviceId']]).fetchall() + for env in envs: + envDict = { + "environment" : env["environment"], + # "environmentDesc" : env["environmentDescription"] + } + envList.append(envDict) + deviceLine = { + "deviceId" : device["deviceId"], + "deviceName" : device["deviceName"], + "deviceDescription" : device["deviceLocation"] + } + deviceLine['environments'] = envList + out.append(deviceLine) + return out + |