added time drift check command
This commit is contained in:
@@ -51,11 +51,13 @@ namespace commands
|
||||
|
||||
// CRONJOBS //
|
||||
// CRONJOBS //
|
||||
const ArduinoJson::JsonDocument Commands::loadCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms){
|
||||
const ArduinoJson::JsonDocument Commands::loadCronJob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
response["cmd"] = "loadCronjob";
|
||||
auto& cron = Cron::getInstance(dev);
|
||||
if(!cron.loadEvents()){
|
||||
response["cmd"] = "loadCronJob";
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
if (!cron.loadEvents())
|
||||
{
|
||||
LOG_ERROR("loadCronJob failed to load events from flash");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
@@ -64,10 +66,10 @@ namespace commands
|
||||
return response;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument Commands::setCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
const ArduinoJson::JsonDocument Commands::setCronJob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
response["cmd"] = "setCronjob";
|
||||
response["cmd"] = "setCronJob";
|
||||
|
||||
const auto &jobName = params["name"].as<std::string>();
|
||||
const auto &timeStr = params["cronExpr"].as<std::string>();
|
||||
@@ -92,16 +94,16 @@ namespace commands
|
||||
response["value"]["status"] = "valid";
|
||||
return response;
|
||||
}
|
||||
const ArduinoJson::JsonDocument Commands::getCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
const ArduinoJson::JsonDocument Commands::getCronJob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
response["cmd"] = "getCronjob";
|
||||
response["cmd"] = "getCronJob";
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
auto eventName = params["name"].as<std::string>();
|
||||
Cron::CronEvent event;
|
||||
if (eventName.empty() || !cron.getEvent(eventName, event))
|
||||
{
|
||||
LOG_ERROR("delCronjob failed to get job [", eventName.c_str(), "]");
|
||||
LOG_ERROR("getCronJob failed to get job [", eventName.c_str(), "]");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
@@ -117,18 +119,18 @@ namespace commands
|
||||
response["values"]["cronExpr"] = cron::to_cronstr(cronExpr);
|
||||
response["values"]["action"] = action;
|
||||
|
||||
LOG_INFO("getCronjob get job [", eventName.c_str(), "]");
|
||||
LOG_INFO("getCronJob get job [", eventName.c_str(), "]");
|
||||
return response;
|
||||
}
|
||||
const ArduinoJson::JsonDocument Commands::delCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
const ArduinoJson::JsonDocument Commands::delCronJob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
response["cmd"] = "delCronjob";
|
||||
response["cmd"] = "delCronJob";
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
auto eventName = params["name"].as<std::string>();
|
||||
if (eventName.empty() || !cron.delEvent(eventName))
|
||||
{
|
||||
LOG_ERROR("delCronjob failed to delete job [", eventName.c_str(), "]");
|
||||
LOG_ERROR("delCronJob failed to delete job [", eventName.c_str(), "]");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
@@ -136,12 +138,13 @@ namespace commands
|
||||
return response;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument Commands::storeCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
const ArduinoJson::JsonDocument Commands::storeCronJob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
response["cmd"] = "storeCronjob";
|
||||
auto& cron = Cron::getInstance(dev);
|
||||
if(!cron.storeEvents()){
|
||||
response["cmd"] = "storeCronJob";
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
if (!cron.storeEvents())
|
||||
{
|
||||
LOG_ERROR("storeCronJob failed to store events in flash");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
@@ -406,6 +409,37 @@ namespace commands
|
||||
LOG_WARN("Comand not yet implemented");
|
||||
return response;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument Commands::getTimeDrift(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
response["cmd"] = "getTimeDrift";
|
||||
auto& eth= dev.eth;
|
||||
auto& rtc = dev.rtc;
|
||||
|
||||
time_t ntpTime;
|
||||
auto ntpOk = eth.getNtpTime(ntpTime);
|
||||
|
||||
drivers::PCF85063::datetime_t rtcTime;
|
||||
auto rtcOk = rtc.readDatetime(rtcTime);
|
||||
auto rtcTimeTm = drivers::PCF85063::datetime2tm(rtcTime);
|
||||
|
||||
if (!rtcOk || !ntpOk) {
|
||||
response["value"]["status"] = "unable to get time";
|
||||
return response;
|
||||
}
|
||||
|
||||
auto ntpTimePoint = std::chrono::system_clock::from_time_t(ntpTime);
|
||||
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";
|
||||
|
||||
response["values"]["drift"] = timeDiff.count();
|
||||
response["values"]["direction"] = "RTC is [" + std::string(direction) + "] NTP time";
|
||||
|
||||
return response;
|
||||
}
|
||||
// GETTERS //
|
||||
// GETTERS //
|
||||
|
||||
|
||||
Reference in New Issue
Block a user