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

Add QoS and Retain to the MQtt node options

This commit is contained in:
Dave C-J 2014-08-22 12:42:07 +01:00
parent f1d2b7ffa0
commit a8ade083d4
2 changed files with 31 additions and 4 deletions

View File

@ -65,16 +65,31 @@
<label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label> <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="Topic"> <input type="text" id="node-input-topic" placeholder="Topic">
</div> </div>
<div class="form-row">
<label for="node-input-qos"><i class="fa fa-empire"></i> QoS</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>
&nbsp;&nbsp;<i class="fa fa-history"></i>&nbsp;Retain &nbsp;<select id="node-input-retain" style="width:125px !important">
<option value=""></option>
<option value="false">false</option>
<option value="true">true</option>
</select>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-tips">Tip: Leave topic, qos or retain blank if you want to set them via msg properties.</div>
</script> </script>
<script type="text/x-red" data-help-name="mqtt out"> <script type="text/x-red" data-help-name="mqtt out">
<p>Connects to a MQTT broker and publishes <b>msg.payload</b> either to the <b>msg.topic</b> OR to the topic specified in the edit window. The value in the edit window has precedence.</p> <p>Connects to a MQTT broker and publishes <b>msg.payload</b> either to the <b>msg.topic</b> or to the topic specified in the edit window. The value in the edit window has precedence.</p>
<p><b>msg.qos</b> and <b>msg.retain</b> may also optionally have been set. If not set they are set to 0 and false respectively.</p> <p>Likewise QoS and/or retain values in the edit panel will overwrite any <b>msg.qos</b> and <b>msg.retain</b> properties. If nothing is set they default to <i>0</i> and <i>false</i> respectively.</p>
<p>If <b>msg.payload</b> contains a buffer or an object it will be stringified before being sent.</p> <p>If <b>msg.payload</b> contains an object it will be stringified before being sent.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -83,6 +98,8 @@
defaults: { defaults: {
name: {value:""}, name: {value:""},
topic: {value:""}, topic: {value:""},
qos: {value:""},
retain: {value:""},
broker: {type:"mqtt-broker", required:true} broker: {type:"mqtt-broker", required:true}
}, },
color:"#d8bfd8", color:"#d8bfd8",

View File

@ -71,8 +71,9 @@ module.exports = function(RED) {
function MQTTOutNode(n) { function MQTTOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.topic = n.topic; this.topic = n.topic;
this.qos = n.qos || null;
this.retain = n.retain;
this.broker = n.broker; this.broker = n.broker;
this.brokerConfig = RED.nodes.getNode(this.broker); this.brokerConfig = RED.nodes.getNode(this.broker);
@ -82,6 +83,15 @@ module.exports = function(RED) {
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
if (msg != null) { if (msg != null) {
if (msg.qos) {
msg.qos = parseInt(msg.qos);
if ((msg.qos !== 0) && (msg.qos !== 1) && (msg.qos !== 2)) {
msg.qos = null;
}
}
msg.qos = Number(node.qos || msg.qos || 0);
msg.retain = node.retain || msg.retain || false;
msg.retain = ((msg.retain === true) || (msg.retain === "true")) || false;
if (node.topic) { if (node.topic) {
msg.topic = node.topic; msg.topic = node.topic;
} }