在类中使用on_log回调查找MQTT PUBACK失败,但是它在平面脚本中有效

时间:2018-11-22 05:39:40

标签: python python-2.7 callback mqtt paho

第一个脚本有效,这意味着将调用回调,并结束打印puback: True

使用类A进行工作的第二个脚本不起作用。回调不会被调用,它以a.puback: False

结尾

我不确定我的问题是否是回调无法通过这种方式工作,在这种情况下,如何使我的类与这些Paho MQTT回调一起使用?还是有些微妙。

作品:

def on_log_puback(client, userdata, level, buf):
    global puback
    if 'PUBACK' in buf:
        puback = True
        print "PUBACK!"

def on_connect(client, userdata, flags, rc):
    print "Connect code: ", rc

def on_disconnect(client, userdata, flags, rc=0):
    print "Disconnect code: ", rc

def on_message(client, userdata, msg):
    print "    Message: ", str(msg.payload.decode("utf-8", "ignore"))

def stop():
    print ("stopping loop")
    client.loop_stop()
    print "disconnecting"
    client.disconnect()

import paho.mqtt.client as mqtt 
import time

client     = mqtt.Client("Luke, I am your client") 
mqtt.Client.connected_flag = False 

client.on_connect     = on_connect  
client.on_disconnect  = on_disconnect
client.on_log         = on_log_puback 
client.on_message     = on_message

status = client.connect(host="test.mosquitto.org",
                        keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)

print "loop_start"
client.loop_start()
time.sleep(1)

sret = client.subscribe("test_topic")
print "subscribe returns sret: ", sret
time.sleep(2)

# initialize global
puback = False

# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
                                payload="hello!",
                                qos=1, retain=True)   
print "publish status: ", status
time.sleep(2)

print "puback: ", puback

stop()

不工作:

import paho.mqtt.client as mqtt 
import time

class A(object):
    def __init__(self):

        client                = mqtt.Client("Luke, I am your client") 
        self.client           = client
        mqtt.Client.connected_flag = False

        client.on_connect     = self.on_connect  
        client.on_disconnect  = self.on_disconnect
        client.on_log         = self.on_log_puback 
        client.on_message     = self.on_message

        status = client.connect(host="test.mosquitto.org",
                                keepalive=60, port=1883)
        print "connect status: ", status
        time.sleep(2)

        print "loop_start"
        client.loop_start()
        time.sleep(1)

        sret = client.subscribe("test_topic")
        print "subscribe returns: ", sret
        time.sleep(2)

        # initialize global
        puback = False

        # test publish with qos=1
        status, msg_id = client.publish(topic="test_topic",
                                        payload="hello!",
                                        qos=1, retain=True)   
        print "publish status: ", status
        time.sleep(2)

        self.puback = puback

    def on_log_puback(client, userdata, level, buf):
        global puback
        if 'PUBACK' in buf:
            puback = True
            print "PUBACK!"

    def on_connect(client, userdata, flags, rc):
        print "Connect code: ", rc

    def on_disconnect(client, userdata, flags, rc=0):
        print "Disconnect code: ", rc

    def on_message(client, userdata, msg):
        print "    Message: ", str(msg.payload.decode("utf-8", "ignore"))

    def stop(self):
        print ("stopping loop")
        self.client.loop_stop()
        print "disconnecting"
        self.client.disconnect()

a = A()

time.sleep(2)
print 'a.puback: ', a.puback
a.stop()

1 个答案:

答案 0 :(得分:0)

找到了。当我将回调移动到类中时,例如on_log_puback(self,...)我只是忘记将self添加到参数的开头。这样,它现在可以很好地工作了。