Skip to content

Commit ef43837

Browse files
Version 1.0.0 (#2)
## Release 1.0.0 ### Improvements - Reconnection timer of 5 seconds in case the connection is lost (broker is rebooted) - Showing in the message log if connection/reconnection failed - Changed a couple of misleading labels for MQTTS in UI ### Bugfix - Bug of multiple function calls by the same event when topic of the event is changed. Solved by deregistering from event if the topic of the event is changed - 'passwords' changed to 'password' as the parameter is called mqttClient_Model.parameters.password --------- Co-authored-by: Roy Gollub <roy.gollub@sick.de>
1 parent 552f51a commit ef43837

7 files changed

Lines changed: 74 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## Release 1.0.0
5+
6+
### Improvements
7+
- Reconnection timer of 5 seconds in case the connection is lost (broker is rebooted)
8+
- Showing in the message log if connection/reconnection failed
9+
- Changed a couple of misleading labels for MQTTS in UI
10+
11+
### Bugfix
12+
- Bug of multiple function calls by the same event when topic of the event is changed. Solved by deregistering from event if the topic of the event is changed
13+
- 'passwords' changed to 'password' as the parameter is called mqttClient_Model.parameters.password
14+
415
## Release 0.4.1
516

617
### Improvements

CSK_Module_MQTTClient/pages/pages/CSK_Module_MQTTClient/CSK_Module_MQTTClient.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
<layout-row id="RowLayout8"
5959
style="justify-content: space-between; align-items: center">
6060
<layout-column id="ColumnLayout14" style="align-items: stretch">
61-
<davinci-value-display id="VD_BrokerIP" value="Broker IP:">
61+
<davinci-value-display id="VD_BrokerURL" value="Broker URL:">
6262
</davinci-value-display>
6363
</layout-column>
6464
<layout-column id="ColumnLayout18" style="align-items: stretch">
65-
<davinci-text-field id="TF_BrokerIP" type="text">
65+
<davinci-text-field id="TF_BrokerURL" type="text">
6666
<crown-edpws-binding property="value"
6767
name="CSK_MQTTClient/OnNewBrokerIP" update-on-resume>
6868
</crown-edpws-binding>
@@ -374,7 +374,7 @@
374374
<layout-row id="RowLayout40" style="justify-content: space-between">
375375
<layout-column id="ColumnLayout54" style="align-items: stretch">
376376
<davinci-value-display id="VD_CABundle"
377-
value="Use Client Certificate:">
377+
value="Use server certificate authority (CA) bundle:">
378378
</davinci-value-display>
379379
</layout-column>
380380
<layout-column id="ColumnLayout55" style="align-items: stretch">

CSK_Module_MQTTClient/project.mf.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ It is also possible to get the MQTTClient handle via "getMQTTHandle" to use this
2323
**4) Publish** +
2424
It is possible to publish MQTT messages via "publish" (via script) or "publishViaUI (via UI) to use preset values (check "presetPublish..."-functions) +
2525
Additionally it is possible to configure the module to listen / wait for specific events of other modules/apps and to forward their content to predefined topics with predefined QoS/Retain. +
26-
This can be used e.g. to listen to "OtherModule.OnNewResult"-events and to forward the results via MQTT. +
26+
This can be used e.g. to listen to "OtherModule.OnNewResult"-events and to forward the results via MQTT (data will be forwarded to data type 'string'). +
2727
To do so make use of "addPublishEvent" (via script) or the "presetPublish"-functions (incl. "presetPublishEvent") and "addPublishEventViaUI". +
2828
{empty} +
2929
**5) WillMessage** +
@@ -308,7 +308,7 @@ Configure a WillMessage via "setWillMessageConfig" (via script) or "setWillMessa
308308
<desc>Function to unsubscribe from preselected topic via UI (see "selectSubscriptionViaUI").</desc>
309309
</function>
310310
<function name="addPublishEvent">
311-
<desc>Function to add an event to listen to and forward content if notified with configured MQTT publish message.</desc>
311+
<desc>Function to add an event to listen to and forward content (as string) if notified with configured MQTT publish message.</desc>
312312
<param desc="Name of event to register (event with one parameter expected)." multiplicity="1" name="event" type="string"/>
313313
<param desc="Data content of the event will be publsihed to this MQTT topic." multiplicity="1" name="topic" type="string"/>
314314
<param desc="QoS of publish message." multiplicity="1" name="qos" ref="CSK_MQTTClient.QOS" type="enum"/>
@@ -323,7 +323,7 @@ Configure a WillMessage via "setWillMessageConfig" (via script) or "setWillMessa
323323
<param desc="Name of event" multiplicity="1" name="name" type="string"/>
324324
</function>
325325
<function name="addPublishEventViaUI">
326-
<desc>Function to add a preset event with preset configuration (topic, QoS, Retain) to listen to and to forward content via MQTT publish.</desc>
326+
<desc>Function to add a preset event with preset configuration (topic, QoS, Retain) to listen to and to forward content (as string) via MQTT publish.</desc>
327327
</function>
328328
<function name="selectPublishEvent">
329329
<desc>Function to select event of list to publish via UI.</desc>
@@ -417,7 +417,7 @@ Configure a WillMessage via "setWillMessageConfig" (via script) or "setWillMessa
417417
</serves>
418418
</crown>
419419
<meta key="author">SICK AG</meta>
420-
<meta key="version">0.4.1</meta>
420+
<meta key="version">1.0.0</meta>
421421
<meta key="priority">low</meta>
422422
<meta key="copy-protected">false</meta>
423423
<meta key="read-protected">false</meta>

