From 246bf7e1de7059274a2e2cf4e56a9c6a76c3a792 Mon Sep 17 00:00:00 2001 From: Konstantin Koslowski Date: Sun, 3 Nov 2019 01:36:23 +0100 Subject: [PATCH] homecontrol: split into sensors and actors, update --- .gitignore | 1 + Client.py | 42 ++++++++++++ homecontrol.py | 170 +++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 179 insertions(+), 34 deletions(-) create mode 100644 Client.py diff --git a/.gitignore b/.gitignore index 77341a5..57ebbb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ Pipfile.lock config.json +.idea/* diff --git a/Client.py b/Client.py new file mode 100644 index 0000000..a882ba4 --- /dev/null +++ b/Client.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +import time + +class Client: + def __init__(self, clientid, clienttype="default"): + self.clienttype = clienttype + self.clientid = clientid + + def get_info(self): + return "clientid: %s, clienttype: %s" % (self.clientid, self.clienttype) + +class Sensor(Client): + def __init__(self, sensorid, sensortype, num_values): + self.num_values = num_values + self.values = {} + super().__init__(sensorid, sensortype) + + def get_values(self): + return self.values + + def add_value(self, value): + ts = time.time() + self.values[ts] = value + + while len(self.values) > self.num_values: + self.values.pop(next(iter(self.values))) + + return len(self.values) + +class Actor(Client): + def __init__(self, actorid, actortype, levels): + self.levels = levels + self.level = 0 + super().__init__(actorid, actortype) + + def get_level(self): + return(self.level) + + def set_level(self, level): + if level < self.levels: + self.level = level + return(self.level) diff --git a/homecontrol.py b/homecontrol.py index 43552f6..63a0bb9 100755 --- a/homecontrol.py +++ b/homecontrol.py @@ -1,20 +1,25 @@ #!/usr/bin/env python3 from flask import Flask, request, json, jsonify, abort, make_response +import logging import argparse -import time +import Client -CONFIG_FILE='config.json' +CONFIG_FILE = 'config.json' PORT = 5000 +core = None + +# sensors +NUM_VALUES = 1000 def setup(): # arguments parser = argparse.ArgumentParser(description='homecontrol') - parser.add_argument('-p', '--port', dest='port', type=int, - help='listening port') + parser.add_argument('-p', '--port', dest='port', type=int, + help='listening port') parser.add_argument('-c', '--config', dest='config', type=str, - help='config file', default=CONFIG_FILE) + help='config file', default=CONFIG_FILE) parser.add_argument('-d', '--debug', dest='debug', action='store_true', - help='debug mode') + help='debug mode') # parse arguments args = parser.parse_args() @@ -22,10 +27,10 @@ def setup(): # initialize config config = {} try: - config_file = open(args.config, 'r') + config_file = open(args.config, 'r') config = json.load(config_file) - except: - pass + except Exception as ex: + logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args)) # fill new keys with defaults if not config.get('port'): @@ -41,47 +46,144 @@ def setup(): return args, config + class Core: - def __init__(self): - self.nodes = {} + def __init__(self, debug): + self.logger = logging.getLogger("Core") + if args.debug: + self.logger.setLevel(logging.DEBUG) + else: + self.logger.setLevel(logging.INFO) + + self.actors = {} + self.sensors = {} + + def actors_get(self): + ret = {} + for a in self.actors: + ret[a] = self.actors[a].get_info() + return ret + + def sensors_get(self): + ret = {} + for s in self.sensors: + ret[s] = self.sensors[s].get_info() + return ret def update(self): + # TODO pass -class Node: - def __init__(self, type): - self.type == type - self.values = {} - def add_value(self, value): - self.values[time.time()] = value +app = Flask(__name__) - def get_values(self): - return self.values - def clean_values(self, n): - while len(self.values) > n: - self.values.pop(next(iter(self.values))) +@app.route("/actors/get", methods=['GET']) +def actors_get(): + ret = core.actors_get() + return make_response(jsonify(ret), 200) -app = Flask (__name__) -@app.route("/node/get", methods = [ 'GET' ]) -def node_get(): - ret = "yay" - return ret -@app.route("/node/add_value", methods = [ 'POST' ]) -def node_add_value(): +@app.route("/actors/update", methods=['POST']) +def actors_update(): ret = {} - content = {} try: content = request.json - print(content) + id_a = content.get("id_a") + type_a = content.get("type_a") + levels = content.get("levels") + level = content.get("level") + if id_a not in core.actors: + core.actors[id_a] = Client.Actor(id_a, type_a, levels) + actor = core.actors[id_a] + actor.type_a = type_a + actor.levels = levels + actor.level = level 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) + + for a in core.actors: + logger.debug("actor: %s" % a) + return make_response(jsonify(ret), 200) + + + +@app.route("/actors/get_level/", methods=['GET']) +def actors_get_level(id_a): + logger.debug("actors/get_level/%s" % id_a) + if id_a in core.actors: + ret = core.actors[id_a].get_level() + return make_response(jsonify(ret), 200) + else: + logger.debug("id_a: %s not in core.actors" % id_a) + for a in core.actors: + logger.debug("actor: %s" % a) + abort(400) + + +@app.route("/actors/set_level", methods=['POST']) +def actors_set_level(): + ret = {} + try: + content = request.json + logger.info("actors/set_level: %s" % (content)) + id_a = content.get("id_a") + level = content.get("level") + if id_a in core.actors: + ret = core.actors[id_a].set_level(level) + except Exception as ex: + logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args)) + abort(400) + return make_response(jsonify(ret), 200) + + +@app.route("/sensors/get", methods=['GET']) +def sensors_get(): + ret = core.sensors_get() + return make_response(jsonify(ret), 200) + + +@app.route("/sensors/get_values/", methods=['GET']) +def sensors_get_values(id_s): + logger.debug("sensors/get_values/%s" % id_s) + if id_s in core.sensors: + ret = core.sensors[id_s].get_values() + return make_response(jsonify(ret), 200) + else: + logger.debug("id_s: %s not in core.sensors" % id_s) + abort(400) + + +@app.route("/sensors/add_value", methods=['POST']) +def sensors_add_value(): + ret = {} + try: + content = request.json + logger.info("sensors_add_value: %s" % (content)) + id_s = content.get("id_s") + value = content.get("value") + if id_s not in core.sensors: + type_s = content.get("sensortype") + core.sensors[id_s] = Client.Sensor(id_s, type_s, NUM_VALUES) + logger.info("created sensor: %s" % (core.sensors[id_s].get_info())) + sensor = core.sensors[id_s] + ret = sensor.add_value(value) + except Exception as ex: + logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args)) + abort(400) + return make_response(jsonify(ret), 200) - return ret if __name__ == "__main__": args, config = setup() + core = Core(args.debug) + print("main") + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger("homecontrol") + if args.debug: + logger.setLevel(logging.DEBUG) + else: + logger.setLevel(logging.INFO) - app.run(host = '0.0.0.0', port=config['port'], debug=args.debug) + app.run(host='0.0.0.0', port=config['port'], debug=args.debug)