added set time via ntp as command and retrieve all cron jobs

This commit is contained in:
Emanuele Trabattoni
2025-07-30 15:24:11 +02:00
parent 581eca124e
commit 1110648978
6 changed files with 128 additions and 43 deletions

View File

@@ -55,13 +55,7 @@ void loop()
auto mqtt = MQTTwrapper();
//////////////// MQTT ////////////////
//////////////// CRONJOB ////////////////
auto &cron = Cron::getInstance(devices);
cron.loadEvents();
cron.startCron();
//////////////// CRONJOB ////////////////
//////////////// MQTT ////////////////
//////////////// MQTT //////////////
/////////////// CALLBACK //////////////
std::function<void(const ArduinoJson::JsonDocument &)> commandsCallback =
[&mqtt, &devices](const ArduinoJson::JsonDocument &doc)
@@ -73,10 +67,10 @@ void loop()
}
const std::string cmd = doc["cmd"].as<std::string>();
const ArduinoJson::JsonDocument params = doc["params"];
if (commands::commandMap.contains(cmd))
if (commands::s_commandMap.contains(cmd))
{ // call command from command map in this same thread (the MQTT thread)
LOG_INFO("Executing command", cmd.c_str());
const auto answer = std::move(commands::commandMap.at(cmd)(devices, params)); // here the magic happens
const auto answer = std::move(commands::s_commandMap.at(cmd)(devices, params)); // here the magic happens
if (answer.isNull())
return;
mqtt.publish(conf.m_mqttPublish["answers"], answer);
@@ -87,6 +81,22 @@ void loop()
}
};
///////////// CRONJOB //////////////
/////////////// CALLBACK //////////////
Cron::CronCallback cronCallback = [&mqtt](const ArduinoJson::JsonDocument &resp)
{
if (resp.isNull())
return;
mqtt.publish(conf.m_mqttPublish["cronjobs"], resp);
};
//////////////// CRONJOB ////////////////
auto &cron = Cron::getInstance(devices);
cron.setResponseCallback(cronCallback);
cron.loadEvents();
cron.startCron();
//////////////// CRONJOB ////////////////
//////////////// NETWORK ////////////////
/////////////// CALLBACK ////////////////
Network.onEvent(
@@ -113,7 +123,7 @@ void loop()
LOG_INFO("NTP Time: ", drivers::PCF85063::datetime2str(dt).c_str());
break;
}
delay(100);
delay(250);
}
while (mqttRetries++ < conf.m_mqttRetries)
{
@@ -124,7 +134,7 @@ void loop()
mqtt.subscribe(conf.m_mqttSubscribe["commands"], commandsCallback);
break;
}
delay(100);
delay(250);
}
});
@@ -170,7 +180,7 @@ void loop()
{
LOG_WARN("RESTART!");
buzzer.beep(450, NOTE_D);
delay(100);
delay(450);
esp_restart();
}