homecontrol: allow selection of range and limits
This commit is contained in:
parent
64f1ce24ad
commit
1a8267b668
1 changed files with 99 additions and 75 deletions
|
@ -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,34 +79,45 @@ 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 = {}
|
||||||
|
try:
|
||||||
if not self.db["actors"].find_one(actorId=actorId):
|
if not self.db["actors"].find_one(actorId=actorId):
|
||||||
self.db['actors'].insert({"actorId": actorId, "actorType": actorType,
|
self.db['actors'].insert({"actorId": actorId, "actorType": actorType,
|
||||||
"maxLevel": maxLevel})
|
"maxLevel": maxLevel})
|
||||||
self.db['actor_levels'].insert({"actorId": actorId, "ts": time.time(), "level":
|
self.db['actor_levels'].insert({"actorId": actorId, "ts": time.time(), "level":
|
||||||
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):
|
||||||
|
try:
|
||||||
|
if self.db["actors"].find_one(actorId=actorId):
|
||||||
self.actor_queue[actorId] = {"command": command, "data": data}
|
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 = {}
|
||||||
|
try:
|
||||||
for s in self.db["actors"]:
|
for s in self.db["actors"]:
|
||||||
actorId = s["actorId"]
|
actorId = s["actorId"]
|
||||||
ret[actorId] = self.actor_get_info(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 = {}
|
||||||
|
try:
|
||||||
q = self.db["actors"].find_one(actorId=actorId)
|
q = self.db["actors"].find_one(actorId=actorId)
|
||||||
ret["actorId"] = actorId
|
ret["actorId"] = actorId
|
||||||
ret["maxLevel"] = "0x%x" % q["maxLevel"]
|
ret["maxLevel"] = "0x%x" % q["maxLevel"]
|
||||||
|
@ -113,71 +125,93 @@ class Core:
|
||||||
q = self.db["actor_levels"].find_one(actorId=actorId, order_by="-ts")
|
q = self.db["actor_levels"].find_one(actorId=actorId, order_by="-ts")
|
||||||
ret["ts"] = q["ts"]
|
ret["ts"] = q["ts"]
|
||||||
ret["level"] = "0x%x" % q["level"]
|
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 = {}
|
||||||
|
try:
|
||||||
|
if self.db["actors"].find_one(actorId=actorId):
|
||||||
ret = self.actor_get_info(actorId)
|
ret = self.actor_get_info(actorId)
|
||||||
|
|
||||||
levels = {}
|
|
||||||
if limit:
|
if limit:
|
||||||
query = self.db["actor_levels"].find(actorId=actorId, _limit=limit)
|
query = self.db["actor_levels"].find(actorId=actorId, _limit=limit)
|
||||||
else:
|
else:
|
||||||
query = self.db["actor_levels"].find(actorId=actorId)
|
query = self.db["actor_levels"].find(actorId=actorId)
|
||||||
k = query.keys
|
levels = []
|
||||||
for q in query:
|
for q in query:
|
||||||
levels[q["ts"]] = "0x%x" % q["level"]
|
levels.append({"ts": q["ts"], "value": "0x%x" % q["level"]})
|
||||||
ret["levels"] = levels
|
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 = {}
|
||||||
|
try:
|
||||||
if actorId in self.actor_queue:
|
if actorId in self.actor_queue:
|
||||||
ret = self.actor_queue.pop(actorId)
|
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 = {}
|
||||||
|
try:
|
||||||
q = self.db["sensors"].find_one(sensorId=sensorId)
|
q = self.db["sensors"].find_one(sensorId=sensorId)
|
||||||
ret["sensorId"] = sensorId
|
ret["sensorId"] = sensorId
|
||||||
ret["sensorType"] = q["sensorType"]
|
ret["sensorType"] = q["sensorType"]
|
||||||
q = self.db["sensor_values"].find_one(sensorId=sensorId, order_by="-ts")
|
q = self.db["sensor_values"].find_one(sensorId=sensorId, order_by="-ts")
|
||||||
ret["ts"] = q["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 = {}
|
||||||
|
try:
|
||||||
|
if self.db["sensors"].find_one(sensorId=sensorId):
|
||||||
ret = self.sensor_get_info(sensorId)
|
ret = self.sensor_get_info(sensorId)
|
||||||
|
|
||||||
values = {}
|
|
||||||
if limit:
|
if limit:
|
||||||
query = self.db["sensor_values"].find(sensorId=sensorId, _limit=limit)
|
query = self.db["sensor_values"].find(sensorId=sensorId,
|
||||||
|
ts={"between": [min_ts, max_ts]}, _limit=limit)
|
||||||
else:
|
else:
|
||||||
query = self.db["sensor_values"].find(sensorId=sensorId)
|
query = self.db["sensor_values"].find(sensorId=sensorId,
|
||||||
k = query.keys
|
ts={"between": [min_ts, max_ts]})
|
||||||
|
values = []
|
||||||
for q in query:
|
for q in query:
|
||||||
values[q["ts"]] = q["value"]
|
values.append({"ts": float(q["ts"]), "value": float(q["value"])})
|
||||||
ret["values"] = values
|
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 = {}
|
||||||
|
try:
|
||||||
for s in self.db["sensors"]:
|
for s in self.db["sensors"]:
|
||||||
sensorId = s["sensorId"]
|
sensorId = s["sensorId"]
|
||||||
ret[sensorId] = self.sensor_get_info(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 = {}
|
||||||
|
try:
|
||||||
if not self.db["sensors"].find_one(sensorId=sensorId):
|
if not self.db["sensors"].find_one(sensorId=sensorId):
|
||||||
self.db['sensors'].insert({"sensorId": sensorId, "sensorType": sensorType})
|
self.db['sensors'].insert({"sensorId": sensorId, "sensorType": sensorType})
|
||||||
self.db['sensor_values'].insert({"sensorId": sensorId, "ts": time.time(), "value":
|
self.db['sensor_values'].insert({"sensorId": sensorId, "ts": time.time(), "value":
|
||||||
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,7 +227,6 @@ 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))
|
||||||
|
@ -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":
|
|
||||||
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)
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue