1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Update mqtt nodes for v5

This commit is contained in:
Nick O'Leary 2021-02-25 15:49:56 +00:00
parent 16088b8a08
commit 255b8f2005
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 586 additions and 449 deletions

View File

@ -11,9 +11,6 @@
limitations under the License.
-->
<style>
.form-row label:first-child {
width: 125px;
}
.mqtt-form-row-cols2 > input.mqtt-form-row-col1 {
width: calc(35% - 75px);
@ -35,20 +32,20 @@
width: calc(35% - 75px);
display: inline-block;
}
.form-row.mqtt5-out > label {
width: 130px;
}
.form-row.mqtt-flags-row > label {
vertical-align: top;
}
.form-row.mqtt-flags-row > .mqtt-flags {
display: inline-block;
display: inline-block;
width: 70%
}
.form-row.mqtt-flags-row > .mqtt-flags > .mqtt-flag > label {
display: block;
padding-left: 15px;
text-indent: -15px;
width: 70%;
width: 100%;
}
.form-row.mqtt-flags-row > .mqtt-flags > .mqtt-flag > label > input {
position: relative;
@ -85,101 +82,38 @@
<option value="json" data-i18n="mqtt.output.json"></option>
<option value="base64" data-i18n="mqtt.output.base64"></option>
</select>
</div>
<!-- FUTURE
<div class="form-row mqtt5">
<label for="node-input-userProperties"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.userProperties"></span></label>
<input id="node-input-userPropertiesType" type="hidden">
<input type="text" id="node-input-userProperties" style="width: 70%;" data-i18n="[placeholder]mqtt.label.userProperties" placeholder="">
</div> -->
<!-- FUTURE
<div class="form-row mqtt5">
<label for="node-input-subscriptionIdentifier" class="mqtt-form-row-col1"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.subscriptionIdentifier"></span></label>
<input type="number" min="0" max="268435455" id="node-input-subscriptionIdentifier" class="mqtt-form-row-col1" >
</div> -->
<div class="form-row mqtt-flags-row mqtt5">
</div>
<div class="form-row mqtt-flags-row mqtt5">
<label for="node-input-nl" ><i class="fa fa-flag"></i> <span data-i18n="mqtt.label.flags">Flags</span></label>
<div class="mqtt-flags">
<div class="mqtt-flag">
<label for="node-input-nl">
<input type="checkbox" id="node-input-nl">
<span data-i18n="mqtt.label.nl"></span>
</label>
</label>
</div>
<div class="mqtt-flag">
<label for="node-input-rap">
<input type="checkbox" id="node-input-rap">
<span data-i18n="mqtt.label.rap"></span>
</label>
</label>
</div>
</div>
</div>
<div class="form-row mqtt5">
<label for="node-input-rh" ><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.rh"></span></label>
<select id="node-input-rh" style="width:70%;">
</div>
<div class="form-row mqtt5">
<label for="node-input-rh" style="width:100%"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.rh"></span></label>
<select id="node-input-rh" style="margin-left: 104px; width: 70%">
<option value="0" data-i18n="mqtt.label.rh0"></option>
<option value="1" data-i18n="mqtt.label.rh1"></option>
<option value="2" data-i18n="mqtt.label.rh2"></option>
</select>
</div>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('mqtt in',{
category: 'network',
defaults: {
name: {value:""},
topic: {value:"",required:true,validate: RED.validators.regex(/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/)},
qos: {value: "2"},
datatype: {value:"auto",required:true},
broker: {type:"mqtt-broker", required:true},
// userProperties: {value:"userProperties"},
// userPropertiesType: {value:"none"},
// subscriptionIdentifier: {value:0},
nl: {value:false},
rap: {value:true},
rh: {value:0},
},
color:"#d8bfd8",
inputs:0,
outputs:1,
icon: "bridge.svg",
label: function() {
return this.name||this.topic||"mqtt";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$("#node-input-broker").change(function(d){
var confNode = RED.nodes.node($("#node-input-broker").val());
var v5 = confNode && confNode.protocolVersion == "5";
if(v5) {
$("div.form-row.mqtt5").show();
} else {
$("div.form-row.mqtt5").hide();
}
});
if (this.qos === undefined) {
$("#node-input-qos").val("2");
}
if (this.datatype === undefined) {
$("#node-input-datatype").val("auto");
}
// FUTURE
// $("#node-input-userProperties").typedInput({
// default: "none",
// types:[{ value: 'none', label: 'None', hasValue: false },'flow','global','json'],
// typeField: $("#node-input-userPropertiesType")
// });
}
});
</script>
<script type="text/html" data-template-name="mqtt out">
<div class="form-row">
<label for="node-input-broker"><i class="fa fa-globe"></i> <span data-i18n="mqtt.label.broker"></span></label>
@ -198,7 +132,7 @@
<option value="1">1</option>
<option value="2">2</option>
</select>
<label for="node-input-retain" class="mqtt-form-row-col2"><i class="fa fa-history"></i> <span data-i18n="mqtt.retain"></span></label>
<select id="node-input-retain" class="mqtt-form-row-col2" >
<option value=""></option>
@ -206,49 +140,27 @@
<option value="true" data-i18n="mqtt.true"></option>
</select>
</div>
<div class="form-row mqtt5">
<label for="node-input-userProperties"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.userProperties"></span></label>
<input id="node-input-userPropertiesType" type="hidden">
<input type="text" id="node-input-userProperties" style="width: 70%;" data-i18n="[placeholder]mqtt.label.userProperties" placeholder="">
<div class="form-row mqtt5 mqtt5-out">
<label for="node-input-userProps"><span data-i18n="mqtt.label.userProperties"></span></label>
<input type="text" id="node-input-userProps" style="width: calc(100% - 166px);">
</div>
<div class="form-row mqtt5">
<label for="node-input-responseTopic"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.responseTopic"></span></label>
<input id="node-input-responseTopicType" type="hidden">
<input type="text" id="node-input-responseTopic" style="width: 70%;" data-i18n="[placeholder]mqtt.label.responseTopic" placeholder="">
<div class="form-row mqtt5 mqtt5-out">
<label for="node-input-respTopic"><span data-i18n="mqtt.label.responseTopic"></span></label>
<input type="text" id="node-input-respTopic" style="width: calc(100% - 166px);">
</div>
<div class="form-row mqtt5">
<label for="node-input-correlationData"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.correlationData"></span></label>
<input id="node-input-correlationDataType" type="hidden">
<input type="text" id="node-input-correlationData" style="width: 70%;" data-i18n="[placeholder]mqtt.label.correlationData" placeholder="">
<div class="form-row mqtt5 mqtt5-out">
<label for="node-input-correl"><span data-i18n="mqtt.label.correlationData"></span></label>
<input type="text" id="node-input-correl" style="width: calc(100% - 166px);">
</div>
<div class="form-row mqtt5">
<label for="node-input-contentType"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.contentType"></span></label>
<input id="node-input-contentTypeType" type="hidden">
<input type="text" id="node-input-contentType" style="width: 70%;" data-i18n="[placeholder]mqtt.label.contentType" placeholder="">
<div class="form-row mqtt5 mqtt5-out">
<label for="node-input-contentType"><span data-i18n="mqtt.label.contentType"></span></label>
<input type="text" id="node-input-contentType" style="width: calc(100% - 166px);">
</div>
<div class="form-row mqtt-form-row-cols2 mqtt5">
<label for="node-input-messageExpiryInterval" class="mqtt-form-row-col1"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.messageExpiryInterval"></span></label>
<input id="node-input-messageExpiryIntervalType" type="hidden">
<input id="node-input-messageExpiryInterval" style="width: 70%;" class="mqtt-form-row-col1" >
<!-- FUTURE
<label for="node-input-topicAlias" class="mqtt-form-row-col2"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.topicAlias">Alias</span></label>
<input type="number" min="0" id="node-input-topicAlias" class="mqtt-form-row-col2" > -->
</div>
<!-- FUTURE
<div class="form-row mqtt-form-row-cols2 mqtt5">
<label for="node-input-subscriptionIdentifier" class="mqtt-form-row-col1"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.subscriptionIdentifier"></span></label>
<input type="number" min="0" max="268435455" id="node-input-subscriptionIdentifier" class="mqtt-form-row-col1" >
<label for="node-input-payloadFormatIndicator" class="mqtt-form-row-col2"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.payloadFormatIndicator"></span></label>
<select id="node-input-payloadFormatIndicator" class="mqtt-form-row-col2" >
<option value=""></option>
<option value="false" data-i18n="mqtt.label.payloadFormatIndicatorFalse"></option>
<option value="true" data-i18n="mqtt.label.payloadFormatIndicatorTrue"></option>
</select>
</div> -->
<div class="form-row mqtt-form-row-cols2 mqtt5 mqtt5-out">
<label for="node-input-expiry" class="mqtt-form-row-col1"><span data-i18n="mqtt.label.expiry"></span></label>
<input id="node-input-expiry" style="width: calc(100% - 166px);" class="mqtt-form-row-col1" >
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
@ -257,149 +169,6 @@
<div class="form-tips"><span data-i18n="mqtt.tip"></span></div>
</script>
<script type="text/javascript">
RED.nodes.registerType('mqtt out',{
category: 'network',
defaults: {
name: {value:""},
topic: {value:""},
qos: {value:""},
retain: {value:""},
responseTopic: {value:"responseTopic"},
responseTopicType: {value:'none'},
contentType: {value:"contentType"},
contentTypeType: {value:'none'},
userProperties: {value:'userProperties'},
userPropertiesType: {value:'none'},
correlationData: {value:'correlationData'},
correlationDataType: {value:'none'},
messageExpiryInterval: {value:"messageExpiryInterval"},
messageExpiryIntervalType: {value:'none'},
// FUTURE topicAlias: {value:0},
// FUTURE payloadFormatIndicator: {value:false},
// FUTURE subscriptionIdentifier: {value:0},
broker: {type:"mqtt-broker", required:true}
},
color:"#d8bfd8",
inputs:1,
outputs:0,
icon: "bridge.svg",
align: "right",
label: function() {
return this.name||this.topic||"mqtt";
// FUTURE return this.name||this.topic||(this.topicAlias ? "@"+this.topicAlias : "mqtt");
},
oneditprepare: function() {
var typedInputNoneOpt = { value: 'none', label: 'None', hasValue: false };
function showHideDynamicFields() {
var confNode = RED.nodes.node($("#node-input-broker").val());
var v5 = confNode && confNode.protocolVersion == "5";
if(v5) {
$("div.form-row.mqtt5").show();
} else {
$("div.form-row.mqtt5").hide();
}
var t = $("#node-input-responseTopic").typedInput("type");
if (t == 'none') {
$("#node-input-correlationData").parent().hide();
} else {
$("#node-input-correlationData").parent().show();
}
}
$("#node-input-broker").change(function(d){
showHideDynamicFields();
});
var responseTopicTI = $("#node-input-responseTopic").typedInput({
default: 'none',
types: [typedInputNoneOpt, 'msg', 'flow', 'global', 'str'],
typeField: $("#node-input-responseTopicType")
});
var correlationDataTI = $("#node-input-correlationData").typedInput({
default: 'none',
types: [typedInputNoneOpt, 'msg', 'flow', 'global', 'bin'],
typeField: $("#node-input-correlationDataType")
});
//show / hide correlation data depending on responseTopic
responseTopicTI.on("change", function() {
showHideDynamicFields();
});
responseTopicTI.triggerHandler("change");
$("#node-input-userProperties").typedInput({
default: 'none',
types: [typedInputNoneOpt, 'msg', 'flow', 'global', 'json'],
typeField: $("#node-input-userPropertiesType")
});
$("#node-input-messageExpiryInterval").typedInput({
default: 'none',
types: [typedInputNoneOpt, 'msg', 'flow', 'global', 'num'],
typeField: $("#node-input-messageExpiryIntervalType")
});
var makeTypedInputOpt = function(value, label, tooltip){
return {
label: label,
value: value || label,
title: tooltip,
multiple: true
}
}
//TODO: i18n
var commonOpts = [
makeTypedInputOpt("application/gzip", "application/gzip", "GZip Compressed Archive"),
makeTypedInputOpt("application/json", "application/json", "JSON format"),
makeTypedInputOpt("application/octet-stream", "application/octet-stream", "Any kind of binary data"),
makeTypedInputOpt("application/ogg", "application/ogg", "OGG"),
makeTypedInputOpt("application/pdf", "application/pdf", "Adobe Portable Document Format (PDF)"),
makeTypedInputOpt("application/zip", "application/zip", "ZIP archive"),
makeTypedInputOpt("audio/aac", "audio/aac", "AAC audio"),
makeTypedInputOpt("audio/midi", "audio/midi", "Musical Instrument Digital Interface (MIDI)"),
makeTypedInputOpt("audio/x-midi", "audio/x-midi", "Musical Instrument Digital Interface (MIDI)"),
makeTypedInputOpt("audio/mpeg", "audio/mpeg", "MP3 audio"),
makeTypedInputOpt("audio/ogg", "audio/ogg", "OGG audio"),
makeTypedInputOpt("audio/wav", "audio/wav", "Waveform Audio Format"),
makeTypedInputOpt("audio/webm", "audio/webm", "WEBM audio"),
makeTypedInputOpt("image/bmp", "image/bmp", "Windows OS/2 Bitmap Graphics"),
makeTypedInputOpt("image/gif", "image/gif", "Graphics Interchange Format (GIF)"),
makeTypedInputOpt("image/jpeg", "image/jpeg", "JPEG images"),
makeTypedInputOpt("image/png", "image/png", "Portable Network Graphics"),
makeTypedInputOpt("image/tiff", "image/tiff", "Tagged Image File Format (TIFF)"),
makeTypedInputOpt("image/webp", "image/webp", "WEBP image"),
makeTypedInputOpt("text/csv", "text/csv", "Comma-separated values (CSV)"),
makeTypedInputOpt("text/html", "text/html", "HyperText Markup Language (HTML)"),
makeTypedInputOpt("text/plain", "text/plain", "Text, (generally ASCII or ISO 8859-n)"),
makeTypedInputOpt("video/mpeg", "video/mpeg", "MPEG Video"),
makeTypedInputOpt("video/ogg", "video/ogg", "OGG video"),
makeTypedInputOpt("video/webm", "video/webm", "WEBM video"),
];
$("#node-input-contentType").typedInput({
default: 'none',
types: [
typedInputNoneOpt,
{
value: "preset",
label: "Preset",
title: "Preset Content-Types",
icon: "fa fa-list",
showLabel: false,
multiple: false,
options: commonOpts,
default: "application/json"
}, 'msg', 'flow', 'global', 'str'],
typeField: $("#node-input-contentTypeType")
});
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>
<script type="text/html" data-template-name="mqtt-broker">
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
@ -412,35 +181,14 @@
<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="mqtt.label.broker"></span></label>
<input type="text" id="node-config-input-broker" style="width:40%;" data-i18n="[placeholder]mqtt.label.example">
<input type="text" id="node-config-input-broker" style="width: calc(100% - 300px);" data-i18n="[placeholder]mqtt.label.example">
<label for="node-config-input-port" style="margin-left:20px; width:43px; "> <span data-i18n="mqtt.label.port"></span></label>
<input type="text" id="node-config-input-port" data-i18n="[placeholder]mqtt.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="mqtt.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="mqtt.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="mqtt.label.clientid"></span></label>
<input type="text" id="node-config-input-clientid" data-i18n="[placeholder]mqtt.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="mqtt.label.keepalive"></span></label>
<input type="text" id="node-config-input-keepalive" style="width: 50px">
</div>
<div class="form-row mqtt-form-row-cols2">
<label for="node-config-input-cleansession" class="mqtt-form-row-col1"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.session"></span>Session</label>
<div class="mqtt-flag mqtt-form-row-col1" style="display: inline-block;width: calc(35% - 75px);">
<label for="node-config-input-cleansession" style="display: block; padding-left: 15px; text-indent: -15px;">
<input type="checkbox" id="node-config-input-cleansession" style="position: relative;vertical-align: bottom; top: -2px; width: 15px;height: 15px;">
<span data-i18n="mqtt.label.cleansession"></span>
</label>
</div>
<label for="node-config-input-sessionExpiryInterval" class="mqtt-form-row-col2 mqtt5 sessionExpiryInterval"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.sessionExpiryInterval"></span></label>
<input type="number" min="0" id="node-config-input-sessionExpiryInterval" class="mqtt-form-row-col2 mqtt5 sessionExpiryInterval" >
<div class="form-row" style="height: 34px;">
<input type="checkbox" id="node-config-input-usetls" style="height: 34px; margin: 0 0 0 104px; display: inline-block; width: auto; vertical-align: top;">
<label for="node-config-input-usetls" style="width: 80px; line-height: 34px;" data-ai18n="mqtt.label.use-tls">Use TLS</label>
<span id="node-config-row-tls" class="hide"><input style="width: 320px;" type="text" id="node-config-input-tls"></span>
</div>
<div class="form-row">
<label for="node-config-input-protocolVersion"><i class="fa fa-cog"></i> <span data-i18n="mqtt.label.protocolVersion"></span></label>
@ -450,24 +198,34 @@
<option value="5" data-i18n="mqtt.label.protocolVersion5"></option>
</select>
</div>
<!-- FUTURE
<div class="form-row">
<label for="node-config-input-clientid"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.clientid"></span></label>
<input type="text" id="node-config-input-clientid" data-i18n="[placeholder]mqtt.placeholder.clientid">
</div>
<div class="form-row">
<label for="node-config-input-keepalive"><i class="fa fa-heartbeat"></i> <span data-i18n="mqtt.label.keepalive"></span></label>
<input type="number" min="0" id="node-config-input-keepalive" style="width: 100px">
</div>
<div class="form-row" style="margin-bottom:0">
<label style="vertical-align:top;"><i class="fa fa-info"></i> Session</label>
<div style="display: inline-block; width:calc(100% - 110px)">
<div class="form-row">
<label for="node-config-input-cleansession" style="width: auto;">
<input type="checkbox" id="node-config-input-cleansession" style="position: relative;vertical-align: bottom; top: -2px; width: 15px;height: 15px;">
<span id="node-config-input-cleansession-label" data-i18n="mqtt.label.cleansession"></span>
</label>
</div>
<div class="form-row mqtt5">
<label style="width:auto" for="node-config-input-sessionExpiry"><span data-i18n="mqtt.label.sessionExpiry"></span></label>
<input type="number" min="0" id="node-config-input-sessionExpiry" style="width: 100px" >
</div>
</div>
</div>
<div class="form-row mqtt5">
<label for="node-config-input-userProperties"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.userProperties"></span></label>
<input id="node-config-input-userPropertiesType" type="hidden">
<input type="text" id="node-config-input-userProperties" style="width: 70%;" data-i18n="[placeholder]mqtt.label.userProperties" placeholder="">
</div>
<div class="form-row mqtt5">
<label for="node-config-input-topicAliasMaximum"><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.topicAliasMaximum"></span></label>
<input type="number" min="0" id="node-config-input-topicAliasMaximum">
</div>
<div class="form-row mqtt5">
<label for="node-config-input-maximumPacketSize" ><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.maximumPacketSize"></span></label>
<input type="number" min="0" id="node-config-input-maximumPacketSize">
</div>
<div class="form-row mqtt5">
<label for="node-config-input-receiveMaximum" ><i class="fa fa-tag"></i> <span data-i18n="mqtt.label.receiveMaximum"></span></label>
<input type="number" min="0" id="node-config-input-receiveMaximum">
</div> -->
<label style="width: 125px;" for="node-config-input-userProps"><span data-i18n="mqtt.label.userProperties"></span></label>
<input type="text" id="node-config-input-userProps" style="width: calc(100% - 166px);">
</div>
<br>
</div>
<div id="mqtt-broker-tab-security" style="display:none">
<div class="form-row">
@ -504,6 +262,26 @@
<option value="2">2</option>
</select>
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-birth-contentType" data-i18n="mqtt.label.contentType"></label>
<input type="text" style="width:calc(100% - 200px);" id="node-config-input-birth-contentType">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-birth-props" data-i18n="mqtt.label.userProperties"></label>
<input type="text" style="width:calc(100% - 200px);" id="node-config-input-birth-props">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-birth-respTopic"><span data-i18n="mqtt.label.responseTopic"></span></label>
<input type="text" id="node-config-input-birth-respTopic" style="width: calc(100% - 200px);">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-birth-correl"><span data-i18n="mqtt.label.correlationData"></span></label>
<input type="text" id="node-config-input-birth-correl" style="width: calc(100% - 200px);">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-birth-expiry"><span data-i18n="mqtt.label.expiry"></span></label>
<input id="node-config-input-birth-expiry" style="width: calc(100% - 200px);">
</div>
</div>
</div>
<div id="mqtt-broker-section-close">
@ -530,6 +308,26 @@
<option value="2">2</option>
</select>
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-close-contentType" data-i18n="mqtt.label.contentType"></label>
<input type="text" style="width:calc(100% - 200px);" id="node-config-input-close-contentType">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-close-props" data-i18n="mqtt.label.userProperties"></label>
<input type="text" style="width:calc(100% - 200px);" id="node-config-input-close-props">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-close-respTopic"><span data-i18n="mqtt.label.responseTopic"></span></label>
<input type="text" id="node-config-input-close-respTopic" style="width: calc(100% - 200px);">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-close-correl"><span data-i18n="mqtt.label.correlationData"></span></label>
<input type="text" id="node-config-input-close-correl" style="width: calc(100% - 200px);">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-close-expiry"><span data-i18n="mqtt.label.expiry"></span></label>
<input id="node-config-input-close-expiry" style="width: calc(100% - 200px);">
</div>
</div>
</div>
<div id="mqtt-broker-section-will">
@ -556,6 +354,30 @@
<option value="2">2</option>
</select>
</div>
<div class="form-row mqtt5">
<label>Delay</label>
<input type="number" min="0" id="node-config-input-will-delay" style="width: 100px" >
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-will-contentType" data-i18n="mqtt.label.contentType"></label>
<input type="text" style="width:calc(100% - 200px);" id="node-config-input-will-contentType">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-will-props" data-i18n="mqtt.label.userProperties"></label>
<input type="text" style="width:calc(100% - 200px);" id="node-config-input-will-props">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-will-respTopic"><span data-i18n="mqtt.label.responseTopic"></span></label>
<input type="text" id="node-config-input-will-respTopic" style="width: calc(100% - 200px);">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-will-correl"><span data-i18n="mqtt.label.correlationData"></span></label>
<input type="text" id="node-config-input-will-correl" style="width: calc(100% - 200px);">
</div>
<div class="form-row mqtt5 mqtt5-out">
<label for="node-config-input-will-expiry"><span data-i18n="mqtt.label.expiry"></span></label>
<input id="node-config-input-will-expiry" style="width: calc(100% - 200px);">
</div>
</div>
</div>
</div>
@ -563,6 +385,40 @@
</script>
<script type="text/javascript">
(function() {
var typedInputNoneOpt = { value: 'none', label: '', hasValue: false };
var makeTypedInputOpt = function(value){
return {
value: value,
label:value,
hasValue: false
}
}
var contentTypeOpts = [
typedInputNoneOpt,
makeTypedInputOpt("application/json"),
makeTypedInputOpt("application/octet-stream"),
makeTypedInputOpt("text/csv"),
makeTypedInputOpt("text/html"),
makeTypedInputOpt("text/plain"),
{value:"other", label:""}
];
function getDefaultContentType(value) {
var defaultContentType;
var matchedContentType = contentTypeOpts.filter(function(v) {
return v.value === value;
})
if (matchedContentType.length > 0) {
defaultContentType = matchedContentType[0].value;
}
if (value && !defaultContentType) {
defaultContentType = 'other';
}
return defaultContentType || 'none'
}
RED.nodes.registerType('mqtt-broker',{
category: 'config',
defaults: {
@ -588,20 +444,18 @@
birthQos: {value:"0"},
birthRetain: {value:false},
birthPayload: {value:""},
birthMsg: { value: {}},
closeTopic: {value:""},
closeQos: {value:"0"},
closeRetain: {value:false},
closePayload: {value:""},
closeMsg: { value: {}},
willTopic: {value:""},
willQos: {value:"0"},
willRetain: {value:false},
willPayload: {value:""},
sessionExpiryInterval: {value:0},
// FUTURE topicAliasMaximum: {value:0},
// FUTURE maximumPacketSize: {value:0},
// FUTURE receiveMaximum: {value:0},
// FUTURE userPropertiesType: {value:"json"},
// FUTURE userProperties: {value:"{}"},
willMsg: { value: {}},
sessionExpiry: {value:0}
},
credentials: {
user: {type:"text"},
@ -643,8 +497,8 @@
label: this._("mqtt.tabs-label.messages")
});
function setUpSection(sectionId, isExpanded) {
var birthMessageSection = $(sectionId);
function setUpSection(sectionId, v5Opts, isExpanded) {
var birthMessageSection = $("#mqtt-broker-section-"+sectionId);
var paletteHeader = birthMessageSection.find('.red-ui-palette-header');
var twistie = paletteHeader.find('i');
var sectionContent = birthMessageSection.find('.section-content');
@ -659,6 +513,27 @@
toggleSection(!isExpanded);
});
toggleSection(isExpanded);
$("#node-config-input-"+sectionId+"-contentType").val(v5Opts?v5Opts.contentType:"").typedInput({
default: getDefaultContentType(v5Opts?v5Opts.contentType:""),
types: contentTypeOpts,
});
$("#node-config-input-"+sectionId+"-props").val(v5Opts?v5Opts.userProps:"").typedInput({
default: !(v5Opts?v5Opts.userProps:null)? 'none':'json',
types: [typedInputNoneOpt, 'json'],
});
$("#node-config-input-"+sectionId+"-respTopic").val(v5Opts?v5Opts.respTopic:"").typedInput({
default: !(v5Opts?v5Opts.respTopic:null)? 'none':'str',
types: [typedInputNoneOpt, 'str'],
});
$("#node-config-input-"+sectionId+"-correl").val(v5Opts?v5Opts.correl:"").typedInput({
default: !(v5Opts?v5Opts.correl:null)? 'none':'str',
types: [typedInputNoneOpt, 'str'],
});
$("#node-config-input-"+sectionId+"-expiry").val(v5Opts?v5Opts.expiry:"").typedInput({
default: !(v5Opts?v5Opts.expiry:null)? 'none':'num',
types: [typedInputNoneOpt, 'num'],
});
}
// show first section if none are set so the user gets the idea
@ -666,9 +541,13 @@
|| this.willTopic === ""
&& this.birthTopic === ""
&& this.closeTopic == "";
setUpSection('#mqtt-broker-section-birth', showBirthSection);
setUpSection('#mqtt-broker-section-close', this.closeTopic !== "");
setUpSection('#mqtt-broker-section-will', this.willTopic !== "");
setUpSection('birth', this.birthMsg, showBirthSection);
setUpSection('close', this.closeMsg, this.closeTopic !== "");
setUpSection('will', this.willMsg, this.willTopic !== "");
if (this.willMsg) {
$("#node-config-input-will-delay").val(this.willMsg.delay);
}
setTimeout(function() { tabs.resize(); },0);
if (typeof this.cleansession === 'undefined') {
@ -686,30 +565,21 @@
if (typeof this.protocolVersion === 'undefined') {
this.protocolVersion = 4;
}
function updateSessionExpiryIntervalVisibility() {
var hide = ($("#node-config-input-cleansession").is(":checked") || $("#node-config-input-protocolVersion").val() != "5");
if(hide) {
$(".sessionExpiryInterval").hide();
} else {
$(".sessionExpiryInterval").show();
}
}
$("#node-config-input-protocolVersion").change(function(){
$("#node-config-input-protocolVersion").on("change", function() {
var v5 = $("#node-config-input-protocolVersion").val() == "5";
if(v5) {
$("#node-config-input-cleansession-label").text(RED._("node-red:mqtt.label.cleanstart"))
$("div.form-row.mqtt5").show();
} else {
$("#node-config-input-cleansession-label").text(RED._("node-red:mqtt.label.cleansession"))
$("div.form-row.mqtt5").hide();
}
updateSessionExpiryIntervalVisibility();
});
$("#node-config-input-protocolVersion").val(this.protocolVersion);
// FUTURE
// $("#node-config-input-userProperties").typedInput({
// default: "json",
// types:['json'],
// typeField: $("#node-config-input-userPropertiesType")
// });
$("#node-config-input-userProps").typedInput({
default: !this.userProps ? 'none':'json',
types: [typedInputNoneOpt, 'json']
});
if (typeof this.keepalive === 'undefined') {
this.keepalive = 15;
$("#node-config-input-keepalive").val(this.keepalive);
@ -727,6 +597,7 @@
$("#node-config-input-willQos").val("0");
}
function updateTLSOptions() {
if ($("#node-config-input-usetls").is(':checked')) {
$("#node-config-row-tls").show();
@ -745,8 +616,7 @@
} else {
$("#node-config-input-clientid").attr("placeholder",node._("mqtt.placeholder.clientid-nonclean"));
}
updateSessionExpiryIntervalVisibility();
$("#node-config-input-clientid").change();
$("#node-config-input-clientid").trigger("change");
}
setTimeout(updateClientId,0);
$("#node-config-input-cleansession").on("click",function() {
@ -773,12 +643,198 @@
updatePortEntry();
});
setTimeout(updatePortEntry,50);
setTimeout(function() {
$("#node-config-input-protocolVersion").trigger("change");
},50);
},
oneditsave: function() {
if (!$("#node-config-input-usetls").is(':checked')) {
$("#node-config-input-tls").val("");
}
var v5 = $("#node-config-input-protocolVersion").val() == "5";
function saveV5Message(section) {
var msg = {};
if ($("#node-config-input-"+section+"Topic").val().trim().length > 0) {
var contentType = $("#node-config-input-"+section+"-contentType").val().trim();
if (contentType === '') {
contentType = $("#node-config-input-"+section+"-contentType").typedInput('type');
if (contentType === 'none' || contentType === 'other') {
contentType = "";
}
}
if (contentType) {
msg.contentType = contentType;
}
var props = $("#node-config-input-"+section+"-props").val().trim();
if (props) {
msg.userProps = props;
}
var resp = $("#node-config-input-"+section+"-respTopic").val().trim();
if (props) {
msg.respTopic = resp;
}
var correl = $("#node-config-input-"+section+"-correl").val().trim();
if (correl) {
msg.correl = correl;
}
var expiry = $("#node-config-input-"+section+"-expiry").val().trim();
if (expiry) {
msg.expiry = expiry;
}
}
return msg;
}
if (v5) {
this.birthMsg = saveV5Message("birth");
this.closeMsg = saveV5Message("close");
this.willMsg = saveV5Message("will");
var willDelay = $("#node-config-input-will-delay").val();
if (willDelay) {
this.willMsg.delay = willDelay;
}
} else {
this.willMsg = {};
this.birthMsg = {};
this.closeMsg = {};
}
}
});
RED.nodes.registerType('mqtt in',{
category: 'network',
defaults: {
name: {value:""},
topic: {value:"",required:true,validate: RED.validators.regex(/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/)},
qos: {value: "2"},
datatype: {value:"auto",required:true},
broker: {type:"mqtt-broker", required:true},
// subscriptionIdentifier: {value:0},
nl: {value:false},
rap: {value:true},
rh: {value:0},
},
color:"#d8bfd8",
inputs:0,
outputs:1,
icon: "bridge.svg",
label: function() {
return this.name||this.topic||"mqtt";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$("#node-input-broker").on("change",function(d){
var confNode = RED.nodes.node($("#node-input-broker").val());
var v5 = confNode && confNode.protocolVersion == "5";
if(v5) {
$("div.form-row.mqtt5").show();
} else {
$("div.form-row.mqtt5").hide();
}
});
if (this.qos === undefined) {
$("#node-input-qos").val("2");
}
if (this.datatype === undefined) {
$("#node-input-datatype").val("auto");
}
}
});
RED.nodes.registerType('mqtt out',{
category: 'network',
defaults: {
name: {value:""},
topic: {value:""},
qos: {value:""},
retain: {value:""},
respTopic: {value:""},
contentType: {value:""},
userProps: {value:''},
correl: {value:''},
expiry: {value:''},
broker: {type:"mqtt-broker", required:true}
},
color:"#d8bfd8",
inputs:1,
outputs:0,
icon: "bridge.svg",
align: "right",
label: function() {
return this.name||this.topic||"mqtt";
},
oneditprepare: function() {
var that = this;
function showHideDynamicFields() {
var confNode = RED.nodes.node($("#node-input-broker").val());
var v5 = confNode && confNode.protocolVersion == "5";
if(v5) {
$("div.form-row.mqtt5").show();
var t = $("#node-input-respTopic").typedInput("type");
if (t == 'none') {
$("#node-input-correl").parent().hide();
} else {
$("#node-input-correl").parent().show();
}
} else {
$("div.form-row.mqtt5").hide();
}
}
$("#node-input-broker").on("change",function(d){
showHideDynamicFields();
});
var respTopicTI = $("#node-input-respTopic").typedInput({
default: !this.respTopic ? 'none':'str',
types: [typedInputNoneOpt, 'str'],
});
var correlTI = $("#node-input-correl").typedInput({
default: !this.correl ? 'none':'str',
types: [typedInputNoneOpt, 'str']
});
//show / hide correlation data depending on respTopic
respTopicTI.on("change", showHideDynamicFields);
respTopicTI.triggerHandler("change");
$("#node-input-userProps").typedInput({
default: !this.userProps ? 'none':'json',
types: [typedInputNoneOpt, 'json'],
});
$("#node-input-expiry").typedInput({
default: !this.expiry ? 'none':'num',
types: [typedInputNoneOpt, 'num']
});
$("#node-input-contentType").on('change', function (event, type, value, urg) {
console.log(event);
console.log("ct change",type,value, urg);
}).typedInput({
default: getDefaultContentType(this.contentType),
types: contentTypeOpts
})
},
oneditsave: function() {
var contentType = $("#node-input-contentType").val().trim();
if (contentType === '') {
contentType = $("#node-input-contentType").typedInput('type');
if (contentType === 'none' || contentType === 'other') {
contentType = "";
}
}
$("#node-input-contentType").val(contentType)
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
})();
</script>

View File

@ -14,8 +14,6 @@
* limitations under the License.
**/
const { debug } = require("console");
module.exports = function(RED) {
"use strict";
var mqtt = require("mqtt");
@ -55,8 +53,8 @@ module.exports = function(RED) {
* @param {integer} [def] An optional default to set in the destination object if prop is NOT present in the soruce object
*/
function setIntProp(src, dst, propName, minVal, maxVal, def) {
if (src.hasOwnProperty(propName)) {
var v = parseInt(src[propName]);
if (src.hasOwnProperty(propName)) {
var v = parseInt(src[propName]);
if(isNaN(v)) return;
if(minVal != null) {
if(v < minVal) return;
@ -69,7 +67,7 @@ module.exports = function(RED) {
if(def != undefined) dst[propName] = def;
}
}
/**
* Helper function for setting string property values in the MQTT V5 properties object
* @param {object} src Source object containing properties
@ -78,7 +76,7 @@ module.exports = function(RED) {
* @param {string} [def] An optional default to set in the destination object if prop is NOT present in the soruce object
*/
function setStrProp(src, dst, propName, def) {
if (src[propName] && typeof src[propName] == "string") {
if (src[propName] && typeof src[propName] == "string") {
dst[propName] = src[propName];
} else {
if(def != undefined) dst[propName] = def;
@ -93,7 +91,7 @@ module.exports = function(RED) {
* @param {boolean} [def] An optional default to set in the destination object if prop is NOT present in the soruce object
*/
function setBoolProp(src, dst, propName, def) {
if (src[propName] != null) {
if (src[propName] != null) {
if(src[propName] === "true" || src[propName] === true) {
dst[propName] = true;
} else if(src[propName] === "false" || src[propName] === false) {
@ -105,14 +103,14 @@ module.exports = function(RED) {
}
/**
* Helper function for copying the MQTT v5 srcUserProperties object (parameter1) to the properties object (parameter2).
* Helper function for copying the MQTT v5 srcUserProperties object (parameter1) to the properties object (parameter2).
* Any property in srcUserProperties that is NOT a key/string pair will be silently discarded.
* NOTE: if no sutable properties are present, the userProperties object will NOT be added to the properties object
* @param {object} srcUserProperties An object with key/value string pairs
* @param {object} properties A properties object in which userProperties will be copied to
*/
function setUserProperties(srcUserProperties, properties) {
if (srcUserProperties && typeof srcUserProperties == "object") {
if (srcUserProperties && typeof srcUserProperties == "object") {
let _clone = {};
let count = 0;
let keys = Object.keys(srcUserProperties);
@ -129,7 +127,7 @@ module.exports = function(RED) {
}
/**
* Helper function for copying the MQTT v5 buffer type properties
* Helper function for copying the MQTT v5 buffer type properties
* NOTE: if src[propName] is not a buffer, dst[propName] will NOT be assigned a value (unless def is set)
* @param {object} src Source object containing properties
* @param {object} dst Destination object to set/add properties
@ -140,7 +138,7 @@ module.exports = function(RED) {
if(!dst) return;
if (src && dst) {
var buf = src[propName];
if (buf && typeof Buffer.isBuffer(buf)) {
if (buf && typeof Buffer.isBuffer(buf)) {
dst[propName] = Buffer.from(buf);
}
} else {
@ -162,7 +160,7 @@ module.exports = function(RED) {
this.protocolVersion = n.protocolVersion;
this.keepalive = n.keepalive;
this.cleansession = n.cleansession;
this.sessionExpiryInterval = n.sessionExpiryInterval;
this.sessionExpiryInterval = n.sessionExpiry;
this.topicAliasMaximum = n.topicAliasMaximum;
this.maximumPacketSize = n.maximumPacketSize;
this.receiveMaximum = n.receiveMaximum;
@ -186,6 +184,20 @@ module.exports = function(RED) {
retain: n.birthRetain=="true"|| n.birthRetain===true,
//TODO: add payloadFormatIndicator, messageExpiryInterval, contentType, responseTopic, correlationData, userProperties
};
if (n.birthMsg) {
setStrProp(n.birthMsg, this.birthMessage, "contentType");
if(n.birthMsg.userProps && /^ *{/.test(n.birthMsg.userProps)) {
try {
this.birthMessage.userProperties = JSON.parse(n.birthMsg.userProps)
} catch(err) {}
}
n.birthMsg.responseTopic = n.birthMsg.respTopic;
setStrProp(n.birthMsg, this.birthMessage, "responseTopic");
n.birthMsg.correlationData = n.birthMsg.correl;
setBufferProp(n.birthMsg, this.birthMessage, "correlationData");
n.birthMsg.messageExpiryInterval = n.birthMsg.expiry
setIntProp(n.willMsg,this.birthMessage, "messageExpiryInterval")
}
}
if (n.closeTopic) {
@ -196,6 +208,20 @@ module.exports = function(RED) {
retain: n.closeRetain=="true"|| n.closeRetain===true,
//TODO: add payloadFormatIndicator, messageExpiryInterval, contentType, responseTopic, correlationData, userProperties
};
if (n.closeMsg) {
setStrProp(n.closeMsg, this.closeMessage, "contentType");
if(n.closeMsg.userProps && /^ *{/.test(n.closeMsg.userProps)) {
try {
this.closeMessage.userProperties = JSON.parse(n.closeMsg.userProps)
} catch(err) {}
}
n.closeMsg.responseTopic = n.closeMsg.respTopic;
setStrProp(n.closeMsg, this.closeMessage, "responseTopic");
n.closeMsg.correlationData = n.closeMsg.correl;
setBufferProp(n.closeMsg, this.closeMessage, "correlationData");
n.closeMsg.messageExpiryInterval = n.closeMsg.expiry
setIntProp(n.willMsg,this.closeMessage, "messageExpiryInterval")
}
}
if (this.credentials) {
@ -302,16 +328,14 @@ module.exports = function(RED) {
this.options.properties = {};
this.options.properties.requestResponseInformation = true;
this.options.properties.requestProblemInformation = true;
if(this.userProperties) {
let userProperties = RED.util.evaluateNodeProperty(this.userProperties, this.userPropertiesType, this, {});
setUserProperties(userProperties, this.options.properties);
if(this.userProperties && /^ *{/.test(this.userProperties)) {
try {
setUserProperties(JSON.parse(this.userProperties), this.options.properties);
} catch(err) {}
}
if (this.sessionExpiryInterval && this.sessionExpiryInterval !== "0") {
setIntProp(this,this.options.properties,"sessionExpiryInterval");
}
setIntProp(this,this.options.properties,"sessionExpiryInterval", 0);
setIntProp(this,this.options.properties,"topicAliasMaximum", 0);
setIntProp(this,this.options.properties,"maximumPacketSize", 0);
setIntProp(this,this.options.properties,"receiveMaximum", 0);
setStrProp(this,this.options.properties,"authenticationMethod"); //TODO: authenticationMethod: the name of the authentication method used for extended authentication string,
setBufferProp(this,this.options.properties,"authenticationData"); //TODO: authenticationData: Binary Data containing authentication data
}
if (this.usetls && n.tls) {
var tlsNode = RED.nodes.getNode(n.tls);
@ -319,7 +343,6 @@ module.exports = function(RED) {
tlsNode.addTLSOptions(this.options);
}
}
// console.log(this.brokerurl,this.options);
// If there's no rejectUnauthorized already, then this could be an
// old config where this option was provided on the broker node and
@ -336,8 +359,29 @@ module.exports = function(RED) {
retain: n.willRetain=="true"|| n.willRetain===true,
//TODO: add willDelayInterval, payloadFormatIndicator, messageExpiryInterval, contentType, responseTopic, correlationData, userProperties
};
if (n.willMsg) {
this.options.will.properties = {};
setStrProp(n.willMsg, this.options.will.properties, "contentType");
if(n.willMsg.userProps && /^ *{/.test(n.willMsg.userProps)) {
try {
this.options.will.properties.userProperties = JSON.parse(n.willMsg.userProps)
} catch(err) {}
}
n.willMsg.responseTopic = n.willMsg.respTopic;
setStrProp(n.willMsg, this.options.will.properties, "responseTopic");
n.willMsg.correlationData = n.willMsg.correl;
setBufferProp(n.willMsg, this.options.will.properties, "correlationData");
n.willMsg.willDelayInterval = n.willMsg.delay
setIntProp(n.willMsg,this.options.will.properties, "willDelayInterval")
n.willMsg.messageExpiryInterval = n.willMsg.expiry
setIntProp(n.willMsg,this.options.will.properties, "messageExpiryInterval")
this.options.will.payloadFormatIndicator = true;
}
}
// console.log(this.brokerurl,this.options);
// Define functions called by MQTT in and out nodes
var node = this;
this.users = {};
@ -376,26 +420,27 @@ module.exports = function(RED) {
node.client.on('connect', function (connack) {
node.connecting = false;
node.connected = true;
node.topicAliases = {};
node.log(RED._("mqtt.state.connected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
if(node.options.protocolVersion == 5 && connack && connack.hasOwnProperty("properties")) {
if(typeof connack.properties == "object") {
//clean & assign all props sent from server.
setIntProp(connack.properties, node.serverProperties, "topicAliasMaximum", 1);
setIntProp(connack.properties, node.serverProperties, "topicAliasMaximum", 0);
setIntProp(connack.properties, node.serverProperties, "receiveMaximum", 0);
setIntProp(connack.properties, node.serverProperties, "sessionExpiryInterval", 0, 0xFFFFFFFF);
setIntProp(connack.properties, node.serverProperties, "maximumQoS", 0, 2);
setBoolProp(connack.properties, node.serverProperties, "retainAvailable");
setBoolProp(connack.properties, node.serverProperties, "wildcardSubscriptionAvailable");
setBoolProp(connack.properties, node.serverProperties, "subscriptionIdentifiersAvailable");
setBoolProp(connack.properties, node.serverProperties, "retainAvailable",true);
setBoolProp(connack.properties, node.serverProperties, "wildcardSubscriptionAvailable", true);
setBoolProp(connack.properties, node.serverProperties, "subscriptionIdentifiersAvailable", true);
setBoolProp(connack.properties, node.serverProperties, "sharedSubscriptionAvailable");
setIntProp(connack.properties, node.serverProperties, "maximumPacketSize", 0);
setIntProp(connack.properties, node.serverProperties, "serverKeepAlive");
setStrProp(connack.properties, node.serverProperties, "responseInformation");
setStrProp(connack.properties, node.serverProperties, "serverReference");
setStrProp(connack.properties, node.serverProperties, "assignedClientIdentifier");
setStrProp(connack.properties, node.serverProperties, "reasonString");
setIntProp(connack.properties, node.serverProperties, "maximumPacketSize", 0);
setIntProp(connack.properties, node.serverProperties, "serverKeepAlive");
setStrProp(connack.properties, node.serverProperties, "responseInformation");
setStrProp(connack.properties, node.serverProperties, "serverReference");
setStrProp(connack.properties, node.serverProperties, "assignedClientIdentifier");
setStrProp(connack.properties, node.serverProperties, "reasonString");
setUserProperties(connack.properties, node.serverProperties);
debug("MQTTBrokerNode: ⬆ CONNECTED. node.serverProperties ==> ", node.serverProperties );//TODO: remove
// node.debug("CONNECTED. node.serverProperties ==> "+JSON.stringify(node.serverProperties));//TODO: remove
}
}
for (var id in node.users) {
@ -416,12 +461,10 @@ module.exports = function(RED) {
if (node.subscriptions[s].hasOwnProperty(r)) {
qos = Math.max(qos,node.subscriptions[s][r].qos);
_options = node.subscriptions[s][r].options;
debug(`MQTTBrokerNode:${node.id}: Re-subscribe - registering handler ref ${r} for ${s} `);//TODO: remove
node.client.on('message',node.subscriptions[s][r].handler);
}
}
_options.qos = _options.qos || qos;
debug(`MQTTBrokerNode:${node.id}: Re-subscribe - subscribing to topic '${topic}'`, _options);//TODO: remove
node.client.subscribe(topic, _options);
}
}
@ -432,7 +475,6 @@ module.exports = function(RED) {
}
});
node.client.on("reconnect", function() {
debug('MQTTBrokerNode reconnect event'); //TODO: remove
for (var id in node.users) {
if (node.users.hasOwnProperty(id)) {
node.users[id].status({fill:"yellow",shape:"ring",text:"node-red:common.status.connecting"});
@ -442,14 +484,12 @@ module.exports = function(RED) {
//TODO: what to do with this event? Anything? Necessary?
node.client.on("disconnect", function(packet) {
//Emitted after receiving disconnect packet from broker. MQTT 5.0 feature.
debug('MQTTBrokerNode disconnect event', packet); //TODO: remove //TODO: remove
var rc = packet && packet.properties && packet.properties.reasonString;
var rc = packet && packet.properties && packet.reasonCode;
//TODO: If keeping this event, do we use these? log these?
});
// Register disconnect handlers
node.client.on('close', function () {
debug('MQTTBrokerNode close event', arguments); //TODO: remove
if (node.connected) {
node.connected = false;
node.log(RED._("mqtt.state.disconnected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
@ -466,7 +506,6 @@ module.exports = function(RED) {
// Register connect error handler
// The client's own reconnect logic will take care of errors
node.client.on('error', function (error) {
debug('MQTTBrokerNode error event', error); //TODO: remove
});
}catch(err) {
console.log(err);
@ -474,6 +513,8 @@ module.exports = function(RED) {
}
};
this.subscriptionIds = {};
this.subid = 1;
this.subscribe = function (topic,options,callback,ref) {
ref = ref||0;
var qos;
@ -484,27 +525,33 @@ module.exports = function(RED) {
options = {};
}
options.qos = qos;
if (!node.subscriptionIds[topic]) {
node.subscriptionIds[topic] = node.subid++;
}
options.properties = options.properties || {};
options.properties.subscriptionIdentifier = node.subscriptionIds[topic];
node.subscriptions[topic] = node.subscriptions[topic]||{};
var sub = {
topic:topic,
qos:qos,
options:options,
handler:function(mtopic,mpayload, mpacket) {
debug(`MQTTBrokerNode:${node.id}: this.subscribe.handler - attempting to match '${topic}' to '${mtopic}' `, mpacket); //TODO: remove
if(mpacket.properties && options.properties && mpacket.properties.subscriptionIdentifier && options.properties.subscriptionIdentifier && (mpacket.properties.subscriptionIdentifier !== options.properties.subscriptionIdentifier) ) {
//do nothing as subscriptionIdentifier does not match
debug(`MQTTBrokerNode:${node.id}: > no match - this nodes subID (${options.properties.subscriptionIdentifier}) !== packet subID (${mpacket.properties.subscriptionIdentifier})`); //TODO: remove
// node.debug(`> no match - this nodes subID (${options.properties.subscriptionIdentifier}) !== packet subID (${mpacket.properties.subscriptionIdentifier})`); //TODO: remove
} else if (matchTopic(topic,mtopic)) {
debug(`MQTTBrokerNode:${node.id}: > MATCHED '${topic}' to '${mtopic}' - performing callback`); //TODO: remove
// node.debug(`> MATCHED '${topic}' to '${mtopic}' - performing callback`); //TODO: remove
callback(mtopic,mpayload, mpacket);
} else
debug(`MQTTBrokerNode:${node.id}: > no match / no callback`); //TODO: remove
} else {
// node.debug(`> no match / no callback`); //TODO: remove
}
},
ref: ref
};
node.subscriptions[topic][ref] = sub;
if (node.connected) {
debug(`MQTTBrokerNode:${node.id}: this.subscribe - registering handler ref ${ref} for ${topic} and subscribing`, options); //TODO: remove
// node.debug(`this.subscribe - registering handler ref ${ref} for ${topic} and subscribing `+JSON.stringify(options)); //TODO: remove
node.client.on('message',sub.handler);
node.client.subscribe(topic, options);
}
@ -513,33 +560,35 @@ module.exports = function(RED) {
this.unsubscribe = function (topic, ref, removed) {
ref = ref||0;
var sub = node.subscriptions[topic];
var _debug = `MQTTBrokerNode ${node.id}: unsubscribe for topic ${topic} called... ` ; //TODO: remove
// var _debug = `unsubscribe for topic ${topic} called... ` ; //TODO: remove
if (sub) {
_debug += "sub found. " //TODO: remove
// _debug += "sub found. " //TODO: remove
if (sub[ref]) {
// debug(`MQTTBrokerNode:${node.id}: this.unsubscribe - removing handler ref ${ref} for ${topic} `); //TODO: remove
_debug += `removing handler ref ${ref} for ${topic}. `
// debug(`this.unsubscribe - removing handler ref ${ref} for ${topic} `); //TODO: remove
// _debug += `removing handler ref ${ref} for ${topic}. `
node.client.removeListener('message',sub[ref].handler);
delete sub[ref];
}
//TODO: Review. The `if(removed)` was commented out to always delete and remove subscriptions.
//TODO: Review. The `if(removed)` was commented out to always delete and remove subscriptions.
// if we dont then property changes dont get applied and old subs still trigger
//if (removed) {
if (Object.keys(sub).length === 0) {
delete node.subscriptions[topic];
delete node.subscriptionIds[topic];
if (node.connected) {
_debug += `calling client.unsubscribe to remove topic ${topic}` //TODO: remove
// _debug += `calling client.unsubscribe to remove topic ${topic}` //TODO: remove
node.client.unsubscribe(topic);
}
}
//}
} else {
_debug += "sub not found! "; //TODO: remove
// _debug += "sub not found! "; //TODO: remove
}
debug(_debug); //TODO: remove
// node.debug(_debug); //TODO: remove
};
this.topicAliases = {};
this.publish = function (msg,done) {
if (node.connected) {
@ -562,17 +611,29 @@ module.exports = function(RED) {
setStrProp(msg, options.properties, "responseTopic");
setBufferProp(msg, options.properties, "correlationData");
setStrProp(msg, options.properties, "contentType");
setIntProp(msg, options.properties, "messageExpiryInterval", 0);
setIntProp(msg, options.properties, "messageExpiryInterval", 0);
if (msg.userProperties) {
options.properties.userProperties = msg.userProperties;
}
setUserProperties(msg.userProperties, options.properties);
setIntProp(msg, options.properties, "topicAlias", 1, node.serverProperties.topicAliasMaximum || 0);
setBoolProp(msg, options.properties, "payloadFormatIndicator");
setBoolProp(msg, options.properties, "payloadFormatIndicator");
//FUTURE setIntProp(msg, options.properties, "subscriptionIdentifier", 1, 268435455);
if (options.properties.topicAlias) {
if (!node.topicAliases.hasOwnProperty(options.properties.topicAlias) && msg.topic == "") {
done("Invalid topicAlias");
return
}
if (node.topicAliases[options.properties.topicAlias] === msg.topic) {
msg.topic = ""
} else {
node.topicAliases[options.properties.topicAlias] = msg.topic
}
}
}
// debug(`MQTTBrokerNode:${node.id}: publish - sending payload to ${msg.topic ? msg.topic : (msg.topicAlias ? 'topicAlias-'+msg.topicAlias : '???') } `, options);//TODO: remove
debug(`MQTTBrokerNode:${node.id}: publish - sending payload to ${msg.topic} `, options);//TODO: remove
node.client.publish(msg.topic, msg.payload, options, function(err) {
if(err) node.error(err,msg);//catch errors
done && done();
done && done(err);
return
});
}
@ -611,8 +672,6 @@ module.exports = function(RED) {
this.topic = n.topic;
this.qos = parseInt(n.qos);
this.subscriptionIdentifier = n.subscriptionIdentifier;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901117
this.userProperties = n.userProperties;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901116
this.userPropertiesType = n.userPropertiesType;
this.nl = n.nl;
this.rap = n.rap;
this.rh = n.rh;
@ -635,21 +694,21 @@ module.exports = function(RED) {
node.brokerConn.register(this);
let options = { qos: this.qos };
if(v5) {
options.properties = {};
if(node.userProperties) {
let userProperties = RED.util.evaluateNodeProperty(node.userProperties, node.userPropertiesType, node, {});
setUserProperties(userProperties, options.properties);
}
setIntProp(node,options.properties,"subscriptionIdentifier", 1);
// options.properties = {};
// if(node.userProperties) {
// let userProperties = RED.util.evaluateNodeProperty(node.userProperties, node.userPropertiesType, node, {});
// setUserProperties(userProperties, options.properties);
// }
// setIntProp(node,options.properties,"subscriptionIdentifier", 1);
setIntProp(node, options, "rh");
if(node.nl === "true" || node.nl === true) options.nl = true;
else if(node.nl === "false" || node.nl === false) options.nl = false;
if(node.rap === "true" || node.rap === true) options.rap = true;
else if(node.rap === "false" || node.rap === false) options.rap = false;
}
this.brokerConn.subscribe(this.topic,options,function(topic,payload,packet) {
debug(`MQTTInNode:${node.id}: Broker sent ${topic}, datatype ${node.datatype}`, packet);//TODO: remove
// node.debug(`Sent ${topic}, datatype ${node.datatype} `+JSON.stringify(packet));//TODO: remove
if (node.datatype === "buffer") {
// payload = payload;
} else if (node.datatype === "base64") {
@ -671,12 +730,12 @@ module.exports = function(RED) {
//msg.properties = packet.properties;
setStrProp(packet.properties, msg, "responseTopic");
setBufferProp(packet.properties, msg, "correlationData");
setStrProp(packet.properties, msg, "contentType");
setStrProp(packet.properties, msg, "contentType");
// setIntProp(packet.properties, msg, "topicAlias", 1, node.brokerConn.serverProperties.topicAliasMaximum || 0);
// setIntProp(packet.properties, msg, "subscriptionIdentifier", 1, 268435455);
setIntProp(packet.properties, msg, "messageExpiryInterval", 0);
setBoolProp(packet.properties, msg, "payloadFormatIndicator");
setStrProp(packet.properties, msg, "reasonString");
setIntProp(packet.properties, msg, "messageExpiryInterval", 0);
setBoolProp(packet.properties, msg, "payloadFormatIndicator");
setStrProp(packet.properties, msg, "reasonString");
setUserProperties(packet.properties.userProperties, msg);
}
if ((node.brokerConn.broker === "localhost")||(node.brokerConn.broker === "127.0.0.1")) {
@ -709,20 +768,19 @@ module.exports = function(RED) {
this.qos = n.qos || null;
this.retain = n.retain;
this.broker = n.broker;
this.responseTopic = n.responseTopic;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901114
this.responseTopicType = n.responseTopicType;
this.correlationData = n.correlationData;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901115
this.correlationDataType = n.correlationDataType;
this.responseTopic = n.respTopic;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901114
this.correlationData = n.correl;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901115
this.contentType = n.contentType;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901118
this.contentTypeType = n.contentTypeType;
this.messageExpiryInterval = n.messageExpiryInterval; //https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901112
this.messageExpiryIntervalType = n.messageExpiryIntervalType;
this.userProperties = n.userProperties;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901116
this.userPropertiesType = n.userPropertiesType;
this.messageExpiryInterval = n.expiry; //https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901112
try {
if (/^ *{/.test(n.userProps)) {
this.userProperties = JSON.parse(n.userProps);//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901116
}
} catch(err) {}
// this.topicAlias = n.topicAlias; //https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113
// this.payloadFormatIndicator = n.payloadFormatIndicator; //https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901111
// this.subscriptionIdentifier = n.subscriptionIdentifier;//https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901117
this.brokerConn = RED.nodes.getNode(this.broker);
var node = this;
var chk = /[\+#]/;
@ -744,25 +802,20 @@ module.exports = function(RED) {
let msgPropOverride = function(propName) { if(node[propName]) { msg[propName] = node[propName]; } }
msgPropOverride("topic");
if(v5) {
if(node.userProperties && node.userPropertiesType !== "none") {
let userProperties = RED.util.evaluateNodeProperty(node.userProperties, node.userPropertiesType, node, msg);
if(userProperties) msg.userProperties = userProperties;
if(node.userProperties) {
msg.userProperties = node.userProperties;
}
if(node.responseTopic && node.responseTopicType !== "none") {
let responseTopic = RED.util.evaluateNodeProperty(node.responseTopic, node.responseTopicType, node, msg);
if(responseTopic) msg.responseTopic = responseTopic;
if(node.responseTopic) {
msg.responseTopic = node.responseTopic;
}
if(node.correlationData && node.correlationDataType !== "none") {
let correlationData = RED.util.evaluateNodeProperty(node.correlationData, node.correlationDataType, node, msg);
if(correlationData) msg.correlationData = correlationData;
if(node.correlationData) {
msg.correlationData = node.correlationData;
}
if(node.contentType && node.contentTypeType !== "none") {
let contentType = RED.util.evaluateNodeProperty(node.contentType, node.contentTypeType, node, msg);
if(contentType) msg.contentType = contentType;
if(node.contentType) {
msg.contentType = node.contentType;
}
if(node.messageExpiryInterval && node.messageExpiryIntervalType !== "none") {
let messageExpiryInterval = RED.util.evaluateNodeProperty(node.messageExpiryInterval, node.messageExpiryIntervalType, node, msg);
if(messageExpiryInterval) msg.messageExpiryInterval = messageExpiryInterval;
if(node.messageExpiryInterval) {
msg.messageExpiryInterval = node.messageExpiryInterval;
}
//Next, update/override the msg.xxxx properties from node config
//TODO: Should we be expecting msg.properties.xxxx instead of msg.xxxx?
@ -774,11 +827,18 @@ module.exports = function(RED) {
//FUTURE setBoolProp(node,msg,"payloadFormatIndicator");
//FUTURE setIntProp(node,msg,"subscriptionIdentifier");
}
if (msg.userProperties && typeof msg.userProperties !== "object") {
delete msg.userProperties;
}
if (msg.hasOwnProperty("topicAlias") && !isNaN(msg.topicAlias) && (msg.topicAlias === 0 || node.brokerConn.serverProperties.topicAliasMaximum === 0 || msg.topicAlias > node.brokerConn.serverProperties.topicAliasMaximum)) {
delete msg.topicAlias;
}
if ( msg.hasOwnProperty("payload")) {
let topicOK = msg.hasOwnProperty("topic") && (typeof msg.topic === "string") && (msg.topic !== "");
if (!topicOK && v5) {
//NOTE: A value of 0 (in server props topicAliasMaximum) indicates that the Server does not accept any Topic Aliases on this connection
if (msg.hasOwnProperty("topicAlias") && typeof msg.topicAlias === "number" && msg.topicAlias >= 0 && node.brokerConn.serverProperties.topicAliasMaximum && node.brokerConn.serverProperties.topicAliasMaximum >= msg.topicAlias) {
if (msg.hasOwnProperty("topicAlias") && !isNaN(msg.topicAlias) && msg.topicAlias >= 0 && node.brokerConn.serverProperties.topicAliasMaximum && node.brokerConn.serverProperties.topicAliasMaximum >= msg.topicAlias) {
topicOK = true;
msg.topic = ""; //must be empty string
} else if (msg.hasOwnProperty("responseTopic") && (typeof msg.responseTopic === "string") && (msg.responseTopic !== "")) {
@ -789,11 +849,11 @@ module.exports = function(RED) {
}
}
if (topicOK) { // topic must exist
debug(`MQTTOutNode:${node.id}: sending msg to ${msg.topic}`, msg);//TODO: remove
this.brokerConn.publish(msg, function(){
// node.debug(`sending msg to ${msg.topic} `+JSON.stringify(msg));//TODO: remove
this.brokerConn.publish(msg, function(err) {
let args = arguments;
let l = args.length;
done();
done(err);
}); // send the message
} else {
node.warn(RED._("mqtt.errors.invalid-topic"));

View File

@ -350,8 +350,9 @@
"retain": "Retain",
"clientid": "Client ID",
"port": "Port",
"keepalive": "Keepalive time(s)",
"keepalive": "Keep Alive",
"cleansession": "Use clean session",
"cleanstart": "Use clean start",
"use-tls": "Enable secure (SSL/TLS) connection",
"tls-config":"TLS Configuration",
"verify-server-cert":"Verify server certificate",
@ -361,23 +362,23 @@
"flags": "Flags",
"nl": "Do not receive messages published by this client",
"rap": "Keep retain flag of original publish",
"rh": "Retain Handling",
"rh0": "0, Always send retained messages",
"rh1": "1, Send retained messages for new subscription",
"rh2": "2, Never send retained messages",
"rh": "Retained message handling ",
"rh0": "Send retained messages",
"rh1": "Only send for new subscriptions",
"rh2": "Do not send",
"responseTopic": "Response topic",
"contentType": "Content Type",
"correlationData": "Correlation Data",
"messageExpiryInterval": "Expiry (secs)",
"expiry": "Expiry (secs)",
"sessionExpiry": "Session Expiry (secs)",
"topicAlias": "Alias",
"payloadFormatIndicator": "Format",
"payloadFormatIndicatorFalse": "unspecified bytes (Default)",
"payloadFormatIndicatorTrue": "UTF-8 encoded payload",
"protocolVersion": "Protocol Version",
"protocolVersion": "Protocol",
"protocolVersion3": "MQTT V3.1 (legacy)",
"protocolVersion4": "MQTT V3.1.1",
"protocolVersion5": "MQTT V5",
"sessionExpiryInterval": "Expiry(s)",
"topicAliasMaximum": "Alias Max",
"maximumPacketSize": "Max Packet Size",
"receiveMaximum": "Receive Max"
@ -988,4 +989,4 @@
"unexpected" : "unexpected mode",
"no-parts" : "no parts property in message"
}
}
}

View File

@ -23,6 +23,17 @@
<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>
<dt class="optional">responseTopic <span class="property-type">string</span></dt>
<dd><b>MQTTv5</b>: the MQTT response topic for the message</dd>
<dt class="optional">correlationData <span class="property-type">Buffer</span></dt>
<dd><b>MQTTv5</b>: the correlation data for the message</dd>
<dt class="optional">contentType <span class="property-type">string</span></dt>
<dd><b>MQTTv5</b>: the content-type of the payload</dd>
<dt class="optional">userProperties <span class="property-type">object</span></dt>
<dd><b>MQTTv5</b>: any user properties of the message</dd>
<dt class="optional">messageExpiryInterval <span class="property-type">number</span></dt>
<dd><b>MQTTv5</b>: the expiry time, in seconds, of the message</dd>
</dl>
<h3>Details</h3>
The subscription topic can include MQTT wildcards, + for one level, # for multiple levels.</p>
@ -37,15 +48,24 @@
<dl class="message-properties">
<dt>payload <span class="property-type">string | buffer</span></dt>
<dd> the payload to publish. If this property is not set, no message will be sent. To send a blank message, set this property to an empty String.</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>
<dt class="optional">responseTopic <span class="property-type">string</span></dt>
<dd><b>MQTTv5</b>: the MQTT response topic for the message</dd>
<dt class="optional">correlationData <span class="property-type">Buffer</span></dt>
<dd><b>MQTTv5</b>: the correlation data for the message</dd>
<dt class="optional">contentType <span class="property-type">string</span></dt>
<dd><b>MQTTv5</b>: the content-type of the payload</dd>
<dt class="optional">userProperties <span class="property-type">object</span></dt>
<dd><b>MQTTv5</b>: any user properties of the message</dd>
<dt class="optional">messageExpiryInterval <span class="property-type">number</span></dt>
<dd><b>MQTTv5</b>: the expiry time, in seconds, of the message</dd>
<dt class="optional">topicAlias <span class="property-type">number</span></dt>
<dd><b>MQTTv5</b>: the MQTT topic alias to use</dd>
</dl>
<h3>Details</h3>
<code>msg.payload</code> is used as the payload of the published message.