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