Explorar el Código

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

Maxime Alves LIRMM@home hace 4 años
padre
commit
4bad744ecb
Se han modificado 1 ficheros con 24 adiciones y 3 borrados
  1. 24
    3
      pyheatpump/cli.py

+ 24
- 3
pyheatpump/cli.py Ver fichero

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

Loading…
Cancelar
Guardar