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.

cli.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. from pyheatpump.logger import logger_init
  11. from pyheatpump.models import *
  12. from pyheatpump.db import commit
  13. CONTEXT_SETTINGS={
  14. 'default_map':{'run': {}}
  15. }
  16. @click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
  17. @click.option('--version', is_flag=True)
  18. @click.pass_context
  19. def cli(ctx, version):
  20. if version:
  21. from pyheatpump import version
  22. return click.echo(pyheatpump.version())
  23. @click.option('--host', default=None)
  24. @click.option('--port', default=None)
  25. @cli.command()
  26. def run(host, port):
  27. logger = logger_init()
  28. from .config import (API_HOST, API_PORT)
  29. if not host:
  30. host = API_HOST
  31. if not port:
  32. port = API_PORT
  33. log_level = 'info'
  34. click.echo('Launching PyHeatpump application')
  35. uvicorn.run('pyheatpump.app:application',
  36. host=host,
  37. port=int(port),
  38. log_level=log_level,
  39. reload=True)
  40. @cli.command()
  41. def fetch():
  42. logger = logger_init()
  43. from pyheatpump import modbus
  44. var_types = VariableType.getall()
  45. # Analog - float
  46. analog = var_types['Analog']
  47. logger.info('Read analog variables in registers [{}, {}]'.format(
  48. analog.start_address, analog.end_address
  49. ))
  50. res = modbus.read_holding_registers(analog.start_address, analog.end_address)
  51. for r in range(len(res)):
  52. var = Variable(**{
  53. 'type': analog,
  54. 'address': r + analog.start_address})
  55. if not var.exists():
  56. logger.info('Insert variable {}:{}'.format(
  57. var.type, var.address))
  58. var.insert()
  59. val = VariableValue(**{
  60. 'type': var.type,
  61. 'address': var.address,
  62. 'value': res[r]})
  63. val.insert()
  64. # Integer - int
  65. integer = var_types['Integer']
  66. logger.info('Read integer variables in registers [{}, {}]'.format(
  67. integer.start_address, integer.end_address
  68. ))
  69. res = modbus.read_holding_registers(integer.start_address, integer.end_address)
  70. for r in range(len(res)):
  71. var = Variable(**{
  72. 'type': integer,
  73. 'address': r + integer.start_address})
  74. if not var.exists():
  75. logger.info('Insert variable {}:{}'.format(
  76. var.type, var.address))
  77. var.insert()
  78. val = VariableValue(**{
  79. 'type': var.type,
  80. 'address': var.address,
  81. 'value': res[r]})
  82. val.insert()
  83. # Digital - bool
  84. digital = var_types['Digital']
  85. logger.info('Read digital variables in coils [{}, {}]'.format(
  86. digital.start_address, digital.end_address
  87. ))
  88. res = modbus.read_coils(digital.start_address, digital.end_address)
  89. for r in range(len(res)):
  90. var = Variable(**{
  91. 'type': digital,
  92. 'address': r + digital.start_address})
  93. if not var.exists():
  94. logger.info('Insert variable {}:{}'.format(
  95. var.type, var.address))
  96. var.insert()
  97. val = VariableValue(**{
  98. 'type': var.type,
  99. 'address': var.address,
  100. 'value': res[r]})
  101. val.insert()
  102. commit()
  103. logger.info('Successfully read all variables')