node-red/packages/node_modules/@node-red/nodes/core/common/20-inject.js

135 lines
4.1 KiB
JavaScript
Raw Normal View History

2013-09-05 16:02:48 +02:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
2013-09-05 16:02:48 +02:00
*
* 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.
**/
2014-05-04 00:32:04 +02:00
module.exports = function(RED) {
"use strict";
2013-09-12 18:21:46 +02:00
var cron = require("cron");
2014-05-04 00:32:04 +02:00
function InjectNode(n) {
RED.nodes.createNode(this,n);
/* Handle legacy */
if(!Array.isArray(n.props)){
n.props = [];
n.props.push({
p:'payload',
v:n.payload,
vt:n.payloadType
});
n.props.push({
p:'topic',
v:n.topic,
vt:'str'
});
}
this.props = n.props;
2014-05-04 00:32:04 +02:00
this.repeat = n.repeat;
this.crontab = n.crontab;
this.once = n.once;
this.onceDelay = (n.onceDelay || 0.1) * 1000;
2014-05-04 00:32:04 +02:00
this.interval_id = null;
this.cronjob = null;
var node = this;
if (node.repeat > 2147483) {
node.error(RED._("inject.errors.toolong", this));
delete node.repeat;
}
node.repeaterSetup = function () {
if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) {
2014-05-04 00:32:04 +02:00
this.repeat = this.repeat * 1000;
if (RED.settings.verbose) {
this.log(RED._("inject.repeat", this));
}
this.interval_id = setInterval(function() {
node.emit("input", {});
}, this.repeat);
} else if (this.crontab) {
if (RED.settings.verbose) {
this.log(RED._("inject.crontab", this));
}
this.cronjob = new cron.CronJob(this.crontab, function() { node.emit("input", {}); }, null, true);
}
};
2014-05-04 00:32:04 +02:00
if (this.once) {
this.onceTimeout = setTimeout( function() {
node.emit("input",{});
node.repeaterSetup();
}, this.onceDelay);
} else {
node.repeaterSetup();
2014-05-04 00:32:04 +02:00
}
this.on("input", function(msg) {
var errors = [];
this.props.forEach(p => {
var property = p.p;
var value = p.v ? p.v : '';
var valueType = p.vt ? p.vt : 'str';
if (!property) return;
try {
msg[property] = RED.util.evaluateNodeProperty(value, valueType, this, msg);
} catch (err) {
errors.push(err);
}
});
if (errors.length) {
node.error(errors.join('; '), msg);
} else {
node.send(msg);
2014-05-04 00:32:04 +02:00
}
});
}
2014-05-04 00:32:04 +02:00
RED.nodes.registerType("inject",InjectNode);
2014-05-04 00:32:04 +02:00
InjectNode.prototype.close = function() {
if (this.onceTimeout) {
clearTimeout(this.onceTimeout);
}
2014-05-04 00:32:04 +02:00
if (this.interval_id != null) {
clearInterval(this.interval_id);
2015-04-26 00:29:53 +02:00
if (RED.settings.verbose) { this.log(RED._("inject.stopped")); }
2014-05-04 00:32:04 +02:00
} else if (this.cronjob != null) {
this.cronjob.stop();
2015-04-26 00:29:53 +02:00
if (RED.settings.verbose) { this.log(RED._("inject.stopped")); }
2014-05-04 00:32:04 +02:00
delete this.cronjob;
}
};
2014-12-10 15:16:07 +01:00
RED.httpAdmin.post("/inject/:id", RED.auth.needsPermission("inject.write"), function(req,res) {
var node = RED.nodes.getNode(req.params.id);
if (node != null) {
try {
node.receive();
2015-07-15 23:43:24 +02:00
res.sendStatus(200);
2014-12-10 15:16:07 +01:00
} catch(err) {
2015-07-15 23:43:24 +02:00
res.sendStatus(500);
2015-06-16 17:09:53 +02:00
node.error(RED._("inject.failed",{error:err.toString()}));
2013-09-07 15:27:41 +02:00
}
2014-12-10 15:16:07 +01:00
} else {
2015-07-15 23:43:24 +02:00
res.sendStatus(404);
2014-12-10 15:16:07 +01:00
}
2014-05-04 00:32:04 +02:00
});
}