updated getCronJobs response with next execution
This commit is contained in:
@@ -109,13 +109,13 @@ namespace commands
|
||||
response["values"]["name"] = eventName;
|
||||
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
if (!cron.c_statusStr2Enum.contains(statusStr))
|
||||
if (Cron::str2Enum(statusStr) == Cron::str2Enum("INVALID"))
|
||||
{
|
||||
LOG_ERROR("setCronJob invalid status [", statusStr.c_str(), "]");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
cron.setEvent(eventName, cron.c_statusStr2Enum.at(statusStr));
|
||||
cron.setEvent(eventName, Cron::str2Enum(statusStr));
|
||||
LOG_INFO("setCronJob set job [", eventName.c_str(), "] to [", statusStr.c_str(), "]");
|
||||
response["values"]["status"] = "valid";
|
||||
return response;
|
||||
@@ -143,7 +143,8 @@ namespace commands
|
||||
ArduinoJson::JsonDocument action;
|
||||
action["cmd"] = event.cmd;
|
||||
action["params"] = event.cmdParams;
|
||||
action["status"] = cron.c_statusEnum2Str.at(event.status);
|
||||
action["status"] = Cron::enum2Str(event.status);
|
||||
action["next"] = drivers::PCF85063::tm2str(event.next);
|
||||
response["values"][name] = action;
|
||||
eventNum++;
|
||||
}
|
||||
@@ -163,8 +164,9 @@ namespace commands
|
||||
ArduinoJson::JsonDocument action;
|
||||
action["cmd"] = event.cmd;
|
||||
action["params"] = event.cmdParams;
|
||||
action["status"] = cron.c_statusEnum2Str.at(event.status);
|
||||
response["values"]["cronExpr"] = cron::to_cronstr(event.cronExpr);
|
||||
action["status"] = Cron::enum2Str(event.status);
|
||||
action["next"] = drivers::PCF85063::tm2str(event.next);
|
||||
action["cronExpr"] = cron::to_cronstr(event.cronExpr);
|
||||
response["values"]["action"] = action;
|
||||
LOG_INFO("getCronJob get job [", eventName.c_str(), "]");
|
||||
return response;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#define STACK_DEPTH 4096
|
||||
#define PRIORITY 3
|
||||
#define PROCESS_INTERVAL 1000
|
||||
#define PROCESS_CORE 0
|
||||
|
||||
const bool Cron::loadEvents()
|
||||
{
|
||||
@@ -31,13 +33,12 @@ const bool Cron::loadEvents()
|
||||
{
|
||||
const auto &eventName = job["name"].as<std::string>();
|
||||
const auto &cronExpr = job["cronExpr"].as<std::string>();
|
||||
const auto status = c_statusStr2Enum.at(job["status"].as<std::string>());
|
||||
const auto status = str2Enum(job["status"].as<std::string>());
|
||||
ArduinoJson::JsonDocument action(job["action"]);
|
||||
if (!addEvent(eventName, cronExpr, action, status))
|
||||
LOG_ERROR("Cron failed to load event [", eventName.c_str(), "]");
|
||||
else
|
||||
LOG_INFO("Cron loaded event [", eventName.c_str(), "]");
|
||||
delay(10);
|
||||
}
|
||||
cronFile.close();
|
||||
return true;
|
||||
@@ -62,7 +63,7 @@ const bool Cron::storeEvents()
|
||||
ArduinoJson::JsonDocument thisJob;
|
||||
thisJob["name"] = eventName;
|
||||
thisJob["cronExpr"] = cron::to_cronstr(eventParams.cronExpr);
|
||||
thisJob["status"] = c_statusEnum2Str.at(eventParams.status);
|
||||
thisJob["status"] = enum2Str(eventParams.status);
|
||||
thisJob["action"]["cmd"] = eventParams.cmd;
|
||||
thisJob["action"]["params"] = eventParams.cmdParams;
|
||||
cronFileArray.add(thisJob);
|
||||
@@ -129,7 +130,7 @@ const bool Cron::setEvent(const std::string &name, const CronStatus status)
|
||||
LOG_ERROR("Cron event [", name.c_str(), "] does not exist");
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("Cron set event [", name.c_str(), "] status [", c_statusEnum2Str.at(status).c_str(), "]");
|
||||
LOG_INFO("Cron set event [", name.c_str(), "] status [", enum2Str(status).c_str(), "]");
|
||||
m_cronMap.at(name).status = status;
|
||||
return true;
|
||||
}
|
||||
@@ -171,7 +172,7 @@ void cronLoop(void *cronPtr)
|
||||
while (true)
|
||||
{
|
||||
cron.processEvents();
|
||||
delay(1000);
|
||||
delay(PROCESS_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +181,7 @@ void Cron::startCron()
|
||||
if (!m_cronTaskHandle)
|
||||
{
|
||||
LOG_INFO("Cron starting loop");
|
||||
xTaskCreate(cronLoop, "cronLoop", STACK_DEPTH, this, PRIORITY, &m_cronTaskHandle);
|
||||
xTaskCreatePinnedToCore(cronLoop, "cronLoop", STACK_DEPTH, this, PRIORITY, &m_cronTaskHandle, PROCESS_CORE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,7 +226,7 @@ const bool Cron::processEvents()
|
||||
resp["values"]["name"] = eventName;
|
||||
resp["values"]["now"] = drivers::PCF85063::tm2str(nowTm).c_str();
|
||||
resp["values"]["next"] = drivers::PCF85063::tm2str(eventParams.next).c_str();
|
||||
resp["values"]["status"] = c_statusEnum2Str.at(eventParams.status).c_str();
|
||||
resp["values"]["status"] = enum2Str(eventParams.status).c_str();
|
||||
switch (eventParams.status)
|
||||
{
|
||||
case CronStatus::ACTIVE:
|
||||
|
||||
@@ -13,25 +13,29 @@
|
||||
#include <filesystem>
|
||||
#include <croncpp.h>
|
||||
|
||||
class Cron
|
||||
{
|
||||
public:
|
||||
enum class CronStatus
|
||||
{
|
||||
ACTIVE,
|
||||
INACTIVE,
|
||||
SKIP
|
||||
SKIP,
|
||||
INVALID
|
||||
};
|
||||
|
||||
const std::map<CronStatus, std::string> c_statusEnum2Str = {
|
||||
static const std::map<const CronStatus, std::string> c_statusEnum2Str = {
|
||||
{CronStatus::ACTIVE, "ACTIVE"},
|
||||
{CronStatus::INACTIVE, "INACTIVE"},
|
||||
{CronStatus::SKIP, "SKIP"}};
|
||||
{CronStatus::SKIP, "SKIP"},
|
||||
{CronStatus::INVALID, "INVALID"}};
|
||||
|
||||
const std::map<std::string, CronStatus> c_statusStr2Enum = {
|
||||
static const std::map<const std::string, CronStatus> c_statusStr2Enum = {
|
||||
{"ACTIVE", CronStatus::ACTIVE},
|
||||
{"INACTIVE", CronStatus::INACTIVE},
|
||||
{"SKIP", CronStatus::SKIP}};
|
||||
{"SKIP", CronStatus::SKIP},
|
||||
{"INVALID", CronStatus::INVALID}};
|
||||
|
||||
class Cron
|
||||
{
|
||||
public:
|
||||
|
||||
struct CronEvent
|
||||
{
|
||||
@@ -75,6 +79,20 @@ public:
|
||||
void stopCron();
|
||||
const bool processEvents();
|
||||
|
||||
static const std::string enum2Str(const CronStatus status)
|
||||
{
|
||||
if (!c_statusEnum2Str.contains(status))
|
||||
return "INVALID";
|
||||
return c_statusEnum2Str.at(status);
|
||||
}
|
||||
|
||||
static const CronStatus str2Enum(const std::string &status)
|
||||
{
|
||||
if (!c_statusStr2Enum.contains(status))
|
||||
return CronStatus::INVALID;
|
||||
return c_statusStr2Enum.at(status);
|
||||
}
|
||||
|
||||
private:
|
||||
const devices_t &m_dev;
|
||||
CronCallback m_callback;
|
||||
|
||||
Reference in New Issue
Block a user