CSK_Module_MQTTClient/scripts/Communication/MQTTClient/MQTTClient_Controller.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ local function connectMQTT(status)
222222
end
223223
MQTTClient.setKeepAliveInterval(mqttClient_Model.mqttClient, mqttClient_Model.parameters.keepAliveInterval)
224224
if mqttClient_Model.parameters.useCredentials then
225-
MQTTClient.setUserCredentials(mqttClient_Model.mqttClient, mqttClient_Model.parameters.username, Cipher.AES.decrypt(mqttClient_Model.parameters.passwords, mqttClient_Model.key))
225+
MQTTClient.setUserCredentials(mqttClient_Model.mqttClient, mqttClient_Model.parameters.username, Cipher.AES.decrypt(mqttClient_Model.parameters.password, mqttClient_Model.key))
226226
end
227227
if mqttClient_Model.parameters.useWillMessage then
228228
MQTTClient.setWillMessage(mqttClient_Model.mqttClient, mqttClient_Model.parameters.willMessageTopic, mqttClient_Model.parameters.willMessageData, mqttClient_Model.parameters.willMessageQOS, mqttClient_Model.parameters.willMessageRetain)
@@ -249,10 +249,14 @@ local function connectMQTT(status)
249249
MQTTClient.setCABundle(mqttClient_Model.mqttClient, mqttClient_Model.parameters.caBundlePath)
250250
end
251251
end
252-
252+
mqttClient_Model.reconnectionTimer:start()
253253
MQTTClient.connect(mqttClient_Model.mqttClient, mqttClient_Model.parameters.connectionTimeout)
254+
if mqttClient_Model.mqttClient:isConnected() == false then
255+
mqttClient_Model.addMessageLog("Connection failed")
256+
end
254257
else
255258
MQTTClient.disconnect(mqttClient_Model.mqttClient)
259+
mqttClient_Model.reconnectionTimer:stop()
256260
end
257261
end
258262
Script.serveFunction('CSK_MQTTClient.connectMQTT', connectMQTT)
@@ -331,15 +335,15 @@ Script.serveFunction('CSK_MQTTClient.setUsername', setUsername)
331335

