Skip to content

Commit

Permalink
Merge branch 'pr1408' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Oct 11, 2023
2 parents c25e93e + 7f99095 commit 97d481f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
4 changes: 3 additions & 1 deletion include/InverterSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <cstdint>

#define INVERTER_UPDATE_SETTINGS_INTERVAL 60000l

class InverterSettingsClass {
public:
void init();
Expand All @@ -12,4 +14,4 @@ class InverterSettingsClass {
uint32_t _lastUpdate = 0;
};

extern InverterSettingsClass InverterSettings;
extern InverterSettingsClass InverterSettings;
12 changes: 8 additions & 4 deletions include/SunPosition.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <mutex>
#include <sunset.h>

#define SUNPOS_UPDATE_INTERVAL 60000l

class SunPositionClass {
public:
SunPositionClass();
Expand All @@ -15,18 +14,23 @@ class SunPositionClass {
bool isSunsetAvailable();
bool sunsetTime(struct tm* info);
bool sunriseTime(struct tm* info);
void setDoRecalc(bool doRecalc);

private:
void updateSunData();
bool checkRecalcDayChanged();
bool getDoRecalc();

SunSet _sun;
bool _isDayPeriod = true;
bool _isSunsetAvailable = true;
uint32_t _sunriseMinutes = 0;
uint32_t _sunsetMinutes = 0;

uint32_t _lastUpdate = 0;
bool _isValidInfo = false;
bool _doRecalc = true;
std::mutex _recalcLock;
uint32_t _lastSunPositionCalculatedYMD = 0;
};

extern SunPositionClass SunPosition;
extern SunPositionClass SunPosition;
4 changes: 2 additions & 2 deletions src/InverterSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void InverterSettingsClass::init()

void InverterSettingsClass::loop()
{
if (millis() - _lastUpdate > SUNPOS_UPDATE_INTERVAL) {
if (millis() - _lastUpdate > INVERTER_UPDATE_SETTINGS_INTERVAL) {
const CONFIG_T& config = Configuration.get();

for (uint8_t i = 0; i < INV_MAX_COUNT; i++) {
Expand All @@ -109,4 +109,4 @@ void InverterSettingsClass::loop()
}

Hoymiles.loop();
}
}
55 changes: 47 additions & 8 deletions src/SunPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ void SunPositionClass::init()

void SunPositionClass::loop()
{
if (millis() - _lastUpdate > SUNPOS_UPDATE_INTERVAL) {
if (getDoRecalc() || checkRecalcDayChanged()) {
updateSunData();
_lastUpdate = millis();
}
}

Expand All @@ -35,21 +34,61 @@ bool SunPositionClass::isSunsetAvailable()
return _isSunsetAvailable;
}

void SunPositionClass::updateSunData()
void SunPositionClass::setDoRecalc(bool doRecalc)
{
CONFIG_T const& config = Configuration.get();
int offset = Utils::getTimezoneOffset() / 3600;
_sun.setPosition(config.Ntp_Latitude, config.Ntp_Longitude, offset);
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;
if (!getLocalTime(&timeinfo, 5)) {
bool gotLocalTime;

gotLocalTime = getLocalTime(&timeinfo, 5);
_lastSunPositionCalculatedYMD = (timeinfo.tm_year << 9) |
(timeinfo.tm_mon << 5) |
timeinfo.tm_mday;
setDoRecalc(false);

if (!gotLocalTime) {
_isDayPeriod = true;
_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;
Expand Down Expand Up @@ -125,4 +164,4 @@ bool SunPositionClass::sunriseTime(struct tm* info)

localtime_r(&midnight, info);
return _isValidInfo;
}
}
4 changes: 3 additions & 1 deletion src/WebApi_ntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ void WebApiNtpClass::onNtpAdminPost(AsyncWebServerRequest* request)

NtpSettings.setServer();
NtpSettings.setTimezone();

SunPosition.setDoRecalc(true);
}

void WebApiNtpClass::onNtpTimeGet(AsyncWebServerRequest* request)
Expand Down Expand Up @@ -350,4 +352,4 @@ void WebApiNtpClass::onNtpTimePost(AsyncWebServerRequest* request)

response->setLength();
request->send(response);
}
}

0 comments on commit 97d481f

Please sign in to comment.