test2.py
Go to the documentation of this file.
1 import paho_mqtt3a as mqttv3, time, random
2 import contextlib
3 
4 print dir(mqttv3)
5 
6 hostname = "localhost"
7 clientid = "myclientid"
8 topic = "test2_topic"
9 
10 def deliveryComplete(context, msgid):
11  print "deliveryComplete", msgid
12 
13 def connectionLost(context, cause):
14  print "connectionLost"
15  print "rc from reconnect is", mqttv3.connect(self.client)
16 
17 def messageArrived(context, topicName, message):
18  print "messageArrived", message
19  #print "clientid", context
20  #print "topicName", topicName
21  return 1
22 
23 def onSuccess(context, successData):
24  print "onSuccess for", context["clientid"], context["state"], successData
25  responseOptions = {"context": context, "onSuccess": onSuccess, "onFailure" : onFailure}
26  #responseOptions = {"context": context}
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"
42 
43 def onFailure(context, failureData):
44  print "onFailure for", context["clientid"]
45  context["state"] = "finished"
46 
47 noclients = 1
48 myclientid = None
49 clients = []
50 for i in range(noclients):
51  myclientid = clientid+str(i)
52  rc, client = mqttv3.create("tcp://"+hostname+":1883", myclientid)
53  #print "client is", hex(client)
54  print "rc from create is", rc
55  print "rc from setcallbacks is", mqttv3.setcallbacks(client, client, connectionLost, messageArrived, deliveryComplete)
56 
57  context = {"client" : client, "clientid" : clientid, "state" : "connecting"}
58 
59  print "rc from connect is", mqttv3.connect(client, {"context": context, "onSuccess": onSuccess, "onFailure": onFailure})
60 
61  clients.append(context)
62 
63 while [x for x in clients if x["state"] != "finished"]:
64  print [x for x in clients if x["state"] != "finished"]
65  time.sleep(1)
66 
67 for client in clients:
68  if mqttv3.isConnected(client["client"]):
69  print "rc from disconnect is", mqttv3.disconnect(client["client"], 1000)
70  time.sleep(1)
71  mqttv3.destroy(client["client"])
72  print "after destroy"
73 
def messageArrived(context, topicName, message)
Definition: test2.py:17
def onSuccess(context, successData)
Definition: test2.py:23
def connectionLost(context, cause)
Definition: test2.py:13
def onFailure(context, failureData)
Definition: test2.py:43
def deliveryComplete(context, msgid)
Definition: test2.py:10


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 04:02:48