332336
local function setPassword(password)
333337
_G.logger:info(nameOfModule .. ": Set password.")
334-
mqttClient_Model.parameters.passwords = Cipher.AES.encrypt(password, mqttClient_Model.key)
338+
mqttClient_Model.parameters.password = Cipher.AES.encrypt(password, mqttClient_Model.key)
335339
end
336340
Script.serveFunction('CSK_MQTTClient.setPassword', setPassword)
337341

338342
local function setUseCredentials(status)
339343
_G.logger:info(nameOfModule .. ": Set usage of credentials to " .. tostring(status))
340344
mqttClient_Model.parameters.useCredentials = status
341345
if status then
342-
MQTTClient.setUserCredentials(mqttClient_Model.mqttClient, mqttClient_Model.parameters.username, Cipher.AES.decrypt(mqttClient_Model.parameters.passwords, mqttClient_Model.key))
346+
MQTTClient.setUserCredentials(mqttClient_Model.mqttClient, mqttClient_Model.parameters.username, Cipher.AES.decrypt(mqttClient_Model.parameters.password, mqttClient_Model.key))
343347
end
344348
end
345349
Script.serveFunction('CSK_MQTTClient.setUseCredentials', setUseCredentials)
@@ -522,10 +526,7 @@ Script.serveFunction('CSK_MQTTClient.presetPublishEvent', presetPublishEvent)
522526

523527
--- Function to create internal publish functions
524528
---@param event string Name of event to register (event with one parameter expected)
525-
---@param topic string Data content of the event will be publsihed to this MQTT topic
526-
---@param qos string QoS of publish message
527-
---@param retain string Retain option of the publish
528-
local function createInternalPublishFunctions(event, topic, qos, retain)
529+
local function createInternalPublishFunctions(event)
529530

530531
local function triggerPublish(event, data)
531532
if mqttClient_Model.isConnected then
@@ -541,21 +542,25 @@ local function createInternalPublishFunctions(event, topic, qos, retain)
541542
mqttClient_Model.publishEventsFunctions[event] = forwardContent
542543

543544
if Script.isServedAsEvent(event) then
544-
_G.logger:info(nameOfModule .. ": Register to event '" .. event .. "' to forward its content via MQTT publish on topic '" .. topic .. "'")
545+
_G.logger:info(nameOfModule .. ": Register to event '" .. event .. "' to forward its content via MQTT publish on topic '" .. mqttClient_Model.parameters.publishEvents.topic[event] .. "'")
545546
Script.register(event, mqttClient_Model.publishEventsFunctions[event])
546547
else
547548
_G.logger:info(nameOfModule .. ": Not possible to register to event '" .. event .. "' as it seems not to be available.")
548549
end
549550
end
550551

551552
local function addPublishEvent(event, topic, qos, retain)
553+
if mqttClient_Model.publishEventsFunctions[event] then
554+
Script.deregister(event, mqttClient_Model.publishEventsFunctions[event])
555+
mqttClient_Model.publishEventsFunctions[event] = nil
556+
end
552557
mqttClient_Model.parameters.publishEvents.topic[event] = topic
553558
mqttClient_Model.parameters.publishEvents.qos[event] = qos
554559
mqttClient_Model.parameters.publishEvents.retain[event] = retain
555560

556561
Script.notifyEvent("MQTTClient_OnNewStatusPublishEventList", mqttClient_Model.helperFuncs.createJsonListPublishEvents(mqttClient_Model.parameters.publishEvents))
557562

558-
createInternalPublishFunctions(event, topic, qos, retain)
563+
createInternalPublishFunctions(event)
559564

560565
end
561566
Script.serveFunction('CSK_MQTTClient.addPublishEvent', addPublishEvent)
@@ -672,7 +677,7 @@ local function loadParameters()
672677

