Skip to content

Commit

Permalink
Selectable range for random number #352 ; fix #353
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypnos3 committed Nov 7, 2021
1 parent 4de3e6a commit 9862843
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 32 deletions.
2 changes: 1 addition & 1 deletion nodes/blind-control.html
Original file line number Diff line number Diff line change
Expand Up @@ -4208,7 +4208,7 @@
min-width: 140px;
}
.rdgtimer-rule-mainblock {
margin-right: 28px;
margin-right: 16px; /*28px;*/
}
.rdgtimer-rule-name-span {
width: 30%; /* width: 120px; */
Expand Down
2 changes: 1 addition & 1 deletion nodes/clock-timer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@
min-width: 140px;
}
.rdgtimer-rule-mainblock {
margin-right: 28px;
margin-right: 16px; /*28px;*/
}
.rdgtimer-rule-name-span {
width: 30%; /* width: 120px; */
Expand Down
5 changes: 4 additions & 1 deletion nodes/locales/de/position-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
"enabled": "aktiviert",
"disabled": "deaktiviert",
"cancel":"Abbrechen",
"ok":"Ok"
"ok":"Ok",
"randNum":"zufällig bis",
"randNumCachedDay":"zufällig (täglich) bis",
"randNumCachedWeek":"zufällig (wöchentlich) bis"
},
"types": {
"unlimited": "keine Limitierung",
Expand Down
5 changes: 4 additions & 1 deletion nodes/locales/en-US/position-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"enabled": "enabled",
"disabled": "disabled",
"cancel":"Cancel",
"ok":"Ok"
"ok":"Ok",
"randNum":"random till",
"randNumCachedDay":"random (daily) till",
"randNumCachedWeek":"random (weekly) till"
},
"types": {
"unlimited":"no limitation",
Expand Down
6 changes: 6 additions & 0 deletions nodes/position-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@
return `"${node._('node-red-contrib-sun-position/position-config:common.types.DayOfYear')}"${suffix}`;
case 'pdbDayOfYearEven':
return `"${node._('node-red-contrib-sun-position/position-config:common.types.isDayOfYearEven')}"${suffix}`;
case 'randomNum':
return `${node._('node-red-contrib-sun-position/position-config:common.label.randNum')} ${RED.nodes.getType('position-config').clipValueLength(v, l)}${suffix}`;
case 'randmNumCachedDay':
return `${node._('node-red-contrib-sun-position/position-config:common.label.randNumCachedDay')} ${RED.nodes.getType('position-config').clipValueLength(v, l)}${suffix}`;
case 'randmNumCachedWeek':
return `${node._('node-red-contrib-sun-position/position-config:common.label.randNumCachedWeek')} ${RED.nodes.getType('position-config').clipValueLength(v, l)}${suffix}`;
default: {
if (v === '') {
return node._('node-red-contrib-sun-position/position-config:common.label.blank');
Expand Down
72 changes: 47 additions & 25 deletions nodes/position-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,40 +563,52 @@ module.exports = function (RED) {
/**
* get a random number for a node cached per day
* @param {*} _srcNode - source node information
* @param {number} value - data object with more information
* @param {number} limit1 - lower limit for random number
* @param {number} limit2 - upper limit for random number
* @param {Date} [dNow] base Date to use for Date time functions
* @returns {number} random number
*/
getCachedRandomDayNumber(_srcNode, value, dNow) {
if (isNaN(value)) {
return value;
}
getCachedRandomDayNumber(_srcNode, limit1, limit2, dNow) {
// _srcNode.debug(`getCachedRandomDayNumber limit1=${String(limit1)} limit2=${String(limit2)} dNow=${dNow}`);
if (isNaN(limit1)) {
_srcNode.error(`the value for random number limit is wrong limit1=${String(limit1)} limit2=${String(limit2)}, using 60`);
limit1 = 60;
}
const low = Math.min(limit1, isNaN(limit2) ? 0 : limit2);
const high = Math.max(limit1, isNaN(limit2) ? 0 : limit2);
const name = 'min_'+low+'_max_'+high;
const store = _srcNode.contextStore || this.contextStore;
const cache = this.getNodeNumberCache(_srcNode, dNow, store);
if (isNaN(cache.d[value])) {
cache.d[value] = Math.random() * ((value || 60) + 1);
if (isNaN(cache.day[name])) {
cache.day[name] = low + (Math.random() * (high - low));
_srcNode.context().set('randomNumberCache', cache, store);
}
return cache.d[value];
return cache.day[name];
}
/**
* get a random number for a node cached per week
* get a random number for a node cached per day
* @param {*} _srcNode - source node information
* @param {number} value - data object with more information
* @param {number} limit1 - lower limit for random number
* @param {number} limit2 - upper limit for random number
* @param {Date} [dNow] base Date to use for Date time functions
* @returns {number} random number
*/
getCachedRandomWeekNumber(_srcNode, value, dNow) {
if (isNaN(value)) {
return value;
}
getCachedRandomWeekNumber(_srcNode, limit1, limit2, dNow) {
// _srcNode.debug(`getCachedRandomWeekNumber limit1=${limit1} limit2=${limit2} dNow=${dNow}`);
if (isNaN(limit1)) {
_srcNode.error(`the value for random number limit is wrong limit1=${limit1} limit2=${limit2}, using 60`);
limit1 = 60;
}
const low = Math.min(limit1, isNaN(limit2) ? 0 : limit2);
const high = Math.max(limit1, isNaN(limit2) ? 0 : limit2);
const name = 'min_'+low+'_max_'+high;
const store = _srcNode.contextStore || this.contextStore;
const cache = this.getNodeNumberCache(_srcNode, dNow, store);
if (isNaN(cache.w[value])) {
cache.w[value] = Math.random() * ((value || 60) + 1);
if (isNaN(cache.week[name])) {
cache.week[name] = low + (Math.random() * (high - low));
_srcNode.context().set('randomNumberCache', cache, store);
}
return cache.w[value];
return cache.week[name];
}
/**
* get a float value from a type input in Node-Red
Expand Down Expand Up @@ -1108,15 +1120,25 @@ module.exports = function (RED) {
return _srcNode.addId || _srcNode.id;
} else if (data.type === 'nodeName') {
return _srcNode.name || _srcNode.id; // if empty fallback to node ID
} else if (data.type === 'randmNumCachedDay ') {
const rv = this.getCachedRandomDayNumber(_srcNode, parseFloat(data.value), dNow);
return (isNaN(rv) ? 0 : rv);
} else if (data.type === 'randmNumCachedWeek ') {
const rv = this.getCachedRandomWeekNumber(_srcNode, parseFloat(data.value), dNow);
return (isNaN(rv) ? 0 : rv);
} else if (data.type === 'randmNumCachedDay') {
const val = data.value.split(/((?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?)/);
return this.getCachedRandomDayNumber(_srcNode, parseFloat(val[1]), parseFloat(val[3]), dNow);
// return (isNaN(rv) ? 0 : rv);
} else if (data.type === 'randmNumCachedWeek') {
const val = data.value.split(/((?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?)/);
return this.getCachedRandomWeekNumber(_srcNode, parseFloat(val[1]), parseFloat(val[3]), dNow);
// return (isNaN(rv) ? 0 : rv);
} else if (data.type === 'randomNum') {
data.value = parseFloat(data.value);
return Math.random() * ((data.value || 60) + 1);
const val = data.value.split(/((?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?)/);
const limit1 = parseFloat(val[1]);
if (isNaN(limit1)) {
_srcNode.error(`the value for random number limit is wrong "${data.value}", using 60`);
return Math.random() * 60;
}
const limit2 = parseFloat(val[3]);
const low = Math.min(limit1, isNaN(limit2) ? 0 : limit2);
const range = Math.max(limit1, isNaN(limit2) ? 0 : limit2) - low;
return low + (Math.random() * range);
} else if (data.type === 'PlT') {
if (msg.topic && data.value && msg.topic.includes(data.value)) {
result = msg.payload;
Expand Down
6 changes: 3 additions & 3 deletions nodes/static/htmlglobal.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,21 @@ function getTypes(node) { // eslint-disable-line no-unused-vars
label: node._('node-red-contrib-sun-position/position-config:common.types.randomNumber','randomNumber'),
icon: 'icons/node-red-contrib-sun-position/inputTypeRandomNumber.svg',
hasValue: true,
validate: RED.validators.number()
validate: RED.validators.regex(/^(?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?([\/|](?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?)?$/)()
},
randmNumCachedDay: {
value: 'randmNumCachedDay',
label: node._('node-red-contrib-sun-position/position-config:common.types.randmNumCachedDay','randmNumCachedDay'),
icon: 'icons/node-red-contrib-sun-position/inputTypeRandomNumber.svg',
hasValue: true,
validate: RED.validators.number()
validate: RED.validators.regex(/^(?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?([\/|](?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?)?$/)()
},
randmNumCachedWeek: {
value: 'randmNumCachedWeek',
label: node._('node-red-contrib-sun-position/position-config:common.types.randmNumCachedWeek','randmNumCachedWeek'),
icon: 'icons/node-red-contrib-sun-position/inputTypeRandomNumber.svg',
hasValue: true,
validate: RED.validators.number()
validate: RED.validators.regex(/^(?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?([\/|](?:[1-9]|-0\.|0\.|-)\d*(?:\.\d+)?)?$/)()
},
TimeSun: {
value: 'pdsTime',
Expand Down

0 comments on commit 9862843

Please sign in to comment.