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.

variables.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 import Variable
  11. async def get_variables(request):
  12. def wo_type(d):
  13. d.pop('type')
  14. return d
  15. return JSONResponse({
  16. key: {
  17. add: wo_type(var.__dict__)
  18. for add, var in var_type.items()
  19. } for key, var_type in Variable.getall().items()
  20. })
  21. async def set_variables(request):
  22. body = json.loads(await request.json())
  23. """
  24. for var_type_label, var_type_values in body.items():
  25. vt = VariableType(label=var_type_label)
  26. for key, val in var_type_values.items():
  27. if key in [ 'start_address', 'end_address' ]:
  28. setattr(vt, key, val)
  29. vt.save()
  30. """
  31. return PlainTextResponse('OK')
  32. async def variables_routes(request, *args, **kwargs):
  33. if request['method'] == 'GET':
  34. return await get_variables(request)
  35. elif request['method'] == 'POST':
  36. return await set_variables(request)
  37. raise NotImplementedError
  38. ROUTES=[
  39. Route('/', variables_routes, methods=['GET', 'POST'])
  40. ]
  41. app = Router(routes=ROUTES)
  42. if __name__ == '__main__':
  43. uvicorn.run('pyHeatpump:variables.app',
  44. host='127.0.0.1',
  45. port=8000)