mqtt wrapper: add tls handling
Signed-off-by: Thomas Klaehn <thomas.klaehn@u-blox.com>
This commit is contained in:
parent
31b6294088
commit
2ce37d3a00
@ -10,7 +10,7 @@ import paho.mqtt.client as mqtt_client
|
||||
|
||||
class Mqtt(object):
|
||||
''' Wrapper class for mqtt communication '''
|
||||
def __init__(self, hostname, port=1883, keepalive=60, qos=2, retain=True, subscribe=[]):
|
||||
def __init__(self, hostname, port=1883, ca_certs=None, certfile=None, keyfile=None, keepalive=60, qos=2, retain=True, subscribe=[]):
|
||||
''' Constructor
|
||||
@param hostname: The hostname of the mqtt broker
|
||||
@param port: The port number used for mqtt transport
|
||||
@ -21,6 +21,9 @@ class Mqtt(object):
|
||||
'''
|
||||
self.hostname = hostname
|
||||
self.port = port
|
||||
self.ca_certs = ca_certs
|
||||
self.certfile = certfile
|
||||
self.keyfile = keyfile
|
||||
self.keepalive = keepalive
|
||||
self.client = mqtt_client.Client()
|
||||
self.client.on_connect = self.__on_connect
|
||||
@ -34,17 +37,20 @@ class Mqtt(object):
|
||||
|
||||
def connect(self):
|
||||
''' Connect to mqtt broker '''
|
||||
ret = False
|
||||
if not self.is_connected:
|
||||
res = self.client.connect(self.hostname, self.port, \
|
||||
self.keepalive)
|
||||
if res != mqtt_client.MQTT_ERR_SUCCESS:
|
||||
return False
|
||||
res = None
|
||||
try:
|
||||
self.client.tls_set(self.ca_certs, self.certfile, self.keyfile)
|
||||
res = self.client.connect(self.hostname, self.port, self.keepalive)
|
||||
if res == mqtt_client.MQTT_ERR_SUCCESS:
|
||||
res = self.client.loop_start()
|
||||
if res == mqtt_client.MQTT_ERR_INVAL:
|
||||
return False
|
||||
if res != mqtt_client.MQTT_ERR_INVAL:
|
||||
ret = True
|
||||
self.is_connected = True
|
||||
return True
|
||||
return False
|
||||
except ValueError:
|
||||
pass
|
||||
return ret
|
||||
|
||||
def disconnect(self):
|
||||
''' Disconnect from mqtt broker '''
|
||||
|
2
setup.py
2
setup.py
@ -9,5 +9,5 @@ type 'python setup.py install' to install the distribution.
|
||||
'''
|
||||
from distutils.core import setup
|
||||
|
||||
setup(name='mqtt', version='1.0.0', author='Thomas Klaehn',
|
||||
setup(name='mqtt', version='1.1.0', author='Thomas Klaehn',
|
||||
author_email='tkl@blackfinn.de', packages=['mqtt'])
|
||||
|
Loading…
Reference in New Issue
Block a user