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

@@ -100,8 +100,30 @@ namespace commands
response["cmd"] = "getCronJob";
auto &cron = Cron::getInstance(dev);
auto eventName = params["name"].as<std::string>();
if (eventName.empty())
{
LOG_ERROR("getCronJob empty job name");
response["values"]["status"] = "invalid";
return response;
}
if (eventName == "all")
{
const auto &eventMap = cron.getAllEvents();
uint8_t eventNum(0);
for (const auto &[name, event] : eventMap)
{
const auto cmd = std::get<0>(event);
response["values"]["name"] = cmd;
eventNum++;
}
LOG_INFO("getCronJob got [", eventNum, "] events");
return response;
}
Cron::CronEvent event;
if (eventName.empty() || !cron.getEvent(eventName, event))
if (!cron.getEvent(eventName, event))
{
LOG_ERROR("getCronJob failed to get job [", eventName.c_str(), "]");
response["values"]["status"] = "invalid";
@@ -176,12 +198,12 @@ namespace commands
LOG_ERROR("setHPlimit invalid level", level.c_str());
return response;
}
for (auto k : c_hpLimitsMap)
for (const auto [lvl, ro] : c_hpLimitsMap)
{
if (level == k.first && level != "UNLIMITED")
dev.io.digitalOutWrite(k.second, true);
if (level == lvl && level != "UNLIMITED")
dev.io.digitalOutWrite(ro, true);
else
dev.io.digitalOutWrite(k.second, false);
dev.io.digitalOutWrite(ro, false);
}
LOG_INFO("setHPlimit -> level", level.c_str());
return response;
@@ -200,19 +222,19 @@ namespace commands
LOG_ERROR("setHeating incorrect paramaters");
return response;
}
for (auto v : c_heatingValveMap)
for (const auto [lvl, ro] : c_heatingValveMap)
{
if (params[v.first].isNull())
if (params[lvl].isNull())
continue;
if (params[v.first] == "ON")
if (params[lvl] == "ON")
{
dev.io.digitalOutWrite(v.second, true);
LOG_INFO("setHeating -> ", v.first.c_str(), "ON");
dev.io.digitalOutWrite(ro, true);
LOG_INFO("setHeating -> ", lvl.c_str(), "ON");
}
else if (params[v.first] == "OFF")
else if (params[lvl] == "OFF")
{
dev.io.digitalOutWrite(v.second, false);
LOG_INFO("setHeating -> ", v.first.c_str(), "OFF");
dev.io.digitalOutWrite(ro, false);
LOG_INFO("setHeating -> ", lvl.c_str(), "OFF");
}
else
LOG_ERROR("setHeating invalid valve state");
@@ -342,6 +364,33 @@ namespace commands
}
return response;
}
const ArduinoJson::JsonDocument Commands::setTimeNTP(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
response["cmd"] = "setTimeNTP";
auto &eth = dev.eth;
auto &rtc = dev.rtc;
time_t ntpTime;
auto ntpOk = eth.getNtpTime(ntpTime);
drivers::PCF85063::datetime_t rtcTime(drivers::PCF85063::fromEpoch(ntpTime));
auto rtcOk = rtc.setDatetime(rtcTime);
if (!rtcOk || !ntpOk)
{
response["values"]["status"] = "unable to get time";
return response;
}
response["values"]["status"] = "valid";
response["status"]["time"] = rtc.getTimeStr();
LOG_INFO("setTimeNTP -> RTC is [", response["status"]["time"].as<std::string>().c_str(), "]");
return response;
}
// SETTERS //
// SETTERS //
@@ -350,8 +399,8 @@ namespace commands
const ArduinoJson::JsonDocument Commands::getHPpower(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
const auto pinfo = dev.seneca.getAll();
response["cmd"] = "getHPpower";
const auto pinfo = dev.seneca.getAll();
auto values = response["values"].to<JsonObject>();
values["power"] = pinfo.pAct;
values["current"] = pinfo.a;
@@ -409,13 +458,13 @@ namespace commands
LOG_WARN("Comand not yet implemented");
return response;
}
const ArduinoJson::JsonDocument Commands::getTimeDrift(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
response["cmd"] = "getTimeDrift";
auto& eth= dev.eth;
auto& rtc = dev.rtc;
auto &eth = dev.eth;
auto &rtc = dev.rtc;
time_t ntpTime;
auto ntpOk = eth.getNtpTime(ntpTime);
@@ -424,8 +473,9 @@ namespace commands
auto rtcOk = rtc.readDatetime(rtcTime);
auto rtcTimeTm = drivers::PCF85063::datetime2tm(rtcTime);
if (!rtcOk || !ntpOk) {
response["value"]["status"] = "unable to get time";
if (!rtcOk || !ntpOk)
{
response["values"]["status"] = "unable to get time";
return response;
}
@@ -433,11 +483,13 @@ namespace commands
auto rtcTimePoint = std::chrono::system_clock::from_time_t(std::mktime(&rtcTimeTm));
auto timeDiff = std::chrono::duration_cast<std::chrono::seconds>(ntpTimePoint - rtcTimePoint);
auto direction = timeDiff.count() >= 0 ? "BEYOND" : "AHEAD";
auto direction = timeDiff.count() >= 0 ? "BEYOND" : "AHEAD";
response["values"]["drift"] = timeDiff.count();
response["values"]["drift"] = (uint32_t)timeDiff.count();
response["values"]["direction"] = "RTC is [" + std::string(direction) + "] NTP time";
LOG_INFO("getTimeDrift -> RTC is [", (uint32_t)timeDiff.count(), "] sec, [", std::string(direction).c_str(), "] NTP time");
return response;
}
// GETTERS //