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

Merge pull request #74 from anna2130/mqlight

Added mqlight nodes with share property
This commit is contained in:
Dave Conway-Jones 2014-10-30 09:33:23 +00:00
commit 2577911b9f
2 changed files with 294 additions and 0 deletions

154
io/mqlight/mqlight.html Normal file
View File

@ -0,0 +1,154 @@
<!--
Copyright 2013,2014 IBM Corp.
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.
-->
<script type="text/x-red" data-template-name="mqlight in">
<div class="form-row">
<label for="node-input-service"><i class="fa fa-globe"></i> Service</label>
<input type="text" id="node-input-service">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="nodered/news">
</div>
<div class="form-row">
<label for="node-input-share"><i class="fa fa-tag"></i> Share</label>
<input type="text" id="node-input-share">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="mqlight in">
<p>Provides an MQ Light receive client.</p>
<p>Subscribes the client to a destination based on the supplied topic and (optional) share arguments.</p>
<p>Topic can contain any character in the Unicode character set, with # representing a multilevel wildcard and + a single level wildcard as described <a href="https://developer.ibm.com/messaging/mq-light/wildcard-topicpatterns/">here</a>.
<p>Share identifies a shared destination for which messages are anycast between connected subscribers. If left blank, it defaults to a private destination.</p>
<p>Received messages will be sent on in <b>msg.payload</b> with their topic sent in <b>msg.topic</b> and the share, if specified, sent in <b>msg.share</b>.</p>
</script>
<script type="text/javascript">
(function() {
RED.nodes.registerType('mqlight in', {
category: 'input',
defaults: {
name: {value: ""},
topic: {value: ""},
share: {value:""},
service: {type: "mqlight service", required: true}
},
color: "#6da7d1",
inputs: 0,
outputs: 1,
icon: "mqlightin.png",
label: function() {
if (this.topic) {
return this.topic+(this.share?" ["+this.share+"]":"");
} else {
return this.name || "mqlight";
}
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
}
});
})();
</script>
<script type="text/x-red" data-template-name="mqlight out">
<div class="form-row">
<label for="node-input-service"><i class="fa fa-globe"></i> Service</label>
<input type="text" id="node-input-service">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="nodered/news">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="mqlight out">
<p>Provides an MQ Light send client.</p>
<p>Sends the value passed in on <b>msg.payload</b> to the specified topic.</p>
<p>The topic can be passed in on <b>msg.topic</b>, however the topic in the node must be blank for this to take effect. If the topic is set in the node <b>msg.topic</b> will be overridden.</p>
</script>
<script type="text/javascript">
(function() {
RED.nodes.registerType('mqlight out', {
category: 'output',
defaults: {
name: {value: ""},
topic: {value: ""},
service: {type: "mqlight service", required: true}
},
color: "#6da7d1",
inputs: 1,
outputs: 0,
align: "right",
icon: "mqlightin.png",
label: function() {
return this.name || this.topic || "mqlight";
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
}
});
})();
</script>
<script type="text/x-red" data-template-name="mqlight service">
<div class="form-row node-input-service">
<label for="node-config-input-service"><i class="fa fa-globe"></i> Service</label>
<input type="text" id="node-config-input-service" placeholder="amqp://localhost">
</div>
<div class="form-row">
<label for="node-config-input-clientid"><i class="fa fa-tag"></i> ID</label>
<input type="text" id="node-config-input-clientid" placeholder="Leave blank for auto generated">
</div>
<div class="form-row">
<label for="node-config-input-user"><i class="fa fa-user"></i> Username</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> Password</label>
<input type="password" id="node-config-input-password">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('mqlight service',{
category: 'config',
defaults: {
service: {value: "amqp://localhost", required: true},
clientid: {value: ""}
},
credentials: {
user: {type: "text"},
password: {type: "password"}
},
label: function() {
if (this.service == "") {
this.service = "localhost";
}
return (this.clientid ? this.clientid + "@" : "") + this.service;
}
});
</script>

140
io/mqlight/mqlight.js Normal file
View File

@ -0,0 +1,140 @@
/**
* Copyright 2013,2014 IBM Corp.
*
* 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.
**/
module.exports = function(RED) {
"use strict";
var mqlight = require('mqlight');
var util = require("util");
function MQLightServiceNode(n) {
RED.nodes.createNode(this,n);
var id = "mqlight_" + (n.clientid ? n.clientid : (1+Math.random()*4294967295).toString(16));
var opts = {
service: n.service,
id: id
};
if (this.credentials) {
opts.user = this.credentials.user;
opts.password = this.credentials.password;
}
this.client = mqlight.createClient(opts, function(err) {
if (err) {
util.log('[mqlight] ['+id+'] not connected to service '+n.service);
} else {
util.log('[mqlight] ['+id+'] connected to service '+n.service);
}
});
}
RED.nodes.registerType("mqlight service",MQLightServiceNode,{
credentials: {
user: {type:"text"},
password: {type: "password"}
}
});
function MQLightIn(n) {
RED.nodes.createNode(this, n);
this.topic = n.topic || "";
this.share = n.share || null;
this.service = n.service;
this.serviceConfig = RED.nodes.getNode(this.service);
var node = this;
if (node.serviceConfig) {
if (node.serviceConfig.client) {
var recvClient = node.serviceConfig.client;
recvClient.on("started", function() {
recvClient.on("message", function(data, delivery) {
var msg = {
topic: delivery.message.topic,
payload: data,
_session: {
type: "mqlight",
id: recvClient.id
}
};
if (delivery.destination.share) {
msg.share = delivery.destination.share;
}
node.send(msg);
});
var subscribeCallback = function(err) {
if (err) {
node.error("Failed to subscribe: " + err);
} else {
node.log("Subscribed to "+node.topic+(node.share?" ["+node.share+"]":""));
}
};
if (node.share) {
recvClient.subscribe(node.topic, node.share, subscribeCallback);
} else {
recvClient.subscribe(node.topic, subscribeCallback);
}
});
recvClient.start();
node.on("close", function (done) {
recvClient.stop(done);
});
}
}
}
RED.nodes.registerType("mqlight in", MQLightIn);
function MQLightOut(n) {
RED.nodes.createNode(this, n);
this.topic = n.topic || "";
this.service = n.service;
this.serviceConfig = RED.nodes.getNode(this.service);
var node = this;
if (node.serviceConfig) {
if (node.serviceConfig.client) {
var sendClient = node.serviceConfig.client;
sendClient.on("started", function () {
node.on("input", function(msg) {
if (node.topic === "") {
if (msg.topic) {
node.topic = msg.topic;
} else {
node.warn("No topic set in MQ Light out node");
return;
}
}
sendClient.send(node.topic, msg.payload, function(err) {
if (err) {
node.error(err);
}
});
});
});
sendClient.start();
node.on("close", function (done) {
sendClient.stop(done);
});
}
}
}
RED.nodes.registerType("mqlight out", MQLightOut);
};