Projet de remplacement du "RPiPasserelle" d'Otec.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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. import uvicorn
  9. import json
  10. # pyHeatpump modules
  11. from pyheatpump.models.variable import Variable
  12. from pyheatpump.models.variable_type import VariableType
  13. from pyheatpump.models.variable_value import VariableValue
  14. async def get_variable_values(request):
  15. return JSONResponse({})
  16. async def set_variable_values(request):
  17. raise NotImplementedError
  18. async def get_variable_value(var_type: str, address: int, time: str=None):
  19. try:
  20. args = {
  21. 'var_type': var_type,
  22. 'address': address,
  23. 'time': time
  24. }
  25. if time is None:
  26. args.pop('time')
  27. else:
  28. args['time'] = datetime.fromisoformat(time)
  29. row = VariableValue.get(**args)
  30. return PlainTextResponse(str(row.get_value()))
  31. except StopIteration:
  32. raise HTTPException(404)
  33. async def set_variable_value(request):
  34. raise NotImplementedError
  35. async def variable_values_routes(request, *args, **kwargs):
  36. if request['method'] == 'GET':
  37. return await get_variable_values(request)
  38. elif request['method'] == 'POST':
  39. return await set_variable_values(request)
  40. raise NotImplementedError
  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)