Skip to content

Commit

Permalink
Added warning if latitude or longitude is not configured correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Mar 15, 2024
1 parent b3d9c13 commit e7fdeee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -50,6 +50,7 @@ Since v5.5.0 of the JavaScript adapter the following locations (relative to the

* (klein0r) Configurable trigger warning limit (default: 100 per script)
* (klein0r) Allow to use objects in create state blocks for common
* (klein0r) Added warning if latitude or longitude is not configured correctly

### 7.9.0 (2024-03-13)

Expand Down
76 changes: 47 additions & 29 deletions main.js
Expand Up @@ -1457,37 +1457,43 @@ function formatHoursMinutesSeconds(date) {

async function sunTimeSchedules(adapter, context) {
if (adapter.config.createAstroStates) {
const calcDate = getAstroStartOfDay();

const times = mods.suncalc.getTimes(calcDate, adapter.config.latitude, adapter.config.longitude);

for (const t in times) {
const timeFormatted = formatHoursMinutesSeconds(times[t]);
const objId = `variables.astro.${t}`;

await adapter.setObjectNotExistsAsync(objId, {
type: 'state',
common: {
name: `Astro ${t}`,
type: 'string',
role: 'value',
read: true,
write: false,
},
native: {},
});
await adapter.setStateAsync(objId, { val: timeFormatted, c: times[t].toISOString(), ack: true });
}
if (!isNaN(adapter.config.longitude) && !isNaN(adapter.config.longitude)) {
const calcDate = getAstroStartOfDay();

const todayDate = new Date();
todayDate.setHours(0);
todayDate.setMinutes(0);
todayDate.setSeconds(1);
todayDate.setMilliseconds(0);
todayDate.setDate(todayDate.getDate() + 1);
const times = mods.suncalc.getTimes(calcDate, adapter.config.latitude, adapter.config.longitude);

for (const t in times) {
try {
const timeFormatted = formatHoursMinutesSeconds(times[t]);
const objId = `variables.astro.${t}`;

adapter.log.debug(`[sunTimeSchedules] Next: ${todayDate.toISOString()}`);
sunScheduleTimer = setTimeout(sunTimeSchedules, todayDate.getTime() - Date.now(), adapter, context);
await adapter.setObjectNotExistsAsync(objId, {
type: 'state',
common: {
name: `Astro ${t}`,
type: 'string',
role: 'value',
read: true,
write: false,
},
native: {},
});
await adapter.setStateAsync(objId, { val: timeFormatted, c: times[t].toISOString(), ack: true });
} catch (err) {
adapter.log.error(`[sunTimeSchedules] Unable to set state for astro time "${t}"`);
}
}

const todayDate = new Date();
todayDate.setHours(0);
todayDate.setMinutes(0);
todayDate.setSeconds(1);
todayDate.setMilliseconds(0);
todayDate.setDate(todayDate.getDate() + 1);

adapter.log.debug(`[sunTimeSchedules] Next: ${todayDate.toISOString()}`);
sunScheduleTimer = setTimeout(sunTimeSchedules, todayDate.getTime() - Date.now(), adapter, context);
}
} else {
// remove astro states if disabled
adapter.delObject('variables.astro', { recursive: true });
Expand Down Expand Up @@ -2303,6 +2309,18 @@ async function getData(callback) {
adapter.config.latitude = parseFloat(adapter.config.latitude);
adapter.config.longitude = parseFloat(adapter.config.longitude);

if (isNaN(adapter.config.latitude)) {
adapter.log.warn(`Configured latitude is not a number - check (instance/system) configuration`);
} else if (adapter.config.latitude < -90 || adapter.config.latitude > 90) {
adapter.log.warn(`Configured latitude "${adapter.config.latitude}" is invalid - check (instance/system) configuration`);
}

if (isNaN(adapter.config.longitude)) {
adapter.log.warn(`Configured longitude is not a number - check (instance/system) configuration`);
} else if (adapter.config.longitude < -180 || adapter.config.longitude > 180) {
adapter.log.warn(`Configured longitude "${adapter.config.longitude}" is invalid - check (instance/system) configuration`);
}

adapter.config.sunriseEvent = adapter.config.sunriseEvent || 'nightEnd';
adapter.config.sunriseOffset = adapter.config.sunriseOffset || 0;
adapter.config.sunriseLimitStart = adapter.config.sunriseLimitStart || '06:00';
Expand Down

0 comments on commit e7fdeee

Please sign in to comment.