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
 
166
 
167
     from .models.heatpump import Heatpump
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
     if not since:
192
     if not since:
171
         last_update = 0
193
         last_update = 0
207
 
229
 
208
     for packet in post_packets:
230
     for packet in post_packets:
209
         try:
231
         try:
210
-            logger.debug(json.dumps(packet))
232
+            logger.debug(json.dumps(shift_response(packet)))
211
         except Exception as e:
233
         except Exception as e:
212
             print(e)
234
             print(e)
213
             sys.exit(1)
235
             sys.exit(1)
214
 
236
 
215
 
237
 
216
     for packet in post_packets:
238
     for packet in post_packets:
217
-        logger.debug('Will send %s', packet)
239
+        logger.debug('Will send %s', shift_response(packet))
218
         post_resp = requests.post(
240
         post_resp = requests.post(
219
             url=build_url(post_url),
241
             url=build_url(post_url),
220
-            json=packet,
242
+            json=shift_response(packet),
221
             verify=False
243
             verify=False
222
         )
244
         )
223
         if post_resp.status_code == 200:
245
         if post_resp.status_code == 200:

Loading…
Cancel
Save