Projet de remplacement du "RPiPasserelle" d'Otec.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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. from pprint import pprint
  8. import uvicorn
  9. import json
  10. # pyHeatpump modules
  11. from pyheatpump.models.variable import Variable
  12. async def get_variables(request):
  13. def wo_type(d):
  14. d.pop('type')
  15. return d
  16. return JSONResponse({
  17. key: {
  18. add: wo_type(var.__dict__)
  19. for add, var in var_type.items()
  20. } for key, var_type in Variable.getall().items()
  21. })
  22. async def set_variables(request):
  23. body = json.loads(await request.json())
  24. """
  25. for var_type_label, var_type_values in body.items():
  26. vt = VariableType(label=var_type_label)
  27. for key, val in var_type_values.items():
  28. if key in [ 'start_address', 'end_address' ]:
  29. setattr(vt, key, val)
  30. vt.save()
  31. """
  32. return PlainTextResponse('OK')
  33. async def variables_routes(request, *args, **kwargs):
  34. if request['method'] == 'GET':
  35. return await get_variables(request)
  36. elif request['method'] == 'POST':
  37. return await set_variables(request)
  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)