Browse Source

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

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

+ 28
- 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
+            if not len(d_k):
173
+                return d
174
+
175
+            d_k.sort()
176
+            offset = d_k[0]-1
177
+            return {
178
+                str(k - offset): d[str(k)]
179
+                for k in d_k
180
+                if str(k) in d
181
+            }
182
+
183
+        if 'Analog' in d.keys():
184
+            d.update({"Analog": shift_dict(d['Analog'])})
185
+
186
+        if 'Integer' in d.keys():
187
+            d.update({"Integer": shift_dict(d['Integer'])})
188
+
189
+        if 'Digital' in d.keys():
190
+            d.update({"Digital": shift_dict(d['Digital'])})
191
+
192
+        return d
193
+
169
 
194
 
170
     if not since:
195
     if not since:
171
         last_update = 0
196
         last_update = 0
207
 
232
 
208
     for packet in post_packets:
233
     for packet in post_packets:
209
         try:
234
         try:
210
-            logger.debug(json.dumps(packet))
235
+            logger.debug(json.dumps(shift_response(packet)))
211
         except Exception as e:
236
         except Exception as e:
212
             print(e)
237
             print(e)
213
             sys.exit(1)
238
             sys.exit(1)
214
 
239
 
215
 
240
 
216
     for packet in post_packets:
241
     for packet in post_packets:
217
-        logger.debug('Will send %s', packet)
242
+        logger.debug('Will send %s', shift_response(packet))
218
         post_resp = requests.post(
243
         post_resp = requests.post(
219
             url=build_url(post_url),
244
             url=build_url(post_url),
220
-            json=packet,
245
+            json=shift_response(packet),
221
             verify=False
246
             verify=False
222
         )
247
         )
223
         if post_resp.status_code == 200:
248
         if post_resp.status_code == 200:

Loading…
Cancel
Save