Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

variable_types.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. import os
  3. from datetime import datetime
  4. from configparser import ConfigParser
  5. from starlette.routing import Route, Router
  6. from starlette.responses import PlainTextResponse, JSONResponse
  7. import uvicorn
  8. import json
  9. # pyHeatpump modules
  10. from pyheatpump.models.variable_type import VariableType
  11. async def get_variable_types(request):
  12. return JSONResponse({
  13. key: val.__dict__
  14. for key, val in VariableType.getall().items()
  15. })
  16. async def set_variable_types(request):
  17. body = json.loads(await request.json())
  18. for var_type_label, var_type_values in body.items():
  19. vt = VariableType(label=var_type_label)
  20. for key, val in var_type_values.items():
  21. if key in [ 'start_address', 'end_address' ]:
  22. setattr(vt, key, val)
  23. vt.save()
  24. return PlainTextResponse('OK')
  25. async def variable_types_routes(request, *args, **kwargs):
  26. if request['method'] == 'GET':
  27. return await get_variable_types(request)
  28. elif request['method'] == 'POST':
  29. return await set_variable_types(request)
  30. raise NotImplementedError
  31. ROUTES=[
  32. Route('/', variable_types_routes, methods=['GET', 'POST'])
  33. ]
  34. app = Router(routes=ROUTES)
  35. if __name__ == '__main__':
  36. uvicorn.run('pyHeatpump:variable_types.app',
  37. host='127.0.0.1',
  38. port=8000)