#!/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.db import sql from pyheatpump.models.variable_type import VariableType async def get_variable_types(request): return JSONResponse(dict([ (key, val.__dict__) for key, val in VariableType.getall().items() ])) async def set_variable_types(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 variable_types_routes(request, *args, **kwargs): if request['method'] == 'GET': return await get_variable_types(request) elif request['method'] == 'POST': return await set_variable_types(request) ROUTES=[ Route('/', variable_types_routes, methods=['GET', 'POST']) ] app = Router(routes=ROUTES) if __name__ == '__main__': uvicorn.run('pyHeatpump:conf.app', host='127.0.0.1', port=8000)