Skip to content

Commit 24f7e17

Browse files
committed
fix(heliotrope): suppress duplicate warnings and guard against missing sunrise/sunset
#1184
1 parent c09f9a2 commit 24f7e17

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

controller/Constants.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export class Heliotrope {
136136
this.dt.getMonth() !== dt.getMonth() ||
137137
this.dt.getDate() !== dt.getDate()) {
138138
this.isCalculated = false;
139+
this._warningSuppressed = false;
139140
// Always store a copy since we don't want to create instances where the change doesn't get reflected. This
140141
// also could hold onto references that we don't want held for garbage cleanup.
141142
this.dt = typeof dt !== 'undefined' && typeof dt.getMonth === 'function' ? new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(),
@@ -146,19 +147,24 @@ export class Heliotrope {
146147
public set longitude(lon: number) {
147148
if (this._longitude !== lon) {
148149
this.isCalculated = false;
150+
this._warningSuppressed = false;
149151
}
150152
this._longitude = lon;
151153
}
152154
public get latitude() { return this._latitude; }
153155
public set latitude(lat: number) {
154156
if (this._latitude !== lat) {
155157
this.isCalculated = false;
158+
this._warningSuppressed = false;
156159
}
157160
this._latitude = lat;
158161
}
159162
public get zenith() { return this._zenith; }
160163
public set zenith(zen: number) {
161-
if (this._zenith !== zen) this.isCalculated = false;
164+
if (this._zenith !== zen) {
165+
this.isCalculated = false;
166+
this._warningSuppressed = false;
167+
}
162168
this._zenith = zen;
163169
}
164170
private dt: Date;
@@ -171,6 +177,7 @@ export class Heliotrope {
171177
private _dtNextSunset: Date;
172178
private _dtPrevSunrise: Date;
173179
private _dtPrevSunset: Date;
180+
private _warningSuppressed: boolean = false;
174181
public get isNight(): boolean {
175182
let times = this.calculatedTimes;
176183
if (this.isValid) {
@@ -214,10 +221,10 @@ export class Heliotrope {
214221
logger.verbose(`Calculated Heliotrope: sunrise:${Timestamp.toISOLocal(this._dtSunrise)} sunset:${Timestamp.toISOLocal(this._dtSunset)}`);
215222
}
216223
else {
217-
// Set isCalculated = true to prevent warning spam (6 warnings per minute).
218-
// When user sets valid lat/lon, the setters reset isCalculated = false, triggering recalculation.
219-
this.isCalculated = true;
220-
logger.warn(`dt:${this.dt} lat:${this._latitude} lon:${this._longitude} Not enough information to calculate Heliotrope. See https://github.com/tagyoureit/nodejs-poolController/issues/245`);
224+
if (!this._warningSuppressed) {
225+
logger.warn(`dt:${this.dt} lat:${this._latitude} lon:${this._longitude} Not enough information to calculate Heliotrope. See https://github.com/tagyoureit/nodejs-poolController/issues/245`);
226+
this._warningSuppressed = true;
227+
}
221228
}
222229
}
223230
public get sunrise(): Date {

controller/boards/AquaLinkBoard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class AquaLinkScheduleCommands extends ScheduleCommands {
223223

224224

225225
// If we have sunrise/sunset then adjust for the values; if heliotrope isn't set just ignore
226-
if (state.heliotrope.isCalculated) {
226+
if (state.heliotrope.isCalculated && state.heliotrope.sunrise && state.heliotrope.sunset) {
227227
const sunrise = state.heliotrope.sunrise.getHours() * 60 + state.heliotrope.sunrise.getMinutes();
228228
const sunset = state.heliotrope.sunset.getHours() * 60 + state.heliotrope.sunset.getMinutes();
229229
if (startTimeType === sys.board.valueMaps.scheduleTimeTypes.getValue('sunrise')) startTime = sunrise;

controller/boards/EasyTouchBoard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ export class TouchScheduleCommands extends ScheduleCommands {
764764

765765

766766
// If we have sunrise/sunset then adjust for the values; if heliotrope isn't set just ignore
767-
if (state.heliotrope.isCalculated) {
767+
if (state.heliotrope.isCalculated && state.heliotrope.sunrise && state.heliotrope.sunset) {
768768
const sunrise = state.heliotrope.sunrise.getHours() * 60 + state.heliotrope.sunrise.getMinutes();
769769
const sunset = state.heliotrope.sunset.getHours() * 60 + state.heliotrope.sunset.getMinutes();
770770
if (startTimeType === sys.board.valueMaps.scheduleTimeTypes.getValue('sunrise')) startTime = (sunrise + startTimeOffset);
@@ -963,7 +963,7 @@ export class TouchScheduleCommands extends ScheduleCommands {
963963
// This will check the schedule and if the existing sunrise/sunset times
964964
// are not matching the desired time it will update the time on the OCP.
965965
// https://github.com/tagyoureit/nodejs-poolController/discussions/560#discussioncomment-3362149
966-
if (!state.heliotrope.isCalculated) { return false; }
966+
if (!state.heliotrope.isCalculated || !state.heliotrope.sunrise || !state.heliotrope.sunset) { return false; }
967967
const sunrise = state.heliotrope.sunrise.getHours() * 60 + state.heliotrope.sunrise.getMinutes();
968968
const sunset = state.heliotrope.sunset.getHours() * 60 + state.heliotrope.sunset.getMinutes();
969969

0 commit comments

Comments
 (0)