Я создал функцию, которая возвращает обратный вызов on_subscribe
для подписки на несколько тем. Однако это не сработало, и обратный вызов не обрабатывается. Я не подписывался на обратный вызов on_connect, это вообще необходимо? Это функция, созданная в классе
def subscriber(self):
def on_subscribe(client, userdata, mid, granted_qos):
client.subscribe(self._topic_sub) #topic_sub is an attribute of the class and the topic to
#subscribe to
return on_subscribe
def connection(self):
def on_connect(client, userdata, flags, rc):
print("Connection to the broker. Result : "+str(rc))
return on_connect
Здесь я вызываю обратный вызов , и это часть кода
from class_light import Light
from test_class import Mqtt
import paho.mqtt.client as mqtt
import sys
import threading
import time
#from untitled3 import Location
from MongoDB import is_circadian_Light
Broker = "broker.mqttdashboard.com"
username = "growthtechnology"
password = "growthtechnology"
PortaBroker = 1883
KeepAliveBroker = 60
client_name = "Local"
topic_sub = "testtopic/1"
topic_pub = "testtopic/3"
light = Mqtt(topic_sub)
occupancy_sensor =Mqtt([("testtopic/4", 0), ("testtopic/5", 0)])
tunable_light = Light("deviceID")
try:
print("[STATUS] Inicializando MQTT...")
#inicializing MQTT:
client = mqtt.Client()
client.username_pw_set(username, password)
client.on_connect = light.connection()
client.on_message = light.message()
client.on_publish = light.publisher()
client.on_disconnect = light.disconnection()
client.on_subscribe = light.subscriber()
client.on_subscribe = occupancy_sensor.subscriber()
client.connect(Broker, PortaBroker, KeepAliveBroker)
def publishing():
while True:
msg = light.messages # calling for the variable "messages" to get PUBLISH messages
#print(msg)
#time.sleep(3)
topic = light.topic
for i, topics in enumerate(topic):
if topics == "testtopic/4":
if msg[i] == "1":
if is_circadian_Light("device2") == 1 :
client.publish("testtopic/Bedroom",tunable_light.circadianLight())
time.sleep(10)