Compare commits
2 Commits
448e1bad15
...
1d1eb6fbfa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d1eb6fbfa | ||
|
|
ad90702ab6 |
@@ -82,7 +82,7 @@
|
||||
"cmd": "setCronJob",
|
||||
"params": {
|
||||
"name": "nomedeljob",
|
||||
"timeStr": "* * * 10,45 5 *",
|
||||
"cronExpr": "* * * 10,45 5 *",
|
||||
"action": "qua ci va un dizionario come se arrivasse da mqtt, cosi li interpreto alla stessa maniera"
|
||||
}
|
||||
},
|
||||
|
||||
@@ -5,39 +5,7 @@
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <FFat.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
class FSmount
|
||||
{
|
||||
public:
|
||||
FSmount()
|
||||
{
|
||||
if (!FFat.begin(false))
|
||||
{
|
||||
LOG_ERROR("Unable to mount filesystem without formatting");
|
||||
if (!FFat.begin(true))
|
||||
{
|
||||
LOG_ERROR("Formatted and mounted filesystem");
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO("Local Filesystem Mounted Correctly");
|
||||
const auto totalBytes = FFat.totalBytes();
|
||||
const auto freeBytes = FFat.freeBytes();
|
||||
const auto usedBytes = FFat.usedBytes();
|
||||
const auto mountPoint = FFat.mountpoint();
|
||||
LOG_INFO("Local filesystem, total", totalBytes / 1024, "KB - used", usedBytes / 1024, "KB - free", freeBytes / 1024, "KB");
|
||||
LOG_INFO("Local filesystem, mountpoint", mountPoint);
|
||||
}
|
||||
|
||||
~FSmount()
|
||||
{
|
||||
FFat.end(); // unmout filesystem to avoid corruption
|
||||
LOG_INFO("Local Filesystem Unmounted Correctly");
|
||||
}
|
||||
};
|
||||
#include <fsmount.h>
|
||||
|
||||
class Config
|
||||
{
|
||||
|
||||
38
include/fsmount.h
Normal file
38
include/fsmount.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
#include <FFat.h>
|
||||
#include <mutex>
|
||||
|
||||
class FSmount
|
||||
{
|
||||
public:
|
||||
FSmount()
|
||||
{
|
||||
if (!FFat.begin(false))
|
||||
{
|
||||
LOG_ERROR("Unable to mount filesystem without formatting");
|
||||
if (!FFat.begin(true))
|
||||
{
|
||||
LOG_ERROR("Formatted and mounted filesystem");
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO("Local Filesystem Mounted Correctly");
|
||||
const auto totalBytes = FFat.totalBytes();
|
||||
const auto freeBytes = FFat.freeBytes();
|
||||
const auto usedBytes = FFat.usedBytes();
|
||||
const auto mountPoint = FFat.mountpoint();
|
||||
LOG_INFO("Local filesystem, total", totalBytes / 1024, "KB - used", usedBytes / 1024, "KB - free", freeBytes / 1024, "KB");
|
||||
LOG_INFO("Local filesystem, mountpoint", mountPoint);
|
||||
}
|
||||
|
||||
~FSmount()
|
||||
{
|
||||
FFat.end(); // unmout filesystem to avoid corruption
|
||||
LOG_INFO("Local Filesystem Unmounted Correctly");
|
||||
}
|
||||
};
|
||||
@@ -185,6 +185,12 @@ namespace drivers
|
||||
return buf.substr(0, std::min(buf.find('\n'),buf.find('\r')));
|
||||
}
|
||||
|
||||
const std::string PCF85063::tm2str(const std::tm &datetime)
|
||||
{
|
||||
const std::string buf(std::asctime(&datetime));
|
||||
return buf.substr(0, std::min(buf.find('\n'),buf.find('\r')));
|
||||
}
|
||||
|
||||
const std::tm PCF85063::datetime2tm(const datetime_t& datetime) {
|
||||
tm dtime;
|
||||
dtime.tm_sec = datetime.second;
|
||||
|
||||
@@ -102,6 +102,7 @@ namespace drivers
|
||||
const std::string getTimeStr();
|
||||
|
||||
static const std::string datetime2str(const datetime_t &datetime);
|
||||
static const std::string tm2str(const std::tm &datetime);
|
||||
static const std::tm datetime2tm(const datetime_t& datetime);
|
||||
static const PCF85063::datetime_t fromEpoch(const time_t currentTime);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <commands.h>
|
||||
#include <cronjobs.h>
|
||||
|
||||
namespace commands
|
||||
{
|
||||
@@ -50,22 +51,102 @@ namespace commands
|
||||
|
||||
// CRONJOBS //
|
||||
// CRONJOBS //
|
||||
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()){
|
||||
LOG_ERROR("loadCronJob failed to load events from flash");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
response["values"]["status"] = "valid";
|
||||
return response;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument Commands::setCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
LOG_WARN("setCronjob not yet implemented");
|
||||
response["cmd"] = "setCronjob";
|
||||
|
||||
const auto &jobName = params["name"].as<std::string>();
|
||||
const auto &timeStr = params["cronExpr"].as<std::string>();
|
||||
const auto &actionStr = params["action"].as<std::string>();
|
||||
|
||||
ArduinoJson::JsonDocument action;
|
||||
if (ArduinoJson::deserializeJson(action, actionStr) != ArduinoJson::DeserializationError::Ok)
|
||||
{
|
||||
LOG_ERROR("setCronJob unable to deserialize cron job [", actionStr.c_str(), "]");
|
||||
response["value"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
if (!cron.addEvent(jobName, timeStr, action))
|
||||
{
|
||||
LOG_ERROR("setCronJob unable to add job [", actionStr.c_str(), "]");
|
||||
response["value"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
LOG_INFO("setCronJob added job [", actionStr.c_str(), "]");
|
||||
response["value"]["status"] = "valid";
|
||||
return response;
|
||||
}
|
||||
const ArduinoJson::JsonDocument Commands::getCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
LOG_WARN("getCronjob not yet implemented");
|
||||
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(), "]");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
|
||||
auto cmd = std::get<0>(event);
|
||||
auto cronExpr = std::get<1>(event);
|
||||
auto cmdParams = std::get<3>(event);
|
||||
|
||||
ArduinoJson::JsonDocument action;
|
||||
action["cmd"] = cmd;
|
||||
action["params"] = cmdParams;
|
||||
response["values"]["name"] = eventName;
|
||||
response["values"]["cronExpr"] = cron::to_cronstr(cronExpr);
|
||||
response["values"]["action"] = action;
|
||||
|
||||
LOG_INFO("getCronjob get job [", eventName.c_str(), "]");
|
||||
return response;
|
||||
}
|
||||
const ArduinoJson::JsonDocument Commands::delCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
LOG_WARN("delCronjob not yet implemented");
|
||||
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(), "]");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
response["values"]["status"] = "valid";
|
||||
return response;
|
||||
}
|
||||
|
||||
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()){
|
||||
LOG_ERROR("storeCronJob failed to store events in flash");
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
response["values"]["status"] = "valid";
|
||||
return response;
|
||||
}
|
||||
// CRONJOBS //
|
||||
|
||||
@@ -69,9 +69,11 @@ namespace commands
|
||||
static const ArduinoJson::JsonDocument getConfig(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms);
|
||||
|
||||
// CRONJOBS //
|
||||
static const ArduinoJson::JsonDocument loadCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms);
|
||||
static const ArduinoJson::JsonDocument setCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms);
|
||||
static const ArduinoJson::JsonDocument getCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms);
|
||||
static const ArduinoJson::JsonDocument delCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms);
|
||||
static const ArduinoJson::JsonDocument storeCronjob(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms);
|
||||
|
||||
// SETTERS //
|
||||
static const ArduinoJson::JsonDocument setHPlimit(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms);
|
||||
@@ -93,9 +95,11 @@ namespace commands
|
||||
{"setConfig", Commands::setConfig},
|
||||
{"getConfig", Commands::getConfig},
|
||||
|
||||
{"loadCronjob", Commands::loadCronjob},
|
||||
{"setCronjob", Commands::setCronjob},
|
||||
{"getCronjob", Commands::getCronjob},
|
||||
{"delCronjob", Commands::delCronjob},
|
||||
{"storeCronjob", Commands::storeCronjob},
|
||||
|
||||
{"setHPlimit", Commands::setHPlimit},
|
||||
{"setHeating", Commands::setHeating},
|
||||
|
||||
153
src/cronjobs.cpp
153
src/cronjobs.cpp
@@ -1,30 +1,97 @@
|
||||
#include <cronjobs.h>
|
||||
#include <chrono>
|
||||
#include <fsmount.h>
|
||||
|
||||
#define STACK_DEPTH 4096
|
||||
#define PRIORITY 3
|
||||
|
||||
Cron::Cron(devices_t &dev) : m_dev(dev)
|
||||
const bool Cron::loadEvents()
|
||||
{
|
||||
}
|
||||
FSmount fs;
|
||||
File cronFile = FFat.open("/cronjobs.json", FILE_READ, false);
|
||||
if (!cronFile)
|
||||
{
|
||||
LOG_ERROR("Cron failed to open cronjobs.json");
|
||||
return false;
|
||||
}
|
||||
ArduinoJson::JsonDocument cronFileContent;
|
||||
if (ArduinoJson::deserializeJson(cronFileContent, cronFile) != ArduinoJson::DeserializationError::Ok)
|
||||
{
|
||||
LOG_ERROR("Cron unable to deserialize cronjobs.json");
|
||||
return false;
|
||||
}
|
||||
|
||||
Cron::~Cron()
|
||||
{
|
||||
}
|
||||
std::string buf;
|
||||
ArduinoJson::serializeJsonPretty(cronFileContent, buf);
|
||||
LOG_INFO("Cron loadEvents loaded cronjobs.json\n", buf.c_str());
|
||||
|
||||
const bool Cron::loadEvents(fs::File &file)
|
||||
{
|
||||
ArduinoJson::JsonArray cronjobList = cronFileContent.as<JsonArray>();
|
||||
LOG_INFO("Cron loadEvents loaded [", cronjobList.size(), "] events");
|
||||
for (const auto &job : cronjobList)
|
||||
{
|
||||
const auto &eventName = job["name"].as<std::string>();
|
||||
const auto &cronExpr = job["cronExpr"].as<std::string>();
|
||||
ArduinoJson::JsonDocument action(job["action"]);
|
||||
if (!addEvent(eventName, cronExpr, action))
|
||||
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;
|
||||
}
|
||||
|
||||
const bool Cron::addEvent(const std::string &name, const std::string &command, const std::string &expr)
|
||||
const bool Cron::storeEvents()
|
||||
{
|
||||
FSmount fs;
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
File cronFile = FFat.open("/cronjobs.json", FILE_WRITE, true);
|
||||
if (!cronFile)
|
||||
{
|
||||
LOG_ERROR("Cron failed to open cronjobs.json");
|
||||
return false;
|
||||
}
|
||||
|
||||
ArduinoJson::JsonDocument cronFileContent;
|
||||
ArduinoJson::JsonArray cronFileArray = cronFileContent.to<JsonArray>();
|
||||
|
||||
for (const auto &job : m_cronMap) // convert cron events map to json file
|
||||
{
|
||||
const auto &eventName = job.first;
|
||||
const auto ¶ms = job.second;
|
||||
|
||||
const auto &cmd = std::get<0>(params);
|
||||
const auto &cronExpr = std::get<1>(params);
|
||||
const auto &cmdParams = std::get<3>(params);
|
||||
|
||||
ArduinoJson::JsonDocument thisJob;
|
||||
thisJob["name"] = eventName;
|
||||
thisJob["cronExpr"] = cron::to_cronstr(cronExpr);
|
||||
thisJob["action"]["cmd"] = cmd;
|
||||
thisJob["action"]["params"] = cmdParams;
|
||||
|
||||
cronFileArray.add(thisJob);
|
||||
}
|
||||
|
||||
std::string buf;
|
||||
ArduinoJson::serializeJsonPretty(cronFileContent, buf);
|
||||
LOG_INFO("Cron storeEvents generated cronjobs.json\n", buf.c_str());
|
||||
|
||||
ArduinoJson::serializeJson(cronFileContent, cronFile);
|
||||
cronFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool Cron::addEvent(const std::string &name, const std::string &expr, const ArduinoJson::JsonDocument action)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
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 +100,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 +114,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,8 +127,22 @@ 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)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
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)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
if (!m_cronMap.contains(name))
|
||||
{
|
||||
LOG_WARN("Cron event [", name.c_str(), "] does not exist");
|
||||
@@ -76,23 +153,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;
|
||||
@@ -101,6 +184,7 @@ void Cron::stopCron() {
|
||||
|
||||
const bool Cron::processEvents()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
LOG_DEBUG("Cron processEvents [", m_cronMap.size(), "]");
|
||||
|
||||
drivers::PCF85063::datetime_t now;
|
||||
@@ -120,20 +204,21 @@ const bool Cron::processEvents()
|
||||
auto &cmd = std::get<0>(eventAction);
|
||||
auto &cronexrp = std::get<1>(eventAction);
|
||||
auto &next = std::get<2>(eventAction);
|
||||
auto ¶ms = std::get<3>(eventAction);
|
||||
auto &cmdParams = std::get<3>(eventAction);
|
||||
|
||||
const auto nowPoint = std::chrono::system_clock::from_time_t(std::mktime(&nowTm));
|
||||
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(), "]");
|
||||
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
|
||||
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, cmdParams); // here the magic happens
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -14,13 +14,26 @@
|
||||
|
||||
class Cron
|
||||
{
|
||||
public: // eventName cronExpression nextExec command parameters
|
||||
using CronEvent = std::tuple<std::string, cron::cronexpr, std::tm, ArduinoJson::JsonDocument>;
|
||||
|
||||
public:
|
||||
Cron(devices_t &dev);
|
||||
~Cron();
|
||||
static Cron &getInstance(const devices_t &dev)
|
||||
{
|
||||
static Cron instance(dev);
|
||||
return instance;
|
||||
}
|
||||
|
||||
const bool loadEvents(fs::File &file);
|
||||
const bool addEvent(const std::string &name, const std::string &command, const std::string &expr);
|
||||
private:
|
||||
Cron(const devices_t &dev) : m_dev(dev) {};
|
||||
Cron(const Cron &) = delete;
|
||||
Cron &operator=(const Cron &) = delete;
|
||||
|
||||
public:
|
||||
const bool loadEvents();
|
||||
const bool storeEvents();
|
||||
const bool addEvent(const std::string &name, const std::string &expr, const ArduinoJson::JsonDocument action);
|
||||
const bool getEvent(const std::string &name, CronEvent &event);
|
||||
const bool delEvent(const std::string &name);
|
||||
|
||||
void startCron();
|
||||
@@ -28,7 +41,8 @@ public:
|
||||
const bool processEvents();
|
||||
|
||||
private:
|
||||
devices_t &m_dev;
|
||||
const devices_t &m_dev;
|
||||
TaskHandle_t m_cronTaskHandle;
|
||||
std::map<std::string, std::tuple<std::string, cron::cronexpr, std::tm, ArduinoJson::JsonDocument>> m_cronMap;
|
||||
std::map<std::string, CronEvent> m_cronMap;
|
||||
std::mutex m_mutex;
|
||||
};
|
||||
24
src/main.cpp
24
src/main.cpp
@@ -51,26 +51,14 @@ void loop()
|
||||
devices_t devices(rtc, tmp, seneca, buzzer, led, io);
|
||||
//////////////// DEVICES ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
//////////////// MQTT ////////////////
|
||||
auto mqtt = MQTTwrapper();
|
||||
//////////////// NETWORK ////////////////
|
||||
|
||||
|
||||
//////////////// CRONJOB ////////////////
|
||||
auto cron = Cron(devices);
|
||||
ArduinoJson::JsonDocument job;
|
||||
job["cmd"] = "setIrrigation";
|
||||
job["params"]["zone"] = "zone1";
|
||||
job["params"]["timeOn"] = 30;
|
||||
job["params"]["timePause"] = 2;
|
||||
//////////////// MQTT ////////////////
|
||||
|
||||
{
|
||||
std::string buf;
|
||||
serializeJson(job,buf);
|
||||
LOG_INFO("Example Cronjob -> ", buf.c_str());
|
||||
cron.addEvent("exampleCronEvent", buf, "0 */10 * * * *");
|
||||
cron.startCron();
|
||||
};
|
||||
//////////////// CRONJOB ////////////////
|
||||
auto &cron = Cron::getInstance(devices);
|
||||
cron.loadEvents();
|
||||
cron.startCron();
|
||||
//////////////// CRONJOB ////////////////
|
||||
|
||||
//////////////// MQTT ////////////////
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
Reference in New Issue
Block a user