string conversion utility in rtc driver
This commit is contained in:
@@ -4,27 +4,19 @@
|
||||
#define STACK_DEPTH 4096
|
||||
#define PRIORITY 3
|
||||
|
||||
Cron::Cron(devices_t &dev) : m_dev(dev)
|
||||
{
|
||||
}
|
||||
|
||||
Cron::~Cron()
|
||||
{
|
||||
}
|
||||
|
||||
const bool Cron::loadEvents(fs::File &file)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool Cron::addEvent(const std::string &name, const std::string &command, const std::string &expr)
|
||||
const bool Cron::addEvent(const std::string &name, const std::string &expr, const ArduinoJson::JsonDocument action)
|
||||
{
|
||||
if (m_cronMap.contains(name))
|
||||
{
|
||||
LOG_ERROR("Cron event [", name.c_str(), "] already scheduled");
|
||||
return false;
|
||||
}
|
||||
if (name.empty() || command.empty() || expr.empty())
|
||||
if (name.empty() || expr.empty() || action.isNull())
|
||||
{
|
||||
LOG_ERROR("Cron event invalid parameters");
|
||||
return false;
|
||||
@@ -33,20 +25,13 @@ const bool Cron::addEvent(const std::string &name, const std::string &command, c
|
||||
try
|
||||
{
|
||||
const auto eventExpr(cron::make_cron(expr));
|
||||
ArduinoJson::JsonDocument action;
|
||||
if (ArduinoJson::deserializeJson(action, command) != ArduinoJson::DeserializationError::Ok)
|
||||
{
|
||||
LOG_ERROR("Cron unable to deserialize command [", command.c_str(), "]");
|
||||
return false;
|
||||
}
|
||||
const auto cmd = action["cmd"].as<std::string>();
|
||||
const auto params = action["params"].as<JsonObject>();
|
||||
const auto params = action["params"];
|
||||
if (!commands::commandMap.contains(cmd))
|
||||
{
|
||||
LOG_ERROR("Cron unknown command [", command.c_str(), "]");
|
||||
LOG_ERROR("Cron unknown command [", cmd.c_str(), "]");
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("Cron added event [", name.c_str(), "]");
|
||||
drivers::PCF85063::datetime_t now;
|
||||
if (!m_dev.rtc.readDatetime(now))
|
||||
{
|
||||
@@ -54,7 +39,10 @@ const bool Cron::addEvent(const std::string &name, const std::string &command, c
|
||||
return false;
|
||||
}
|
||||
std::tm nowTm = drivers::PCF85063::datetime2tm(now);
|
||||
m_cronMap[name] = std::make_tuple(cmd, eventExpr, cron::cron_next(eventExpr, nowTm), params);
|
||||
auto next = cron::cron_next(eventExpr, nowTm);
|
||||
JsonDocument act(params);
|
||||
LOG_INFO("Cron adding event [", name.c_str(), "] next execution [", drivers::PCF85063::tm2str(next).c_str(), "]");
|
||||
m_cronMap[name] = std::make_tuple(cmd, eventExpr, next, act);
|
||||
}
|
||||
catch (cron::bad_cronexpr const &ex)
|
||||
{
|
||||
@@ -64,6 +52,18 @@ const bool Cron::addEvent(const std::string &name, const std::string &command, c
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool Cron::getEvent(const std::string &name, CronEvent &event)
|
||||
{
|
||||
if (!m_cronMap.contains(name))
|
||||
{
|
||||
LOG_ERROR("Cron event [", name.c_str(), "] does not exist");
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("Cron get event [", name.c_str(), "]");
|
||||
event = m_cronMap.at(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool Cron::delEvent(const std::string &name)
|
||||
{
|
||||
if (!m_cronMap.contains(name))
|
||||
@@ -76,23 +76,29 @@ const bool Cron::delEvent(const std::string &name)
|
||||
return true;
|
||||
}
|
||||
|
||||
void cronLoop(void* params) {
|
||||
auto cron = (Cron*)(params);
|
||||
while (true) {
|
||||
cron->processEvents();
|
||||
void cronLoop(void *dev)
|
||||
{
|
||||
auto &cron = Cron::getInstance(*(devices_t*)dev);
|
||||
while (true)
|
||||
{
|
||||
cron.processEvents();
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void Cron::startCron() {
|
||||
if (!m_cronTaskHandle) {
|
||||
void Cron::startCron()
|
||||
{
|
||||
if (!m_cronTaskHandle)
|
||||
{
|
||||
LOG_INFO("Cron starting loop");
|
||||
xTaskCreate(cronLoop, "cronLoop", STACK_DEPTH, this, PRIORITY, &m_cronTaskHandle);
|
||||
xTaskCreate(cronLoop, "cronLoop", STACK_DEPTH, (void *)&m_dev, PRIORITY, &m_cronTaskHandle);
|
||||
}
|
||||
}
|
||||
|
||||
void Cron::stopCron() {
|
||||
if (m_cronTaskHandle) {
|
||||
void Cron::stopCron()
|
||||
{
|
||||
if (m_cronTaskHandle)
|
||||
{
|
||||
LOG_WARN("Cron stopping loop");
|
||||
vTaskDelete(m_cronTaskHandle);
|
||||
m_cronTaskHandle = NULL;
|
||||
@@ -126,14 +132,15 @@ const bool Cron::processEvents()
|
||||
const auto nextEventPoint = std::chrono::system_clock::from_time_t(std::mktime(&next));
|
||||
|
||||
LOG_DEBUG("Cron current time [", std::asctime(&nowTm), "]");
|
||||
LOG_DEBUG("Cron checking event [", eventName.c_str(), "] executionTime [", std::asctime(&next), "]");
|
||||
LOG_DEBUG("Cron checking event [", eventName.c_str(), "] executionTime [", drivers::PCF85063::tm2str(next).c_str(), "]");
|
||||
|
||||
if (nextEventPoint <= nowPoint) // execution time hs passed, run event
|
||||
{
|
||||
LOG_INFO("Cron executing event [", eventName.c_str(), "]");
|
||||
next = cron::cron_next(cronexrp, nowTm); // update next execution time only if event was executed
|
||||
// otherwise time tracking is lost
|
||||
LOG_INFO("Cron running event [", eventName.c_str(), "] next execution time [", drivers::PCF85063::tm2str(next).c_str(), "]");
|
||||
commands::commandMap.at(cmd)(m_dev, params); // here the magic happens
|
||||
next = cron::cron_next(cronexrp, nowTm); // update next execution time only if event was executed
|
||||
} // otherwise time tracking is lost
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user