Skip to content

Commit

Permalink
fix: [BUG]: When using "Fetch all configs concurrent" you cannot pass…
Browse files Browse the repository at this point in the history
… in a msg.variable for the Preview field. #150
  • Loading branch information
naimo84 committed Jan 7, 2023
1 parent e6be93d commit c281c04
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
11 changes: 6 additions & 5 deletions src/nodes/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ function extendEvent(event: IKalenderEvent, config: IcalEventsConfig, kalenderEv
return event;
}

export async function getICal(node: IcalNode, RED) {
export async function getICal(node: IcalNode, RED,n:any) {
const kalenderEvents = new KalenderEvents()

let configs: IcalEventsConfig[] = [];
if (node.config.checkall) {
node.red.nodes.eachNode(n => {
if (n.type === 'ical-config') {
configs.push(node.red.nodes.getNode(n.id));
node.red.nodes.eachNode(configNode => {
if (configNode.type === 'ical-config') {
const config = getConfig(node.red.nodes.getNode(configNode.id) as unknown as IcalEventsConfig, RED, n, node.msg)
configs.push(config);
}
})
} else {
Expand All @@ -115,7 +116,7 @@ export async function getICal(node: IcalNode, RED) {

for (let config of configs) {
try {
let icalConfig = configs.length === 1 ? node.config : getConfig(config, RED, node.config?.nodeconfig);
let icalConfig = config;

if (moment(node.msg.payload, moment.ISO_8601).isValid()) {
icalConfig = Object.assign(icalConfig, { now: moment(node.msg.payload).toDate() })
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/ical-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function (RED: any) {
node.msg = RED.util.cloneMessage(msg);
send = send || function () { node.send.apply(node, arguments) }
node.config = getConfig(RED.nodes.getNode(config.confignode) as unknown as IcalEventsConfig, RED, config, msg);
cronCheckJob(node, msg, send, done, config.confignode, startedCronJobs);
cronCheckJob(node, msg, send, done, config.confignode, startedCronJobs,config);
});

node.on('close', () => {
Expand Down Expand Up @@ -80,7 +80,7 @@ module.exports = function (RED: any) {
}
}

function cronCheckJob(node: IcalNode, msg: NodeMessageInFlow, send: (msg: NodeMessage | NodeMessage[]) => void, done: (err?: Error) => void, config, startedCronJobs) {
function cronCheckJob(node: IcalNode, msg: NodeMessageInFlow, send: (msg: NodeMessage | NodeMessage[]) => void, done: (err?: Error) => void, config, startedCronJobs,n) {
let newCronJobs = new Map();
if (node.job && node.job.running) {
node.status({ fill: "green", shape: "dot", text: `next check: ${node.job.nextDate().toLocaleString()}` });
Expand All @@ -90,7 +90,7 @@ module.exports = function (RED: any) {
}
let dateNow = moment().utc().toDate();
let possibleUids = [];
getICal(node, RED).then((data: IKalenderEvent[]) => {
getICal(node, RED,n).then((data: IKalenderEvent[]) => {
if (data) {
for (let k in data) {
if (data.hasOwnProperty(k)) {
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/ical-sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (RED: any) {
node.on('input', (msg, send, done) => {
send = send || function () { node.send.apply(node, arguments) }
node.config = getConfig(RED.nodes.getNode(config.confignode) as unknown as IcalEventsConfig, RED, config, msg);
cronCheckJob(node, msg, send, done);
cronCheckJob(node, msg, send, done,config);
});

if (config.timeout && config.timeout !== "" && config.timeoutUnits && config.timeoutUnits !== "") {
Expand Down Expand Up @@ -57,7 +57,7 @@ module.exports = function (RED: any) {
}


function cronCheckJob(node: IcalNode, msg: NodeMessageInFlow, send: (msg: NodeMessage | NodeMessage[]) => void, done: (err?: Error) => void) {
function cronCheckJob(node: IcalNode, msg: NodeMessageInFlow, send: (msg: NodeMessage | NodeMessage[]) => void, done: (err?: Error) => void,config) {
if (node.job && node.job.running) {
node.status({ fill: "green", shape: "dot", text: `next check: ${node.job.nextDate().toLocaleString()}` });
}
Expand All @@ -67,7 +67,7 @@ module.exports = function (RED: any) {
//@ts-ignore
if (!msg) msg = {};
var dateNow = new Date();
getICal(node, RED).then(data => {
getICal(node, RED,config).then(data => {
if (!data) return;

let current = false;
Expand Down
30 changes: 15 additions & 15 deletions src/nodes/ical-upcoming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { getConfig, getICal, CalEvent, IcalNode } from './helper';
var parser = require('cron-parser');

module.exports = function (RED: any) {
function upcomingNode(n: any) {
RED.nodes.createNode(this, n);
function upcomingNode(config: any) {
RED.nodes.createNode(this, config);
let node: IcalNode = this;
node.cache = new NodeCache();
node.red = RED;
Expand All @@ -16,35 +16,35 @@ module.exports = function (RED: any) {
node.on('input', (msg, send, done) => {
node.msg = RED.util.cloneMessage(msg);
send = send || function () { node.send.apply(node, arguments) }
node.config = getConfig(RED.nodes.getNode(n.confignode) as unknown as IcalEventsConfig, RED, n, msg);
cronCheckJob(node, msg, send, done);
node.config = getConfig(RED.nodes.getNode(config.confignode) as unknown as IcalEventsConfig, RED, config, msg);
cronCheckJob(node, msg, send, done,config);
});


try {
let cron = '';
if (n.timeout && n.timeout !== '' && parseInt(n.timeout) > 0 && n.timeoutUnits && n.timeoutUnits !== '') {
switch (n.timeoutUnits) {
if (config.timeout && config.timeout !== '' && parseInt(config.timeout) > 0 && config.timeoutUnits && config.timeoutUnits !== '') {
switch (config.timeoutUnits) {
case 'seconds':
cron = `*/${n.timeout} * * * * *`;
cron = `*/${config.timeout} * * * * *`;
break;
case 'minutes':
cron = `0 */${n.timeout} * * * *`;
cron = `0 */${config.timeout} * * * *`;
break;
case 'hours':
cron = `0 0 */${n.timeout} * * *`;
cron = `0 0 */${config.timeout} * * *`;
break;
case 'days':
cron = `0 0 0 */${n.timeout} * *`;
cron = `0 0 0 */${config.timeout} * *`;

break;
default:
break;
}
}
if (n.cron && n.cron !== '') {
parser.parseExpression(n.cron);
cron = n.cron;
if (config.cron && config.cron !== '') {
parser.parseExpression(config.cron);
cron = config.cron;
}

if (cron !== '') {
Expand All @@ -61,7 +61,7 @@ module.exports = function (RED: any) {
}
}

function cronCheckJob(node: IcalNode, msg: NodeMessageInFlow, send: (msg: NodeMessage | NodeMessage[]) => void, done: (err?: Error) => void) {
function cronCheckJob(node: IcalNode, msg: NodeMessageInFlow, send: (msg: NodeMessage | NodeMessage[]) => void, done: (err?: Error) => void,n:any) {

if (node.job && node.job.running) {
node.status({ fill: 'green', shape: 'dot', text: `next check: ${node.job.nextDate().toISOString()}` });
Expand All @@ -71,7 +71,7 @@ module.exports = function (RED: any) {


node.datesArray = [];
getICal(node, RED).then(data => {
getICal(node, RED,n).then(data => {
node.datesArray = data || [];

let todayEventcounter = 0;
Expand Down

0 comments on commit c281c04

Please sign in to comment.