-
Notifications
You must be signed in to change notification settings - Fork 12
/
mqttdb.html
417 lines (403 loc) · 21.4 KB
/
mqttdb.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!--
Copyright JS Foundation and other contributors, http://js.foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Modifications Copyright 2017 Sense Tecnic Systems, Inc.
-->
<script type="text/x-red" data-template-name="mqttdb in">
<div class="form-row">
<label for="node-input-broker"><i class="fa fa-globe"></i> <span data-i18n="mqttdb.label.broker"></span></label>
<input type="text" id="node-input-broker">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tasks"></i> <span data-i18n="mqttdb.label.topic"></span></label>
<input type="text" id="node-input-topic" data-i18n="[placeholder]mqttdb.label.topic">
</div>
<div class="form-row">
<label for="node-input-qos"><i class="fa fa-empire"></i> <span data-i18n="mqttdb.label.qos"></span></label>
<select id="node-input-qos" style="width:125px !important">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="mqttdb.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]mqttdb.label.name">
</div>
</script>
<script type="text/x-red" data-help-name="mqttdb in">
<p>Connects to a MQTT broker and subscribes to messages from the specified topic backed by a persistent
message store.</p>
<h3>Outputs</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">string | buffer</span></dt>
<dd>a string unless detected as a binary buffer.</dd>
<dt>topic <span class="property-type">string</span></dt>
<dd>the MQTT topic, uses / as a heirarchy separator.</dd>
<dt>qos <span class="property-type">number</span> </dt>
<dd>0, fire and forget - 1, at least once - 2, once and once only.</dd>
<dt>retain <span class="property-type">boolean</span></dt>
<dd>true indicates the message was retained and may be old.</dd>
</dl>
<h3>Details</h3>
The subscription topic can include MQTT wildcards, + for one level, # for multiple levels.</p>
<p>This node requires a connection to a MQTT broker to be configured. This is configured by clicking
the pencil icon.</p>
<p>Several MQTT nodes (in or out) can share the same broker connection if required.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('mqttdb in',{
category: 'input',
defaults: {
name: {value:""},
topic: {value:"",required:true,validate: RED.validators.regex(/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/)},
qos: {value: "2"},
broker: {type:"mqttdb-broker", required:true}
},
color:"#AAAA66",
inputs:0,
outputs:1,
icon: "bridge.png",
label: function() {
return this.name||this.topic||"mqttdb";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
if (this.qos === undefined) {
$("#node-input-qos").val("2");
}
}
});
</script>
<script type="text/x-red" data-template-name="mqttdb out">
<div class="form-row">
<label for="node-input-broker"><i class="fa fa-globe"></i> <span data-i18n="mqttdb.label.broker"></span></label>
<input type="text" id="node-input-broker">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tasks"></i> <span data-i18n="mqttdb.label.topic"></span></label>
<input type="text" id="node-input-topic" data-i18n="[placeholder]mqttdb.label.topic">
</div>
<div class="form-row">
<label for="node-input-qos"><i class="fa fa-empire"></i> <span data-i18n="mqttdb.label.qos"></span></label>
<select id="node-input-qos" style="width:125px !important">
<option value=""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<i class="fa fa-history"></i> <span data-i18n="mqttdb.retain"></span> <select id="node-input-retain" style="width:125px !important">
<option value=""></option>
<option value="false" data-i18n="mqttdb.false"></option>
<option value="true" data-i18n="mqttdb.true"></option>
</select>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="mqttdb.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]mqttdb.label.name">
</div>
<div class="form-tips"><span data-i18n="mqttdb.tip"></span></div>
</script>
<script type="text/x-red" data-help-name="mqttdb out">
<p>Connects to a MQTT broker and publishes messages backed by a persistent
message store.</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">string | buffer</span></dt>
<dd> most users prefer simple text payloads, but binary buffers can also be published.</dd>
<dt class="optional">topic <span class="property-type">string</span></dt>
<dd> the MQTT topic to publish to.</dd>
<dt class="optional">qos <span class="property-type">number</span></dt>
<dd>0, fire and forget - 1, at least once - 2, once and once only. Default 0.</dd>
<dt class="optional">retain <span class="property-type">boolean</span></dt>
<dd>set to true to retain the message on the broker. Default false.</dd>
</dl>
<h3>Details</h3>
<code>msg.payload</code> is used as the payload of the published message.
If it contains an Object it will be converted to a JSON string before being sent.
If it contains a binary Buffer the message will be published as-is.</p>
<p>The topic used can be configured in the node or, if left blank, can be set by <code>msg.topic</code>.</p>
<p>Likewise the QoS and retain values can be configured in the node or, if left
blank, set by <code>msg.qos</code> and <code>msg.retain</code> respectively. To clear a previously
retained topic from the broker, send a blank message to that topic with the retain flag set.</p>
<p>This node requires a connection to a MQTT broker to be configured. This is configured by clicking
the pencil icon.</p>
<p>Several MQTT nodes (in or out) can share the same broker connection if required.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('mqttdb out',{
category: 'output',
defaults: {
name: {value:""},
topic: {value:""},
qos: {value:""},
retain: {value:""},
broker: {type:"mqttdb-broker", required:true}
},
color:"#AAAA66",
inputs:1,
outputs:0,
icon: "bridge.png",
align: "right",
label: function() {
return this.name||this.topic||"mqttdb";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>
<script type="text/x-red" data-template-name="mqttdb-broker">
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="mqttdb.label.name"></span></label>
<input type="text" id="node-config-input-name" data-i18n="[placeholder]mqttdb.label.name">
</div>
<div class="form-row">
<ul style="background: #fff; min-width: 600px; margin-bottom: 20px;" id="node-config-mqtt-broker-tabs"></ul>
</div>
<div id="node-config-mqtt-broker-tabs-content" style="min-height: 170px;">
<div id="mqtt-broker-tab-connection" style="display:none">
<div class="form-row node-input-broker">
<label for="node-config-input-broker"><i class="fa fa-globe"></i> <span data-i18n="mqttdb.label.broker"></span></label>
<input class="input-append-left" type="text" id="node-config-input-broker" style="width:40%;" data-i18n="[placeholder]mqttdb.label.example">
<label for="node-config-input-port" style="margin-left:20px; width:43px; "> <span data-i18n="mqttdb.label.port"></span></label>
<input type="text" id="node-config-input-port" data-i18n="[placeholder]mqttdb.label.port" style="width:55px">
</div>
<div class="form-row">
<input type="checkbox" id="node-config-input-usetls" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-config-input-usetls" style="width: auto" data-i18n="mqttdb.label.use-tls"></label>
<div id="node-config-row-tls" class="hide">
<label style="width: auto; margin-left: 20px; margin-right: 10px;" for="node-config-input-tls"><span data-i18n="mqttdb.label.tls-config"></span></label><input style="width: 300px;" type="text" id="node-config-input-tls">
</div>
</div>
<div class="form-row">
<label for="node-config-input-clientid"><i class="fa fa-tag"></i> <span data-i18n="mqttdb.label.clientid"></span></label>
<input type="text" id="node-config-input-clientid" data-i18n="[placeholder]mqttdb.placeholder.clientid">
</div>
<div class="form-row">
<label for="node-config-input-keepalive" style="width: auto"><i class="fa fa-clock-o"></i> <span data-i18n="mqttdb.label.keepalive"></span></label>
<input type="text" id="node-config-input-keepalive" style="width: 50px">
<input type="checkbox" id="node-config-input-cleansession" style="margin-left: 30px; height: 1em;display: inline-block; width: auto; vertical-align: middle;">
<label for="node-config-input-cleansession" style="width: auto;" data-i18n="mqttdb.label.cleansession"></label>
</div>
<div class="form-row">
<input type="checkbox" id="node-config-input-compatmode" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-config-input-compatmode" style="width: auto;" data-i18n="mqttdb.label.compatmode"></label>
</div>
<div class="form-row">
<input type="checkbox" id="node-config-input-persistout" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-config-input-persistout" style="width: auto;" data-i18n="mqttdb.label.persistout"></label>
<input type="checkbox" id="node-config-input-persistin" style="margin-left: 30px; display: inline-block; width: auto; vertical-align: top;">
<label for="node-config-input-persistin" style="width: auto;" data-i18n="mqttdb.label.persistin"></label>
</div>
<div class="form-row">
<label for="node-config-input-compactinterval" style="width: auto"><i class="fa fa-clock-o"></i> <span data-i18n="mqttdb.label.compactinterval"></span></label>
<input type="text" id="node-config-input-compactinterval" style="width: 50px">
</div>
</div>
<div id="mqtt-broker-tab-security" style="display:none">
<div class="form-row">
<label for="node-config-input-user"><i class="fa fa-user"></i> <span data-i18n="mqttdb.label.username"></span></label>
<input type="text" id="node-config-input-user">
</div>
<div class="form-row">
<label for="node-config-input-password"><i class="fa fa-lock"></i> <span data-i18n="mqttdb.label.password"></span></label>
<input type="password" id="node-config-input-password">
</div>
</div>
<div id="mqtt-broker-tab-birth" style="display:none">
<div class="form-row">
<label for="node-config-input-birthTopic"><i class="fa fa-tasks"></i> <span data-i18n="mqttdb.label.topic"></span></label>
<input type="text" id="node-config-input-birthTopic" data-i18n="[placeholder]mqttdb.placeholder.birth-topic">
</div>
<div class="form-row">
<label for="node-config-input-birthQos"><i class="fa fa-empire"></i> <span data-i18n="mqttdb.label.qos"></span></label>
<select id="node-config-input-birthQos" style="width:125px !important">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<i class="fa fa-history"></i> <span data-i18n="mqttdb.retain"></span> <select id="node-config-input-birthRetain" style="width:125px !important">
<option value="false" data-i18n="mqttdb.false"></option>
<option value="true" data-i18n="mqttdb.true"></option>
</select>
</div>
<div class="form-row">
<label for="node-config-input-birthPayload"><i class="fa fa-envelope"></i> <span data-i18n="mqttdb.label.payload"></span></label>
<input type="text" id="node-config-input-birthPayload" data-i18n="[placeholder]mqttdb.label.payload">
</div>
</div>
<div id="mqtt-broker-tab-will" style="display:none">
<div class="form-row">
<label for="node-config-input-willTopic"><i class="fa fa-tasks"></i> <span data-i18n="mqttdb.label.topic"></span></label>
<input type="text" id="node-config-input-willTopic" data-i18n="[placeholder]mqttdb.placeholder.will-topic">
</div>
<div class="form-row">
<label for="node-config-input-willQos"><i class="fa fa-empire"></i> <span data-i18n="mqttdb.label.qos"></span></label>
<select id="node-config-input-willQos" style="width:125px !important">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<i class="fa fa-history"></i> <span data-i18n="mqttdb.retain"></span> <select id="node-config-input-willRetain" style="width:125px !important">
<option value="false" data-i18n="mqttdb.false"></option>
<option value="true" data-i18n="mqttdb.true"></option>
</select>
</div>
<div class="form-row">
<label for="node-config-input-willPayload"><i class="fa fa-envelope"></i> <span data-i18n="mqttdb.label.payload"></span></label>
<input type="text" id="node-config-input-willPayload" data-i18n="[placeholder]mqttdb.label.payload">
</div>
</div>
</div>
</script>
<script type="text/x-red" data-help-name="mqttdb-broker">
<p>A minimum MQTT broker connection requires only a broker server address to be added to the default configuration.</p>
<p>To secure the connection with SSL/TLS, a TLS Configuration must also be configured and selected.</p>
<p>If you create a Client ID it must be unique to the broker you are connecting to.</p>
<p>Unlike the built in MQTT node, this broker connection will persist messages when connectivity is lost,
and between Node-RED restarts to reduce data loss.</p>
<p>To enable persistent storage for input and output messages, click on the appropriate check boxes.<p>
<p>Change the <em>compaction interval time</em> to compact the internal database and remove deleted messages.<p>
<p>Database files for a connection are stored in the Node-RED directory under the <em>mqttdb</em> directory. These
can be safely removed after stopping Node-RED to clear unsent messages. The database
used is <a target="_blank" href="https://github.com/louischatriot/nedb">NeDB.</a>
<p>For more information about MQTT see the <a href="http://www.eclipse.org/paho/" target="_blank">Eclipse Paho</a> site.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('mqttdb-broker',{
category: 'config',
defaults: {
name: {value:""},
broker: {value:"",required:true},
port: {value:1883,required:true,validate:RED.validators.number()},
tls: {type:"tls-config",required: false},
clientid: {value:"", validate: function(v) {
if ($("#node-config-input-clientid").length) {
// Currently editing the node
return $("#node-config-input-cleansession").is(":checked") || (v||"").length > 0;
} else {
return (this.cleansession===undefined || this.cleansession) || (v||"").length > 0;
}
}},
usetls: {value: false},
verifyservercert: { value: false},
compatmode: { value: true},
keepalive: {value:60,validate:RED.validators.number()},
cleansession: {value: true},
willTopic: {value:""},
willQos: {value:"0"},
willRetain: {value:false},
willPayload: {value:""},
birthTopic: {value:""},
birthQos: {value:"0"},
birthRetain: {value:false},
birthPayload: {value:""},
persistout: {value:true},
persistin: {value:false},
compactinterval: {value:30,validate:RED.validators.number()}
},
credentials: {
user: {type:"text"},
password: {type: "password"}
},
label: function() {
var lab = this.name;
if ((lab === undefined) || (lab ==="")) {
var b = this.broker;
if (b === "") { b = "undefined"; }
lab = (this.clientid?this.clientid+"@":"")+b+":"+this.port;
}
return lab;
},
oneditprepare: function () {
var tabs = RED.tabs.create({
id: "node-config-mqtt-broker-tabs",
onchange: function(tab) {
$("#node-config-mqtt-broker-tabs-content").children().hide();
$("#" + tab.id).show();
}
});
tabs.addTab({
id: "mqtt-broker-tab-connection",
label: this._("mqttdb.tabs-label.connection")
});
tabs.addTab({
id: "mqtt-broker-tab-security",
label: this._("mqttdb.tabs-label.security")
});
tabs.addTab({
id: "mqtt-broker-tab-birth",
label: this._("mqttdb.tabs-label.birth")
});
tabs.addTab({
id: "mqtt-broker-tab-will",
label: this._("mqttdb.tabs-label.will")
});
setTimeout(function() { tabs.resize(); },0);
if (typeof this.cleansession === 'undefined') {
this.cleansession = true;
$("#node-config-input-cleansession").prop("checked",true);
}
if (typeof this.usetls === 'undefined') {
this.usetls = false;
$("#node-config-input-usetls").prop("checked",false);
}
if (typeof this.compatmode === 'undefined') {
this.compatmode = true;
$("#node-config-input-compatmode").prop('checked', true);
}
if (typeof this.keepalive === 'undefined') {
this.keepalive = 15;
$("#node-config-input-keepalive").val(this.keepalive);
}
if (typeof this.willQos === 'undefined') {
this.willQos = "0";
$("#node-config-input-willQos").val("0");
}
if (typeof this.birthQos === 'undefined') {
this.birthQos = "0";
$("#node-config-input-birthQos").val("0");
}
function updateTLSOptions() {
if ($("#node-config-input-usetls").is(':checked')) {
$("#node-config-row-tls").show();
} else {
$("#node-config-row-tls").hide();
}
}
updateTLSOptions();
$("#node-config-input-usetls").on("click",function() {
updateTLSOptions();
});
var node = this;
function updateClientId() {
if ($("#node-config-input-cleansession").is(":checked")) {
$("#node-config-input-clientid").attr("placeholder",node._("mqttdb.placeholder.clientid"));
} else {
$("#node-config-input-clientid").attr("placeholder",node._("mqttdb.placeholder.clientid-nonclean"));
}
$("#node-config-input-clientid").change();
}
setTimeout(updateClientId,0);
$("#node-config-input-cleansession").on("click",function() {
updateClientId();
});
},
oneditsave: function() {
if (!$("#node-config-input-usetls").is(':checked')) {
$("#node-config-input-tls").val("");
}
}
});
</script>