From 1ca0dcc76b4bd2bf84085918e7f9b9808cb2e453 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 8 Aug 2024 18:29:52 +0300 Subject: [PATCH] ntp: more settings in webui sntp startup and update delays & dhcp toggle --- code/espurna/ntp.cpp | 47 ++++++++++++++++++++++++------------ code/html/src/local.mjs | 5 ++++ code/html/src/panel-ntp.html | 31 ++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/code/espurna/ntp.cpp b/code/espurna/ntp.cpp index 94b6f45c..e21001f1 100644 --- a/code/espurna/ntp.cpp +++ b/code/espurna/ntp.cpp @@ -89,9 +89,19 @@ namespace settings { STRING_VIEW_INLINE(Prefix, "ntp"); +namespace keys { + +STRING_VIEW_INLINE(StartDelay, "ntpStartDelay"); +STRING_VIEW_INLINE(UpdateInterval, "ntpUpdateIntvl"); +STRING_VIEW_INLINE(Server, "ntpServer"); +STRING_VIEW_INLINE(Tz, "ntpTZ"); +STRING_VIEW_INLINE(Dhcp, "ntpDhcp"); + +} // namespace keys + espurna::duration::Seconds startDelay() { return std::clamp( - getSetting("ntpStartDelay", build::StartDelay), + getSetting(keys::StartDelay, build::StartDelay), build::StartMin, build::StartMax); } @@ -101,7 +111,7 @@ espurna::duration::Seconds randomStartDelay() { espurna::duration::Seconds updateInterval() { return std::clamp( - getSetting("ntpUpdateIntvl", build::UpdateInterval), + getSetting(keys::UpdateInterval, build::UpdateInterval), build::UpdateMin, build::UpdateMax); } @@ -111,27 +121,27 @@ espurna::duration::Seconds randomUpdateInterval() { // as either DNS name or IP address String server() { - return getSetting("ntpServer", build::server()); + return getSetting(keys::Server, build::server()); } void server(const String& value) { - setSetting("ntpServer", value); + setSetting(keys::Server, value); } // POSIX TZ variable, ref. // - https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03 // - https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html String tz() { - return getSetting("ntpTZ", build::tz()); + return getSetting(keys::Tz, build::tz()); } // in case DHCP packet contains a SNTP option, switch to that server instead of the one from settings bool dhcp() { - return getSetting("ntpDhcp", build::dhcp()); + return getSetting(keys::Dhcp, build::dhcp()); } void dhcp(bool value) { - setSetting("ntpDhcp", value); + setSetting(keys::Dhcp, value); } } // namespace settings @@ -383,6 +393,8 @@ time_t now() { #if WEB_SUPPORT namespace web { +STRING_VIEW_INLINE(Status, "ntpStatus"); + bool onKeyCheck(StringView key, const JsonVariant&) { return key.startsWith(settings::Prefix); } @@ -392,12 +404,17 @@ void onVisible(JsonObject& root) { } void onData(JsonObject& root) { - root["ntpStatus"] = synced(); + root[Status] = synced(); } void onConnected(JsonObject& root) { - root["ntpServer"] = settings::server(); - root["ntpTZ"] = settings::tz(); + root[settings::keys::Dhcp] = settings::dhcp(); + + root[settings::keys::Server] = settings::server(); + root[settings::keys::Tz] = settings::tz(); + + root[settings::keys::StartDelay] = settings::startDelay().count(); + root[settings::keys::UpdateInterval] = settings::updateInterval().count(); } } // namespace web @@ -800,15 +817,15 @@ void onStationModeGotIP(WiFiEventStationModeGotIP) { const auto server = activeServer(); if (!server.length()) { - DEBUG_MSG_P(PSTR("[NTP] Updating `ntpDhcp` to ignore the DHCP values\n")); - settings::dhcp(false); - sntp_servermode_dhcp(0); - ::espurnaRegisterOnce(configure); + if (settings::dhcp()) { + settings::dhcp(false); + sntp_servermode_dhcp(0); + ::espurnaRegisterOnce(configure); + } return; } if (!internal::server.length() || (server != internal::server)) { - DEBUG_MSG_P(PSTR("[NTP] Updating from DHCP option - \"ntpServer\" => \"%s\"\n"), server.c_str()); internal::server = server; settings::server(server); } diff --git a/code/html/src/local.mjs b/code/html/src/local.mjs index ae4db7e9..c1823083 100644 --- a/code/html/src/local.mjs +++ b/code/html/src/local.mjs @@ -2,6 +2,11 @@ import { updateVariables } from './settings.mjs'; export function init() { updateVariables({ + ntpServer: "192.168.1.1", + ntpTZ: 'UTC-2', + ntpStartDelay: 10, + ntpDhcp: true, + ntpUpdateIntvl: 1800, webMode: 0, useWhite: false, useCCT: false, diff --git a/code/html/src/panel-ntp.html b/code/html/src/panel-ntp.html index 47ec12c5..692f32a8 100644 --- a/code/html/src/panel-ntp.html +++ b/code/html/src/panel-ntp.html @@ -11,13 +11,16 @@
- + + + IP or hostname +
- + POSIX TZ variable, defaults to UTC0 aka "Coordinated Universal Time". For example, basic offset is expressed as either UTC2 (meaning "UTC minus 2 hours"), or UTC-2 ("UTC plus 2 hours"). For the list of possible values based on IANA Time Zone Database, see our wiki entry. For the technical documentation and more complicated examples, see libc tzset(3) manual page @@ -51,6 +54,30 @@
+
+ + + + s (seconds) + +
+ +
+ + + + s (seconds) + +
+ +
+ + + + Always use NTP server provided by DHCP + +
+