From 79a681181cd9a4ffe38c036208d0eeddeb8cdee6 Mon Sep 17 00:00:00 2001 From: James Harrow Date: Thu, 9 Nov 2023 22:45:18 +0000 Subject: [PATCH] Small refactor of EnergyEvse Delegate - untested --- .../include/EnergyEvseDelegateImpl.h | 26 ++++ .../src/EnergyEvseDelegateImpl.cpp | 138 +++++++++++++++++- 2 files changed, 163 insertions(+), 1 deletion(-) diff --git a/examples/energy-management-app/energy-management-common/include/EnergyEvseDelegateImpl.h b/examples/energy-management-app/energy-management-common/include/EnergyEvseDelegateImpl.h index a844c39666c0f0..333610c9f9b477 100644 --- a/examples/energy-management-app/energy-management-common/include/EnergyEvseDelegateImpl.h +++ b/examples/energy-management-app/energy-management-common/include/EnergyEvseDelegateImpl.h @@ -38,13 +38,39 @@ class EnergyEvseDelegate : public Delegate public: EnergyEvseDelegate() : Delegate(){}; + /** + * @brief Called when EVSE cluster receives Disable command + */ virtual CHIP_ERROR Disable() override; + + /** + * @brief Called when EVSE cluster receives EnableCharging command + * + * @param enableChargeTime (in elapsed_s) + * @param minimumChargeCurrent (in mA) + * @param maximumChargeCurrent (in mA) + */ virtual CHIP_ERROR EnableCharging(const chip::app::DataModel::Nullable & enableChargeTime, const uint32_t & minimumChargeCurrent, const uint32_t & maximumChargeCurrent) override; + + /** + * @brief Called when EVSE cluster receives EnableDischarging command + * + * @param enableChargeTime (in elapsed_s) + * @param maximumChargeCurrent (in mA) + */ virtual CHIP_ERROR EnableDischarging(const chip::app::DataModel::Nullable & enableDischargeTime, const uint32_t & maximumDischargeCurrent) override; + + /** + * @brief Called when EVSE cluster receives StartDiagnostics command + */ virtual CHIP_ERROR StartDiagnostics() override; + // ----------------------------------------------------------------- + // Internal API to allow an EVSE to change its internal state etc + CHIP_ERROR SetFault(FaultStateEnum fault); + private: static EnergyEvseDelegate sInstance; }; diff --git a/examples/energy-management-app/energy-management-common/src/EnergyEvseDelegateImpl.cpp b/examples/energy-management-app/energy-management-common/src/EnergyEvseDelegateImpl.cpp index d36f953f78350d..0fc524265aba26 100644 --- a/examples/energy-management-app/energy-management-common/src/EnergyEvseDelegateImpl.cpp +++ b/examples/energy-management-app/energy-management-common/src/EnergyEvseDelegateImpl.cpp @@ -24,31 +24,167 @@ using namespace chip; using namespace chip::app; using namespace chip::app::Clusters; using namespace chip::app::Clusters::EnergyEvse; +using namespace chip::app::Clusters::EnergyEvse::Attributes; EnergyEvseDelegate EnergyEvseDelegate::sInstance; +/** + * @brief Called when EVSE cluster receives Disable command + */ CHIP_ERROR EnergyEvseDelegate::Disable() { ChipLogProgress(AppServer, "EnergyEvseDelegate::Disable()"); + + EndpointId endpointId = this->GetEndpoint(); + + /* update State */ + /* Get EVSE hardware state to understand if vehicle is plugged in and asking for demand */ + // hwState = EVSE_HW::GetEVSEState(); + // TODO add a mechanism to get real EVSE state + EvseStateEnum hwState = EvseStateEnum::kNotPluggedIn; + + switch (hwState) + { + case EvseStateEnum::kNotPluggedIn: + State::Set(endpointId, EvseStateEnum::kNotPluggedIn); + break; + + case EvseStateEnum::kPluggedInNoDemand: + State::Set(endpointId, EvseStateEnum::kPluggedInNoDemand); + break; + + case EvseStateEnum::kPluggedInDemand: + State::Set(endpointId, EvseStateEnum::kPluggedInDemand); + break; + + default: + ChipLogError(AppServer, "Unexpected EVSE hardware state\n"); + State::Set(endpointId, EvseStateEnum::kFault); + break; + } + + /* update SupplyState */ + SupplyState::Set(endpointId, SupplyStateEnum::kDisabled); + + /* update EnableChargeTime & EnableDischargeTime to show 0 */ + EnableChargeTime::Set(endpointId, 0); + EnableDischargeTime::Set(endpointId, 0); + + /* update MinimumChargeCurrent & MaximumChargeCurrent to 0 */ + MinimumChargeCurrent::Set(endpointId, 0); + MaximumChargeCurrent::Set(endpointId, 0); + + /* update MaximumDischargeCurrent to 0 */ + MaximumDischargeCurrent::Set(endpointId, 0); + + // TODO: Generate events + return CHIP_NO_ERROR; } +/** + * @brief Called when EVSE cluster receives EnableCharging command + * + * @param enableChargeTime (in elapsed_s) + * @param minimumChargeCurrent (in mA) + * @param maximumChargeCurrent (in mA) + */ CHIP_ERROR EnergyEvseDelegate::EnableCharging(const chip::app::DataModel::Nullable & evseEnableTime, const uint32_t & minimumChargeCurrent, const uint32_t & maximumChargeCurrent) { ChipLogProgress(AppServer, "EnergyEvseDelegate::EnableCharging()"); + + EndpointId endpointId = this->GetEndpoint(); + + /* Check current state isn't already enabled */ + + /* If charging is already enabled, check that the parameters may have + changed, these may override an existing charging command */ + + /* update SupplyState */ + SupplyState::Set(endpointId, SupplyStateEnum::kChargingEnabled); + + /* If it looks ok, store the min & max charging current */ + + /* If a evseEnableTime is not null then check the value */ + /* if 0 - then disable charging */ + /* otherwise compute future timestamp when charging should auto disable */ + + /* else - NULL - enable permanently */ + + // TODO: Generate events + return CHIP_NO_ERROR; } +/** + * @brief Called when EVSE cluster receives EnableDischarging command + * + * @param enableChargeTime (in elapsed_s) + * @param maximumChargeCurrent (in mA) + */ CHIP_ERROR EnergyEvseDelegate::EnableDischarging(const chip::app::DataModel::Nullable & evseEnableTime, const uint32_t & maximumDischargeCurrent) { - ChipLogProgress(AppServer, "EnergyEvseDelegate::EnableDischarging()"); + ChipLogProgress(AppServer, "EnergyEvseDelegate::EnableDischarging() called."); + + EndpointId endpointId = this->GetEndpoint(); + + /* update SupplyState */ + SupplyState::Set(endpointId, SupplyStateEnum::kDischargingEnabled); + + /* TODO: Generate events */ + return CHIP_NO_ERROR; } +/** + * @brief Called when EVSE cluster receives StartDiagnostics command + */ CHIP_ERROR EnergyEvseDelegate::StartDiagnostics() { + /* For EVSE manufacturers to customize */ ChipLogProgress(AppServer, "EnergyEvseDelegate::StartDiagnostics()"); + + EndpointId endpointId = this->GetEndpoint(); + + /* update SupplyState */ + SupplyState::Set(endpointId, SupplyStateEnum::kDisabledDiagnostics); + + // TODO: Generate events + return CHIP_NO_ERROR; } + +/** + * @brief Called by EVSE Hardware to indicate a fault + * + * @param FaultStateEnum + */ +CHIP_ERROR EnergyEvseDelegate::SetFault(FaultStateEnum fault) +{ + ChipLogProgress(AppServer, "EnergyEvseDelegate::Fault()"); + + EndpointId endpointId = this->GetEndpoint(); + + if (fault == FaultStateEnum::kNoError) + { + /* Update State to previous state */ + // TODO: need to work out the logic here! + + /* Update SupplyState to previous state */ + } + else + { + /* Update State & SupplyState */ + State::Set(endpointId, EvseStateEnum::kFault); + SupplyState::Set(endpointId, SupplyStateEnum::kDisabledError); + } + + /* Update FaultState */ + FaultState::Set(endpointId, fault); + + // TODO: Generate events + + return CHIP_NO_ERROR; +} \ No newline at end of file