feat: added ntp server and timezone settings

This commit is contained in:
Yurii
2025-01-30 01:25:05 +03:00
parent cc2d6ef385
commit 3bc9fa81a8
10 changed files with 562 additions and 0 deletions

View File

@@ -38,6 +38,7 @@ protected:
unsigned long heatingDisabledTime = 0;
PumpStartReason extPumpStartReason = PumpStartReason::NONE;
unsigned long externalPumpStartTime = 0;
bool ntpStarted = false;
bool telnetStarted = false;
bool emergencyDetected = false;
unsigned long emergencyFlipTime = 0;
@@ -109,6 +110,16 @@ protected:
}
if (network->isConnected()) {
if (!this->ntpStarted) {
if (strlen(settings.system.ntp.server)) {
configTime(0, 0, settings.system.ntp.server);
setenv("TZ", settings.system.ntp.timezone, 1);
tzset();
this->ntpStarted = true;
}
}
if (!this->telnetStarted && telnetStream != nullptr) {
telnetStream->begin(23, false);
this->telnetStarted = true;
@@ -124,6 +135,10 @@ protected:
Sensors::setConnectionStatusByType(Sensors::Type::MANUAL, !settings.mqtt.enabled || vars.mqtt.connected, false);
} else {
if (this->ntpStarted) {
this->ntpStarted = false;
}
if (this->telnetStarted) {
telnetStream->stop();
this->telnetStarted = false;

View File

@@ -36,6 +36,11 @@ struct Settings {
unsigned short port = DEFAULT_TELNET_PORT;
} telnet;
struct {
char server[49] = "pool.ntp.org";
char timezone[49] = "UTC0";
} ntp;
UnitSystem unitSystem = UnitSystem::METRIC;
byte statusLedGpio = DEFAULT_STATUS_LED_GPIO;
} system;

View File

@@ -135,6 +135,7 @@ const char S_MQTT[] PROGMEM = "mqtt";
const char S_NAME[] PROGMEM = "name";
const char S_NATIVE_HEATING_CONTROL[] PROGMEM = "nativeHeatingControl";
const char S_NETWORK[] PROGMEM = "network";
const char S_NTP[] PROGMEM = "ntp";
const char S_N_FACTOR[] PROGMEM = "n_factor";
const char S_OFFSET[] PROGMEM = "offset";
const char S_ON_ENABLED_HEATING[] PROGMEM = "onEnabledHeating";
@@ -190,6 +191,7 @@ const char S_TEMPERATURE[] PROGMEM = "temperature";
const char S_THRESHOLD_HIGH[] PROGMEM = "thresholdHigh";
const char S_THRESHOLD_LOW[] PROGMEM = "thresholdLow";
const char S_THRESHOLD_TIME[] PROGMEM = "thresholdTime";
const char S_TIMEZONE[] PROGMEM = "timezone";
const char S_TOTAL[] PROGMEM = "total";
const char S_TRESHOLD_TIME[] PROGMEM = "tresholdTime";
const char S_TURBO[] PROGMEM = "turbo";

View File

@@ -359,6 +359,10 @@ void settingsToJson(const Settings& src, JsonVariant dst, bool safe = false) {
telnet[FPSTR(S_ENABLED)] = src.system.telnet.enabled;
telnet[FPSTR(S_PORT)] = src.system.telnet.port;
auto ntp = system[FPSTR(S_NTP)].to<JsonObject>();
ntp[FPSTR(S_SERVER)] = src.system.ntp.server;
ntp[FPSTR(S_TIMEZONE)] = src.system.ntp.timezone;
system[FPSTR(S_UNIT_SYSTEM)] = static_cast<uint8_t>(src.system.unitSystem);
system[FPSTR(S_STATUS_LED_GPIO)] = src.system.statusLedGpio;
@@ -527,6 +531,24 @@ bool jsonToSettings(const JsonVariantConst src, Settings& dst, bool safe = false
}
}
if (!src[FPSTR(S_SYSTEM)][FPSTR(S_NTP)][FPSTR(S_SERVER)].isNull()) {
String value = src[FPSTR(S_SYSTEM)][FPSTR(S_NTP)][FPSTR(S_SERVER)].as<String>();
if (value.length() < sizeof(dst.system.ntp.server) && !value.equals(dst.system.ntp.server)) {
strcpy(dst.system.ntp.server, value.c_str());
changed = true;
}
}
if (!src[FPSTR(S_SYSTEM)][FPSTR(S_NTP)][FPSTR(S_TIMEZONE)].isNull()) {
String value = src[FPSTR(S_SYSTEM)][FPSTR(S_NTP)][FPSTR(S_TIMEZONE)].as<String>();
if (value.length() < sizeof(dst.system.ntp.timezone) && !value.equals(dst.system.ntp.timezone)) {
strcpy(dst.system.ntp.timezone, value.c_str());
changed = true;
}
}
if (!src[FPSTR(S_SYSTEM)][FPSTR(S_UNIT_SYSTEM)].isNull()) {
uint8_t value = src[FPSTR(S_SYSTEM)][FPSTR(S_UNIT_SYSTEM)].as<unsigned char>();
UnitSystem prevUnitSystem = dst.system.unitSystem;