improved logging

This commit is contained in:
Emanuele Trabattoni
2025-07-26 11:21:32 +02:00
parent 5459148538
commit 91f4c5c750

View File

@@ -76,6 +76,11 @@ namespace commands
const ArduinoJson::JsonDocument Commands::setHPlimit(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
{
std::string msg;
serializeJson(params, msg);
LOG_INFO("setHPlimit params ->", msg.c_str());
};
if (!params["level"].is<std::string>())
{
LOG_ERROR("setHPlimit incorrect parameters");
@@ -101,6 +106,11 @@ namespace commands
const ArduinoJson::JsonDocument Commands::setHeating(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{
ArduinoJson::JsonDocument response;
{
std::string msg;
serializeJson(params, msg);
LOG_INFO("setHeating params ->", msg.c_str());
};
if (params.isNull())
{
LOG_ERROR("setHeating incorrect paramaters");
@@ -130,7 +140,7 @@ namespace commands
{
devices_t *dev = (devices_t *)pvTimerGetTimerID(th);
const char *timerName = pcTimerGetName(th);
LOG_INFO("Reset irrigation zone -> ", timerName);
LOG_INFO("setIrrigation shutdown zone [", timerName, "]");
if (!c_irrigationValveMap.contains(timerName))
{
LOG_ERROR("Irrigation timer name invalid");
@@ -144,7 +154,7 @@ namespace commands
void resetWaterPump(TimerHandle_t th)
{
devices_t *dev = (devices_t *)pvTimerGetTimerID(th);
LOG_INFO("Shutdown irrigation pump");
LOG_INFO("setIrrigation shutdown pump");
dev->io.digitalOutWrite(RO::IRR_PUMP, false);
s_irrigationPumpTimer = NULL;
xTimerDelete(th, 0); // delete the timer on expiry
@@ -154,6 +164,11 @@ 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())
{
@@ -181,21 +196,21 @@ namespace commands
timerHandle = NULL;
}
}
LOG_INFO("setIrrigation closing ", zoneName.c_str());
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);
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
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);
LOG_ERROR("setIrrigation incorrect zone[", zone.c_str(), "] or time values tOn[", tOn, "] tPause[", tPause, "]");
response["values"]["status"] = "invalid";
return response;
}
@@ -207,38 +222,39 @@ namespace commands
if (timerHandle)
{ // this timer was alteady started, ignore command
LOG_WARN("setIrrigation zone", timerName, "already started");
LOG_WARN("setIrrigation zone [", timerName, "] already started");
response["values"]["status"] = "conflict";
return response;
}
const uint32_t pumpTime((tOn + 30) * 1000);
const uint32_t zoneTime(tOn * 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);
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);
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));
TimerHandle_t shTimer(xTimerCreate(timerName, pdMS_TO_TICKS(zoneTime), false, (void *)&dev, resetZone));
if (shTimer)
{
dev.io.digitalOutWrite(zoneIoNumber, true);
xTimerStart(shTimer, pdMS_TO_TICKS(tPause * 1000));
xTimerStart(shTimer, 0);
timerHandle = shTimer;
response["values"]["status"] = "ok";
LOG_INFO("setIrrigation zone -> ", timerName, "tOn", tOn, "tPause", tPause);
LOG_INFO("setIrrigation zone [", timerName, "] tOn[", tOn, "] tPause[", tPause, "]");
}
return response;
}