MqttMonitor/MqttClient.py

branch
connection_profiles
changeset 24
b4e18aadc311
parent 22
545979c7dcd4
child 30
17ef10819773
equal deleted inserted replaced
23:0b23bd856e43 24:b4e18aadc311
158 @type bool 158 @type bool
159 """ 159 """
160 self.__mqttClient.will_set(topic, payload=payload, qos=qos, 160 self.__mqttClient.will_set(topic, payload=payload, qos=qos,
161 retain=retain) 161 retain=retain)
162 162
163 def setTLS(self, caCerts=None, certFile=None, keyFile=None):
164 """
165 Public method to enable secure connections and set the TLS parameters.
166
167 @param caCerts path to the Certificate Authority certificates file
168 @type str
169 @param certFile PEM encoded client certificate file
170 @type str
171 @param keyFile PEM encoded private key file
172 @type str
173 """
174 self.__mqttClient.tls_set(ca_certs=caCerts, certfile=certFile,
175 keyfile=keyFile)
176
163 def startLoop(self): 177 def startLoop(self):
164 """ 178 """
165 Public method to start the MQTT client loop. 179 Public method to start the MQTT client loop.
166 """ 180 """
167 self.__mqttClient.loop_start() 181 self.__mqttClient.loop_start()
210 this client to 224 this client to
211 @type str 225 @type str
212 @param options dictionary containing the connection options. This 226 @param options dictionary containing the connection options. This
213 dictionary should contain the keys "ClientId", "Keepalive", 227 dictionary should contain the keys "ClientId", "Keepalive",
214 "CleanSession", "Username", "Password", "WillTopic", "WillMessage", 228 "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
215 "WillQos", "WillRetain" 229 "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", "TlsClientCert",
230 "TlsClientKey"
216 @type dict 231 @type dict
217 """ 232 """
218 if options: 233 if options:
219 parametersDict = self.defaultConnectionOptions() 234 parametersDict = self.defaultConnectionOptions()
220 parametersDict.update(options) 235 parametersDict.update(options)
244 self.setLastWill(parametersDict["WillTopic"], 259 self.setLastWill(parametersDict["WillTopic"],
245 willMessage, 260 willMessage,
246 parametersDict["WillQos"], 261 parametersDict["WillQos"],
247 parametersDict["WillRetain"]) 262 parametersDict["WillRetain"])
248 263
249 # step 4: connect to server 264 # step 4: set TLS parameters
265 if parametersDict["TlsEnable"]:
266 if parametersDict["TlsCaCert"] and \
267 parametersDict["TlsClientCert"]:
268 # use self signed client certificate
269 self.setTLS(caCerts=parametersDict["TlsCaCert"],
270 certFile=parametersDict["TlsClientCert"],
271 keyFile=parametersDict["TlsClientKey"])
272 elif parametersDict["TlsCaCert"]:
273 # use CA certificate file
274 self.setTLS(caCerts=parametersDict["TlsCaCert"])
275 else:
276 # use default TLS configuration
277 self.setTLS()
278
279 # step 5: connect to server
250 self.connectToServer(host, port=port, 280 self.connectToServer(host, port=port,
251 keepalive=parametersDict["Keepalive"]) 281 keepalive=parametersDict["Keepalive"])
252 else: 282 else:
253 keepalive = self.defaultConnectionOptions["Keepalive"] 283 keepalive = self.defaultConnectionOptions["Keepalive"]
254 self.connectToServer(host, port=port, keepalive=keepalive, 284 self.connectToServer(host, port=port, keepalive=keepalive,
259 Public method to get a connection options dictionary with default 289 Public method to get a connection options dictionary with default
260 values. 290 values.
261 291
262 @return dictionary containing the default connection options. It has 292 @return dictionary containing the default connection options. It has
263 the keys "ClientId", "Keepalive", "CleanSession", "Username", 293 the keys "ClientId", "Keepalive", "CleanSession", "Username",
264 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain" 294 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain",
295 "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey".
265 @rtype dict 296 @rtype dict
266 """ 297 """
267 return { 298 return {
268 "ClientId": "ERIC6_MQTT_MONITOR_CLIENT", 299 "ClientId": "ERIC6_MQTT_MONITOR_CLIENT",
269 "Keepalive": 60, 300 "Keepalive": 60,
272 "Password": "", 303 "Password": "",
273 "WillTopic": "", 304 "WillTopic": "",
274 "WillMessage": "", 305 "WillMessage": "",
275 "WillQos": 0, 306 "WillQos": 0,
276 "WillRetain": False, 307 "WillRetain": False,
308 "TlsEnable": False,
309 "TlsCaCert": "",
310 "TlsClientCert": "",
311 "TlsClientKey": "",
277 } 312 }
278 313
279 def reconnectToServer(self): 314 def reconnectToServer(self):
280 """ 315 """
281 Public method to reconnect the client with the same parameters. 316 Public method to reconnect the client with the same parameters.

eric ide

mercurial