greenhouseui/gardenui/app.py

162 lines
5.0 KiB
Python
Raw Normal View History

2021-05-05 12:17:10 +00:00
import json
import logging
import xmlrpc.client
from flask import Flask
from flask import render_template
from flask import make_response
from flask import request
from flask import jsonify
LOG_LEVEL = logging.INFO
LOG_FILE = "/var/log/sauna.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
URL = 'http://{}:{}'.format('localhost', 64001)
# logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE)
logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL)
log = logging.getLogger()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/garden')
def garden():
return render_template('garden.html')
@app.route('/greenhouse_1')
def greenhouse_1():
return render_template('greenhouse_1.html')
@app.route('/greenhouse_2')
def greenhouse_2():
return render_template('greenhouse_2.html')
@app.route('/sample/<idx>', methods=['GET'])
def get_sample(idx='0'):
idx = int(idx)
res = {}
client = xmlrpc.client.ServerProxy(URL)
if idx == 1:
heat = {}
heat['id'] = str(idx)
heat['state'] = client.get_current_heat_state(idx)
temp = client.get_current_temperature(idx)
if temp:
temperature = {}
temperature['id'] = str(idx)
temperature['value'] = temp
res['heat'] = heat
res['temperature'] = temperature
water = {}
water['id'] = str(idx)
water['state'] = client.get_current_water_state(idx)
res['water'] = water
return make_response(jsonify(res), 200)
@app.route('/config/<idx>', methods=['GET'])
def get_config(idx='0'):
idx = int(idx)
res = {}
client = xmlrpc.client.ServerProxy(URL)
if idx == 1:
heat = {}
heat['id'] = str(idx)
heat['autostate'] = client.get_heat_autostate(idx)
heat['on_temperature'] = client.get_on_temperature(idx)
heat['off_temperature'] = client.get_off_temperature(idx)
res['heat'] = heat
water = {}
water['id'] = str(idx)
water['autostate'] = client.get_water_autostate(idx)
water['times'] = client.get_water_times(idx)
res['water'] = water
return make_response(jsonify(res), 200)
@app.route('/sample', methods=['PATCH'])
def patch_sample():
client = xmlrpc.client.ServerProxy(URL)
record = json.loads(request.data)
if 'id' in record and record['id'] == '1':
if 'heatstate' in record:
if record['heatstate']:
client.set_heat_state(record['id'])
else:
client.clear_heat_state(record['id'])
if 'id' in record and 'waterstate' in record:
if record['waterstate']:
client.set_water_state(record['id'])
else:
client.clear_water_state(record['id'])
return make_response("", 204)
@app.route('/config', methods=['PATCH'])
def patch_config():
client = xmlrpc.client.ServerProxy(URL)
record = json.loads(request.data)
if 'water' in record:
water = record['water']
if 'autostate' in water:
if water['autostate']:
client.set_water_autostate(water['id'])
else:
client.clear_water_autostate(water['id'])
if 'times' in water:
client.set_water_times(water['id'], water['times'])
if 'heat' in record:
if 'id' in record['heat'] and record['heat']['id'] == '1':
if 'autostate' in record['heat']:
if record['heat']['autostate']:
client.set_heat_autostate('1')
else:
client.clear_heat_autostate('1')
if 'increase_on_temperature' in record['heat']:
if record['heat']['increase_on_temperature']:
client.increase_on_temperature('1')
if 'decrease_on_temperature' in record['heat']:
if record['heat']['decrease_on_temperature']:
client.decrease_on_temperature('1')
if 'increase_off_temperature' in record['heat']:
if record['heat']['increase_off_temperature']:
client.increase_off_temperature('1')
if 'decrease_off_temperature' in record['heat']:
if record['heat']['decrease_off_temperature']:
client.decrease_off_temperature('1')
# prepare answer
res = {}
if 'heat' in record and record['heat']['id'] == '1':
heat = {}
heat['id'] = '1'
heat['autostate'] = client.get_heat_autostate(1)
heat['on_temperature'] = client.get_on_temperature(1)
heat['off_temperature'] = client.get_off_temperature(1)
res['heat'] = heat
if 'water' in record:
water = {}
water['id'] = record['water']['id']
water['autostate'] = client.get_water_autostate(int(record['water']['id']))
water['times'] = client.get_water_times(int(record['water']['id']))
res['water'] = water
return make_response(jsonify(res), 200)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)