align timing with EMS-ESP

This commit is contained in:
Proddy
2023-02-26 20:59:03 +01:00
parent 4c186606bd
commit 84c90dd587
3 changed files with 42 additions and 37 deletions

View File

@@ -49,7 +49,7 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) {
void NTPSettingsService::configureNTP() { void NTPSettingsService::configureNTP() {
emsesp::EMSESP::system_.ntp_connected(false); emsesp::EMSESP::system_.ntp_connected(false);
if (connected_ && _state.enabled) { if (connected_ && _state.enabled) {
emsesp::EMSESP::logger().info("Starting NTP"); emsesp::EMSESP::logger().info("Starting NTP service");
sntp_set_sync_interval(3600000); // one hour sntp_set_sync_interval(3600000); // one hour
sntp_set_time_sync_notification_cb(ntp_received); sntp_set_time_sync_notification_cb(ntp_received);
configTzTime(_state.tzFormat.c_str(), _state.server.c_str()); configTzTime(_state.tzFormat.c_str(), _state.server.c_str());

View File

@@ -252,7 +252,7 @@ void System::syslog_init() {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
if (syslog_enabled_) { if (syslog_enabled_) {
// start & configure syslog // start & configure syslog
EMSESP::logger().info("Starting Syslog"); EMSESP::logger().info("Starting Syslog service");
syslog_.start(); syslog_.start();
syslog_.log_level((uuid::log::Level)syslog_level_); syslog_.log_level((uuid::log::Level)syslog_level_);

View File

@@ -30,7 +30,7 @@ WebSchedulerService::WebSchedulerService(AsyncWebServer * server, FS * fs, Secur
// load the settings when the service starts // load the settings when the service starts
void WebSchedulerService::begin() { void WebSchedulerService::begin() {
_fsPersistence.readFromFS(); _fsPersistence.readFromFS();
EMSESP::logger().info("Starting Scheduler"); EMSESP::logger().info("Starting Scheduler service");
} }
// this creates the scheduler file, saving it to the FS // this creates the scheduler file, saving it to the FS
@@ -102,52 +102,57 @@ void WebSchedulerService::loop() {
time_t now = time(nullptr); time_t now = time(nullptr);
tm * tm = localtime(&now); tm * tm = localtime(&now);
static int last_min = 0; static int last_min = 100; // some high marker
static uint32_t starttime = now / 60; auto uptime_seconds = uuid::get_uptime_sec(); // sync to EMS-ESP clock
auto uptime_min = uptime_seconds / 60; // sync to EMS-ESP clock
// check if we're on the minute // check if we're on the minute - taking the EMS-ESP uptime
if (tm->tm_min == last_min || tm->tm_year < 122) { // with the exception of allowing the first pass (on boot) through
if (uptime_min == last_min || tm->tm_year < 122) {
return; return;
} }
last_min = uptime_min;
// find the real dow and minute from NTP or RTC
uint8_t real_dow = 1 << tm->tm_wday; // 1 is Sunday
uint16_t real_min = tm->tm_hour * 60 + tm->tm_min;
last_min = tm->tm_min;
bool has_NTP = tm->tm_year > 120; bool has_NTP = tm->tm_year > 120;
uint8_t dow = 1 << tm->tm_wday; // 1 is Sunday
uint16_t min = tm->tm_hour * 60 + tm->tm_min;
uint32_t check_time = now / 60 - starttime;
#ifdef EMSESP_DEBUG
EMSESP::logger().debug("On the minute. dow=%d, min=%d", dow, min);
#endif
for (const ScheduleItem & scheduleItem : *scheduleItems) { for (const ScheduleItem & scheduleItem : *scheduleItems) {
#ifdef EMSESP_DEBUG
EMSESP::logger().debug("Checking active=%d, flags=%d and elapsed_min=%d", scheduleItem.active, scheduleItem.flags, scheduleItem.elapsed_min);
#endif
if (scheduleItem.active if (scheduleItem.active
&& ((has_NTP && (dow & scheduleItem.flags) && min == scheduleItem.elapsed_min) // day of week scheduling - Weekly && ((has_NTP && (real_dow & scheduleItem.flags) && real_min == scheduleItem.elapsed_min) // day of week scheduling - Weekly
|| (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min == 0 && check_time == 0) // only on boot || (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min == 0 && uptime_min == 0) // only on boot
|| (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min > 0 || (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min > 0 && (uptime_seconds % scheduleItem.elapsed_min == 0)
&& (check_time % scheduleItem.elapsed_min == 0)))) { // periodic - Timer && uptime_min))) { // periodic - Timer, avoid first minute
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> docin; StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> doc_input;
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> docout; // only for commands without output JsonObject input = doc_input.to<JsonObject>();
JsonObject in = docin.to<JsonObject>(); input["data"] = scheduleItem.value;
JsonObject out = docout.to<JsonObject>();
in["data"] = scheduleItem.value;
#ifdef EMSESP_DEBUG StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> doc_output; // only for commands without output
EMSESP::logger().debug("Calling scheduled command %s with data %s", scheduleItem.cmd.c_str(), scheduleItem.value.c_str()); JsonObject output = doc_output.to<JsonObject>();
#endif
if (CommandRet::OK != Command::process(scheduleItem.cmd.c_str(), true, in, out)) { // prefix "api/" to command string
EMSESP::logger().warning("Scheduled command %s with data %s failed", scheduleItem.cmd.c_str(), scheduleItem.value.c_str()); auto command_str = "/api/" + scheduleItem.cmd;
uint8_t return_code = Command::process(command_str.c_str(), true, input, output); // admin set
if (return_code != CommandRet::OK) {
char error[100];
if (output.size()) {
snprintf(error,
sizeof(error),
"Call failed with error: %s (%s)",
(const char *)output["message"],
Command::return_code_string(return_code).c_str());
} else { } else {
EMSESP::logger().debug("Scheduled command %s with data %s executed", scheduleItem.cmd.c_str(), scheduleItem.value.c_str()); snprintf(error, sizeof(error), "Call failed with error code (%s)", Command::return_code_string(return_code).c_str());
}
emsesp::EMSESP::logger().err(error);
} else {
EMSESP::logger().debug("Scheduled command %s with data %s successfully", scheduleItem.cmd.c_str(), scheduleItem.value.c_str());
} }
} }
} }
} }
} // namespace emsesp } // namespace emsesp