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_values.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(var_type: str, address: int, time: str=None):
  20. try:
  21. args = {
  22. 'var_type': var_type,
  23. 'address': address,
  24. 'time': time
  25. }
  26. print(args)
  27. if time is None:
  28. args.pop('time')
  29. else:
  30. args['time'] = datetime.fromisoformat(time)
  31. row = VariableValue.get(**args)
  32. return PlainTextResponse(str(row.get_value()))
  33. except StopIteration:
  34. raise HTTPException(404)
  35. async def set_variable_value(request):
  36. raise NotImplementedError
  37. async def variable_values_routes(request, *args, **kwargs):
  38. if request['method'] == 'GET':
  39. return await get_variable_values(request)
  40. elif request['method'] == 'POST':
  41. return await set_variable_values(request)
  42. raise NotImplementedError
  43. async def variable_value_routes(request, *args, **kwargs):
  44. if request['method'] == 'GET':
  45. if 'type' not in request.path_params.keys():
  46. raise HTTPException(422)
  47. if 'address' not in request.path_params.keys():
  48. raise HTTPException(422)
  49. if 'time' not in request.path_params.keys():
  50. return await get_variable_value(
  51. request.path_params['type'],
  52. request.path_params['address'])
  53. return await get_variable_value(
  54. request.path_params['type'],
  55. request.path_params['address'],
  56. request.path_params['time'])
  57. elif request['method'] == 'POST':
  58. return await set_variable_value(request *args, **kwargs)
  59. ROUTES=[
  60. Route('/', variable_values_routes, methods=['GET', 'POST']),
  61. Route('/{type:str}/{address:int}', variable_value_routes, methods=['GET', 'POST']),
  62. Route('/{type:str}/{address:int}/{time:str}', variable_value_routes, methods=['GET'])
  63. ]
  64. app = Router(routes=ROUTES)
  65. if __name__ == '__main__':
  66. uvicorn.run('pyHeatpump:variable_values.app',
  67. host='127.0.0.1',
  68. port=8000)