From ee4dcff214839b18f66c16e901ac326a48de3135 Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Sat, 9 Apr 2022 17:47:30 +0200
Subject: [PATCH] Using JSON instead of YAML as the ESPs only do JSON
 serialization atm

---
 devtools/mqtt/FakeQthingDevice.py | 31 +++++++++++++++++++++----------
 devtools/mqtt/MMQTT.py            |  6 ++++--
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/devtools/mqtt/FakeQthingDevice.py b/devtools/mqtt/FakeQthingDevice.py
index 3769ebc..dd2bf93 100755
--- a/devtools/mqtt/FakeQthingDevice.py
+++ b/devtools/mqtt/FakeQthingDevice.py
@@ -3,7 +3,6 @@
 
 import time
 import random
-from xmlrpc.client import Boolean
 from MMQTT import MMQTT
 
 
@@ -52,7 +51,7 @@ class PublishAction(Action):
 
 class OnlineStatusAction:
 
-  def __init__(self, online: Boolean):
+  def __init__(self, online: bool):
     self.online = online
 
   def execute(self, dev: Device) -> None:
@@ -84,7 +83,7 @@ class ListenerAction:
 
 class MetricsAction:
 
-  def __init__(self, nameSpace: str = 'CyanLight', reset: int = None, uptime: int = None, lifetime: int = None, **metrics):
+  def __init__(self, nameSpace: str = 'CyanLight', reset: int = (1, 1337), uptime: int = (1, 1e6), lifetime: int = None, birth: int = (1e9, time.time()), **metrics):
     '''Setting a metric to None means it gets randomized every action
     
     The predefined metrics are mandatory, but may be overwritten through **kwargs
@@ -93,6 +92,7 @@ class MetricsAction:
     self.nameSpace = nameSpace
 
     self.metrics = {}
+    self.metrics['birth'] = birth
     self.metrics['reset'] = reset
     self.metrics['uptime'] = uptime
     self.metrics['lifetime'] = lifetime
@@ -108,7 +108,7 @@ class MetricsAction:
       if v is None:
         metrics[k] = random.randrange(1, 2**32)
       elif issubclass(type(v), tuple) and len(v) >= 2 and v[0] < v[1]:
-        metrics[k] = random.randrange(v[0], v[1])
+        metrics[k] = random.randrange(int(v[0]), int(v[1]))
       else:
         metrics[k] = v
 
@@ -126,12 +126,23 @@ if __name__ == '__main__':
 
   on = OnlineStatusAction(True)
   off = OnlineStatusAction(False)
-  pause = SleepAction()
+  p1 = SleepAction(1)
+  p3 = SleepAction(3)
+  p5 = SleepAction(5)
+  # p10 = SleepAction(10)
+  listener = ListenerAction()
+  metrics = MetricsAction()
 
   dev = Device('ringclock', mqtt)
 
-  on.execute(dev)
-  pause.execute(dev)
-  off.execute(dev)
-
-  time.sleep(0.5)
+  while True:
+    on.execute(dev)
+    p1.execute(dev)
+    listener.execute(dev)
+    p1.execute(dev)
+    for _ in range(10):
+      metrics.execute(dev)
+      p3.execute(dev)
+    p3.execute(dev)
+    off.execute(dev)
+    p5.execute(dev)
diff --git a/devtools/mqtt/MMQTT.py b/devtools/mqtt/MMQTT.py
index 661f20a..c9f034c 100644
--- a/devtools/mqtt/MMQTT.py
+++ b/devtools/mqtt/MMQTT.py
@@ -1,5 +1,6 @@
 
-import yaml
+# import yaml
+import json
 import time
 import random
 import paho.mqtt.client as mqtt
@@ -50,7 +51,8 @@ class MMQTT:
     elif issubclass(payloadType, (bool, int, float, complex)):
       pl = str(payload).encode('utf-8')
     elif issubclass(payloadType, (list, set, dict)):
-      pl = yaml.dump(payload, default_flow_style=not prettyPrintYAML).encode('utf-8')
+      pl = json.dumps(payload).encode('utf-8')
+      # pl = yaml.dump(payload, default_flow_style=not prettyPrintYAML).encode('utf-8')
     else:
       pl = payload
 
-- 
GitLab