-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub.py
More file actions
59 lines (46 loc) · 1.86 KB
/
sub.py
File metadata and controls
59 lines (46 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import threading
from threading import Thread
import paho.mqtt.client as mqtt
import sys
import threadOnMessage
import re
import json
import threadOnMessageFromPubAfterCheck
#Broker connection parameters
broker_url = "test.mosquitto.org"
broker_port = 1883
#Global variables
threadsOperatesOnMessage = []
#Action to execute when arrive a new message from publisher
def on_connect(client, userdata, flags, rc):
if(rc == 0):
print("Connected succesfully")
else:
print("Bad connection, returned code: " + str(rc))
def on_message(client, userdata, message):
global threadsOperatesOnMessage
if(re.match("^networkInformationAbout\/([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\/from$", message.topic) != None):
#Translate the JSON message to a python object
messageIN=json.loads(message.payload.decode())
#Call a thread that operates
threadsOperatesOnMessage.append(threadOnMessage.ThreadOnMessage(messageIN))
lastThread = len(threadsOperatesOnMessage) - 1
#Start and join threads
threadsOperatesOnMessage[lastThread].start()
threadsOperatesOnMessage[lastThread].join()
if(re.match("^checkFromWebServer\/([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\/to$", message.topic) != None):
#Call a thread that operates
threadOperatesOnMessage = threadOnMessageFromPubAfterCheck.ThreadOnMessageFromPubAfterCheck(message.payload.decode())
#Start and join threads
threadOperatesOnMessage.start()
threadOperatesOnMessage.join()
client = mqtt.Client("subscriberClient", True)
client.connect(broker_url, broker_port)
client.on_connect = on_connect
client.on_message = on_message
client.subscribe([("checkFromWebServer/+/to", 1), ("networkInformationAbout/+/from", 1)])
try:
client.loop_forever()
except(KeyboardInterrupt, SystemExit):
raise