test_offline.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", cause
15  client = context
16  responseOptions = {"context": context, "onSuccess": onSuccess, "onFailure" : onFailure}
17  print "rc from publish while disconnected is", mqttv3.send(client, topic, "message while disconnected", 2, 0, responseOptions)
18 
19 def messageArrived(context, topicName, message):
20  print "messageArrived", message
21  #print "clientid", context
22  #print "topicName", topicName
23  return 1
24 
25 def connected(context, cause):
26  print "connected", cause
27 
28 def onSuccess(context, successData):
29  print "onSuccess for", context["clientid"], context["state"], successData
30  responseOptions = {"context": context, "onSuccess": onSuccess, "onFailure" : onFailure}
31  if context["state"] == "connecting":
32  context["state"] = "subscribing"
33  print "rc from subscribe is", mqttv3.subscribe(client, topic, 2, responseOptions)
34  elif context["state"] == "subscribing":
35  context["state"] = "publishing qos 0"
36  print "rc from publish is", mqttv3.send(client, topic, "a QoS 0 message", 0, 0, responseOptions)
37  elif context["state"] == "publishing qos 0":
38  context["state"] = "publishing qos 1"
39  print "rc from publish is", mqttv3.send(client, topic, "a QoS 1 message", 1, 0, responseOptions)
40  elif context["state"] == "publishing qos 1":
41  context["state"] = "publishing qos 2"
42  print "rc from publish is", mqttv3.send(client, topic, "a QoS 2 message", 2, 0, responseOptions)
43  elif context["state"] == "publishing qos 2":
44  context["state"] = "finished"
45  print "leaving onSuccess"
46 
47 def onFailure(context, failureData):
48  print "onFailure for", context["clientid"]
49  context["state"] = "finished"
50 
51 noclients = 1
52 myclientid = None
53 clients = []
54 for i in range(noclients):
55  myclientid = clientid+str(i)
56  rc, client = mqttv3.create("tcp://"+hostname+":1883", myclientid, mqttv3.PERSISTENCE_DEFAULT, {"sendWhileDisconnected" : 1})
57  #print "client is", hex(client)
58  print "rc from create is", rc
59  print "rc from setcallbacks is", mqttv3.setcallbacks(client, client, connectionLost, messageArrived, deliveryComplete)
60 
61  print "rc from setconnected is", mqttv3.setconnected(client, client, connected)
62 
63 
64  context = {"client" : client, "clientid" : clientid, "state" : "connecting"}
65 
66  print "rc from connect is", mqttv3.connect(client, {"cleansession" : 0, "automaticReconnect": 1, "context": context, "onSuccess": onSuccess, "onFailure": onFailure})
67 
68  clients.append(context)
69 
70 while [x for x in clients if x["state"] != "finished"]:
71  print [x for x in clients if x["state"] != "finished"]
72  time.sleep(1)
73 
74 print "waiting for 60 seconds"
75 time.sleep(60)
76 
77 for client in clients:
78  if mqttv3.isConnected(client["client"]):
79  print "rc from disconnect is", mqttv3.disconnect(client["client"], 1000)
80  time.sleep(1)
81  mqttv3.destroy(client["client"])
82  print "after destroy"
83 
def onSuccess(context, successData)
Definition: test_offline.py:28
def connected(context, cause)
Definition: test_offline.py:25
def onFailure(context, failureData)
Definition: test_offline.py:47
def deliveryComplete(context, msgid)
Definition: test_offline.py:10
def messageArrived(context, topicName, message)
Definition: test_offline.py:19
def connectionLost(context, cause)
Definition: test_offline.py:13


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