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. 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(dict([
  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. 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)