Browse Source

[supervise] shift dictionary values to index (#30)

Maxime Alves LIRMM@home 4 years ago
parent
commit
d01a559f75
1 changed files with 25 additions and 3 deletions
  1. 25
    3
      pyheatpump/cli.py

+ 25
- 3
pyheatpump/cli.py View File

@@ -166,6 +166,28 @@ def supervise(since):
166 166
 
167 167
     from .models.heatpump import Heatpump
168 168
 
169
+    def shift_response(d):
170
+        def shift_dict(d):
171
+            d_k = list(map(int, d.keys()))
172
+            d_k.sort()
173
+            offset = d_k[0]-1
174
+            return {
175
+                str(k - offset): d[str(k)]
176
+                for k in d_k
177
+                if str(k) in d
178
+            }
179
+
180
+        if 'Analog' in d.keys():
181
+            d.update({"Analog": shift_dict(d['Analog'])})
182
+
183
+        if 'Integer' in d.keys():
184
+            d.update({"Integer": shift_dict(d['Integer'])})
185
+
186
+        if 'Digital' in d.keys():
187
+            d.update({"Digital": shift_dict(d['Digital'])})
188
+
189
+        return d
190
+
169 191
 
170 192
     if not since:
171 193
         last_update = 0
@@ -207,17 +229,17 @@ def supervise(since):
207 229
 
208 230
     for packet in post_packets:
209 231
         try:
210
-            logger.debug(json.dumps(packet))
232
+            logger.debug(json.dumps(shift_response(packet)))
211 233
         except Exception as e:
212 234
             print(e)
213 235
             sys.exit(1)
214 236
 
215 237
 
216 238
     for packet in post_packets:
217
-        logger.debug('Will send %s', packet)
239
+        logger.debug('Will send %s', shift_response(packet))
218 240
         post_resp = requests.post(
219 241
             url=build_url(post_url),
220
-            json=packet,
242
+            json=shift_response(packet),
221 243
             verify=False
222 244
         )
223 245
         if post_resp.status_code == 200:

Loading…
Cancel
Save