From b8d7b6c5302cf33b66d8b13eaa03360cfc5372b8 Mon Sep 17 00:00:00 2001 From: Ronald Steinke Date: Wed, 10 Jan 2018 10:20:28 +0100 Subject: [PATCH] fixes MQTT client --- .../src/openmtc_onem2m/client/mqtt.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/common/openmtc-onem2m/src/openmtc_onem2m/client/mqtt.py b/common/openmtc-onem2m/src/openmtc_onem2m/client/mqtt.py index fa875b8..9ec8787 100644 --- a/common/openmtc-onem2m/src/openmtc_onem2m/client/mqtt.py +++ b/common/openmtc-onem2m/src/openmtc_onem2m/client/mqtt.py @@ -6,7 +6,7 @@ from futile.caching import LRUCache import gevent from gevent import monkey; monkey.patch_all() from . import OneM2MClient -from openmtc.exc import ConnectionFailed, OpenMTCNetworkError +from openmtc.exc import ConnectionFailed from ..exc import ( ERROR_MIN, CSEValueError, @@ -131,7 +131,8 @@ class OneM2MMQTTClient(OneM2MClient): return wrapper return decorator - def __init__(self, m2m_ep, _, client_id, handle_request_func=None, subscribe_sys_topics=False): + def __init__(self, m2m_ep, _, client_id, handle_request_func=None, subscribe_sys_topics=False, ca_certs=None, + cert_file=None, key_file=None, insecure=False): """ :param str m2m_ep: :param bool _: @@ -348,6 +349,9 @@ class OneM2MMQTTClient(OneM2MClient): if parsed_url.username: self._client.username_pw_set(parsed_url.username, parsed_url.password) + self._client.tls_set(ca_certs=ca_certs, certfile=cert_file, keyfile=key_file) + self._client.tls_insecure_set(insecure) + try: self._client.connect( parsed_url.hostname, @@ -423,9 +427,11 @@ class OneM2MMQTTClient(OneM2MClient): return p def stop(self): - self._client.disconnect() - # TODO(sho): this is abominable. But for the time being, there seems to be no elegant solution to this. - self._client._clean_session = True - # TS 0010, sec. 6.3 mandates a reconnect in order to leave a clean state with the MQTT broker - self._client.reconnect() - self._client.disconnect() + if self._client: + self._client.disconnect() + # TODO(sho): this is abominable. But for the time being, there seems to be no elegant solution to this. + self._client._clean_session = True + # TS 0010, sec. 6.3 mandates a reconnect in order to leave a clean state with the MQTT broker + self._client.reconnect() + self._client.disconnect() + self._client = None