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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. default_config = {
  11. 'heatpump': {
  12. 'serial_port': '/dev/ttyUSB0',
  13. 'baudrate': 19200,
  14. 'mac_address': '00:00:00:00:00:00',
  15. 'ip_address': 'dhcp',
  16. 'read_only': 'False',
  17. 'database': '/var/run/pyheatpump/pyheatpump.sqlite3'
  18. },
  19. 'supervisor': {
  20. 'scheme': 'http',
  21. 'host': '127.0.0.1:8000',
  22. 'get_path': '/',
  23. 'post_path': '/',
  24. 'interval': 10000,
  25. 'auth': 'None',
  26. 'initialization': str(datetime.now()),
  27. 'heatpump_id': 'None'
  28. }
  29. }
  30. CONFIG_FILES=[
  31. './pyheatpump.ini',
  32. '/etc/pyheatpump.ini']
  33. config = ConfigParser(
  34. default_section='heatpump')
  35. config.read_dict(default_config)
  36. config.read(filenames=CONFIG_FILES)
  37. async def get_config(request):
  38. items = dict([ (
  39. section, dict([ (key, config.get(section, key)) for key in items ])
  40. ) for section, items in config.items() ])
  41. return JSONResponse(items)
  42. async def set_config(request):
  43. body = json.loads(await request.json())
  44. for sect in body.keys():
  45. for item in body[sect].keys():
  46. if not config.has_option(sect, item):
  47. raise HTTPException(404, f'The option {sect}.{item} does not exists')
  48. else:
  49. config.set(sect, item, value=body[sect][item])
  50. with open(CONFIG_FILES[0], 'w') as conf_file:
  51. config.write(conf_file)
  52. return PlainTextResponse('OK')
  53. async def config_route(request, *args, **kwargs):
  54. if request['method'] == 'GET':
  55. return await get_config(request)
  56. elif request['method'] == 'POST':
  57. return await set_config(request)
  58. ROUTES=[
  59. Route('/', config_route, methods=['GET', 'POST'])
  60. ]
  61. app = Router(routes=ROUTES)
  62. if __name__ == '__main__':
  63. uvicorn.run('pyHeatpump:conf.app',
  64. host='127.0.0.1',
  65. port=8000)