homecontrol: allow selection of range and limits

This commit is contained in:
Konstantin Koslowski 2019-11-14 00:51:51 +01:00
parent 64f1ce24ad
commit 1a8267b668

View file

@ -5,6 +5,7 @@ import argparse
import time import time
import dataset import dataset
import sys import sys
from math import inf
CONFIG_FILE = 'config.json' CONFIG_FILE = 'config.json'
DB_FILE = 'db.sqlite' DB_FILE = 'db.sqlite'
@ -78,106 +79,139 @@ class Core:
self.logger = Logger("Core", debug).getLogger() self.logger = Logger("Core", debug).getLogger()
self.logger.info("initialization starting...") self.logger.info("initialization starting...")
self.actor_queue = {} self.actor_queue = {}
self.db = dataset.connect('sqlite:///db.sqlite') self.db = dataset.connect("sqlite:///%s?check_same_thread=False" % dbfile)
self.logger.info("initialization complete.") self.logger.info("initialization complete.")
def actor_add_level(self, actorId, actorType, maxLevel, level): def actor_add_level(self, actorId, actorType, maxLevel, level):
ret = {} ret = {}
if not self.db["actors"].find_one(actorId=actorId): try:
self.db['actors'].insert({"actorId": actorId, "actorType": actorType, if not self.db["actors"].find_one(actorId=actorId):
"maxLevel": maxLevel}) self.db['actors'].insert({"actorId": actorId, "actorType": actorType,
self.db['actor_levels'].insert({"actorId": actorId, "ts": time.time(), "level": "maxLevel": maxLevel})
level}) self.db['actor_levels'].insert({"actorId": actorId, "ts": time.time(), "level":
level})
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def actor_add_queue(self, actorId, command, data): def actor_add_queue(self, actorId, command, data):
self.actor_queue[actorId] = {"command": command, "data": data} try:
if self.db["actors"].find_one(actorId=actorId):
self.actor_queue[actorId] = {"command": command, "data": data}
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
def actor_get_actors(self): def actor_get_actors(self):
ret = {} ret = {}
for s in self.db["actors"]: try:
actorId = s["actorId"] for s in self.db["actors"]:
ret[actorId] = self.actor_get_info(actorId) actorId = s["actorId"]
ret[actorId] = self.actor_get_info(actorId)
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def actor_get_info(self, actorId): def actor_get_info(self, actorId):
ret = {} ret = {}
q = self.db["actors"].find_one(actorId=actorId) try:
ret["actorId"] = actorId q = self.db["actors"].find_one(actorId=actorId)
ret["maxLevel"] = "0x%x" % q["maxLevel"] ret["actorId"] = actorId
ret["actorType"] = q["actorType"] ret["maxLevel"] = "0x%x" % q["maxLevel"]
q = self.db["actor_levels"].find_one(actorId=actorId, order_by="-ts") ret["actorType"] = q["actorType"]
ret["ts"] = q["ts"] q = self.db["actor_levels"].find_one(actorId=actorId, order_by="-ts")
ret["level"] = "0x%x" % q["level"] ret["ts"] = q["ts"]
ret["level"] = "0x%x" % q["level"]
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def actor_get_levels(self, actorId, limit=None): def actor_get_levels(self, actorId, limit=None):
ret = self.actor_get_info(actorId) ret = {}
try:
levels = {} if self.db["actors"].find_one(actorId=actorId):
if limit: ret = self.actor_get_info(actorId)
query = self.db["actor_levels"].find(actorId=actorId, _limit=limit) if limit:
else: query = self.db["actor_levels"].find(actorId=actorId, _limit=limit)
query = self.db["actor_levels"].find(actorId=actorId) else:
k = query.keys query = self.db["actor_levels"].find(actorId=actorId)
for q in query: levels = []
levels[q["ts"]] = "0x%x" % q["level"] for q in query:
ret["levels"] = levels levels.append({"ts": q["ts"], "value": "0x%x" % q["level"]})
ret["levels"] = levels
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def actor_get_queue(self, actorId): def actor_get_queue(self, actorId):
ret = {} ret = {}
if actorId in self.actor_queue: try:
ret = self.actor_queue.pop(actorId) if actorId in self.actor_queue:
ret = self.actor_queue.pop(actorId)
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def sensor_get_info(self, sensorId): def sensor_get_info(self, sensorId):
ret = {} ret = {}
q = self.db["sensors"].find_one(sensorId=sensorId) try:
ret["sensorId"] = sensorId q = self.db["sensors"].find_one(sensorId=sensorId)
ret["sensorType"] = q["sensorType"] ret["sensorId"] = sensorId
q = self.db["sensor_values"].find_one(sensorId=sensorId, order_by="-ts") ret["sensorType"] = q["sensorType"]
ret["ts"] = q["ts"] q = self.db["sensor_values"].find_one(sensorId=sensorId, order_by="-ts")
ret["ts"] = q["ts"]
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def sensor_get_values(self, sensorId, limit=None): def sensor_get_values(self, sensorId, min_ts, max_ts, limit=None):
ret = self.sensor_get_info(sensorId) ret = {}
try:
values = {} if self.db["sensors"].find_one(sensorId=sensorId):
if limit: ret = self.sensor_get_info(sensorId)
query = self.db["sensor_values"].find(sensorId=sensorId, _limit=limit) if limit:
else: query = self.db["sensor_values"].find(sensorId=sensorId,
query = self.db["sensor_values"].find(sensorId=sensorId) ts={"between": [min_ts, max_ts]}, _limit=limit)
k = query.keys else:
for q in query: query = self.db["sensor_values"].find(sensorId=sensorId,
values[q["ts"]] = q["value"] ts={"between": [min_ts, max_ts]})
ret["values"] = values values = []
for q in query:
values.append({"ts": float(q["ts"]), "value": float(q["value"])})
ret["values"] = values
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def sensor_get_sensors(self): def sensor_get_sensors(self):
ret = {} ret = {}
for s in self.db["sensors"]: try:
sensorId = s["sensorId"] for s in self.db["sensors"]:
ret[sensorId] = self.sensor_get_info(sensorId) sensorId = s["sensorId"]
ret[sensorId] = self.sensor_get_info(sensorId)
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
def sensor_add_value(self, sensorId, sensorType, value): def sensor_add_value(self, sensorId, sensorType, value):
ret = {} ret = {}
if not self.db["sensors"].find_one(sensorId=sensorId): try:
self.db['sensors'].insert({"sensorId": sensorId, "sensorType": sensorType}) if not self.db["sensors"].find_one(sensorId=sensorId):
self.db['sensor_values'].insert({"sensorId": sensorId, "ts": time.time(), "value": self.db['sensors'].insert({"sensorId": sensorId, "sensorType": sensorType})
value}) self.db['sensor_values'].insert({"sensorId": sensorId, "ts": time.time(), "value":
value})
except Exception as ex:
self.logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
return ret return ret
@ -193,8 +227,7 @@ def actor_command():
actorId = content.get("id") actorId = content.get("id")
command = content.get("command") command = content.get("command")
data = content.get("data") data = content.get("data")
if core.db["actors"].find_one(actorId=actorId): core.actor_add_queue(actorId, command, data)
core.actor_add_queue(actorId, command, data)
except Exception as ex: except Exception as ex:
logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args)) logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
abort(400) abort(400)
@ -216,14 +249,7 @@ def actor_get_level(actorId):
else: else:
limit=None limit=None
logger.debug("actor/get_levels/%s, limit: %s" % (actorId, limit)) logger.debug("actor/get_levels/%s, limit: %s" % (actorId, limit))
if actorId == "all": ret[actorId] = core.actor_get_levels(actorId, limit=limit)
for q in core.db["actors"]:
s = q["actorId"]
ret[s] = core.actor_get_levels(s, limit=limit)
elif core.db["actors"].find_one(actorId=actorId):
ret[actorId] = core.actor_get_levels(actorId, limit=limit)
else:
abort(404)
return make_response(jsonify(ret), 200) return make_response(jsonify(ret), 200)
@ -254,19 +280,17 @@ def sensor_get_sensors():
@app.route("/sensor/get_values/<sensorId>", methods=['GET']) @app.route("/sensor/get_values/<sensorId>", methods=['GET'])
def sensor_get_values(sensorId): def sensor_get_values(sensorId):
ret = {} ret = {}
min_ts = 0
if request.args.get("min_ts"):
min_ts=int(request.args.get("min_ts"))
max_ts = inf
limit=None
if request.args.get("limit"): if request.args.get("limit"):
limit=int(request.args.get("limit")) limit=int(request.args.get("limit"))
else: if request.args.get("max_ts"):
limit=None max_ts=int(request.args.get("max_ts"))
logger.debug("sensor/get_values/%s, limit: %s" % (sensorId, limit)) logger.debug("sensor/get_values/%s, [%f, %f], limit: %s" % (sensorId, min_ts, max_ts, limit))
if sensorId == "all": ret[sensorId] = core.sensor_get_values(sensorId, min_ts=min_ts, max_ts=max_ts, limit=limit)
for q in core.db["sensors"]:
s = q["sensorId"]
ret[s] = core.sensor_get_values(s, limit=limit)
elif core.db["sensors"].find_one(sensorId=sensorId):
ret[sensorId] = core.sensor_get_values(sensorId, limit=limit)
else:
abort(404)
return make_response(jsonify(ret), 200) return make_response(jsonify(ret), 200)