Projet de remplacement du "RPiPasserelle" d'Otec.
Ви не можете вибрати більше 25 тем Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

variable_values.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 starlette.exceptions import HTTPException
  8. from pprint import pprint
  9. import uvicorn
  10. import json
  11. # pyHeatpump modules
  12. from pyheatpump.models.variable import Variable
  13. from pyheatpump.models.variable_type import VariableType
  14. from pyheatpump.models.variable_value import VariableValue
  15. async def get_variable_values(request):
  16. return JSONResponse({})
  17. async def set_variable_values(request):
  18. raise NotImplementedError
  19. async def get_variable_value(type: str, address: int, time: str=None):
  20. try:
  21. args = {
  22. 'type': type,
  23. 'address': address,
  24. 'time': time
  25. }
  26. if time is None:
  27. args.pop('time')
  28. else:
  29. args['time'] = datetime.fromisoformat(time)
  30. row = VariableValue.get(**args)
  31. return PlainTextResponse(str(row.value))
  32. except StopIteration:
  33. raise HTTPException(404)
  34. async def set_variable_value(request):
  35. raise NotImplementedError
  36. async def variable_values_routes(request, *args, **kwargs):
  37. if request['method'] == 'GET':
  38. return await get_variable_values(request)
  39. elif request['method'] == 'POST':
  40. return await set_variable_values(request)
  41. async def variable_value_routes(request, *args, **kwargs):
  42. if request['method'] == 'GET':
  43. if 'type' not in request.path_params.keys():
  44. raise HTTPException(422)
  45. if 'address' not in request.path_params.keys():
  46. raise HTTPException(422)
  47. if 'time' not in request.path_params.keys():
  48. return await get_variable_value(
  49. request.path_params['type'],
  50. request.path_params['address'])
  51. return await get_variable_value(
  52. request.path_params['type'],
  53. request.path_params['address'],
  54. request.path_params['time'])
  55. elif request['method'] == 'POST':
  56. return await set_variable_value(request *args, **kwargs)
  57. ROUTES=[
  58. Route('/', variable_values_routes, methods=['GET', 'POST']),
  59. Route('/{type:str}/{address:int}', variable_value_routes, methods=['GET', 'POST']),
  60. Route('/{type:str}/{address:int}/{time:str}', variable_value_routes, methods=['GET'])
  61. ]
  62. app = Router(routes=ROUTES)
  63. if __name__ == '__main__':
  64. uvicorn.run('pyHeatpump:variable_values.app',
  65. host='127.0.0.1',
  66. port=8000)