improved responses content for commands and cronjobs
This commit is contained in:
@@ -71,9 +71,10 @@ namespace commands
|
||||
ArduinoJson::JsonDocument response;
|
||||
response["cmd"] = "setCronJob";
|
||||
|
||||
const auto &jobName = params["name"].as<std::string>();
|
||||
const auto &eventName = params["name"].as<std::string>();
|
||||
const auto &timeStr = params["cronExpr"].as<std::string>();
|
||||
const auto &actionStr = params["action"].as<std::string>();
|
||||
response["value"]["name"] = eventName;
|
||||
|
||||
ArduinoJson::JsonDocument action;
|
||||
if (ArduinoJson::deserializeJson(action, actionStr) != ArduinoJson::DeserializationError::Ok)
|
||||
@@ -84,7 +85,7 @@ namespace commands
|
||||
}
|
||||
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
if (!cron.addEvent(jobName, timeStr, action))
|
||||
if (!cron.addEvent(eventName, timeStr, action))
|
||||
{
|
||||
LOG_ERROR("setCronJob unable to add job [", actionStr.c_str(), "]");
|
||||
response["value"]["status"] = "invalid";
|
||||
@@ -100,6 +101,7 @@ namespace commands
|
||||
response["cmd"] = "getCronJob";
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
auto eventName = params["name"].as<std::string>();
|
||||
response["values"]["name"] = eventName;
|
||||
|
||||
if (eventName.empty())
|
||||
{
|
||||
@@ -115,7 +117,7 @@ namespace commands
|
||||
for (const auto &[name, event] : eventMap)
|
||||
{
|
||||
const auto cmd = std::get<0>(event);
|
||||
response["values"]["name"] = cmd;
|
||||
response["values"][name] = cmd;
|
||||
eventNum++;
|
||||
}
|
||||
LOG_INFO("getCronJob got [", eventNum, "] events");
|
||||
@@ -137,7 +139,6 @@ namespace commands
|
||||
ArduinoJson::JsonDocument action;
|
||||
action["cmd"] = cmd;
|
||||
action["params"] = cmdParams;
|
||||
response["values"]["name"] = eventName;
|
||||
response["values"]["cronExpr"] = cron::to_cronstr(cronExpr);
|
||||
response["values"]["action"] = action;
|
||||
|
||||
@@ -150,6 +151,7 @@ namespace commands
|
||||
response["cmd"] = "delCronJob";
|
||||
auto &cron = Cron::getInstance(dev);
|
||||
auto eventName = params["name"].as<std::string>();
|
||||
response["values"]["name"] = eventName;
|
||||
if (eventName.empty() || !cron.delEvent(eventName))
|
||||
{
|
||||
LOG_ERROR("delCronJob failed to delete job [", eventName.c_str(), "]");
|
||||
@@ -182,20 +184,18 @@ namespace commands
|
||||
const ArduinoJson::JsonDocument Commands::setHPlimit(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
{
|
||||
std::string msg;
|
||||
serializeJson(params, msg);
|
||||
LOG_INFO("setHPlimit params ->", msg.c_str());
|
||||
};
|
||||
response["cmd"] = "setHPlimit";
|
||||
if (!params["level"].is<std::string>())
|
||||
{
|
||||
LOG_ERROR("setHPlimit incorrect parameters");
|
||||
return response;
|
||||
}
|
||||
const auto level = params["level"].as<std::string>();
|
||||
response["values"]["level"] = level;
|
||||
if (!c_hpLimitsMap.contains(level))
|
||||
{
|
||||
LOG_ERROR("setHPlimit invalid level", level.c_str());
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
for (const auto [lvl, ro] : c_hpLimitsMap)
|
||||
@@ -206,17 +206,14 @@ namespace commands
|
||||
dev.io.digitalOutWrite(ro, false);
|
||||
}
|
||||
LOG_INFO("setHPlimit -> level", level.c_str());
|
||||
response["values"]["status"] = "valid";
|
||||
return response;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument Commands::setHeating(const devices_t &dev, const ArduinoJson::JsonDocument ¶ms)
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
{
|
||||
std::string msg;
|
||||
serializeJson(params, msg);
|
||||
LOG_INFO("setHeating params ->", msg.c_str());
|
||||
};
|
||||
response["cmd"] = "setHeating";
|
||||
if (params.isNull())
|
||||
{
|
||||
LOG_ERROR("setHeating incorrect paramaters");
|
||||
@@ -229,15 +226,20 @@ namespace commands
|
||||
if (params[lvl] == "ON")
|
||||
{
|
||||
dev.io.digitalOutWrite(ro, true);
|
||||
response["values"][lvl] = "ON";
|
||||
LOG_INFO("setHeating -> ", lvl.c_str(), "ON");
|
||||
}
|
||||
else if (params[lvl] == "OFF")
|
||||
{
|
||||
dev.io.digitalOutWrite(ro, false);
|
||||
response["values"][lvl] = "OFF";
|
||||
LOG_INFO("setHeating -> ", lvl.c_str(), "OFF");
|
||||
}
|
||||
else
|
||||
{
|
||||
response["values"][lvl] = "invalid";
|
||||
LOG_ERROR("setHeating invalid valve state");
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
@@ -270,11 +272,6 @@ namespace commands
|
||||
{
|
||||
ArduinoJson::JsonDocument response;
|
||||
auto &conf = Config::getInstance();
|
||||
{
|
||||
std::string msg;
|
||||
serializeJson(params, msg);
|
||||
LOG_INFO("setIrrigation params ->", msg.c_str());
|
||||
};
|
||||
response["cmd"] = "setIrrigation";
|
||||
if (params.isNull())
|
||||
{
|
||||
@@ -285,6 +282,8 @@ namespace commands
|
||||
const uint16_t tOn(params["timeOn"].as<uint16_t>());
|
||||
const uint16_t tPause(params["timePause"].as<uint16_t>());
|
||||
|
||||
response["values"]["zone"] = zone;
|
||||
|
||||
if (zone == "stop")
|
||||
{ // stop all zones and reset timers
|
||||
LOG_INFO("setIrrigation stop all zones");
|
||||
@@ -314,6 +313,8 @@ namespace commands
|
||||
return response;
|
||||
}
|
||||
|
||||
response["values"]["timeOn"] = tOn;
|
||||
response["values"]["timePause"] = tPause;
|
||||
if (!c_irrigationValveMap.contains(zone) || tOn <= 0 || tPause <= 0) // verify if zone is a valid map key
|
||||
{
|
||||
LOG_ERROR("setIrrigation incorrect zone[", zone.c_str(), "] or time values tOn[", tOn, "] tPause[", tPause, "]");
|
||||
@@ -359,7 +360,7 @@ namespace commands
|
||||
dev.io.digitalOutWrite(zoneIoNumber, true);
|
||||
xTimerStart(shTimer, 0);
|
||||
timerHandle = shTimer;
|
||||
response["values"]["status"] = "ok";
|
||||
response["values"]["status"] = "valid";
|
||||
LOG_INFO("setIrrigation zone [", timerName, "] tOn[", tOn, "] tPause[", tPause, "]");
|
||||
}
|
||||
return response;
|
||||
@@ -380,15 +381,13 @@ namespace commands
|
||||
|
||||
if (!rtcOk || !ntpOk)
|
||||
{
|
||||
response["values"]["status"] = "unable to get time";
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
|
||||
response["values"]["status"] = "valid";
|
||||
response["status"]["time"] = rtc.getTimeStr();
|
||||
|
||||
response["values"]["time"] = rtc.getTimeStr();
|
||||
LOG_INFO("setTimeNTP -> RTC is [", response["status"]["time"].as<std::string>().c_str(), "]");
|
||||
|
||||
return response;
|
||||
}
|
||||
// SETTERS //
|
||||
@@ -475,7 +474,7 @@ namespace commands
|
||||
|
||||
if (!rtcOk || !ntpOk)
|
||||
{
|
||||
response["values"]["status"] = "unable to get time";
|
||||
response["values"]["status"] = "invalid";
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -485,6 +484,7 @@ namespace commands
|
||||
auto timeDiff = std::chrono::duration_cast<std::chrono::seconds>(ntpTimePoint - rtcTimePoint);
|
||||
auto direction = timeDiff.count() >= 0 ? "BEYOND" : "AHEAD";
|
||||
|
||||
response["values"]["status"] = "valid";
|
||||
response["values"]["drift"] = (uint32_t)timeDiff.count();
|
||||
response["values"]["direction"] = "RTC is [" + std::string(direction) + "] NTP time";
|
||||
|
||||
|
||||
@@ -222,7 +222,13 @@ const bool Cron::processEvents()
|
||||
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(), "]");
|
||||
auto resp = commands::s_commandMap.at(cmd)(m_dev, cmdParams); // here the magic happens
|
||||
auto action = commands::s_commandMap.at(cmd)(m_dev, cmdParams); // here the magic happens
|
||||
ArduinoJson::JsonDocument resp;
|
||||
resp["cmd"] = "logCronJob";
|
||||
resp["values"]["name"] = eventName;
|
||||
resp["values"]["now"] = drivers::PCF85063::tm2str(nowTm).c_str();
|
||||
resp["values"]["next"] = drivers::PCF85063::tm2str(next).c_str();
|
||||
resp["values"]["action"] = action;
|
||||
if (m_callback)
|
||||
{
|
||||
m_callback(resp);
|
||||
|
||||
14
src/main.cpp
14
src/main.cpp
@@ -57,7 +57,7 @@ void loop()
|
||||
|
||||
//////////////// MQTT //////////////
|
||||
/////////////// CALLBACK //////////////
|
||||
std::function<void(const ArduinoJson::JsonDocument &)> commandsCallback =
|
||||
MQTTwrapper::ActionCallback commandsCallback =
|
||||
[&mqtt, &devices](const ArduinoJson::JsonDocument &doc)
|
||||
{
|
||||
if (!doc["cmd"].is<std::string>())
|
||||
@@ -81,6 +81,16 @@ void loop()
|
||||
}
|
||||
};
|
||||
|
||||
MQTTwrapper::MessageCallback onMessage = [&devices](const MQTTwrapper::Topic &topic, const MQTTwrapper::Message &message)
|
||||
{
|
||||
// devices.led.flashColor(250, devices.led.COLOR_BLUE);
|
||||
};
|
||||
|
||||
MQTTwrapper::MessageCallback onPublish = [&devices](const MQTTwrapper::Topic &topic, const MQTTwrapper::Message &message)
|
||||
{
|
||||
// devices.led.flashColor(250, devices.led.COLOR_ORANGE);
|
||||
};
|
||||
|
||||
///////////// CRONJOB //////////////
|
||||
/////////////// CALLBACK //////////////
|
||||
Cron::CronCallback cronCallback = [&mqtt](const ArduinoJson::JsonDocument &resp)
|
||||
@@ -132,6 +142,8 @@ void loop()
|
||||
buzzer.beep(250, NOTE_B);
|
||||
led.setColor(led.COLOR_GREEN);
|
||||
mqtt.subscribe(conf.m_mqttSubscribe["commands"], commandsCallback);
|
||||
mqtt.setOnMessageCb(onMessage);
|
||||
mqtt.setOnPublishCb(onPublish);
|
||||
break;
|
||||
}
|
||||
delay(250);
|
||||
|
||||
Reference in New Issue
Block a user