#!/usr/bin/env python3 import os from datetime import datetime from configparser import ConfigParser from starlette.routing import Route, Router from starlette.responses import PlainTextResponse, JSONResponse from pprint import pprint import uvicorn import json # pyHeatpump modules from pyheatpump.models.variable import Variable async def get_variables(request): def wo_type(d): d.pop('type') return d return JSONResponse({ key: { add: wo_type(var.__dict__) for add, var in var_type.items() } for key, var_type in Variable.getall().items() }) async def set_variables(request): body = json.loads(await request.json()) """ for var_type_label, var_type_values in body.items(): vt = VariableType(label=var_type_label) for key, val in var_type_values.items(): if key in [ 'start_address', 'end_address' ]: setattr(vt, key, val) vt.save() """ return PlainTextResponse('OK') async def variables_routes(request, *args, **kwargs): if request['method'] == 'GET': return await get_variables(request) elif request['method'] == 'POST': return await set_variables(request) ROUTES=[ Route('/', variables_routes, methods=['GET', 'POST']) ] app = Router(routes=ROUTES) if __name__ == '__main__': uvicorn.run('pyHeatpump:variables.app', host='127.0.0.1', port=8000)