2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01: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) {
|
2015-02-03 12:21:29 +01:00
|
|
|
"use strict";
|
2021-04-26 15:47:50 +02:00
|
|
|
const {scheduleTask} = require("cronosjs");
|
2014-12-15 18:03:18 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function InjectNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2020-05-11 17:07:26 +02:00
|
|
|
|
2020-01-23 14:55:50 +01:00
|
|
|
/* 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'
|
|
|
|
});
|
2020-05-11 17:07:26 +02:00
|
|
|
} else {
|
|
|
|
for (var i=0,l=n.props.length; i<l; i++) {
|
|
|
|
if (n.props[i].p === 'payload' && !n.props[i].hasOwnProperty('v')) {
|
|
|
|
n.props[i].v = n.payload;
|
|
|
|
n.props[i].vt = n.payloadType;
|
|
|
|
} else if (n.props[i].p === 'topic' && n.props[i].vt === 'str' && !n.props[i].hasOwnProperty('v')) {
|
|
|
|
n.props[i].v = n.topic;
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 14:55:50 +01:00
|
|
|
}
|
2020-05-11 17:07:26 +02:00
|
|
|
|
2020-01-23 14:55:50 +01:00
|
|
|
this.props = n.props;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.repeat = n.repeat;
|
|
|
|
this.crontab = n.crontab;
|
|
|
|
this.once = n.once;
|
2018-01-11 22:50:53 +01:00
|
|
|
this.onceDelay = (n.onceDelay || 0.1) * 1000;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.interval_id = null;
|
|
|
|
this.cronjob = null;
|
2018-01-17 10:35:47 +01:00
|
|
|
var node = this;
|
|
|
|
|
2020-05-11 07:51:47 +02:00
|
|
|
node.props.forEach(function (prop) {
|
|
|
|
if (prop.vt === "jsonata") {
|
|
|
|
try {
|
|
|
|
var val = prop.v ? prop.v : "";
|
|
|
|
prop.exp = RED.util.prepareJSONataExpression(val, node);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
node.error(RED._("inject.errors.invalid-expr", {error:err.message}));
|
|
|
|
prop.exp = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-17 10:35:47 +01:00
|
|
|
if (node.repeat > 2147483) {
|
|
|
|
node.error(RED._("inject.errors.toolong", this));
|
|
|
|
delete node.repeat;
|
|
|
|
}
|
2014-12-15 18:03:18 +01:00
|
|
|
|
2018-01-11 22:50:53 +01:00
|
|
|
node.repeaterSetup = function () {
|
2020-05-11 15:37:19 +02:00
|
|
|
if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) {
|
|
|
|
this.repeat = this.repeat * 1000;
|
2021-12-17 10:58:10 +01:00
|
|
|
this.debug(RED._("inject.repeat", this));
|
2020-05-11 15:37:19 +02:00
|
|
|
this.interval_id = setInterval(function() {
|
|
|
|
node.emit("input", {});
|
|
|
|
}, this.repeat);
|
|
|
|
} else if (this.crontab) {
|
2021-12-17 10:58:10 +01:00
|
|
|
this.debug(RED._("inject.crontab", this));
|
2021-04-26 15:47:50 +02:00
|
|
|
this.cronjob = scheduleTask(this.crontab,() => { node.emit("input", {})});
|
2018-01-11 22:50:53 +01:00
|
|
|
}
|
|
|
|
};
|
2014-12-15 18:03:18 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (this.once) {
|
2018-01-13 22:17:14 +01:00
|
|
|
this.onceTimeout = setTimeout( function() {
|
2020-05-11 15:37:19 +02:00
|
|
|
node.emit("input",{});
|
|
|
|
node.repeaterSetup();
|
2018-01-11 22:50:53 +01:00
|
|
|
}, this.onceDelay);
|
|
|
|
} else {
|
2020-05-11 15:37:19 +02:00
|
|
|
node.repeaterSetup();
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-12-15 18:03:18 +01:00
|
|
|
|
2019-11-29 01:17:30 +01:00
|
|
|
this.on("input", function(msg, send, done) {
|
2023-01-23 21:42:25 +01:00
|
|
|
const errors = [];
|
|
|
|
let props = this.props;
|
2021-07-28 11:59:16 +02:00
|
|
|
if (msg.__user_inject_props__ && Array.isArray(msg.__user_inject_props__)) {
|
2021-05-20 23:35:42 +02:00
|
|
|
props = msg.__user_inject_props__;
|
2021-05-20 22:12:32 +02:00
|
|
|
}
|
2021-05-20 23:35:42 +02:00
|
|
|
delete msg.__user_inject_props__;
|
2023-01-23 21:42:25 +01:00
|
|
|
props = [...props]
|
|
|
|
function evaluateProperty(doneEvaluating) {
|
|
|
|
if (props.length === 0) {
|
|
|
|
doneEvaluating()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const p = props.shift()
|
|
|
|
const property = p.p;
|
|
|
|
const value = p.v ? p.v : '';
|
|
|
|
const valueType = p.vt ? p.vt : 'str';
|
2020-01-23 14:55:50 +01:00
|
|
|
|
2023-01-23 21:42:25 +01:00
|
|
|
if (property) {
|
|
|
|
if (valueType === "jsonata") {
|
|
|
|
if (p.v) {
|
|
|
|
try {
|
|
|
|
var exp = RED.util.prepareJSONataExpression(p.v, node);
|
2023-03-03 12:43:06 +01:00
|
|
|
RED.util.evaluateJSONataExpression(exp, msg, (err, newValue) => {
|
|
|
|
if (err) {
|
|
|
|
errors.push(err.toString())
|
|
|
|
} else {
|
|
|
|
RED.util.setMessageProperty(msg,property,newValue,true);
|
|
|
|
}
|
|
|
|
evaluateProperty(doneEvaluating)
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2023-01-23 21:42:25 +01:00
|
|
|
errors.push(err.message);
|
2023-03-03 12:43:06 +01:00
|
|
|
evaluateProperty(doneEvaluating)
|
2023-01-23 21:42:25 +01:00
|
|
|
}
|
2023-03-03 12:43:06 +01:00
|
|
|
} else {
|
|
|
|
evaluateProperty(doneEvaluating)
|
2020-05-11 07:51:47 +02:00
|
|
|
}
|
2023-01-23 21:42:25 +01:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
RED.util.evaluateNodeProperty(value, valueType, node, msg, (err, newValue) => {
|
|
|
|
if (err) {
|
|
|
|
errors.push(err.toString())
|
|
|
|
} else {
|
|
|
|
RED.util.setMessageProperty(msg,property,newValue,true);
|
|
|
|
}
|
|
|
|
evaluateProperty(doneEvaluating)
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
errors.push(err.toString());
|
|
|
|
evaluateProperty(doneEvaluating)
|
2020-05-11 07:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-23 21:42:25 +01:00
|
|
|
} else {
|
|
|
|
evaluateProperty(doneEvaluating)
|
2020-05-11 07:51:47 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2023-01-23 21:42:25 +01:00
|
|
|
|
|
|
|
evaluateProperty(() => {
|
|
|
|
if (errors.length) {
|
|
|
|
done(errors.join('; '));
|
|
|
|
} else {
|
|
|
|
send(msg);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
})
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-02-03 20:06:49 +01:00
|
|
|
}
|
2014-12-15 18:03:18 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("inject",InjectNode);
|
2014-12-15 18:03:18 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
InjectNode.prototype.close = function() {
|
2018-01-13 22:17:14 +01:00
|
|
|
if (this.onceTimeout) {
|
|
|
|
clearTimeout(this.onceTimeout);
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
if (this.interval_id != null) {
|
|
|
|
clearInterval(this.interval_id);
|
|
|
|
} else if (this.cronjob != null) {
|
|
|
|
this.cronjob.stop();
|
|
|
|
delete this.cronjob;
|
2014-02-03 20:06:49 +01:00
|
|
|
}
|
2018-01-11 22:50:53 +01:00
|
|
|
};
|
2015-02-25 20:10:44 +01:00
|
|
|
|
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 {
|
2021-05-20 23:35:42 +02:00
|
|
|
if (req.body && req.body.__user_inject_props__) {
|
2021-05-20 22:12:32 +02:00
|
|
|
node.receive(req.body);
|
|
|
|
} else {
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|