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) const ArduinoJson::JsonDocument Commands::setHPlimit(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{ {
ArduinoJson::JsonDocument response; ArduinoJson::JsonDocument response;
{
std::string msg;
serializeJson(params, msg);
LOG_INFO("setHPlimit params ->", msg.c_str());
};
if (!params["level"].is<std::string>()) if (!params["level"].is<std::string>())
{ {
LOG_ERROR("setHPlimit incorrect parameters"); LOG_ERROR("setHPlimit incorrect parameters");
@@ -101,6 +106,11 @@ namespace commands
const ArduinoJson::JsonDocument Commands::setHeating(const devices_t &dev, const ArduinoJson::JsonDocument &params) const ArduinoJson::JsonDocument Commands::setHeating(const devices_t &dev, const ArduinoJson::JsonDocument &params)
{ {
ArduinoJson::JsonDocument response; ArduinoJson::JsonDocument response;
{
std::string msg;
serializeJson(params, msg);
LOG_INFO("setHeating params ->", msg.c_str());
};
if (params.isNull()) if (params.isNull())
{ {
LOG_ERROR("setHeating incorrect paramaters"); LOG_ERROR("setHeating incorrect paramaters");
@@ -130,7 +140,7 @@ namespace commands
{ {
devices_t *dev = (devices_t *)pvTimerGetTimerID(th); devices_t *dev = (devices_t *)pvTimerGetTimerID(th);
const char *timerName = pcTimerGetName(th); const char *timerName = pcTimerGetName(th);
LOG_INFO("Reset irrigation zone -> ", timerName); LOG_INFO("setIrrigation shutdown zone [", timerName, "]");
if (!c_irrigationValveMap.contains(timerName)) if (!c_irrigationValveMap.contains(timerName))
{ {
LOG_ERROR("Irrigation timer name invalid"); LOG_ERROR("Irrigation timer name invalid");
@@ -144,7 +154,7 @@ namespace commands
void resetWaterPump(TimerHandle_t th) void resetWaterPump(TimerHandle_t th)
{ {
devices_t *dev = (devices_t *)pvTimerGetTimerID(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); dev->io.digitalOutWrite(RO::IRR_PUMP, false);
s_irrigationPumpTimer = NULL; s_irrigationPumpTimer = NULL;
xTimerDelete(th, 0); // delete the timer on expiry xTimerDelete(th, 0); // delete the timer on expiry
@@ -154,6 +164,11 @@ namespace commands
{ {
ArduinoJson::JsonDocument response; ArduinoJson::JsonDocument response;
auto &conf = Config::getInstance(); auto &conf = Config::getInstance();
{
std::string msg;
serializeJson(params, msg);
LOG_INFO("setIrrigation params ->", msg.c_str());
};
response["cmd"] = "setIrrigation"; response["cmd"] = "setIrrigation";
if (params.isNull()) if (params.isNull())
{ {
@@ -181,7 +196,7 @@ namespace commands
timerHandle = NULL; 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 dev.io.digitalOutWrite(c_irrigationValveMap.at(zoneName), false); // shuto down the valve
} }
if (s_irrigationPumpTimer) if (s_irrigationPumpTimer)
@@ -193,9 +208,9 @@ namespace commands
return response; 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"; response["values"]["status"] = "invalid";
return response; return response;
} }
@@ -207,38 +222,39 @@ namespace commands
if (timerHandle) if (timerHandle)
{ // this timer was alteady started, ignore command { // 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"; response["values"]["status"] = "conflict";
return response; return response;
} }
const uint32_t pumpTime((tOn + 30) * 1000); const uint32_t pumpTime((tOn + 30) * 1000);
const uint32_t zoneTime(tOn * 1000);
if (!s_irrigationPumpTimer) // Pump has not yet started if (!s_irrigationPumpTimer) // Pump has not yet started
{ {
s_irrigationPumpTimer = xTimerCreate("pumpTimer", pdMS_TO_TICKS(pumpTime), false, (void *)&dev, resetWaterPump); s_irrigationPumpTimer = xTimerCreate("pumpTimer", pdMS_TO_TICKS(pumpTime), false, (void *)&dev, resetWaterPump);
dev.io.digitalOutWrite(RO::IRR_PUMP, true); dev.io.digitalOutWrite(RO::IRR_PUMP, true);
xTimerStart(s_irrigationPumpTimer, 0); // immediate start pump timer xTimerStart(s_irrigationPumpTimer, 0); // immediate start pump timer
LOG_INFO("setIrrigation pump time ", pumpTime); LOG_INFO("setIrrigation pump time", pumpTime);
} }
else else
{ {
const auto currentRemaining(xTimerGetExpiryTime(s_irrigationPumpTimer) - xTaskGetTickCount()); const auto currentRemaining(xTimerGetExpiryTime(s_irrigationPumpTimer) - xTaskGetTickCount());
const auto newRemaining(pumpTime); 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 xTimerChangePeriod(s_irrigationPumpTimer, newPeriod, 0); // set new period based on timing of new zone
xTimerReset(s_irrigationPumpTimer, 0); // if timer was already started, restart xTimerReset(s_irrigationPumpTimer, 0); // if timer was already started, restart
LOG_INFO("setIrrigation pump time reset", newRemaining); 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) if (shTimer)
{ {
dev.io.digitalOutWrite(zoneIoNumber, true); dev.io.digitalOutWrite(zoneIoNumber, true);
xTimerStart(shTimer, pdMS_TO_TICKS(tPause * 1000)); xTimerStart(shTimer, 0);
timerHandle = shTimer; timerHandle = shTimer;
response["values"]["status"] = "ok"; response["values"]["status"] = "ok";
LOG_INFO("setIrrigation zone -> ", timerName, "tOn", tOn, "tPause", tPause); LOG_INFO("setIrrigation zone [", timerName, "] tOn[", tOn, "] tPause[", tPause, "]");
} }
return response; return response;
} }