93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
#!/usr/bin/env python
|
|
|
|
from paho.mqtt import client as mqtt_client
|
|
import time
|
|
|
|
mqtt_broker = "mqtt-dev-server.westus2-1.ts.eventgrid.azure.net"
|
|
mqtt_port = 8883
|
|
mqtt_topic = "wellnuotopics/topic1"
|
|
mqtt_client_id = "client1-authn-ID"
|
|
#mqtt_client_id = "292"
|
|
|
|
connected = False
|
|
|
|
monitor = ["64B70889043C", "64B70888F8C0", "64B70888FAD0", "64B70888FAC8"]
|
|
|
|
macs = []
|
|
hist = {}
|
|
|
|
def connect_mqtt():
|
|
def on_connect(client, userdata, flags, rc, props):
|
|
global connected
|
|
if rc == 0:
|
|
print("Connected to MQTT Broker!")
|
|
connected = True
|
|
else:
|
|
print("Failed to connect, return code %d\n", rc)
|
|
|
|
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2, "asdasxzxdadaswd")
|
|
client.tls_set(
|
|
ca_certs='eventgrid.azure_full.pem',
|
|
certfile='../certs/client1-authn-ID.pem',
|
|
keyfile='../certs/client1-authn-ID.key'
|
|
)
|
|
|
|
def on_message(client, userdata, message):
|
|
bin = message.payload
|
|
secs = bin[0] + bin[1]*256 + bin[2]*256*256 + bin[3]*256*256*256
|
|
mac = ''.join('{:02x}'.format(x) for x in bin[8:14])
|
|
|
|
grp_id = bin[14] + bin[15]*256 + bin[16]*256*256 + bin[17]*256*256*256
|
|
|
|
if not mac in macs:
|
|
macs.append(mac)
|
|
|
|
if not mac in hist:
|
|
hist[mac] = 1
|
|
print (f"{len(macs)} {mac} {grp_id}")
|
|
else:
|
|
hist[mac] += 1
|
|
|
|
if mac in monitor:
|
|
print(f"*** {mac} ****")
|
|
|
|
client.username_pw_set(mqtt_client_id, None)
|
|
client.on_connect = on_connect
|
|
client.on_message = on_message
|
|
client.connect(mqtt_broker, mqtt_port)
|
|
client.subscribe(mqtt_topic)
|
|
return client
|
|
|
|
def publish(client, topic, msg):
|
|
result = client.publish(topic, msg)
|
|
# result: [0, 1]
|
|
# status = result[0]
|
|
# if status == 0:
|
|
# print(f"Sent `{msg}` to topic `{topic}`")
|
|
# else:
|
|
# print(f"Failed to send message to topic {topic}")
|
|
|
|
def main() -> None:
|
|
client = connect_mqtt()
|
|
client.loop_start()
|
|
|
|
while(connected == False):
|
|
time.sleep(1)
|
|
|
|
# for n in range(100000000):
|
|
# publish(client, f"/{monitor[0]}", "pin|7856")
|
|
# publish(client, f"/{monitor[0]}", "s")
|
|
|
|
time.sleep(60)
|
|
|
|
sorted_dict = dict(sorted(hist.items(), key=lambda x:x[1]))
|
|
|
|
for key in sorted_dict:
|
|
print(f"{key} : {sorted_dict[key]}")
|
|
|
|
client.disconnect()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|