From 13cbc0310a054309db595451a787bc10f0ab5ca2 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 31 Mar 2021 04:59:09 +0300 Subject: [PATCH] ntp: simplify tick callback, dont use broker --- code/espurna/ntp.cpp | 70 ++++++++++++++++++++------------------ code/espurna/ntp.h | 10 ++---- code/espurna/rpnrules.cpp | 2 +- code/espurna/scheduler.cpp | 19 ++++++----- 4 files changed, 50 insertions(+), 51 deletions(-) diff --git a/code/espurna/ntp.cpp b/code/espurna/ntp.cpp index 21a902ea..756cafbe 100644 --- a/code/espurna/ntp.cpp +++ b/code/espurna/ntp.cpp @@ -22,6 +22,8 @@ Copyright (C) 2019 by Maxim Prokhorov #include #include +#include + static_assert( (SNTP_SERVER_DNS == 1), "lwip must be configured with SNTP_SERVER_DNS" @@ -30,11 +32,8 @@ static_assert( #include "config/buildtime.h" #include "ntp_timelib.h" -#include "broker.h" #include "ws.h" -BrokerBind(NtpBroker); - // Arduino/esp8266 lwip2 custom functions that can be redefined // Must return time in milliseconds, legacy settings are in seconds. @@ -54,7 +53,6 @@ uint32_t sntp_update_delay_MS_rfc_not_less_than_15000() { // We also must shim TimeLib functions until everything else is ported. // We can't sometimes avoid TimeLib as dependancy though, which would be really bad -static Ticker _ntp_broker_timer; static bool _ntp_synced = false; static time_t _ntp_last = 0; @@ -296,20 +294,20 @@ String ntpDateTime() { // ----------------------------------------------------------------------------- -#if BROKER_SUPPORT +using NtpTickCallbacks = std::forward_list; +NtpTickCallbacks _ntp_tick_callbacks; -void _ntpBrokerSchedule(int offset); +static Ticker _ntp_tick; -void _ntpBrokerCallback() { +void _ntpTickSchedule(int offset); +void _ntpTickCallback() { if (!ntpSynced()) { - _ntpBrokerSchedule(60); + _ntpTickSchedule(60); return; } const auto ts = now(); - - // current time and formatter string is in local TZ tm local_tm; localtime_r(&ts, &local_tm); @@ -319,48 +317,48 @@ void _ntpBrokerCallback() { static int last_hour = -1; static int last_minute = -1; - String datetime; - - if ((last_minute != now_minute) || (last_hour != now_hour)) { - datetime = ntpDateTime(&local_tm); - } - // notify subscribers about each tick interval (note that both can happen simultaneously) if (last_hour != now_hour) { last_hour = now_hour; - NtpBroker::Publish(NtpTick::EveryHour, ts, datetime); + for (auto& callback : _ntp_tick_callbacks) { + callback(NtpTick::EveryHour); + } } if (last_minute != now_minute) { last_minute = now_minute; - NtpBroker::Publish(NtpTick::EveryMinute, ts, datetime); + for (auto& callback : _ntp_tick_callbacks) { + callback(NtpTick::EveryMinute); + } } // try to autocorrect each invocation - _ntpBrokerSchedule(60 - local_tm.tm_sec); - + _ntpTickSchedule(60 - local_tm.tm_sec); } -// XXX: Nonos docs for some reason mention 100 micros as minimum time. Schedule next second in case this is 0 -void _ntpBrokerSchedule(int offset) { - _ntp_broker_timer.once_scheduled(offset ?: 1, _ntpBrokerCallback); +// XXX: NONOS SDK docs for some reason mention 100 micros as minimum time. Schedule next second in case this is 0 +void _ntpTickSchedule(int offset) { + static bool scheduled { false }; + if (!scheduled) { + scheduled = true; + _ntp_tick.once_scheduled(offset ? offset : 1, []() { + scheduled = false; + _ntpTickCallback(); + }); + } } -#endif - void _ntpSetTimeOfDayCallback() { _ntp_synced = true; _ntp_last = time(nullptr); - #if BROKER_SUPPORT static bool once = true; if (once) { - schedule_function(_ntpBrokerCallback); + schedule_function(_ntpTickCallback); once = false; } - #endif - #if WEB_SUPPORT - wsPost(_ntpWebSocketOnData); - #endif +#if WEB_SUPPORT + wsPost(_ntpWebSocketOnData); +#endif schedule_function(_ntpReport); } @@ -385,13 +383,13 @@ void _ntpConvertLegacyOffsets() { if (key == F("ntpTZ")) { save = false; } else if (key == F("ntpOffset")) { - offset = kv.value.read().toInt(); + offset = settings::internal::convert(kv.value.read()); found = true; } else if (key == F("ntpDST")) { - dst = (1 == kv.value.read().toInt()); + dst = settings::internal::convert(kv.value.read()); found = true; } else if (key == F("ntpRegion")) { - europe = (0 == kv.value.read().toInt()); + europe = (0 == settings::internal::convert(kv.value.read())); found = true; } }); @@ -423,6 +421,10 @@ void _ntpConvertLegacyOffsets() { delSetting("ntpRegion"); } +void ntpOnTick(NtpTickCallback callback) { + _ntp_tick_callbacks.push_front(callback); +} + void ntpSetup() { // Randomized in time to avoid clogging the server with simultaneous requests from multiple devices diff --git a/code/espurna/ntp.h b/code/espurna/ntp.h index 3f4e0398..d4756cfa 100644 --- a/code/espurna/ntp.h +++ b/code/espurna/ntp.h @@ -10,13 +10,14 @@ Copyright (C) 2019-2021 by Maxim Prokhorov