1 import paho_mqtt3a
as mqttv3, time, random
7 clientid =
"myclientid" 11 print "deliveryComplete", msgid
14 print "connectionLost" 15 print "rc from reconnect is", mqttv3.connect(self.client)
18 print "messageArrived", message
24 print "onSuccess for", context[
"clientid"], context[
"state"], successData
25 responseOptions = {
"context": context,
"onSuccess": onSuccess,
"onFailure" : onFailure}
27 if context[
"state"] ==
"connecting":
28 context[
"state"] =
"subscribing" 29 print "rc from subscribe is", mqttv3.subscribe(client, topic, 2, responseOptions)
30 elif context[
"state"] ==
"subscribing":
31 context[
"state"] =
"publishing qos 0" 32 print "rc from publish is", mqttv3.send(client, topic,
"a QoS 0 message", 0, 0, responseOptions)
33 elif context[
"state"] ==
"publishing qos 0":
34 context[
"state"] =
"publishing qos 1" 35 print "rc from publish is", mqttv3.send(client, topic,
"a QoS 1 message", 1, 0, responseOptions)
36 elif context[
"state"] ==
"publishing qos 1":
37 context[
"state"] =
"publishing qos 2" 38 print "rc from publish is", mqttv3.send(client, topic,
"a QoS 2 message", 2, 0, responseOptions)
39 elif context[
"state"] ==
"publishing qos 2":
40 context[
"state"] =
"finished" 41 print "leaving onSuccess" 44 print "onFailure for", context[
"clientid"]
45 context[
"state"] =
"finished" 50 for i
in range(noclients):
51 myclientid = clientid+str(i)
52 rc, client = mqttv3.create(
"tcp://"+hostname+
":1883", myclientid)
54 print "rc from create is", rc
55 print "rc from setcallbacks is", mqttv3.setcallbacks(client, client, connectionLost, messageArrived, deliveryComplete)
57 context = {
"client" : client,
"clientid" : clientid,
"state" :
"connecting"}
59 print "rc from connect is", mqttv3.connect(client, {
"context": context,
"onSuccess": onSuccess,
"onFailure": onFailure})
61 clients.append(context)
63 while [x
for x
in clients
if x[
"state"] !=
"finished"]:
64 print [x
for x
in clients
if x[
"state"] !=
"finished"]
67 for client
in clients:
68 if mqttv3.isConnected(client[
"client"]):
69 print "rc from disconnect is", mqttv3.disconnect(client[
"client"], 1000)
71 mqttv3.destroy(client[
"client"])
def messageArrived(context, topicName, message)
def onSuccess(context, successData)
def connectionLost(context, cause)
def onFailure(context, failureData)
def deliveryComplete(context, msgid)