mirror of
https://github.com/tbnobody/OpenDTU.git
synced 2025-12-13 02:09:58 +01:00
Simplify code for sunset calculation
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2023-2024 Thomas Basler and others
|
* Copyright (C) 2023-2025 Thomas Basler and others
|
||||||
*/
|
*/
|
||||||
#include "SunPosition.h"
|
#include "SunPosition.h"
|
||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#define CALC_UNIQUE_ID (((timeinfo.tm_year << 9) | (timeinfo.tm_mon << 5) | timeinfo.tm_mday) << 1 | timeinfo.tm_isdst)
|
#define CALC_UNIQUE_ID(tm) (((tm.tm_year << 9) | (tm.tm_mon << 5) | tm.tm_mday) << 1 | tm.tm_isdst)
|
||||||
|
|
||||||
SunPositionClass SunPosition;
|
SunPositionClass SunPosition;
|
||||||
|
|
||||||
@@ -35,9 +35,9 @@ bool SunPositionClass::isDayPeriod() const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct tm timeinfo;
|
time_t now = time(NULL);
|
||||||
getLocalTime(&timeinfo, 5);
|
struct tm tm = *localtime(&now);
|
||||||
const uint32_t minutesPastMidnight = timeinfo.tm_hour * 60 + timeinfo.tm_min;
|
const uint32_t minutesPastMidnight = tm.tm_hour * 60 + tm.tm_min;
|
||||||
return (minutesPastMidnight >= _sunriseMinutes) && (minutesPastMidnight < _sunsetMinutes);
|
return (minutesPastMidnight >= _sunriseMinutes) && (minutesPastMidnight < _sunsetMinutes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,15 +53,10 @@ void SunPositionClass::setDoRecalc(const bool doRecalc)
|
|||||||
|
|
||||||
bool SunPositionClass::checkRecalcDayChanged() const
|
bool SunPositionClass::checkRecalcDayChanged() const
|
||||||
{
|
{
|
||||||
time_t now;
|
time_t now = time(NULL);
|
||||||
struct tm timeinfo;
|
struct tm tm = *localtime(&now);
|
||||||
|
|
||||||
time(&now);
|
return _lastSunPositionCalculatedYMD != CALC_UNIQUE_ID(tm);
|
||||||
localtime_r(&now, &timeinfo); // don't use getLocalTime() as there could be a delay of 10ms
|
|
||||||
|
|
||||||
const uint32_t ymd = CALC_UNIQUE_ID;
|
|
||||||
|
|
||||||
return _lastSunPositionCalculatedYMD != ymd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SunPositionClass::updateSunData()
|
void SunPositionClass::updateSunData()
|
||||||
@@ -69,7 +64,7 @@ void SunPositionClass::updateSunData()
|
|||||||
struct tm timeinfo;
|
struct tm timeinfo;
|
||||||
const bool gotLocalTime = getLocalTime(&timeinfo, 5);
|
const bool gotLocalTime = getLocalTime(&timeinfo, 5);
|
||||||
|
|
||||||
_lastSunPositionCalculatedYMD = CALC_UNIQUE_ID;
|
_lastSunPositionCalculatedYMD = CALC_UNIQUE_ID(timeinfo);
|
||||||
setDoRecalc(false);
|
setDoRecalc(false);
|
||||||
|
|
||||||
if (!gotLocalTime) {
|
if (!gotLocalTime) {
|
||||||
@@ -126,18 +121,15 @@ void SunPositionClass::updateSunData()
|
|||||||
|
|
||||||
bool SunPositionClass::getSunTime(struct tm* info, const uint32_t offset) const
|
bool SunPositionClass::getSunTime(struct tm* info, const uint32_t offset) const
|
||||||
{
|
{
|
||||||
// Get today's date
|
time_t now = time(NULL);
|
||||||
time_t aTime = time(NULL);
|
struct tm tm = *localtime(&now);
|
||||||
|
|
||||||
// Set the time to midnight
|
|
||||||
struct tm tm;
|
|
||||||
localtime_r(&aTime, &tm);
|
|
||||||
tm.tm_sec = 0;
|
tm.tm_sec = 0;
|
||||||
tm.tm_min = offset;
|
tm.tm_min = offset;
|
||||||
tm.tm_hour = 0;
|
tm.tm_hour = 0;
|
||||||
tm.tm_isdst = -1;
|
tm.tm_isdst = -1;
|
||||||
const time_t midnight = mktime(&tm);
|
|
||||||
|
|
||||||
|
const time_t midnight = mktime(&tm);
|
||||||
localtime_r(&midnight, info);
|
localtime_r(&midnight, info);
|
||||||
return _isValidInfo;
|
return _isValidInfo;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user