commit 775c570de5be68a0ee9ebd576a269c590e0b3ef5 Author: Konstantin Koslowski Date: Tue Oct 22 23:55:00 2019 +0200 homecontrol: baby steps diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77341a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Pipfile.lock +config.json diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..b0acb60 --- /dev/null +++ b/Pipfile @@ -0,0 +1,10 @@ +[[source]] +url = "https://pypi.python.org/simple" +verify_ssl = true +name = "pypi" + +[dev-packages] + +[packages] +requests = {version="*"} +flask = {version="*"} diff --git a/homecontrol.py b/homecontrol.py new file mode 100755 index 0000000..43552f6 --- /dev/null +++ b/homecontrol.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +from flask import Flask, request, json, jsonify, abort, make_response +import argparse +import time + +CONFIG_FILE='config.json' +PORT = 5000 + +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: + pass + + # 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): + self.nodes = {} + + def update(self): + pass + +class Node: + def __init__(self, type): + self.type == type + self.values = {} + + def add_value(self, value): + self.values[time.time()] = value + + 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 = 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(): + ret = {} + content = {} + try: + content = request.json + print(content) + except Exception as ex: + logger.error('Exception, Type:%s, args:\n%s' % (type(ex).__name__, ex.args)) + + return ret + +if __name__ == "__main__": + args, config = setup() + + app.run(host = '0.0.0.0', port=config['port'], debug=args.debug)