My purpose is to achieve MQTT message publish upon postsave signal sent my DJango models. I have registered on_connect, on disconnect, on_publish callbacks to check when the relevant process is executed. However, none of callbacks I registered works. Here is my post_save signal code. Can someone please give me a hint where I do mistake?
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("connecting")
print("client:" str(client))
print("userdata:" str(userdata))
print("flags:" str(flags))
print("result:" str(rc))
global flag_connected
flag_connected=1
def on_publish(client, userdata, mid):
print("message published")
def on_connect_fail(client, userdata, flags, rc):
print("failed to connect")
print("client:" str(client))
print("userdata:" str(userdata))
print("flags:" str(flags))
print("result:" str(rc))
global flag_connected
flag_connected=0
def on_disconnect(client, userdata, rc):
global flag_connected
flag_connected = 0
def notify_windowsApp(sender,instance, **kwargs):
new_truck_registered={
"identity":str(instance.truck_identity),
"doorName":str(instance.registered_door.door_name),
"doorNumber":str(instance.registered_door.door_number),
"entryTag":str(instance.entry_tag),
#"depatureTag":str(instance.departure_tag),
"entrytime":instance.entry_time.strftime('%Y-%m-%d::%H-%M'),
}
sendToClient(new_truck_registered)
def sendToClient(payloadTobeSent):
client=mqtt.Client(client_id="django_post_save_signal",
clean_session=True, )
mqttBroker="broker.hivemq.com"
client.on_connect=on_connect
client.on_publish=on_publish
client.on_connect_fail=on_connect_fail
client.on_disconnect=on_disconnect
client.connect(mqttBroker)
client.publish('baydoor/truckentrance',payload=str(payloadTobeSent),qos=1, retain=True)
CodePudding user response:
First, there is no on_connect_fail
callback in the docs, where did you get that from?
And more importantly, you have not started the client network loop so none of the callbacks will ever get called.