implemented config and irrigation commands

This commit is contained in:
Emanuele Trabattoni
2025-07-25 21:53:49 +02:00
parent 74a97a7dd6
commit 5459148538
5 changed files with 184 additions and 41 deletions

View File

@@ -3,21 +3,34 @@
namespace commands
{
void restart(TimerHandle_t t)
{
esp_restart();
}
// CONFIG //
// CONFIG //
const ArduinoJson::JsonDocument Commands::setConfig(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
auto &conf = Config::getInstance();
std::string buf;
response["cmd"] = "setConfig";
auto values = response["values"].to<JsonObject>();
if (params.isNull())
{
values["status"] = "Invalid";
return response;
}
else
conf.setConfig(params);
values["status"] = "Valid";
serializeJson(params, buf);
LOG_INFO("setConfig ->", buf.c_str());
TimerHandle_t resetTimer(xTimerCreate("restartTimer", pdMS_TO_TICKS(5000), false, NULL, restart));
LOG_WARN("setConfig will cause restart!");
if (resetTimer)
{
conf.setConfig(params);
values["status"] = "Valid";
xTimerStart(resetTimer, 0);
}
return response;
}
@@ -25,8 +38,11 @@ namespace commands
{
ArduinoJson::JsonDocument response;
auto &conf = Config::getInstance();
std::string buf;
response["cmd"] = "getConfig";
response["values"] = conf.getConfig();
serializeJson(response["values"], buf);
LOG_INFO("getConfig ->", buf.c_str());
return response;
}
// CONFIG //
@@ -85,14 +101,145 @@ namespace commands
const ArduinoJson::JsonDocument Commands::setHeating(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
LOG_WARN("Comand not yet implemented");
if (params.isNull())
{
LOG_ERROR("setHeating incorrect paramaters");
return response;
}
for (auto v : c_heatingValveMap)
{
if (params[v.first].isNull())
continue;
if (params[v.first] == "ON")
{
dev.io.digitalOutWrite(v.second, true);
LOG_INFO("setHeating -> ", v.first.c_str(), "ON");
}
else if (params[v.first] == "OFF")
{
dev.io.digitalOutWrite(v.second, false);
LOG_INFO("setHeating -> ", v.first.c_str(), "OFF");
}
else
LOG_ERROR("setHeating invalid valve state");
}
return response;
}
void resetZone(TimerHandle_t th)
{
devices_t *dev = (devices_t *)pvTimerGetTimerID(th);
const char *timerName = pcTimerGetName(th);
LOG_INFO("Reset irrigation zone -> ", timerName);
if (!c_irrigationValveMap.contains(timerName))
{
LOG_ERROR("Irrigation timer name invalid");
return;
}
dev->io.digitalOutWrite(c_irrigationValveMap.at(timerName), false);
c_irrigationTimerMap.at(timerName).second = NULL; // reset timer handle for this timer
xTimerDelete(th, 0); // delete the timer on expiry
}
void resetWaterPump(TimerHandle_t th)
{
devices_t *dev = (devices_t *)pvTimerGetTimerID(th);
LOG_INFO("Shutdown irrigation pump");
dev->io.digitalOutWrite(RO::IRR_PUMP, false);
s_irrigationPumpTimer = NULL;
xTimerDelete(th, 0); // delete the timer on expiry
}
const ArduinoJson::JsonDocument Commands::setIrrigation(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
LOG_WARN("Comand not yet implemented");
auto &conf = Config::getInstance();
response["cmd"] = "setIrrigation";
if (params.isNull())
{
LOG_ERROR("setIrrigation incorrect paramaters");
return response;
}
const std::string zone(params["zone"].as<std::string>());
const uint16_t tOn(params["timeOn"].as<uint16_t>());
const uint16_t tPause(params["timePause"].as<uint16_t>());
if (zone == "stop")
{ // stop all zones and reset timers
LOG_INFO("setIrrigation stop all zones");
for (auto &h : c_irrigationTimerMap)
{
const auto zoneName = h.first;
auto &timerHandle = h.second.second; // get the timer handle
if (timerHandle) // if handle is not null (not from a deleted timer)
{
if (xTimerIsTimerActive(timerHandle)) // stop the timer if active
{
LOG_INFO("setIrrigation stopping timer", zoneName.c_str());
xTimerStop(timerHandle, 0);
xTimerDelete(timerHandle, pdMS_TO_TICKS(10)); // delete it
timerHandle = NULL;
}
}
LOG_INFO("setIrrigation closing ", zoneName.c_str());
dev.io.digitalOutWrite(c_irrigationValveMap.at(zoneName), false); // shuto down the valve
}
if (s_irrigationPumpTimer)
{
xTimerChangePeriod(s_irrigationPumpTimer, pdMS_TO_TICKS(30 * 1000), 0); // shutdown the pump in 30s after the stop
xTimerReset(s_irrigationPumpTimer, 0);
}
response["values"]["status"] = "stop";
return response;
}
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, tPause);
response["values"]["status"] = "invalid";
return response;
}
// verify if timer was already started, zone is already on
const auto timerName = c_irrigationTimerMap.at(zone).first;
const auto zoneIoNumber = c_irrigationValveMap.at(zone);
auto &timerHandle = c_irrigationTimerMap.at(zone).second;
if (timerHandle)
{ // this timer was alteady started, ignore command
LOG_WARN("setIrrigation zone", timerName, "already started");
response["values"]["status"] = "conflict";
return response;
}
const uint32_t pumpTime((tOn + 30) * 1000);
if (!s_irrigationPumpTimer) // Pump has not yet started
{
s_irrigationPumpTimer = xTimerCreate("pumpTimer", pdMS_TO_TICKS(pumpTime), false, (void *)&dev, resetWaterPump);
dev.io.digitalOutWrite(RO::IRR_PUMP, true);
xTimerStart(s_irrigationPumpTimer, 0); // immediate start pump timer
LOG_INFO("setIrrigation pump time ", pumpTime);
}
else
{
const auto currentRemaining(xTimerGetExpiryTime(s_irrigationPumpTimer) - xTaskGetTickCount());
const auto newRemaining(pumpTime);
const auto newPeriod = std::max(newRemaining, currentRemaining);
xTimerChangePeriod(s_irrigationPumpTimer, newPeriod, 0); // set new period based on timing of new zone
xTimerReset(s_irrigationPumpTimer, 0); // if timer was already started, restart
LOG_INFO("setIrrigation pump time reset", newRemaining);
}
TimerHandle_t shTimer(xTimerCreate(timerName, pdMS_TO_TICKS(tOn * 1000), false, (void *)&dev, resetZone));
if (shTimer)
{
dev.io.digitalOutWrite(zoneIoNumber, true);
xTimerStart(shTimer, pdMS_TO_TICKS(tPause * 1000));
timerHandle = shTimer;
response["values"]["status"] = "ok";
LOG_INFO("setIrrigation zone -> ", timerName, "tOn", tOn, "tPause", tPause);
}
return response;
}
// SETTERS //
@@ -114,13 +261,6 @@ namespace commands
return response;
}
const ArduinoJson::JsonDocument Commands::getHPlimit(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
LOG_WARN("Comand not yet implemented");
return response;
}
const ArduinoJson::JsonDocument Commands::getInputStatus(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;