673678
-- Configured/activated with new loaded data
674679
for key in pairs(mqttClient_Model.parameters.publishEvents.topic) do
675-
createInternalPublishFunctions(key, mqttClient_Model.parameters.publishEvents.topic[key], mqttClient_Model.parameters.publishEvents.qos[key], mqttClient_Model.parameters.publishEvents.retain[key])
680+
createInternalPublishFunctions(key)
676681
end
677682
connectMQTT(mqttClient_Model.parameters.connect)
678683

CSK_Module_MQTTClient/scripts/Communication/MQTTClient/MQTTClient_Model.lua

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ end
4949
-- Create parameters / instances for this module
5050
mqttClient_Model.isConnected = false
5151

52+
mqttClient_Model.reconnectionTimer = Timer.create() -- Timer to reconnect in case the connection to the broker is lost
53+
mqttClient_Model.reconnectionTimer:setExpirationTime(5000)
54+
mqttClient_Model.reconnectionTimer:setPeriodic(true)
55+
5256
mqttClient_Model.mqttClient = MQTTClient.create()
5357

5458
mqttClient_Model.tempSubscriptionTopic = '' -- temporary preset topic to subscribe
@@ -181,10 +185,9 @@ mqttClient_Model.subscripeToAllTopics = subscripeToAllTopics
181185

