node-red-contrib-posixmq-read
Version:
A Node-RED node that receives messsages form a Posix message queue
48 lines (43 loc) • 1.72 kB
JavaScript
/*Copyright 2017 Denis Francesconi, Hydro-Quebec
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.
*/
var PosixMQ = require('posix-mq');
module.exports = function (RED) {
function PosixMQReadNode(config) {
RED.nodes.createNode(this,config);
this.msgname = config.msgname;
this.msgsize = Number(config.msgsize);
this.maxmsgs = Number(config.maxmsgs);
var posixmq = new PosixMQ();
var node = this;
var msg;
var n;
var send = false;
posixmq.open({ name: node.msgname.toString(),create: true,mode: '0777',maxmsgs: node.maxmsgs, msgsize: node.msgsize });
node.status({fill: "green", shape: "dot", text: node.msgname.toString()});
node.warn("the " + node.msgname.toString() + " message queue is open");
readbuf = new Buffer(posixmq.msgsize);
node.on('input', function() {
var str = "";
while ((n = posixmq.shift(readbuf)) !== false){
send = true;
str = str + readbuf.toString('utf8', 0, n);
};
if (send){node.send({payload: str})};
send = false;
});
node.on('close', function() {
posixmq.unlink();
posixmq.close();
node.status({fill: "red", shape: "dot", text: node.msgname.toString()});});
}
RED.nodes.registerType("posixmq-read", PosixMQReadNode);
}