Skip to content

Commit

Permalink
fix #119
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypnos3 committed Mar 31, 2020
1 parent f6ed90b commit 2b0ef26
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion nodes/lib/dateTimeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ function isValidDate(d) {
*/
function addOffset(d, offset, multiplier) {
if (offset && !isNaN(offset) && offset !== 0) {
if (offset !== 0 && multiplier > 0) {
if (multiplier > 0) {
return new Date(d.getTime() + Math.floor(offset * multiplier));
} else if (multiplier === -1) {
d.setMonth(Math.floor(d.getMonth() + offset));
Expand Down
10 changes: 6 additions & 4 deletions nodes/position-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ module.exports = function (RED) {
* @return {timeresult|erroresult} result object of sunTime
*/
getSunTimeByName(dNow, value, offset, multiplier, limit, latitude, longitude) {
// this.debug('getSunTimeByName dNow=' + dNow + ' limit=' + util.inspect(limit, { colors: true, compact: 10, breakLength: Infinity }));
let result;
const dayId = hlp.getDayId(dNow); // this._getUTCDayId(dNow);
if (latitude && longitude) {
Expand Down Expand Up @@ -240,7 +241,7 @@ module.exports = function (RED) {
result.value = hlp.addOffset(new Date(result.value), offset, multiplier);
if (limit.next && result.value.getTime() <= dNow.getTime()) {
if (dayId === this.cache.sunTimesToday.dayId) {
result = Object.assign(result, this.cache.sunTimesTomorrow[value]);
result = Object.assign({}, this.cache.sunTimesTomorrow.times[value]);
result.value = hlp.addOffset(new Date(result.value), offset, multiplier);
}
const datebase = new Date(dNow);
Expand All @@ -263,6 +264,7 @@ module.exports = function (RED) {
result.error = 'No valid day of week found!';
}
}

if (limit.months && (limit.months !== '*') && (limit.months !== '')) {
const monthx = hlp.calcMonthOffset(limit.months, result.value.getMonth());
if (monthx > 0) {
Expand Down Expand Up @@ -1217,15 +1219,15 @@ module.exports = function (RED) {
_sunTimesRefresh(todayValue, dayId) {
this._checkCoordinates();
if (this.cache.sunTimesToday.dayId === (dayId + 1)) {
this.cache.sunTimesToday.dayId = this.cache.sunTimesTomorrow.dayId;
this.cache.sunTimesToday.times = this.cache.sunTimesTomorrow.times;
this.cache.sunTimesToday.sunPosAtSolarNoon = this.cache.sunTimesTomorrow.sunPosAtSolarNoon;
this.cache.sunTimesTomorrow = {};
this.cache.sunTimesToday.dayId = this.cache.sunTimesTomorrow.dayId;
} else {
this.cache.sunTimesToday.dayId = dayId;
this.cache.sunTimesToday.times = sunCalc.getSunTimes(todayValue, this.latitude, this.longitude);
this.cache.sunTimesToday.sunPosAtSolarNoon = sunCalc.getPosition(this.cache.sunTimesToday.times.solarNoon.value.valueOf(), this.latitude, this.longitude);
this.cache.sunTimesToday.dayId = dayId;
}
this.cache.sunTimesTomorrow = {};
this.cache.sunTimesTomorrow.times = sunCalc.getSunTimes(todayValue + dayMs, this.latitude, this.longitude);
this.cache.sunTimesTomorrow.sunPosAtSolarNoon = sunCalc.getPosition(this.cache.sunTimesTomorrow.times.solarNoon.value.valueOf(), this.latitude, this.longitude);
this.cache.sunTimesTomorrow.dayId = (dayId + 1);
Expand Down
23 changes: 16 additions & 7 deletions nodes/time-inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,20 @@ module.exports = function (RED) {
* creates the timeout
* @param {*} node - the node representation
* @param {boolean} [_onInit] - _true_ if is in initialisation
* @param {Date} [dNow] - Date object with the calculation base
* @returns {object} state or error
*/
function doCreateTimeout(node, _onInit) {
node.debug(`doCreateTimeout _onInit=${_onInit} node=${util.inspect(node, { colors: true, compact: 10, breakLength: Infinity })}`);
function doCreateTimeout(node, _onInit, dNow) {
node.debug(`doCreateTimeout _onInit=${_onInit}`);
let errorStatus = '';
let warnStatus = '';
let isAltFirst = false;
let isFixedTime = true;
let timeValid = (node.timeData && node.timeData.type !== 'none');
let timeAltValid = (node.timeAltData && node.propertyType !== 'none' && node.timeAltData.type !== 'none');
node.timeData.now = dNow || new Date();
node.timeAltData.now = node.timeData.now;

node.nextTime = null;
node.nextTimeAlt = null;

Expand Down Expand Up @@ -402,11 +406,12 @@ module.exports = function (RED) {
// If this is pre-1.0, 'done' will be undefined
done = done || function (text, msg) { if (text) { return node.error(text, msg); } return null; };
send = send || function (...args) { node.send.apply(node, args); };
let dNow = new Date();

try {
msg._srcid = node.id;
node.debug('--------- time-inject - input (type=' + msg.type + ')');
doCreateTimeout(node);
doCreateTimeout(node, false, dNow);
msg.topic = config.topic;
if (!node.positionConfig) {
throw new Error('configuration missing!');
Expand All @@ -418,7 +423,8 @@ module.exports = function (RED) {
offsetType: config.payloadOffsetType,
offset: config.payloadOffset,
multiplier: config.payloadOffsetMultiplier,
next: true
next: true,
now: dNow
});
if (value === null || (typeof value === 'undefined')) {
throw new Error('could not evaluate ' + config.payloadType + '.' + config.payload);
Expand All @@ -440,7 +446,8 @@ module.exports = function (RED) {
offset: config.addPayload1Offset,
multiplier: config.addPayload1OffsetMultiplier,
next: config.addPayload1Next,
days: config.addPayload1Days
days: config.addPayload1Days,
now: dNow
});
tsSetAddProp(this, msg, {
outType: config.addPayload2Type,
Expand All @@ -452,7 +459,8 @@ module.exports = function (RED) {
offset: config.addPayload2Offset,
multiplier: config.addPayload2OffsetMultiplier,
next: config.addPayload2Next,
days: config.addPayload2Days
days: config.addPayload2Days,
now: dNow
});
tsSetAddProp(this, msg, {
outType: config.addPayload3Type,
Expand All @@ -464,7 +472,8 @@ module.exports = function (RED) {
offset: config.addPayload3Offset,
multiplier: config.addPayload3OffsetMultiplier,
next: config.addPayload3Next,
days: config.addPayload3Days
days: config.addPayload3Days,
now: dNow
});
}
send(msg); // node.send(msg);
Expand Down

0 comments on commit 2b0ef26

Please sign in to comment.