Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b21f2c

Browse files
committed
yo
1 parent d7b6c02 commit 8b21f2c

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/main/python/programmingtheiot/cda/connection/MqttClientConnector.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,87 @@ def unsubscribeFromTopic(self, resource: ResourceNameEnum = None):
8585

8686
def setDataMessageListener(self, listener: IDataMessageListener = None) -> bool:
8787
pass
88+
89+
def __init__(self, clientID: str = None):
90+
self.config = ConfigUtil()
91+
self.dataMsgListener = None
92+
93+
self.host = \
94+
self.config.getProperty( \
95+
ConfigConst.MQTT_GATEWAY_SERVICE, ConfigConst.HOST_KEY, ConfigConst.DEFAULT_HOST)
96+
97+
self.port = \
98+
self.config.getInteger( \
99+
ConfigConst.MQTT_GATEWAY_SERVICE, ConfigConst.PORT_KEY, ConfigConst.DEFAULT_MQTT_PORT)
100+
101+
self.keepAlive = \
102+
self.config.getInteger( \
103+
ConfigConst.MQTT_GATEWAY_SERVICE, ConfigConst.KEEP_ALIVE_KEY, ConfigConst.DEFAULT_KEEP_ALIVE)
104+
105+
self.defaultQos = \
106+
self.config.getInteger( \
107+
ConfigConst.MQTT_GATEWAY_SERVICE, ConfigConst.DEFAULT_QOS_KEY, ConfigConst.DEFAULT_QOS)
108+
109+
self.mqttClient = None
110+
111+
# IMPORTANT:
112+
#
113+
# You can choose to set clientID in a number of ways:
114+
# 1 - use the locationID value in PiotConfig.props as the clientID (see below)
115+
# 2 - pass a custom clientID into constructor (from DeviceDataManager or your test)
116+
# 3 - hard code a clientID in this constructor (generally not recommended)
117+
# 4 - if using Python Paho, set NO client ID and let broker auto-assign
118+
# a random value (not recommended if setting clean session flag to False)
119+
120+
# TODO: the following is just a sample; use your own unique ID
121+
if not clientID:
122+
self.clientID = \
123+
self.config.getProperty( \
124+
ConfigConst.CONSTRAINED_DEVICE, ConfigConst.DEVICE_LOCATION_ID_KEY)
125+
126+
# TODO: be sure to validate the clientID!
127+
128+
logging.info('\tMQTT Client ID: ' + self.clientID)
129+
logging.info('\tMQTT Broker Host: ' + self.host)
130+
logging.info('\tMQTT Broker Port: ' + str(self.port))
131+
logging.info('\tMQTT Keep Alive: ' + str(self.keepAlive))
132+
133+
def connectClient(self) -> bool:
134+
if not self.mqttClient:
135+
# TODO: make clean_session configurable
136+
self.mqttClient = mqttClient.Client(client_id = self.clientID, clean_session = True)
137+
138+
self.mqttClient.on_connect = self.onConnect
139+
self.mqttClient.on_disconnect = self.onDisconnect
140+
self.mqttClient.on_message = self.onMessage
141+
self.mqttClient.on_publish = self.onPublish
142+
self.mqttClient.on_subscribe = self.onSubscribe
143+
144+
if not self.mqttClient.is_connected():
145+
logging.info('MQTT client connecting to broker at host: ' + self.host)
146+
self.mqttClient.connect(self.host, self.port, self.keepAlive)
147+
self.mqttClient.loop_start()
148+
149+
return True
150+
else:
151+
logging.warning('MQTT client is already connected. Ignoring connect request.')
152+
153+
return False
154+
155+
def disconnectClient(self) -> bool:
156+
if self.mqttClient.is_connected():
157+
logging.info('Disconnecting MQTT client from broker: ' + self.host)
158+
self.mqttClient.loop_stop()
159+
self.mqttClient.disconnect()
160+
161+
return True
162+
else:
163+
logging.warning('MQTT client already disconnected. Ignoring.')
164+
165+
return False
166+
167+
def setDataMessageListener(self, listener: IDataMessageListener = None):
168+
if listener:
169+
self.dataMsgListener = listener
170+
171+

src/test/python/programmingtheiot/part03/integration/connection/MqttClientConnectorTest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def setUp(self):
4545
def tearDown(self):
4646
pass
4747

48-
@unittest.skip("Ignore for now.")
48+
#@unittest.skip("Ignore for now.")
4949
def testConnectAndDisconnect(self):
5050
delay = self.cfg.getInteger(ConfigConst.MQTT_GATEWAY_SERVICE, ConfigConst.KEEP_ALIVE_KEY, ConfigConst.DEFAULT_KEEP_ALIVE)
5151

0 commit comments

Comments
 (0)