12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #!/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
- 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)
- raise NotImplementedError
-
-
- 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)
|