瀏覽代碼

[cli][fetch] add "--type" option to filter the variables that we update

Maxime Alves LIRMM@home 4 年之前
父節點
當前提交
5cc9e64e95
共有 1 個檔案被更改,包括 79 行新增68 行删除
  1. 79
    68
      pyheatpump/cli.py

+ 79
- 68
pyheatpump/cli.py 查看文件

@@ -54,82 +54,93 @@ def run(host, port):
54 54
         reload=True)
55 55
 
56 56
 
57
+@click.option('--type', '-t', default=None, multiple=True)
57 58
 @cli.command()
58
-def fetch():
59
+def fetch(type):
59 60
     logger = logger_init()
60 61
 
61 62
     from pyheatpump import modbus
62
-    var_types = VariableType.getall()
63 63
 
64
-    # Analog - float
65
-    analog = var_types['Analog']
66
-    logger.info('Read analog variables in registers [{}, {}]'.format(
67
-        analog.start_address, analog.end_address
68
-    ))
69
-    res = modbus.read_holding_registers(analog.start_address, analog.end_address)
70
-
71
-    for r in range(len(res)):
72
-        var = Variable(**{
73
-            'type': analog,
74
-            'address': r + analog.start_address})
75
-
76
-        if not var.exists():
77
-            logger.info('Insert variable {}:{}'.format(
78
-                var.type, var.address))
79
-            var.insert()
64
+    if type is None:
65
+        var_types = VariableType.getall()
66
+    else:
67
+        var_types = {}
68
+        for label, var_type in VariableType.getall().items():
69
+            if label in type or var_type.slabel in type:
70
+                var_types[label] = var_type
80 71
 
81
-        val = VariableValue(**{
82
-            'type': var.type,
83
-            'address': var.address,
84
-            'value': res[r]})
85
-        val.insert()
72
+    # Analog - float
73
+    if 'Analog' in var_types.keys():
74
+        analog = var_types['Analog']
75
+        logger.info('Read analog variables in registers [{}, {}]'.format(
76
+            analog.start_address, analog.end_address
77
+        ))
78
+        res = modbus.read_holding_registers(analog.start_address, analog.end_address)
79
+
80
+        for r in range(len(res)):
81
+            var = Variable(**{
82
+                'type': analog,
83
+                'address': r + analog.start_address})
84
+
85
+            if not var.exists():
86
+                logger.info('Insert variable {}:{}'.format(
87
+                    var.type, var.address))
88
+                var.insert()
89
+
90
+            val = VariableValue(**{
91
+                'type': var.type,
92
+                'address': var.address,
93
+                'value': res[r]})
94
+            val.insert()
86 95
 
87 96
 
88 97
     # Integer - int
89
-    integer = var_types['Integer']
90
-    logger.info('Read integer variables in registers [{}, {}]'.format(
91
-        integer.start_address, integer.end_address
92
-    ))
93
-    res = modbus.read_holding_registers(integer.start_address, integer.end_address)
94
-
95
-    for r in range(len(res)):
96
-        var = Variable(**{
97
-            'type': integer,
98
-            'address': r + integer.start_address})
99
-
100
-        if not var.exists():
101
-            logger.info('Insert variable {}:{}'.format(
102
-                var.type, var.address))
103
-            var.insert()
104
-
105
-        val = VariableValue(**{
106
-            'type': var.type,
107
-            'address': var.address,
108
-            'value': res[r]})
109
-        val.insert()
98
+    if 'Integer' in var_types.keys():
99
+        integer = var_types['Integer']
100
+        logger.info('Read integer variables in registers [{}, {}]'.format(
101
+            integer.start_address, integer.end_address
102
+        ))
103
+        res = modbus.read_holding_registers(integer.start_address, integer.end_address)
104
+
105
+        for r in range(len(res)):
106
+            var = Variable(**{
107
+                'type': integer,
108
+                'address': r + integer.start_address})
109
+
110
+            if not var.exists():
111
+                logger.info('Insert variable {}:{}'.format(
112
+                    var.type, var.address))
113
+                var.insert()
114
+
115
+            val = VariableValue(**{
116
+                'type': var.type,
117
+                'address': var.address,
118
+                'value': res[r]})
119
+            val.insert()
110 120
 
111 121
     # Digital - bool
112
-    digital = var_types['Digital']
113
-    logger.info('Read digital variables in coils [{}, {}]'.format(
114
-        digital.start_address, digital.end_address
115
-    ))
116
-    res = modbus.read_coils(digital.start_address, digital.end_address)
117
-
118
-    for r in range(len(res)):
119
-        var = Variable(**{
120
-            'type': digital,
121
-            'address': r + digital.start_address})
122
-
123
-        if not var.exists():
124
-            logger.info('Insert variable {}:{}'.format(
125
-                var.type, var.address))
126
-            var.insert()
127
-
128
-        val = VariableValue(**{
129
-            'type': var.type,
130
-            'address': var.address,
131
-            'value': res[r]})
132
-        val.insert()
122
+    if 'Digital' in var_types.keys():
123
+        digital = var_types['Digital']
124
+        logger.info('Read digital variables in coils [{}, {}]'.format(
125
+            digital.start_address, digital.end_address
126
+        ))
127
+        res = modbus.read_coils(digital.start_address, digital.end_address)
128
+
129
+        for r in range(len(res)):
130
+            var = Variable(**{
131
+                'type': digital,
132
+                'address': r + digital.start_address})
133
+
134
+            if not var.exists():
135
+                logger.info('Insert variable {}:{}'.format(
136
+                    var.type, var.address))
137
+                var.insert()
138
+
139
+            val = VariableValue(**{
140
+                'type': var.type,
141
+                'address': var.address,
142
+                'value': res[r]})
143
+            val.insert()
133 144
 
134 145
     commit()
135 146
     logger.info('Successfully read all variables')
@@ -175,17 +186,17 @@ def supervise(since):
175 186
     }
176 187
 
177 188
     logger.info(build_url(post_url))
189
+    data = h.__dict__()
178 190
     try:
179
-        json.dumps(h.__dict__())
191
+        logger.debug(json.dumps(data))
180 192
     except Exception as e:
181 193
         print(e)
182 194
         sys.exit(1)
183 195
 
184
-    logger.debug(h.__dict__())
185 196
 
186 197
     post_resp = requests.post(
187 198
         url=build_url(post_url),
188
-        json=h.__dict__(),
199
+        json=data,
189 200
         verify=False
190 201
     )
191 202
     if post_resp.status_code == 200:

Loading…
取消
儲存