Projet de remplacement du "RPiPasserelle" d'Otec.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cli.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. # builtins
  3. import click
  4. import uvicorn
  5. import os
  6. import sys
  7. import re
  8. import importlib
  9. from pprint import pprint
  10. CONTEXT_SETTINGS={
  11. 'default_map':{'run': {}}
  12. }
  13. @click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
  14. @click.option('--version', is_flag=True)
  15. @click.pass_context
  16. def cli(ctx, version):
  17. if version:
  18. from pyheatpump import version
  19. return click.echo(pyheatpump.version())
  20. if ctx.invoked_subcommand is None:
  21. return run()
  22. @click.option('--host', default=None)
  23. @click.option('--port', default=None)
  24. @cli.command()
  25. def run(host, port):
  26. from pyheatpump.conf import (API_HOST, API_PORT)
  27. if not host:
  28. host = API_HOST
  29. if not port:
  30. port = API_PORT
  31. log_level = 'info'
  32. click.echo('Launching PyHeatpump application')
  33. uvicorn.run('pyheatpump.app:application',
  34. host=host,
  35. port=int(port),
  36. log_level=log_level,
  37. reload=False)
  38. @cli.command()
  39. def fetch():
  40. from pyheatpump.models import *
  41. from pyheatpump import modbus
  42. var_types = VariableType.getall()
  43. # Analog - float
  44. analog = var_types['Analog']
  45. res = modbus.read_holding_registers(analog.start_address, analog.end_address)
  46. for r in range(len(res)):
  47. val = VariableValue(str(analog), r + analog.start_address, res[r])
  48. val.insert()
  49. # Integer - int
  50. integer = var_types['Integer']
  51. res = modbus.read_holding_registers(integer.start_address, integer.end_address)
  52. for r in res:
  53. val = VariableValue(str(integer), r + integer.start_address, res[r])
  54. val.insert()
  55. # Digital - bool
  56. digital = var_types['Digital']
  57. res = modbus.read_coils(digital.start_address, digital.end_address)
  58. for r in res:
  59. val = VariableValue(str(integer), r + integer.start_address, res[r])
  60. val.insert()