182186
--- Function to react on "OnConnected" event of MQTT client
183187
local function handleOnConnected()
184-
_G.logger:info(nameOfModule .. ": Connected to MQQT broker.")
185-
186-
addMessageLog('Connected to MQQT broker.')
187-
188+
_G.logger:info(nameOfModule .. ": Connected to MQTT broker.")
189+
addMessageLog('Connected to MQTT broker.')
190+
mqttClient_Model.reconnectionTimer:stop()
188191
mqttClient_Model.isConnected = true
189192
Script.notifyEvent("MQTTClient_OnNewStatusCurrentlyConnected", mqttClient_Model.isConnected)
190193
subscripeToAllTopics()
@@ -193,18 +196,33 @@ MQTTClient.register(mqttClient_Model.mqttClient, 'OnConnected', handleOnConnecte
193196

194197
--- Function to react on "OnDisconnected" event of MQTT client
195198
local function handleOnDisconnected()
196-
_G.logger:info(nameOfModule .. ": Disconnected from MQQT broker.")
197-
addMessageLog('Disconnected from MQQT broker.')
199+
_G.logger:info(nameOfModule .. ": Disconnected from MQTT broker.")
200+
addMessageLog('Disconnected from MQTT broker.')
201+
if mqttClient_Model.parameters.connect == true then
202+
mqttClient_Model.reconnectionTimer:start()
203+
end
198204
mqttClient_Model.isConnected = false
199205
Script.notifyEvent("MQTTClient_OnNewStatusCurrentlyConnected", mqttClient_Model.isConnected)
200206
end
201207
MQTTClient.register(mqttClient_Model.mqttClient, 'OnDisconnected', handleOnDisconnected)
202208

209+
--- Function to reconnect to broker if the connection is lost
210+
local function handleOnReconnectionTimerExpired()
211+
if mqttClient_Model.parameters.connect == true then
212+
MQTTClient.connect(mqttClient_Model.mqttClient, mqttClient_Model.parameters.connectionTimeout)
213+
if mqttClient_Model.mqttClient:isConnected() == false then
214+
addMessageLog("Reconnection trial failed")
215+
end
216+
else
217+
mqttClient_Model.reconnectionTimer:stop()
218+
end
219+
end
220+
mqttClient_Model.reconnectionTimer:register('OnExpired', handleOnReconnectionTimerExpired)
221+
203222
--- Function to reset the MQTTClient
204223
local function recreateMQTTClient()
205224
Script.releaseObject(mqttClient_Model.mqttClient)
206225
mqttClient_Model.mqttClient = MQTTClient.create()
207-
208226
MQTTClient.register(mqttClient_Model.mqttClient, 'OnReceive', handleOnReceive)
209227
MQTTClient.register(mqttClient_Model.mqttClient, 'OnConnected', handleOnConnected)
210228
MQTTClient.register(mqttClient_Model.mqttClient, 'OnDisconnected', handleOnDisconnected)

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ Data of registered events to be forwarded via MQTT are always published as conve
1616

1717
Tested on:
1818

19-
1. SIM1012 - Firmware 2.2.0
20-
2. SICK AppEngine - Firmware 1.3.2
21-
3. TDC-E - L4M 2023.1
19+
|Device|Firmware version|Module version|
20+
|--|--|--|
21+
|SIM1012|V2.4.1|v1.0.0|
22+
|SIM1012|V2.2.0|v0.4.1|
23+
|SICK AppEngine|V1.5.0|v1.0.0|
24+
|SICK AppEngine|V1.3.2|v0.4.1|
25+
|TDC-E|L4M 2023.1|v0.4.1|
2226

2327
This module is part of the SICK AppSpace Coding Starter Kit developing approach.
2428
It is programmed in an object oriented way. Some of these modules use kind of "classes" in Lua to make it possible to reuse code / classes in other projects.

docu/CSK_Module_MQTTClient.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<meta name="generator" content="Asciidoctor 2.0.12">
88
<meta name="author" content="SICK AG">
9-
<title>Documentation - CSK_Module_MQTTClient 0.4.1</title>
9+
<title>Documentation - CSK_Module_MQTTClient 1.0.0</title>
1010
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
1111
<style>
1212
/* Stylesheet for CodeRay to match GitHub theme | MIT License | http://foundation.zurb.com */
@@ -615,11 +615,11 @@
615615
</head>
616616
<body class="article toc2 toc-left">
617617
<div id="header">
618-
<h1>Documentation - CSK_Module_MQTTClient 0.4.1</h1>
618+
<h1>Documentation - CSK_Module_MQTTClient 1.0.0</h1>
619619
<div class="details">
620620
<span id="author" class="author">SICK AG</span><br>
621-
<span id="revnumber">version 0.4.1,</span>
622-
<span id="revdate">2023-09-19</span>
621+
<span id="revnumber">version 1.0.0,</span>
622+
<span id="revdate">2024-03-20</span>
623623
</div>
624624
<div id="toc" class="toc2">
625625
<div id="toctitle">Table of Contents</div>
@@ -765,11 +765,11 @@ <h2 id="_document_metadata">Document metadata</h2>
765765
</tr>
766766
<tr>
767767
<th class="tableblock halign-left valign-top"><p class="tableblock">Version</p></th>
768-
<td class="tableblock halign-left valign-top"><p class="tableblock">0.4.1</p></td>
768+
<td class="tableblock halign-left valign-top"><p class="tableblock">1.0.0</p></td>
769769
</tr>
770770
<tr>
771771
<th class="tableblock halign-left valign-top"><p class="tableblock">Date</p></th>
772-
<td class="tableblock halign-left valign-top"><p class="tableblock">2023-09-19</p></td>
772+
<td class="tableblock halign-left valign-top"><p class="tableblock">2024-03-20</p></td>
773773
</tr>
774774
<tr>
775775
<th class="tableblock halign-left valign-top"><p class="tableblock">Author</p></th>
@@ -5286,8 +5286,8 @@ <h3 id="API:Enum:CSK_MQTTClient.TLSVersion"><span class="api-enum">CSK_MQTTClien
52865286
</div>
52875287
<div id="footer">
52885288
<div id="footer-text">
5289-
Version 0.4.1<br>
5290-
Last updated 2023-09-19 11:19:46 +0200
5289+
Version 1.0.0<br>
5290+
Last updated 2024-03-20 10:53:00 +0100
52915291
</div>
52925292
</div>
52935293
<script type="text/javascript">

0 commit comments

Comments
 (0)