MqttMonitor/MqttClient.py

changeset 10
7e0e921dc7ea
parent 9
f75a385e9127
child 11
90d3ebed4cc0
equal deleted inserted replaced
9:f75a385e9127 10:7e0e921dc7ea
77 lambda client, userdata, mid, grantedQos: self.onSubscribe.emit( 77 lambda client, userdata, mid, grantedQos: self.onSubscribe.emit(
78 mid, grantedQos) 78 mid, grantedQos)
79 self.__mqttClient.on_unsubscribe = \ 79 self.__mqttClient.on_unsubscribe = \
80 lambda client, userdata, mid: self.onUnsubscribe.emit(mid) 80 lambda client, userdata, mid: self.onUnsubscribe.emit(mid)
81 81
82 def reinitialise(self, clientId="", cleanSession=True, userdata=None):
83 """
84 Public method to reinitialize the client with given data.
85
86 @param clientId ID to be used for the client
87 @type str
88 @param cleanSession flag indicating to start a clean session
89 @type bool
90 @param userdata user data
91 @type any
92 """
93 self.__mqttClient.reinitialise(
94 client_id=clientId, clean_session=cleanSession, userdata=userdata)
95
96 def setMaxInflightMessages(self, inflight=20):
97 """
98 Public method to set the maximum number of messages with QoS > 0 that
99 can be part way through their network flow at once.
100
101 @param inflight maximum number of messages in flight
102 @type int
103 """
104 self.__mqttClient.max_inflight_messages_set(inflight)
105
106 def setMaxQueuedMessages(self, queueSize=0):
107 """
108 Public method to set the maximum number of messages with QoS > 0 that
109 can be pending in the outgoing message queue.
110
111 @param queueSize maximum number of queued messages (0 = unlimited)
112 @type int
113 """
114 self.__mqttClient.max_queued_messages_set(queueSize)
115
116 def setUserCredentials(self, username, password=None):
117 """
118 Public method to set the user name and optionally the password.
119
120 @param username user name to be set
121 @type str
122 @param password optional password
123 @type str
124 """
125 self.__mqttClient.username_pw_set(username, password=password)
126
127 def setUserData(self, userdata):
128 """
129 Public method to set the user data.
130
131 @param userdata user data
132 @type any
133 """
134 self.__mqttClient.user_data_set(userdata)
135
136 def setLastWill(self, topic, payload=None, qos=0, retain=False):
137 """
138 Public method to set the last will of the client.
139
140 @param topic topic the will message should be published on
141 @type str
142 @param payload message to send as a will
143 @type str, bytes, int or float
144 @param qos quality of service level to use for the will
145 @type int, one of 0, 1 or 2
146 @param retain flag indicating to set as the "last known good"/retained
147 message for the will topic
148 @type bool
149 """
150 self.__mqttClient.will_set(topic, payload=payload, qos=qos,
151 retain=retain)
152
82 def startLoop(self): 153 def startLoop(self):
83 """ 154 """
84 Public method to start the MQTT client loop. 155 Public method to start the MQTT client loop.
85 """ 156 """
86 self.__mqttClient.loop_start() 157 self.__mqttClient.loop_start()
112 self.__mqttClient.connect_async( 183 self.__mqttClient.connect_async(
113 host, port=port, keepalive=keepalive, bind_address=bindAddress) 184 host, port=port, keepalive=keepalive, bind_address=bindAddress)
114 185
115 if not self.__loopStarted: 186 if not self.__loopStarted:
116 self.startLoop() 187 self.startLoop()
188
189 def connectToServerWithOptions(self, host, port=1883, bindAddress="",
190 options=None):
191 """
192 Public method to connect to a remote MQTT broker.
193
194 @param host host name or IP address of the remote broker
195 @type str
196 @param port network port of the server host to connect to (default:
197 1883, using TLS: 8883)
198 @type int
199 @param bindAddress IP address of a local network interface to bind
200 this client to
201 @type str
202 @param options dictionary containing the connection options. This
203 dictionary should contain the keys "ClientId", "Keepalive",
204 "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
205 "WillQos", "WillRetain"
206 @type dict
207 """
208 if options:
209 parametersDict = self.defaultConnectionOptions()
210 parametersDict.update(options)
211
212 # step 1: reinitialize to set the client ID and clean session flag
213 self.reinitialise(
214 clientId=parametersDict["ClientId"],
215 cleanSession=parametersDict["CleanSession"]
216 )
217
218 # step 2: set username and password
219 if parametersDict["Username"]:
220 if parametersDict["Password"]:
221 self.setUserCredentials(parametersDict["Username"],
222 parametersDict["Password"])
223 else:
224 self.setUserCredentials(parametersDict["Username"])
225
226 # step 3: set last will data
227 if parametersDict["WillTopic"]:
228 if parametersDict["WillMessage"]:
229 willMessage = parametersDict["WillMessage"]
230 else:
231 # empty message to clear the will
232 willMessage = None
233 self.setLastWill(parametersDict["WillTopic"],
234 willMessage,
235 parametersDict["WillQos"],
236 parametersDict["WillRetain"])
237
238 # step 4: connect to server
239 self.connectToServer(host, port=port,
240 keepalive=parametersDict["Keepalive"])
241 else:
242 keepalive = self.defaultConnectionOptions["Keepalive"]
243 self.connectToServer(host, port=port, keepalive=keepalive,
244 bindAddress=bindAddress)
245
246 def defaultConnectionOptions(self):
247 """
248 Public method to get a connection options dictionary with default
249 values.
250
251 @return dictionary containing the default connection options. It has
252 the keys "ClientId", "Keepalive", "CleanSession", "Username",
253 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain"
254 @rtype dict
255 """
256 return {
257 "ClientId": "ERIC6_MQTT_MONITOR_CLIENT",
258 "Keepalive": 60,
259 "CleanSession": True,
260 "Username": "",
261 "Password": "",
262 "WillTopic": "",
263 "WillMessage": "",
264 "WillQos": 0,
265 "WillRetain": False,
266 }
117 267
118 def reconnectToServer(self): 268 def reconnectToServer(self):
119 """ 269 """
120 Public method to reconnect the client with the same parameters. 270 Public method to reconnect the client with the same parameters.
121 """ 271 """

eric ide

mercurial