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

Skip to content

Commit c37319e

Browse files
committed
Add support for MQTT LWT
1 parent f495e52 commit c37319e

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ If you don't specify a client_id, the SDK will use a random hex string.
331331
> Definition: `System.Messaging(user, port=1883, keepalive=30, url="", client_id="")`
332332
> Returns: MQTT messaging object.
333333
334-
There are a slew of callback functions you may assign.
334+
There are a number of callback functions you may assign.
335335
Typically, you want to set these callbacks before you connect to the broker.
336336
This is a list of the function names and their expected parameters.
337337
For more information about the individual callbacks, see the [paho-mqtt](https://github.com/eclipse/paho.mqtt.python#callbacks) documentation.
@@ -343,6 +343,7 @@ For more information about the individual callbacks, see the [paho-mqtt](https:/
343343
- `on_message(client, userdata, mid)`
344344
- `on_log(client, userdata, level, buf)`
345345

346+
#### Connecting and Disconnecting
346347
Before publishing or subscribing, you must connect your client to the broker.
347348
After you're finished, it's good practice to disconnect from the broker before quitting your program.
348349
These are both simple functions that take no parameters.
@@ -352,13 +353,31 @@ These are both simple functions that take no parameters.
352353
> Definition: `Messaging.disconnect()`
353354
> Returns: Nothing.
354355
356+
#### Last Will and Testament (LWT)
357+
MWTT brokers support the concept of a last will and testament. The last will and testament is a set of parameters that allow the MQTT broker
358+
publish a specified message to a specific topic in the event of an abnormal disconnection. Setting the last will and testament can be accomplished
359+
by invoking the `set_will` function. The last will and testament can also be removed from a MQTT client by invoking `clear_will`.
360+
361+
**Note: set_will() and clear_will() must be invoked prior to invoking Messaging.connect()**
362+
363+
- `set_will(topic, payload, qos, retain)`
364+
- `clear_will()`
365+
366+
> Definition: `Messaging.set_will()`
367+
> Returns: Nothing.
368+
> Definition: `Messaging.clear_will()`
369+
> Returns: Nothing.
370+
371+
372+
#### Subscribing to topics
355373
You can subscribe to as many topics as you like and unsubscribe from them using the following two commands.
356374

357375
> Definition: `Messaging.subscribe(topic)`
358376
> Returns: Nothing.
359377
> Definition: `Messaging.unsubscribe(topic)`
360378
> Returns: Nothing.
361379
380+
#### Publishing to topics
362381
Publishing takes the topic to publish to and the message to publish as arguments. The type of message can be string or bytes.
363382

364383
> Definition: `Messaging.publish(topic, message)`

clearblade/Messaging.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,31 @@ def __log_cb(self, client, userdata, level, buf):
113113
if self.on_log:
114114
self.on_log(client, userdata, level, buf)
115115

116-
def connect(self):
116+
def set_will(self, topic, payload, qos = 0, retain = False):
117+
"""
118+
Set a Will to be sent by the broker in case the client disconnects unexpectedly.
119+
This must be called before connect() to have any effect.
120+
121+
:param str topic: The topic that the will message should be published on.
122+
:param payload: The message to send as a will. If not given, or set to None a
123+
zero length message will be used as the will. Passing an int or float
124+
will result in the payload being converted to a string representing
125+
that number. If you wish to send a true int/float, use struct.pack() to
126+
create the payload you require.
127+
:param int qos: The quality of service level to use for the will.
128+
:param bool retain: If set to true, the will message will be set as the retained message for the topic.
129+
"""
130+
131+
self.__mqttc.will_set(topic, payload, qos, retain)
132+
133+
def clear_will(self):
134+
"""
135+
Removes a will that was previously configured with `set_will()`.
136+
Must be called before connect() to have any effect.
137+
"""
138+
self.__mqttc.will_clear()
139+
140+
def connect(self, will_topic=None, will_payload=1883):
117141
cbLogs.info("Connecting to MQTT.")
118142
if self.__use_tls:
119143
self.__mqttc.tls_set()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
name='clearblade',
55
packages=['clearblade'],
66
install_requires=['requests', 'paho-mqtt>=1.3.0'],
7-
version='2.4.4',
7+
version='2.4.5',
88
description='A Python SDK for interacting with the ClearBlade Platform.',
99
url='https://github.com/ClearBlade/ClearBlade-Python-SDK',
1010
download_url='https://github.com/ClearBlade/ClearBlade-Python-SDK/archive/v2.4.4.tar.gz',

0 commit comments

Comments
 (0)