#!/usr/bin/env python3 # builtins import click import uvicorn import os import sys import re import importlib from pprint import pprint CONTEXT_SETTINGS={ 'default_map':{'run': {}} } @click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS) @click.option('--version', is_flag=True) @click.pass_context def cli(ctx, version): if version: from pyheatpump import version return click.echo(pyheatpump.version()) if ctx.invoked_subcommand is None: return run() @click.option('--host', default=None) @click.option('--port', default=None) @cli.command() def run(host, port): from pyheatpump.conf import (API_HOST, API_PORT) if not host: host = API_HOST if not port: port = API_PORT log_level = 'info' click.echo('Launching PyHeatpump application') uvicorn.run('pyheatpump.app:application', host=host, port=int(port), log_level=log_level, reload=False) @cli.command() def fetch(): from pyheatpump.models import * from pyheatpump import modbus var_types = VariableType.getall() # Analog - float analog = var_types['Analog'] res = modbus.read_holding_registers(analog.start_address, analog.end_address) for r in range(len(res)): val = VariableValue(str(analog), r + analog.start_address, res[r]) val.insert() # Integer - int integer = var_types['Integer'] res = modbus.read_holding_registers(integer.start_address, integer.end_address) for r in res: val = VariableValue(str(integer), r + integer.start_address, res[r]) val.insert() # Digital - bool digital = var_types['Digital'] res = modbus.read_coils(digital.start_address, digital.end_address) for r in res: val = VariableValue(str(integer), r + integer.start_address, res[r]) val.insert()