From 5affe6614e43bf657f3ea7bba440811c50b44f1b Mon Sep 17 00:00:00 2001 From: Martin Lukas Date: Fri, 2 Aug 2024 08:12:34 +0200 Subject: [PATCH] Implementation of play pause depending on action issuer Signed-off-by: Martin Lukas --- inro/integrationplugininro.cpp | 67 +++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/inro/integrationplugininro.cpp b/inro/integrationplugininro.cpp index 5eda2bc..5c54585 100644 --- a/inro/integrationplugininro.cpp +++ b/inro/integrationplugininro.cpp @@ -162,27 +162,58 @@ void IntegrationPluginInro::executeAction(ThingActionInfo *info) if (info->action().actionTypeId() == pantaboxPowerActionTypeId) { bool power = info->action().paramValue(pantaboxPowerActionPowerParamTypeId).toBool(); - qCDebug(dcInro()) << "PANTABOX: Set power" << (power ? 1 : 0); - QModbusReply *reply = connection->setChargingEnabled(power ? 1 : 0); - if (!reply) { - qCWarning(dcInro()) << "Execute action failed because the reply could not be created."; - info->finish(Thing::ThingErrorHardwareFailure); + if (info->action().triggeredBy() == Action::TriggeredByUser) { + + // When power is set by user, charging is going to stop or start depending on setting + qCDebug(dcInro()) << "PANTABOX: Set power by user" << (power ? 1 : 0); + QModbusReply *reply = connection->setChargingEnabled(power ? 1 : 0); + + if (!reply) { + qCWarning(dcInro()) << "Execute action failed because the reply could not be created."; + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + + connect(reply, &QModbusReply::finished, reply, &QModbusReply::deleteLater); + connect(reply, &QModbusReply::finished, info, [info, reply, power](){ + if (reply->error() == QModbusDevice::NoError) { + info->thing()->setStateValue(pantaboxPowerStateTypeId, power); + qCDebug(dcInro()) << "PANTABOX: Set power by user finished successfully"; + info->finish(Thing::ThingErrorNoError); + } else { + qCWarning(dcInro()) << "Error setting power by user:" << reply->error() << reply->errorString(); + info->finish(Thing::ThingErrorHardwareFailure); + } + }); + return; + } else { + + // When power is set to 0 by energy manager, max charging current is set to 0 otherwise take the configured max charging current + qCDebug(dcInro()) << "PANTABOX: Pause session by energy manager"; + + quint16 chargingCurrent = power ? info->action().paramValue(pantaboxMaxChargingCurrentActionMaxChargingCurrentParamTypeId).toUInt() : 0; + QModbusReply *reply = connection->setMaxChargingCurrent(chargingCurrent); + + if (!reply) { + qCWarning(dcInro()) << "Execute action failed because the reply could not be created."; + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + + connect(reply, &QModbusReply::finished, reply, &QModbusReply::deleteLater); + connect(reply, &QModbusReply::finished, info, [info, reply, chargingCurrent](){ + if (reply->error() == QModbusDevice::NoError) { + info->thing()->setStateValue(pantaboxMaxChargingCurrentStateTypeId, chargingCurrent); + qCDebug(dcInro()) << "PANTABOX: Set max charging current finished successfully"; + info->finish(Thing::ThingErrorNoError); + } else { + qCWarning(dcInro()) << "Error setting charging current:" << reply->error() << reply->errorString(); + info->finish(Thing::ThingErrorHardwareFailure); + } + }); return; } - - connect(reply, &QModbusReply::finished, reply, &QModbusReply::deleteLater); - connect(reply, &QModbusReply::finished, info, [info, reply, power](){ - if (reply->error() == QModbusDevice::NoError) { - info->thing()->setStateValue(pantaboxPowerStateTypeId, power); - qCDebug(dcInro()) << "PANTABOX: Set power finished successfully"; - info->finish(Thing::ThingErrorNoError); - } else { - qCWarning(dcInro()) << "Error setting power:" << reply->error() << reply->errorString(); - info->finish(Thing::ThingErrorHardwareFailure); - } - }); - return; } if (info->action().actionTypeId() == pantaboxMaxChargingCurrentActionTypeId) {