update sensorID and deviceID in API call, override invaled IDs
This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
				
			|||||||
#!/usr/bin/python
 | 
					#!/usr/bin/python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# example client application
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
import numpy as np
 | 
					import numpy as np
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										51
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								main.py
									
									
									
									
									
								
							@@ -19,6 +19,8 @@ import cv2 as cv
 | 
				
			|||||||
sublist = []
 | 
					sublist = []
 | 
				
			||||||
sensorlist = []
 | 
					sensorlist = []
 | 
				
			||||||
devicelist = []
 | 
					devicelist = []
 | 
				
			||||||
 | 
					sensorcount = 0
 | 
				
			||||||
 | 
					devicecount = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
showData = True # debug print
 | 
					showData = True # debug print
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -29,44 +31,53 @@ async def broker_coro(brokerconfig):
 | 
				
			|||||||
async def uptime_coro():
 | 
					async def uptime_coro():
 | 
				
			||||||
    C = MQTTClient()
 | 
					    C = MQTTClient()
 | 
				
			||||||
    await C.connect('mqtt://127.0.0.1:1883/') # connect to my own broker
 | 
					    await C.connect('mqtt://127.0.0.1:1883/') # connect to my own broker
 | 
				
			||||||
    # Subscribe to '$SYS/broker/uptime' with QOS=1
 | 
					    await C.subscribe(sublist) # subscribe to list of sensor topics
 | 
				
			||||||
    # Subscribe to '$SYS/broker/load/#' with QOS=2
 | 
					 | 
				
			||||||
    #await C.subscribe([
 | 
					 | 
				
			||||||
    #        ('a/b', QOS_1),
 | 
					 | 
				
			||||||
    #     ])
 | 
					 | 
				
			||||||
    await C.subscribe(sublist)
 | 
					 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        count = 0
 | 
					 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
            count = count + 1
 | 
					 | 
				
			||||||
            message = await C.deliver_message()
 | 
					            message = await C.deliver_message()
 | 
				
			||||||
            packet = message.publish_packet
 | 
					            packet = message.publish_packet
 | 
				
			||||||
            dictout = decoder(packet.payload.data)
 | 
					            dictout = decoder(packet.payload.data)
 | 
				
			||||||
            print("%d:  %s => %s" % (count, packet.variable_header.topic_name, dictout))
 | 
					            sensorID = 99999 # in case no sensor ID is found somehow
 | 
				
			||||||
 | 
					            if 'sensorID' in dictout.keys() and dictout["sensorID"] > sensorcount:
 | 
				
			||||||
 | 
					                sensorID = dictout["sensorID"]
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                for sensor in sensorlist:
 | 
				
			||||||
 | 
					                    if sensor[0] == packet.variable_header.topic_name:
 | 
				
			||||||
 | 
					                        sensorID = sensor[1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            deviceID = 99999 # in case no device ID is found somehow
 | 
				
			||||||
 | 
					            if 'deviceID' in dictout.keys() and dictout["deviceID"] > devicecount:
 | 
				
			||||||
 | 
					                deviceID = dictout["deviceID"]
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                for device in devicelist:
 | 
				
			||||||
 | 
					                    print('/'.join(packet.variable_header.topic_name.split("/")[0:-1]) + "/")
 | 
				
			||||||
 | 
					                    if device[0] == '/'.join(packet.variable_header.topic_name.split("/")[0:-1]) + "/": # remove the last field (sensor)
 | 
				
			||||||
 | 
					                        deviceID = device[1]
 | 
				
			||||||
 | 
					            timestamp = time.time_ns()
 | 
				
			||||||
 | 
					            if 'timestamp' in dictout.keys():
 | 
				
			||||||
 | 
					                timestamp = dictout["timestamp"]
 | 
				
			||||||
 | 
					            #print("%s => %s" % (packet.variable_header.topic_name, dictout))
 | 
				
			||||||
            if packet.variable_header.topic_name.split("/")[-1] == "camera": # trigger image loading
 | 
					            if packet.variable_header.topic_name.split("/")[-1] == "camera": # trigger image loading
 | 
				
			||||||
                print(len(dictout["data"]))
 | 
					                print(len(dictout["data"]))
 | 
				
			||||||
                img = np.frombuffer(base64.b64decode(dictout["data"]), np.uint8)
 | 
					                img = np.frombuffer(base64.b64decode(dictout["data"]), np.uint8)
 | 
				
			||||||
                cv.imwrite('./output.jpg', img)
 | 
					                cv.imwrite('./output.jpg', img) # write to file for testing
 | 
				
			||||||
 | 
					            publishData(str(dictout["data"]), str(sensorID), str(deviceID), str(packet.variable_header.topic_name.split("/")[-1]), timestamp)
 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                publishData(str(dictout["data"]), str(dictout["sensorID"]), str(packet.variable_header.topic_name.split("/")[-1]), dictout["timestamp"])
 | 
					 | 
				
			||||||
        #await C.unsubscribe(['a/b'])
 | 
					        #await C.unsubscribe(['a/b'])
 | 
				
			||||||
        #await C.disconnect()
 | 
					        #await C.disconnect()
 | 
				
			||||||
    except ClientException as ce:
 | 
					    except ClientException as ce:
 | 
				
			||||||
        logger.error("Client exception: %s" % ce)
 | 
					        logger.error("Client exception: %s" % ce)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def publishData(data, sensorID, sensorType, dataTimestamp):
 | 
					def publishData(data, sensorID, deviceID, sensorType, dataTimestamp):
 | 
				
			||||||
    with Plugin() as plugin:
 | 
					    with Plugin() as plugin:
 | 
				
			||||||
            if showData is True:
 | 
					            if showData is True:
 | 
				
			||||||
                print("publishing network.bridge.sensor." + sensorType + " with metadata:", {"sensorID": sensorID, "sensorType": sensorType}, "and timestamp:", dataTimestamp, "and data:", data)
 | 
					                print("publishing network.bridge.sensor." + sensorType + " with metadata:", {"sensorID": sensorID, "deviceID": deviceID, "sensorType": sensorType}, "and timestamp:", dataTimestamp, "and data:", data)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                print("publishing network.bridge.sensor." + sensorType + "with metadata", {"sensorID": sensorID, "sensorType": sensorType})
 | 
					                print("publishing network.bridge.sensor." + sensorType + "with metadata", {"sensorID": sensorID, "deviceID": deviceID, "sensorType": sensorType})
 | 
				
			||||||
            # send data to waggle:
 | 
					            # send data to waggle:
 | 
				
			||||||
            plugin.publish("network.bridge.sensor." + sensorType, data, meta={"sensorID": str(sensorID), "sensorType": str(sensorType)}, timestamp=dataTimestamp)
 | 
					            plugin.publish("network.bridge.sensor." + sensorType, data, meta={"sensorID": str(sensorID), "deviceID": str(deviceID), "sensorType": str(sensorType)}, timestamp=dataTimestamp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sensorcount = 0
 | 
					 | 
				
			||||||
devicecount = 0
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def subdict(d, header):
 | 
					def subdict(d, header):
 | 
				
			||||||
    # coordinate mqtt topic names and waggle topics / sensor names
 | 
					    # coordinate mqtt topic names and waggle topics / sensor names
 | 
				
			||||||
@@ -83,7 +94,7 @@ def subdict(d, header):
 | 
				
			|||||||
                sublist.append((header + k + "/" + sensor, QOS_2))
 | 
					                sublist.append((header + k + "/" + sensor, QOS_2))
 | 
				
			||||||
                sensorlist.append((header + k + "/" + sensor, sensorcount))
 | 
					                sensorlist.append((header + k + "/" + sensor, sensorcount))
 | 
				
			||||||
                sensorcount = sensorcount + 1
 | 
					                sensorcount = sensorcount + 1
 | 
				
			||||||
                if (header + k + "/", devicecount) not in devicelist:
 | 
					                if (header + k + "/", devicecount - 1) not in devicelist:
 | 
				
			||||||
                    devicelist.append((header + k + "/", devicecount))
 | 
					                    devicelist.append((header + k + "/", devicecount))
 | 
				
			||||||
                    devicecount = devicecount + 1
 | 
					                    devicecount = devicecount + 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user