|
|
@ -10,8 +10,9 @@ from amqtt.broker import Broker
|
|
|
|
import yaml
|
|
|
|
import yaml
|
|
|
|
from amqtt.client import MQTTClient, ClientException
|
|
|
|
from amqtt.client import MQTTClient, ClientException
|
|
|
|
from amqtt.mqtt.constants import QOS_1, QOS_2
|
|
|
|
from amqtt.mqtt.constants import QOS_1, QOS_2
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sublist = []
|
|
|
|
|
|
|
|
|
|
|
|
sensortypes = ["microphone", "camera", "humidity"]
|
|
|
|
sensortypes = ["microphone", "camera", "humidity"]
|
|
|
|
showData = True
|
|
|
|
showData = True
|
|
|
@ -25,16 +26,21 @@ async def uptime_coro():
|
|
|
|
await C.connect('mqtt://127.0.0.1:1883/')
|
|
|
|
await C.connect('mqtt://127.0.0.1:1883/')
|
|
|
|
# Subscribe to '$SYS/broker/uptime' with QOS=1
|
|
|
|
# Subscribe to '$SYS/broker/uptime' with QOS=1
|
|
|
|
# Subscribe to '$SYS/broker/load/#' with QOS=2
|
|
|
|
# Subscribe to '$SYS/broker/load/#' with QOS=2
|
|
|
|
await C.subscribe([
|
|
|
|
#await C.subscribe([
|
|
|
|
('a/b', QOS_1),
|
|
|
|
# ('a/b', QOS_1),
|
|
|
|
])
|
|
|
|
# ])
|
|
|
|
|
|
|
|
await C.subscribe(sublist)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
for i in range(1, 100):
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
count = count + 1
|
|
|
|
message = await C.deliver_message()
|
|
|
|
message = await C.deliver_message()
|
|
|
|
packet = message.publish_packet
|
|
|
|
packet = message.publish_packet
|
|
|
|
print("%d: %s => %s" % (i, packet.variable_header.topic_name, str(packet.payload.data)))
|
|
|
|
dictout = decoder(packet.payload.data)
|
|
|
|
await C.unsubscribe(['a/b'])
|
|
|
|
print("%d: %s => %s" % (count, packet.variable_header.topic_name, dictout))
|
|
|
|
await C.disconnect()
|
|
|
|
publishData(dictout["data"], dictout["sensorID"], packet.variable_header.topic_name.split("/")[-1], dictout["timestamp"])
|
|
|
|
|
|
|
|
#await C.unsubscribe(['a/b'])
|
|
|
|
|
|
|
|
#await C.disconnect()
|
|
|
|
except ClientException as ce:
|
|
|
|
except ClientException as ce:
|
|
|
|
logger.error("Client exception: %s" % ce)
|
|
|
|
logger.error("Client exception: %s" % ce)
|
|
|
|
|
|
|
|
|
|
|
@ -46,16 +52,38 @@ def publishData(data, sensorID, sensorType, dataTimestamp):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print("publishing network.bridge.sensor." + sensorType + "with metadata", {"sensorID": sensorID, "sensorType": sensorType})
|
|
|
|
print("publishing network.bridge.sensor." + sensorType + "with metadata", {"sensorID": sensorID, "sensorType": sensorType})
|
|
|
|
|
|
|
|
|
|
|
|
plugin.publish("network.bridge.sensor." + sensorType, data, meta={"sensorID": sensorID, "sensorType": sensorType}, timestamp=dataTimestamp)
|
|
|
|
plugin.publish("network.bridge.sensor." + sensorType, data, meta={"sensorID": str(sensorID), "sensorType": str(sensorType)}, timestamp=dataTimestamp)
|
|
|
|
#time.sleep(1)
|
|
|
|
#time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def subdict(d, header):
|
|
|
|
|
|
|
|
for k,v in d.items():
|
|
|
|
|
|
|
|
if isinstance(v, dict):
|
|
|
|
|
|
|
|
if header is not None:
|
|
|
|
|
|
|
|
subdict(v, header + k + "/")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
subdict(v, k + "/")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
for sensor in v:
|
|
|
|
|
|
|
|
sublist.append((header + k + "/" + sensor, QOS_2))
|
|
|
|
|
|
|
|
#print (header + k + "/" + sensor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def decoder(data):
|
|
|
|
|
|
|
|
print (data)
|
|
|
|
|
|
|
|
return json.loads(data.decode("utf-8"))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# testing
|
|
|
|
# mqtt broker config
|
|
|
|
with open('broker.yaml', 'r') as fileread:
|
|
|
|
with open('broker.yaml', 'r') as fileread:
|
|
|
|
brokerconfig = yaml.safe_load(fileread)
|
|
|
|
brokerconfig = yaml.safe_load(fileread)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# clients config
|
|
|
|
|
|
|
|
with open('subscribe.yaml', 'r') as fileread:
|
|
|
|
|
|
|
|
clientconfig = yaml.safe_load(fileread)
|
|
|
|
|
|
|
|
subdict(clientconfig, None)
|
|
|
|
|
|
|
|
print(sublist)
|
|
|
|
|
|
|
|
|
|
|
|
formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s"
|
|
|
|
formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s"
|
|
|
|
logging.basicConfig(level=logging.INFO, format=formatter)
|
|
|
|
logging.basicConfig(level=logging.ERROR, format=formatter)
|
|
|
|
asyncio.get_event_loop().run_until_complete(broker_coro(brokerconfig))
|
|
|
|
asyncio.get_event_loop().run_until_complete(broker_coro(brokerconfig))
|
|
|
|
asyncio.get_event_loop().run_until_complete(uptime_coro())
|
|
|
|
asyncio.get_event_loop().run_until_complete(uptime_coro())
|
|
|
|
asyncio.get_event_loop().run_forever()
|
|
|
|
asyncio.get_event_loop().run_forever()
|
|
|
|