homecontrol: baby steps
This commit is contained in:
commit
775c570de5
3 changed files with 99 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
Pipfile.lock
|
||||
config.json
|
10
Pipfile
Normal file
10
Pipfile
Normal file
|
@ -0,0 +1,10 @@
|
|||
[[source]]
|
||||
url = "https://pypi.python.org/simple"
|
||||
verify_ssl = true
|
||||
name = "pypi"
|
||||
|
||||
[dev-packages]
|
||||
|
||||
[packages]
|
||||
requests = {version="*"}
|
||||
flask = {version="*"}
|
87
homecontrol.py
Executable file
87
homecontrol.py
Executable file
|
@ -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)
|
Loading…
Reference in a new issue