Browse Source

[fetch] ferme la connection modbus dans fetch

Maxime Alves LIRMM@home 4 years ago
parent
commit
3772029bf9
1 changed files with 92 additions and 85 deletions
  1. 92
    85
      pyheatpump/cli.py

+ 92
- 85
pyheatpump/cli.py View File

@@ -60,91 +60,98 @@ def fetch(type):
60 60
 
61 61
     from pyheatpump import modbus
62 62
 
63
-    if type is None:
64
-        var_types = VariableType.getall()
65
-    else:
66
-        var_types = {}
67
-        for label, var_type in VariableType.getall().items():
68
-            if label in type or var_type.slabel in type:
69
-                var_types[label] = var_type
70
-
71
-    # Analog - float
72
-    if 'Analog' in var_types.keys():
73
-        analog = var_types['Analog']
74
-        logger.info('Read analog variables in registers [{}, {}]'.format(
75
-            analog.start_address, analog.end_address
76
-        ))
77
-        res = modbus.read_holding_registers(analog.start_address, analog.end_address)
78
-
79
-        logger.debug(f'analog length : {len(res)}')
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()
95
-
96
-
97
-    # Integer - int
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
-        logger.debug(f'integer length : {len(res)}')
106
-        for r in range(len(res)):
107
-            var = Variable(**{
108
-                'type': integer,
109
-                'address': r + integer.start_address})
110
-
111
-            if not var.exists():
112
-                logger.info('Insert variable {}:{}'.format(
113
-                    var.type, var.address))
114
-                var.insert()
115
-
116
-            val = VariableValue(**{
117
-                'type': var.type,
118
-                'address': var.address,
119
-                'value': res[r]})
120
-            val.insert()
121
-
122
-    # Digital - bool
123
-    if 'Digital' in var_types.keys():
124
-        digital = var_types['Digital']
125
-        logger.info('Read digital variables in coils [{}, {}]'.format(
126
-            digital.start_address, digital.end_address
127
-        ))
128
-        res = modbus.read_coils(digital.start_address, digital.end_address)
129
-
130
-        logger.debug(f'digital length : {len(res)}')
131
-        for r in range(len(res)):
132
-            var = Variable(**{
133
-                'type': digital,
134
-                'address': r + digital.start_address})
135
-
136
-            if not var.exists():
137
-                logger.info('Insert variable {}:{}'.format(
138
-                    var.type, var.address))
139
-                var.insert()
140
-
141
-            val = VariableValue(**{
142
-                'type': var.type,
143
-                'address': var.address,
144
-                'value': res[r]})
145
-            val.insert()
146
-
147
-    logger.info('Successfully read all variables')
63
+    try:
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
71
+
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
+            logger.debug(f'analog length : {len(res)}')
81
+            for r in range(len(res)):
82
+                var = Variable(**{
83
+                    'type': analog,
84
+                    'address': r + analog.start_address})
85
+
86
+                if not var.exists():
87
+                    logger.info('Insert variable {}:{}'.format(
88
+                        var.type, var.address))
89
+                    var.insert()
90
+
91
+                val = VariableValue(**{
92
+                    'type': var.type,
93
+                    'address': var.address,
94
+                    'value': res[r]})
95
+                val.insert()
96
+
97
+
98
+        # Integer - int
99
+        if 'Integer' in var_types.keys():
100
+            integer = var_types['Integer']
101
+            logger.info('Read integer variables in registers [{}, {}]'.format(
102
+                integer.start_address, integer.end_address
103
+            ))
104
+            res = modbus.read_holding_registers(integer.start_address, integer.end_address)
105
+
106
+            logger.debug(f'integer length : {len(res)}')
107
+            for r in range(len(res)):
108
+                var = Variable(**{
109
+                    'type': integer,
110
+                    'address': r + integer.start_address})
111
+
112
+                if not var.exists():
113
+                    logger.info('Insert variable {}:{}'.format(
114
+                        var.type, var.address))
115
+                    var.insert()
116
+
117
+                val = VariableValue(**{
118
+                    'type': var.type,
119
+                    'address': var.address,
120
+                    'value': res[r]})
121
+                val.insert()
122
+
123
+        # Digital - bool
124
+        if 'Digital' in var_types.keys():
125
+            digital = var_types['Digital']
126
+            logger.info('Read digital variables in coils [{}, {}]'.format(
127
+                digital.start_address, digital.end_address
128
+            ))
129
+            res = modbus.read_coils(digital.start_address, digital.end_address)
130
+
131
+            logger.debug(f'digital length : {len(res)}')
132
+            for r in range(len(res)):
133
+                var = Variable(**{
134
+                    'type': digital,
135
+                    'address': r + digital.start_address})
136
+
137
+                if not var.exists():
138
+                    logger.info('Insert variable {}:{}'.format(
139
+                        var.type, var.address))
140
+                    var.insert()
141
+
142
+                val = VariableValue(**{
143
+                    'type': var.type,
144
+                    'address': var.address,
145
+                    'value': res[r]})
146
+                val.insert()
147
+
148
+        logger.info('Successfully read all variables')
149
+    except Exception as exc:
150
+        logger.error(exc)
151
+
152
+    if modbus.serial_conn:
153
+        modbus.serial_conn.close()
154
+
148 155
 
149 156
 
150 157
 @click.option('--since', is_flag=True)

Loading…
Cancel
Save