node-red-contrib-topic-timeframe-trigger
Advanced tools
Comparing version 0.2.0 to 0.2.1
79
index.js
@@ -0,1 +1,3 @@ | ||
/* eslint-disable prefer-rest-params */ | ||
/* eslint-disable prefer-spread */ | ||
/** | ||
@@ -26,26 +28,41 @@ The MIT License (MIT) | ||
module.exports = function (RED) { | ||
'use strict'; | ||
const _ = require('lodash'); | ||
var _ = require("lodash"); | ||
RED.nodes.registerType('topic-timeframe-trigger', function (config) { | ||
RED.nodes.createNode(this, config); | ||
var node = this, | ||
triggers = null, | ||
timeout = null, | ||
globalConfig = { | ||
debug: false | ||
}; | ||
// eslint-disable-next-line consistent-this | ||
const node = this; | ||
const globalConfig = { | ||
debug: false, | ||
}; | ||
let triggers = null; | ||
let timeout = null; | ||
function getGlobalConfig() { | ||
const getGlobalConfig = function () { | ||
return _.assign(globalConfig, node.context().global.get('topic-timeframe-trigger')); | ||
} | ||
}; | ||
function debug() { | ||
if (getGlobalConfig().debug) node.log.apply(node, arguments); | ||
} | ||
const debug = function () { | ||
if (getGlobalConfig().debug) { | ||
node.log.apply(node, arguments); | ||
} | ||
}; | ||
node.on('input', function (msg) { | ||
const reset = function () { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
triggers = null; | ||
node.status({ | ||
fill: 'blue', | ||
shape: 'dot', | ||
text: 'Idle', | ||
}); | ||
debug('Timeframe reset after ' + config.timeframe + 's'); | ||
}; | ||
node.on('input', (msg) => { | ||
if (!msg.topic) { | ||
@@ -56,6 +73,7 @@ node.error('Message has no topic: ' + msg.payload); | ||
shape: 'dot', | ||
text: 'Msg has no topic' | ||
text: 'Msg has no topic', | ||
}); | ||
return; | ||
} | ||
if (!triggers) { | ||
@@ -65,6 +83,7 @@ timeout = setTimeout(reset, config.timeframe * 1000); | ||
} | ||
triggers.push(msg); | ||
var grouped = _.groupBy(triggers, 'topic'); | ||
var countExceeded = _.filter(grouped, function (msgs) { | ||
const grouped = _.groupBy(triggers, 'topic'); | ||
const countExceeded = _.filter(grouped, (msgs) => { | ||
return msgs.length >= config.count; | ||
@@ -77,3 +96,3 @@ }); | ||
payload: config.triggeredpayload, | ||
triggers: triggers | ||
triggers, | ||
}); | ||
@@ -87,3 +106,3 @@ reset(); | ||
shape: 'dot', | ||
text: msg.topic + ':' + grouped[msg.topic].length | ||
text: msg.topic + ':' + grouped[msg.topic].length, | ||
}); | ||
@@ -97,19 +116,5 @@ } | ||
shape: 'dot', | ||
text: 'Idle' | ||
text: 'Idle', | ||
}); | ||
function reset() { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
triggers = null; | ||
node.status({ | ||
fill: 'blue', | ||
shape: 'dot', | ||
text: 'Idle' | ||
}); | ||
debug('Timeframe reset after ' + config.timeframe + 's'); | ||
} | ||
}); | ||
}; | ||
}; |
{ | ||
"name": "node-red-contrib-topic-timeframe-trigger", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Node-RED node which observes input topics and only produces an output if n distinct topics are received n times each within the specified time frame.", | ||
@@ -13,3 +13,5 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec ./tests/test.js" | ||
"test": "mocha -R spec ./tests/test.js", | ||
"update-dependencies": "./node_modules/.bin/ncu -u && npm install", | ||
"lint": "eslint ." | ||
}, | ||
@@ -26,23 +28,63 @@ "author": "@biddster", | ||
"dependencies": { | ||
"lodash": "^4.6.1" | ||
"lodash": "^4.17.20" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^3.5.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-biddster": "^0.3.0", | ||
"node-red-contrib-mock-node": "^0.3.0" | ||
"@hapi/eslint-config-hapi": "13.0.2", | ||
"@hapi/eslint-plugin-hapi": "4.3.5", | ||
"babel-eslint": "10.1.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^7.7.0", | ||
"eslint-config-biddster": "^0.6.0", | ||
"husky": "^4.2.5", | ||
"lint-staged": "^10.2.13", | ||
"mocha": "^8.1.3", | ||
"node-red-contrib-mock-node": "^0.5.0", | ||
"npm-check-updates": "^7.1.1", | ||
"prettier": "^2.1.1" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"extends": "@hapi/eslint-config-hapi", | ||
"parser": "babel-eslint", | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"sourceType": "module" | ||
"ecmaVersion": 2020, | ||
"sourceType": "script" | ||
}, | ||
"extends": "eslint-config-biddster/es6-node" | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"*.js" | ||
], | ||
"rules": { | ||
"@hapi/hapi/scope-start": "off", | ||
"comma-dangle": "off", | ||
"brace-style": "off", | ||
"strict": "off" | ||
} | ||
} | ||
] | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"tabWidth": 4, | ||
"printWidth": 96 | ||
}, | ||
"nyc": { | ||
"exclude": "tests/**" | ||
}, | ||
"lint-staged": { | ||
"*.js": [ | ||
"prettier --write", | ||
"eslint", | ||
"git add" | ||
], | ||
"*.{md,html,json}": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"node-red": { | ||
@@ -53,2 +95,2 @@ "nodes": { | ||
} | ||
} | ||
} |
@@ -0,1 +1,4 @@ | ||
/* eslint-disable no-undef */ | ||
/* eslint-disable func-style */ | ||
/* eslint-disable prefer-arrow-callback */ | ||
/** | ||
@@ -25,32 +28,29 @@ The MIT License (MIT) | ||
"use strict"; | ||
var assert = require('chai').assert; | ||
var _ = require('lodash'); | ||
var mock = require('node-red-contrib-mock-node'); | ||
var nodeRedModule = require('../index.js'); | ||
const Assert = require('chai').assert; | ||
const _ = require('lodash'); | ||
const Mock = require('node-red-contrib-mock-node'); | ||
const NodeRedModule = require('../index.js'); | ||
describe('topic-timeframe-trigger', function () { | ||
it('should work', function () { | ||
var node = newNode(); | ||
var msg1 = { | ||
const node = NewNode(); | ||
const msg1 = { | ||
topic: 't1', | ||
payload: 'p1' | ||
payload: 'p1', | ||
}; | ||
node.emit('input', msg1); | ||
var msg2 = { | ||
const msg2 = { | ||
topic: 't2', | ||
payload: 'p2' | ||
payload: 'p2', | ||
}; | ||
node.emit('input', msg2); | ||
assert.strictEqual(node.sent(0).payload, 'triggered payload'); | ||
assert.strictEqual(node.sent(0).topic, 'triggered topic'); | ||
assert.strictEqual(node.sent(0).triggers[0], msg1); | ||
assert.strictEqual(node.sent(0).triggers[1], msg2); | ||
Assert.strictEqual(node.sent(0).payload, 'triggered payload'); | ||
Assert.strictEqual(node.sent(0).topic, 'triggered topic'); | ||
Assert.strictEqual(node.sent(0).triggers[0], msg1); | ||
Assert.strictEqual(node.sent(0).triggers[1], msg2); | ||
}); | ||
}); | ||
function newNode(configOverrides) { | ||
var config = { | ||
function NewNode(configOverrides) { | ||
const config = { | ||
timeframe: 1000, | ||
@@ -60,3 +60,3 @@ count: 1, | ||
triggeredtopic: 'triggered topic', | ||
triggeredpayload: 'triggered payload' | ||
triggeredpayload: 'triggered payload', | ||
}; | ||
@@ -66,3 +66,4 @@ if (configOverrides) { | ||
} | ||
return mock(nodeRedModule, config); | ||
} | ||
return Mock(NodeRedModule, config); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13291
156
12
Updatedlodash@^4.17.20