#!/usr/bin/env python3 from flask import Flask, request, json, jsonify, abort, make_response import logging import argparse import Client 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('-c', '--config', dest='config', type=str, help='config file', default=CONFIG_FILE) parser.add_argument('-d', '--debug', dest='debug', action='store_true', help='debug mode') # parse arguments args = parser.parse_args() # initialize config config = {} try: config_file = open(args.config, 'r') config = json.load(config_file) 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'): config['port'] = PORT # overwrite with arguments if args.port: config['port'] = args.port # save to file with open(args.config, 'w') as config_file: json.dump(config, config_file) return args, config class Core: 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 app = Flask(__name__) @app.route("/actors/get", methods=['GET']) def actors_get(): ret = core.actors_get() return make_response(jsonify(ret), 200) @app.route("/actors/update", methods=['POST']) def actors_update(): ret = {} try: content = request.json 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)) 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) 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)