forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SunPosition.cpp
165 lines (136 loc) · 3.76 KB
/
SunPosition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2023 Thomas Basler and others
*/
#include "SunPosition.h"
#include "Configuration.h"
#include "Utils.h"
#include <Arduino.h>
SunPositionClass SunPosition;
SunPositionClass::SunPositionClass()
{
}
void SunPositionClass::init()
{
}
void SunPositionClass::loop()
{
if (getDoRecalc() || checkRecalcDayChanged()) {
updateSunData();
}
}
bool SunPositionClass::isDayPeriod()
{
if (!_isValidInfo) {
return true;
}
struct tm timeinfo;
getLocalTime(&timeinfo, 5);
uint32_t minutesPastMidnight = timeinfo.tm_hour * 60 + timeinfo.tm_min;
return (minutesPastMidnight >= _sunriseMinutes) && (minutesPastMidnight < _sunsetMinutes);
}
bool SunPositionClass::isSunsetAvailable()
{
return _isSunsetAvailable;
}
void SunPositionClass::setDoRecalc(bool doRecalc)
{
std::lock_guard<std::mutex> lock(_recalcLock);
_doRecalc = doRecalc;
}
bool SunPositionClass::getDoRecalc()
{
std::lock_guard<std::mutex> lock(_recalcLock);
return _doRecalc;
}
bool SunPositionClass::checkRecalcDayChanged()
{
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo); // don't use getLocalTime() as there could be a delay of 10ms
uint32_t ymd;
ymd = (timeinfo.tm_year << 9) | (timeinfo.tm_mon << 5) | timeinfo.tm_mday;
if (_lastSunPositionCalculatedYMD != ymd) {
return true;
}
return false;
}
void SunPositionClass::updateSunData()
{
struct tm timeinfo;
bool gotLocalTime;
gotLocalTime = getLocalTime(&timeinfo, 5);
_lastSunPositionCalculatedYMD = (timeinfo.tm_year << 9) | (timeinfo.tm_mon << 5) | timeinfo.tm_mday;
setDoRecalc(false);
if (!gotLocalTime) {
_sunriseMinutes = 0;
_sunsetMinutes = 0;
_isValidInfo = false;
return;
}
CONFIG_T const& config = Configuration.get();
int offset = Utils::getTimezoneOffset() / 3600;
_sun.setPosition(config.Ntp_Latitude, config.Ntp_Longitude, offset);
_sun.setCurrentDate(1900 + timeinfo.tm_year, timeinfo.tm_mon + 1, timeinfo.tm_mday);
double sunset_type;
switch (config.Ntp_SunsetType) {
case 0:
sunset_type = SunSet::SUNSET_OFFICIAL;
break;
case 2:
sunset_type = SunSet::SUNSET_CIVIL;
break;
case 3:
sunset_type = SunSet::SUNSET_ASTONOMICAL;
break;
default:
sunset_type = SunSet::SUNSET_NAUTICAL;
break;
}
double sunriseRaw = _sun.calcCustomSunrise(sunset_type);
double sunsetRaw = _sun.calcCustomSunset(sunset_type);
// If no sunset/sunrise exists (e.g. astronomical calculation in summer)
// assume it's day period
if (std::isnan(sunriseRaw) || std::isnan(sunsetRaw)) {
_isSunsetAvailable = false;
_sunriseMinutes = 0;
_sunsetMinutes = 0;
_isValidInfo = false;
return;
}
_sunriseMinutes = static_cast<int>(sunriseRaw);
_sunsetMinutes = static_cast<int>(sunsetRaw);
_isSunsetAvailable = true;
_isValidInfo = true;
}
bool SunPositionClass::sunsetTime(struct tm* info)
{
// Get today's date
time_t aTime = time(NULL);
// Set the time to midnight
struct tm tm;
localtime_r(&aTime, &tm);
tm.tm_sec = 0;
tm.tm_min = _sunsetMinutes;
tm.tm_hour = 0;
tm.tm_isdst = -1;
time_t midnight = mktime(&tm);
localtime_r(&midnight, info);
return _isValidInfo;
}
bool SunPositionClass::sunriseTime(struct tm* info)
{
// Get today's date
time_t aTime = time(NULL);
// Set the time to midnight
struct tm tm;
localtime_r(&aTime, &tm);
tm.tm_sec = 0;
tm.tm_min = _sunriseMinutes;
tm.tm_hour = 0;
tm.tm_isdst = -1;
time_t midnight = mktime(&tm);
localtime_r(&midnight, info);
return _isValidInfo;
}