diff --git a/everest/integrationplugineverest.cpp b/everest/integrationplugineverest.cpp index 367e8791..5afe61e6 100644 --- a/everest/integrationplugineverest.cpp +++ b/everest/integrationplugineverest.cpp @@ -30,7 +30,9 @@ #include "integrationplugineverest.h" #include "plugininfo.h" + #include "mqtt/everestmqttdiscovery.h" +#include "jsonrpc/everestevse.h" #include "jsonrpc/everestjsonrpcdiscovery.h" #include @@ -470,9 +472,78 @@ void IntegrationPluginEverest::executeAction(ThingActionInfo *info) return; } + if (info->action().actionTypeId() == everestChargerAcPowerActionTypeId) { + bool power = info->action().paramValue(everestChargerAcPowerActionPowerParamTypeId).toBool(); + qCDebug(dcEverest()) << "Execute power action" << power; + EverestJsonRpcReply *reply = evse->setChargingAllowed(power) ; + connect(reply, &EverestJsonRpcReply::finished, reply, &EverestJsonRpcReply::deleteLater); + connect(reply, &EverestJsonRpcReply::finished, this, [info, reply, power](){ + if (reply->error()) { + qCWarning(dcEverest()) << "Execute action reply finished with error" << reply->error(); + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + QVariantMap result = reply->response().value("result").toMap(); + EverestJsonRpcClient::ResponseError error = EverestJsonRpcClient::parseResponseError(result.value("error").toString()); + if (error) { + qCWarning(dcEverest()) << "Execute action reply finished with an error" << reply->method() << error; + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + info->thing()->setStateValue(everestChargerAcCurrentPowerStateTypeId, power); + info->finish(Thing::ThingErrorNoError); + }); + } else if (info->action().actionTypeId() == everestChargerAcMaxChargingCurrentActionTypeId) { + double current = info->action().paramValue(everestChargerAcMaxChargingCurrentActionMaxChargingCurrentParamTypeId).toDouble(); + qCDebug(dcEverest()) << "Execute action set max charging current" << current << "[A]"; + EverestJsonRpcReply *reply = evse->setACChargingCurrent(current) ; + connect(reply, &EverestJsonRpcReply::finished, reply, &EverestJsonRpcReply::deleteLater); + connect(reply, &EverestJsonRpcReply::finished, this, [info, reply, current](){ + if (reply->error()) { + qCWarning(dcEverest()) << "Execute action reply finished with error" << reply->error(); + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + QVariantMap result = reply->response().value("result").toMap(); + EverestJsonRpcClient::ResponseError error = EverestJsonRpcClient::parseResponseError(result.value("error").toString()); + if (error) { + qCWarning(dcEverest()) << "Execute action reply finished with an error" << reply->method() << error; + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + + info->thing()->setStateValue(everestChargerAcMaxChargingCurrentStateTypeId, current); + info->finish(Thing::ThingErrorNoError); + }); + } else if (info->action().actionTypeId() == everestChargerAcDesiredPhaseCountActionTypeId) { + int phaseCount = info->action().paramValue(everestChargerAcDesiredPhaseCountActionDesiredPhaseCountParamTypeId).toInt(); + qCDebug(dcEverest()) << "Execute action set phase count" << phaseCount; + EverestJsonRpcReply *reply = evse->setACChargingPhaseCount(phaseCount); + connect(reply, &EverestJsonRpcReply::finished, reply, &EverestJsonRpcReply::deleteLater); + connect(reply, &EverestJsonRpcReply::finished, this, [info, reply, phaseCount](){ + if (reply->error()) { + qCWarning(dcEverest()) << "Execute action reply finished with error" << reply->error(); + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + + QVariantMap result = reply->response().value("result").toMap(); + EverestJsonRpcClient::ResponseError error = EverestJsonRpcClient::parseResponseError(result.value("error").toString()); + if (error) { + qCWarning(dcEverest()) << "Execute action reply finished with an error" << reply->method() << error; + info->finish(Thing::ThingErrorHardwareFailure); + return; + } + + info->thing()->setStateValue(everestChargerAcDesiredPhaseCountStateTypeId, phaseCount); + info->finish(Thing::ThingErrorNoError); + }); + } + + return; } diff --git a/everest/jsonrpc/everestevse.cpp b/everest/jsonrpc/everestevse.cpp index 4d0eb3a4..1689d2a6 100644 --- a/everest/jsonrpc/everestevse.cpp +++ b/everest/jsonrpc/everestevse.cpp @@ -59,6 +59,20 @@ int EverestEvse::index() const return m_index; } +EverestJsonRpcReply *EverestEvse::setChargingAllowed(bool allowed) +{ + return m_client->evseSetChargingAllowed(m_index, allowed); +} + +EverestJsonRpcReply *EverestEvse::setACChargingCurrent(double current) +{ + return m_client->evseSetACChargingCurrent(m_index, current); +} + +EverestJsonRpcReply *EverestEvse::setACChargingPhaseCount(int phaseCount) +{ + return m_client->evseSetACChargingPhaseCount(m_index, phaseCount); +} void EverestEvse::initialize() { @@ -156,8 +170,8 @@ void EverestEvse::evaluateInitFinished(EverestJsonRpcReply *reply) m_thing->setStateValue("connected", true); processEvseStatus(); + processHardwareCapabilities(); } - } void EverestEvse::processEvseStatus() diff --git a/everest/jsonrpc/everestevse.h b/everest/jsonrpc/everestevse.h index b6486af4..c556fc2c 100644 --- a/everest/jsonrpc/everestevse.h +++ b/everest/jsonrpc/everestevse.h @@ -43,7 +43,9 @@ public: int index() const; -signals: + EverestJsonRpcReply *setChargingAllowed(bool allowed); + EverestJsonRpcReply *setACChargingCurrent(double current); + EverestJsonRpcReply *setACChargingPhaseCount(int phaseCount); private: EverestJsonRpcClient *m_client = nullptr; diff --git a/everest/jsonrpc/everestjsonrpcclient.cpp b/everest/jsonrpc/everestjsonrpcclient.cpp index 3a390ba6..6c91eee6 100644 --- a/everest/jsonrpc/everestjsonrpcclient.cpp +++ b/everest/jsonrpc/everestjsonrpcclient.cpp @@ -221,6 +221,30 @@ EverestJsonRpcReply *EverestJsonRpcClient::evseSetChargingAllowed(int evseIndex, return reply; } +EverestJsonRpcReply *EverestJsonRpcClient::evseSetACChargingCurrent(int evseIndex, double current) +{ + QVariantMap params; + params.insert("evse_index", evseIndex); + params.insert("max_current", current); + + EverestJsonRpcReply *reply = new EverestJsonRpcReply(m_commandId, "EVSE.SetACChargingCurrent", params, this); + qCDebug(dcEverest()) << "Calling" << reply->method() << params; + sendRequest(reply); + return reply; +} + +EverestJsonRpcReply *EverestJsonRpcClient::evseSetACChargingPhaseCount(int evseIndex, int phaseCount) +{ + QVariantMap params; + params.insert("evse_index", evseIndex); + params.insert("phase_count", phaseCount); + + EverestJsonRpcReply *reply = new EverestJsonRpcReply(m_commandId, "EVSE.SetACChargingPhaseCount", params, this); + qCDebug(dcEverest()) << "Calling" << reply->method() << params; + sendRequest(reply); + return reply; +} + EverestJsonRpcClient::ResponseError EverestJsonRpcClient::parseResponseError(const QString &responseErrorString) { QMetaEnum metaEnum = QMetaEnum::fromType(); diff --git a/everest/jsonrpc/everestjsonrpcclient.h b/everest/jsonrpc/everestjsonrpcclient.h index 2bcdbc71..194fe63c 100644 --- a/everest/jsonrpc/everestjsonrpcclient.h +++ b/everest/jsonrpc/everestjsonrpcclient.h @@ -182,6 +182,8 @@ public: EverestJsonRpcReply *evseGetMeterData(int evseIndex); EverestJsonRpcReply *evseSetChargingAllowed(int evseIndex, bool allowed); + EverestJsonRpcReply *evseSetACChargingCurrent(int evseIndex, double current); + EverestJsonRpcReply *evseSetACChargingPhaseCount(int evseIndex, int phaseCount); // API parser methods