2019-10-22 23:55:00 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from flask import Flask, request, json, jsonify, abort, make_response
|
2019-11-03 01:36:23 +01:00
|
|
|
import logging
|
2019-10-22 23:55:00 +02:00
|
|
|
import argparse
|
2019-11-03 01:36:23 +01:00
|
|
|
import Client
|
2019-10-22 23:55:00 +02:00
|
|
|
|
2019-11-03 01:36:23 +01:00
|
|
|
CONFIG_FILE = 'config.json'
|
2019-10-22 23:55:00 +02:00
|
|
|
PORT = 5000
|
2019-11-03 01:36:23 +01:00
|
|
|
core = None
|
|
|
|
|
|
|
|
# sensors
|
|
|
|
NUM_VALUES = 1000
|
2019-10-22 23:55:00 +02:00
|
|
|
|
|
|
|
def setup():
|
|
|
|
# arguments
|
|
|
|
parser = argparse.ArgumentParser(description='homecontrol')
|
2019-11-03 01:36:23 +01:00
|
|
|
parser.add_argument('-p', '--port', dest='port', type=int,
|
|
|
|
help='listening port')
|
2019-10-22 23:55:00 +02:00
|
|
|
parser.add_argument('-c', '--config', dest='config', type=str,
|
2019-11-03 01:36:23 +01:00
|
|
|
help='config file', default=CONFIG_FILE)
|
2019-10-22 23:55:00 +02:00
|
|
|
parser.add_argument('-d', '--debug', dest='debug', action='store_true',
|
2019-11-03 01:36:23 +01:00
|
|
|
help='debug mode')
|
2019-10-22 23:55:00 +02:00
|
|
|
|
|
|
|
# parse arguments
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# initialize config
|
|
|
|
config = {}
|
|
|
|
try:
|
2019-11-03 01:36:23 +01:00
|
|
|
config_file = open(args.config, 'r')
|
2019-10-22 23:55:00 +02:00
|
|
|
config = json.load(config_file)
|
2019-11-03 01:36:23 +01:00
|
|
|
except Exception as ex:
|
|
|
|
logger.error('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
|
2019-10-22 23:55:00 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2019-11-03 01:36:23 +01:00
|
|
|
|
2019-10-22 23:55:00 +02:00
|
|
|
class Core:
|
2019-11-03 01:36:23 +01:00
|
|
|
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
|
2019-10-22 23:55:00 +02:00
|
|
|
|
|
|
|
def update(self):
|
2019-11-03 01:36:23 +01:00
|
|
|
# TODO
|
2019-10-22 23:55:00 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-11-03 01:36:23 +01:00
|
|
|
app = Flask(__name__)
|
2019-10-22 23:55:00 +02:00
|
|
|
|
|
|
|
|
2019-11-03 01:36:23 +01:00
|
|
|
@app.route("/actors/get", methods=['GET'])
|
|
|
|
def actors_get():
|
|
|
|
ret = core.actors_get()
|
|
|
|
return make_response(jsonify(ret), 200)
|
2019-10-22 23:55:00 +02:00
|
|
|
|
|
|
|
|
2019-11-03 01:36:23 +01:00
|
|
|
@app.route("/actors/update", methods=['POST'])
|
|
|
|
def actors_update():
|
2019-10-22 23:55:00 +02:00
|
|
|
ret = {}
|
|
|
|
try:
|
|
|
|
content = request.json
|
2019-11-03 01:36:23 +01:00
|
|
|
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
|
2019-10-22 23:55:00 +02:00
|
|
|
except Exception as ex:
|
2019-11-03 01:36:23 +01:00
|
|
|
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/<id_a>", 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/<id_s>", 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)
|
2019-10-22 23:55:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args, config = setup()
|
2019-11-03 01:36:23 +01:00
|
|
|
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)
|