Skip to content

Commit

Permalink
Small refactor of EnergyEvse Delegate - untested
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesharrow committed Nov 9, 2023
1 parent 863e84b commit 79a6811
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> & 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<uint32_t> & 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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> & 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<uint32_t> & 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;
}

0 comments on commit 79a6811

Please sign in to comment.