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