|
@@ -8,8 +8,11 @@ import re
|
8
|
8
|
import importlib
|
9
|
9
|
from pprint import pprint
|
10
|
10
|
|
|
11
|
+from pyheatpump.logger import logger_init
|
|
12
|
+from pyheatpump.models import *
|
|
13
|
+
|
11
|
14
|
CONTEXT_SETTINGS={
|
12
|
|
- 'default_map':{'run': {}}
|
|
15
|
+ 'default_map':{'run': {}}
|
13
|
16
|
}
|
14
|
17
|
|
15
|
18
|
@click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
|
|
@@ -20,14 +23,13 @@ def cli(ctx, version):
|
20
|
23
|
from pyheatpump import version
|
21
|
24
|
return click.echo(pyheatpump.version())
|
22
|
25
|
|
23
|
|
- if ctx.invoked_subcommand is None:
|
24
|
|
- return run()
|
25
|
|
-
|
26
|
26
|
|
27
|
27
|
@click.option('--host', default=None)
|
28
|
28
|
@click.option('--port', default=None)
|
29
|
29
|
@cli.command()
|
30
|
30
|
def run(host, port):
|
|
31
|
+ logger = logger_init()
|
|
32
|
+
|
31
|
33
|
from pyheatpump.conf import (API_HOST, API_PORT)
|
32
|
34
|
|
33
|
35
|
if not host:
|
|
@@ -49,33 +51,50 @@ def run(host, port):
|
49
|
51
|
|
50
|
52
|
@cli.command()
|
51
|
53
|
def fetch():
|
52
|
|
- from pyheatpump.models import *
|
53
|
|
- from pyheatpump import modbus
|
54
|
|
-
|
|
54
|
+ logger = logger_init()
|
55
|
55
|
|
|
56
|
+ from pyheatpump import modbus
|
56
|
57
|
var_types = VariableType.getall()
|
57
|
58
|
|
58
|
59
|
# Analog - float
|
59
|
60
|
analog = var_types['Analog']
|
|
61
|
+ logger.info('Read analog variables in registers [{}, {}]'.format(
|
|
62
|
+ analog.start_address, analog.end_address
|
|
63
|
+ ))
|
60
|
64
|
res = modbus.read_holding_registers(analog.start_address, analog.end_address)
|
61
|
65
|
|
62
|
66
|
for r in range(len(res)):
|
63
|
|
- val = VariableValue(analog, r + analog.start_address, res[r])
|
|
67
|
+ val = VariableValue(**{
|
|
68
|
+ 'type': analog,
|
|
69
|
+ 'address': r + analog.start_address,
|
|
70
|
+ 'value': res[r]})
|
64
|
71
|
val.insert()
|
65
|
72
|
|
66
|
73
|
|
67
|
74
|
# Integer - int
|
68
|
75
|
integer = var_types['Integer']
|
|
76
|
+ logger.info('Read integer variables in registers [{}, {}]'.format(
|
|
77
|
+ integer.start_address, integer.end_address
|
|
78
|
+ ))
|
69
|
79
|
res = modbus.read_holding_registers(integer.start_address, integer.end_address)
|
70
|
80
|
|
71
|
|
- for r in res:
|
72
|
|
- val = VariableValue(integer, r + integer.start_address, res[r])
|
|
81
|
+ for r in range(len(res)):
|
|
82
|
+ val = VariableValue(**{
|
|
83
|
+ 'type': integer,
|
|
84
|
+ 'address': r + integer.start_address,
|
|
85
|
+ 'value': res[r]})
|
73
|
86
|
val.insert()
|
74
|
87
|
|
75
|
88
|
# Digital - bool
|
76
|
89
|
digital = var_types['Digital']
|
|
90
|
+ logger.info('Read digital variables in coils [{}, {}]'.format(
|
|
91
|
+ digital.start_address, digital.end_address
|
|
92
|
+ ))
|
77
|
93
|
res = modbus.read_coils(digital.start_address, digital.end_address)
|
78
|
94
|
|
79
|
|
- for r in res:
|
80
|
|
- val = VariableValue(integer), r + integer.start_address, res[r])
|
|
95
|
+ for r in range(len(res)):
|
|
96
|
+ val = VariableValue(**{
|
|
97
|
+ 'type': digital,
|
|
98
|
+ 'address': r + digital.start_address,
|
|
99
|
+ 'value': res[r]})
|
81
|
100
|
val.insert()
|