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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.db import sql
  13. from pyheatpump.models.variable import Variable
  14. from pyheatpump.models.variable_type import VariableType
  15. from pyheatpump.models.variable_value import VariableValue
  16. async def get_variable_values(request):
  17. return JSONResponse({})
  18. async def set_variable_values(request):
  19. raise NotImplementedError
  20. async def get_variable_value(type: str, address: int, time: str=None):
  21. try:
  22. args = {
  23. 'type': type,
  24. 'address': address,
  25. 'time': time
  26. }
  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.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. async def variable_value_routes(request, *args, **kwargs):
  43. if request['method'] == 'GET':
  44. if 'type' not in request.path_params.keys():
  45. raise HTTPException(422)
  46. if 'address' not in request.path_params.keys():
  47. raise HTTPException(422)
  48. if 'time' not in request.path_params.keys():
  49. return await get_variable_value(
  50. request.path_params['type'],
  51. request.path_params['address'])
  52. return await get_variable_value(
  53. request.path_params['type'],
  54. request.path_params['address'],
  55. request.path_params['time'])
  56. elif request['method'] == 'POST':
  57. return await set_variable_value(request *args, **kwargs)
  58. ROUTES=[
  59. Route('/', variable_values_routes, methods=['GET', 'POST']),
  60. Route('/{type:str}/{address:int}', variable_value_routes, methods=['GET', 'POST']),
  61. Route('/{type:str}/{address:int}/{time:str}', variable_value_routes, methods=['GET'])
  62. ]
  63. app = Router(routes=ROUTES)
  64. if __name__ == '__main__':
  65. uvicorn.run('pyHeatpump:variable_values.app',
  66. host='127.0.0.1',
  67. port=8000)