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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. from pprint import pprint
  8. import uvicorn
  9. import json
  10. # pyHeatpump modules
  11. from pyheatpump.models.variable_type import VariableType
  12. async def get_variable_types(request):
  13. return JSONResponse({
  14. key: val.__dict__
  15. for key, val in VariableType.getall().items()
  16. })
  17. async def set_variable_types(request):
  18. body = json.loads(await request.json())
  19. for var_type_label, var_type_values in body.items():
  20. vt = VariableType(label=var_type_label)
  21. for key, val in var_type_values.items():
  22. if key in [ 'start_address', 'end_address' ]:
  23. setattr(vt, key, val)
  24. vt.save()
  25. return PlainTextResponse('OK')
  26. async def variable_types_routes(request, *args, **kwargs):
  27. if request['method'] == 'GET':
  28. return await get_variable_types(request)
  29. elif request['method'] == 'POST':
  30. return await set_variable_types(request)
  31. raise NotImplementedError
  32. ROUTES=[
  33. Route('/', variable_types_routes, methods=['GET', 'POST'])
  34. ]
  35. app = Router(routes=ROUTES)
  36. if __name__ == '__main__':
  37. uvicorn.run('pyHeatpump:variable_types.app',
  38. host='127.0.0.1',
  39. port=8000)