-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
39 lines (33 loc) · 1.03 KB
/
receiver.py
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
import paho.mqtt.client as mqtt
import json
class Receiver:
def on_connect(self, client, userdata, flags, rc):
print("Connected with result code ", str(rc))
client.subscribe("leapLescoParse")
self.connected[0] = 1
def on_disconnect(self, client, userdata, rc):
print("Desconectado")
self.connected[0] = 0
def on_message(self, client, userdata, msg):
data = msg.payload.decode("utf-8")
print("Data: " + data)
if str(json.loads(data)["id"]) == "98":
self.opq.put(98)
else:
self.queue.put(str(json.loads(data)["id"]))
self.receivingList.append(str(json.loads(data)["id"]))
def __init__(self, queue, receivingList, connected, opq):
self.queue = queue
self.receivingList = receivingList
self.connected = connected
self.opq = opq
client = mqtt.Client()
client.on_connect = self.on_connect
client.on_message = self.on_message
client.on_disconnect = self.on_disconnect
try:
client.connect("iot.eclipse.org", 1883, 60)
client.loop_start()
except:
print("No se puede conectar")
self.connected[0] = 0