From 4b454d9d6fe1db35a4affe50d6291df5009e084b Mon Sep 17 00:00:00 2001 From: Cole Deck Date: Fri, 5 May 2023 16:22:22 -0500 Subject: [PATCH] update sensor and device ID code --- client.py | 4 ++-- main.py | 55 +++++++++++++++++++++++++------------------- subscribe.yaml | 13 +++++++---- test-run/data.ndjson | 30 ++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 31 deletions(-) diff --git a/client.py b/client.py index 18fd5e9..d1ee6a5 100644 --- a/client.py +++ b/client.py @@ -40,9 +40,9 @@ def get_frame(): async def test_coro(): C = MQTTClient() print("START" + str(get_uptime())) - await C.connect('mqtt://100.79.112.65:1883') + await C.connect('mqtt://127.0.0.1:1883') tasks = [ - asyncio.ensure_future(C.publish('sensors/pi1/camera', encoder({"data": str(get_frame()), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)), + asyncio.ensure_future(C.publish('sensors/pi1/camera', encoder({"data": str(get_frame()), "sensorID": 0, "timestamp": time.time_ns() }), qos=QOS_2)), asyncio.ensure_future(C.publish('sensors/pi1/uptime', encoder({"data": get_uptime(), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)), ] await asyncio.wait(tasks) diff --git a/main.py b/main.py index 3ebc1a0..0132f89 100644 --- a/main.py +++ b/main.py @@ -17,9 +17,10 @@ import cv2 as cv sublist = [] +sensorlist = [] +devicelist = [] -sensortypes = ["microphone", "camera", "humidity"] -showData = True +showData = True # debug print async def broker_coro(brokerconfig): broker = Broker(config=brokerconfig) @@ -27,7 +28,7 @@ async def broker_coro(brokerconfig): async def uptime_coro(): C = MQTTClient() - await C.connect('mqtt://127.0.0.1:1883/') + await C.connect('mqtt://127.0.0.1:1883/') # connect to my own broker # Subscribe to '$SYS/broker/uptime' with QOS=1 # Subscribe to '$SYS/broker/load/#' with QOS=2 #await C.subscribe([ @@ -45,13 +46,8 @@ async def uptime_coro(): if packet.variable_header.topic_name.split("/")[-1] == "camera": # trigger image loading print(len(dictout["data"])) img = np.frombuffer(base64.b64decode(dictout["data"]), np.uint8) - #height, width = img.shape[:2] - #print(height, width) - - #cv.imshow('frame', img) cv.imwrite('./output.jpg', img) - #if cv.waitKey(1) == ord('q'): - # break + else: publishData(str(dictout["data"]), str(dictout["sensorID"]), str(packet.variable_header.topic_name.split("/")[-1]), dictout["timestamp"]) #await C.unsubscribe(['a/b']) @@ -66,23 +62,32 @@ def publishData(data, sensorID, sensorType, dataTimestamp): print("publishing network.bridge.sensor." + sensorType + " with metadata:", {"sensorID": sensorID, "sensorType": sensorType}, "and timestamp:", dataTimestamp, "and data:", data) else: print("publishing network.bridge.sensor." + sensorType + "with metadata", {"sensorID": sensorID, "sensorType": sensorType}) - + # send data to waggle: plugin.publish("network.bridge.sensor." + sensorType, data, meta={"sensorID": str(sensorID), "sensorType": str(sensorType)}, timestamp=dataTimestamp) - #time.sleep(1) - print("DONE") + +sensorcount = 0 +devicecount = 0 def subdict(d, header): - # coordinate mqtt topic names and waggle topics / sensor names - for k,v in d.items(): - if isinstance(v, dict): - if header is not None: - subdict(v, header + k + "/") # recurse further - else: - subdict(v, k + "/") - else: - for sensor in v: - sublist.append((header + k + "/" + sensor, QOS_2)) - #print (header + k + "/" + sensor) + # coordinate mqtt topic names and waggle topics / sensor names + global sensorcount + global devicecount + for k,v in d.items(): + if isinstance(v, dict): + if header is not None: + subdict(v, header + k + "/") # recurse further + else: + subdict(v, k + "/") + else: + for sensor in v: + sublist.append((header + k + "/" + sensor, QOS_2)) + sensorlist.append((header + k + "/" + sensor, sensorcount)) + sensorcount = sensorcount + 1 + if (header + k + "/", devicecount) not in devicelist: + devicelist.append((header + k + "/", devicecount)) + devicecount = devicecount + 1 + + #print (((header + k + "/" + sensor, QOS_2), sensorcount)) def decoder(data): print (data) @@ -97,7 +102,9 @@ if __name__ == "__main__": with open('subscribe.yaml', 'r') as fileread: clientconfig = yaml.safe_load(fileread) subdict(clientconfig, None) - print(sublist) + print("sublist:", sublist) + print("sensors:", sensorlist) + print("devices:", devicelist) formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s" logging.basicConfig(level=logging.ERROR, format=formatter) diff --git a/subscribe.yaml b/subscribe.yaml index b1d64df..a765dc4 100644 --- a/subscribe.yaml +++ b/subscribe.yaml @@ -2,10 +2,13 @@ sensors: pico1: - "distance" - "uptime" - pico2: - cameras: - - "1" - - "2" pi1: - "camera" - - "uptime" \ No newline at end of file + - "uptime" + subnet2: + pi2: + - "microphone" + - "uptime" + pico1: + - "soil" + - "uptime" \ No newline at end of file diff --git a/test-run/data.ndjson b/test-run/data.ndjson index e7babf6..902bed1 100644 --- a/test-run/data.ndjson +++ b/test-run/data.ndjson @@ -19,3 +19,33 @@ {"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T15:33:23.011959212","value":"1098.75"} {"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T15:34:44.917333986","value":"1180.65"} {"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T15:34:53.142359741","value":"1188.88"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:11.443747983","value":"3247.18"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:14.465929434","value":"3250.2"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:24.438959594","value":"3260.17"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:32.625187148","value":"3268.36"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:40.726503997","value":"3276.46"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:48.881783996","value":"3284.62"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:57.108593092","value":"3292.84"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:04.981753079","value":"3300.72"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:12.597964062","value":"3308.33"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:20.662644273","value":"3316.4"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:28.753982972","value":"3324.49"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:37.557337680","value":"3333.29"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:45.428986457","value":"3341.16"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:53.654332320","value":"3349.39"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:11:02.256857301","value":"3357.99"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:28.485337540","value":"155228.65"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:33.640031336","value":"155233.8"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:34.126547740","value":"155234.29"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:44.140272441","value":"155244.3"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:44.640592407","value":"155244.8"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:49.720956112","value":"155249.89"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:59.638603072","value":"155259.8"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:04.890556821","value":"155265.05"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:10.141516037","value":"155270.31"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:15.391559847","value":"155275.56"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:20.641153525","value":"155280.81"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:25.891244907","value":"155286.06"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:31.140422116","value":"155291.3"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:36.641924478","value":"155296.81"} +{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:42.141766853","value":"155302.31"}