Skip to content

Commit

Permalink
Fixed bug where PowerAdjustReason wasn't calling MatterReportingAttri…
Browse files Browse the repository at this point in the history
…buteChangeCallback when reason was changing.
  • Loading branch information
jamesharrow committed Aug 29, 2024
1 parent 58e6bf3 commit 81175d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class DeviceEnergyManagementDelegate : public DeviceEnergyManagement::Delegate
CHIP_ERROR SetAbsMinPower(int64_t);
CHIP_ERROR SetAbsMaxPower(int64_t);
CHIP_ERROR SetPowerAdjustmentCapability(const DataModel::Nullable<Structs::PowerAdjustCapabilityStruct::Type> &);
CHIP_ERROR SetPowerAdjustmentCapabilityPowerAdjustReason( const PowerAdjustReasonEnum );

// The DeviceEnergyManagementDelegate owns the master copy of the ForecastStruct object which is accessed via GetForecast and
// SetForecast. The slots field of forecast is owned and managed by the object that implements the DEMManufacturerDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ Status DeviceEnergyManagementDelegate::PowerAdjustRequest(const int64_t powerMw,
switch (cause)
{
case AdjustmentCauseEnum::kLocalOptimization:
mPowerAdjustCapabilityStruct.Value().cause = PowerAdjustReasonEnum::kLocalOptimizationAdjustment;
SetPowerAdjustmentCapabilityPowerAdjustReason(PowerAdjustReasonEnum::kLocalOptimizationAdjustment);
break;

case AdjustmentCauseEnum::kGridOptimization:
mPowerAdjustCapabilityStruct.Value().cause = PowerAdjustReasonEnum::kGridOptimizationAdjustment;
SetPowerAdjustmentCapabilityPowerAdjustReason(PowerAdjustReasonEnum::kGridOptimizationAdjustment);
break;

default:
Expand Down Expand Up @@ -175,7 +175,7 @@ void DeviceEnergyManagementDelegate::HandlePowerAdjustRequestFailure()

mPowerAdjustmentInProgress = false;

mPowerAdjustCapabilityStruct.Value().cause = PowerAdjustReasonEnum::kNoAdjustment;
SetPowerAdjustmentCapabilityPowerAdjustReason(PowerAdjustReasonEnum::kNoAdjustment);

// TODO
// Should we inform the mpDEMManufacturerDelegate that PowerAdjustRequest has failed?
Expand Down Expand Up @@ -210,7 +210,7 @@ void DeviceEnergyManagementDelegate::HandlePowerAdjustTimerExpiry()

SetESAState(ESAStateEnum::kOnline);

mPowerAdjustCapabilityStruct.Value().cause = PowerAdjustReasonEnum::kNoAdjustment;
SetPowerAdjustmentCapabilityPowerAdjustReason(PowerAdjustReasonEnum::kNoAdjustment);

// Generate a PowerAdjustEnd event
GeneratePowerAdjustEndEvent(CauseEnum::kNormalCompletion);
Expand Down Expand Up @@ -264,8 +264,7 @@ CHIP_ERROR DeviceEnergyManagementDelegate::CancelPowerAdjustRequestAndGenerateEv
SetESAState(ESAStateEnum::kOnline);

mPowerAdjustmentInProgress = false;

mPowerAdjustCapabilityStruct.Value().cause = PowerAdjustReasonEnum::kNoAdjustment;
SetPowerAdjustmentCapabilityPowerAdjustReason(PowerAdjustReasonEnum::kNoAdjustment);

CHIP_ERROR err = GeneratePowerAdjustEndEvent(cause);

Expand Down Expand Up @@ -922,6 +921,18 @@ DeviceEnergyManagementDelegate::SetPowerAdjustmentCapability(
return CHIP_NO_ERROR;
}

CHIP_ERROR
DeviceEnergyManagementDelegate::SetPowerAdjustmentCapabilityPowerAdjustReason(PowerAdjustReasonEnum powerAdjustReason)
{
assertChipStackLockedByCurrentThread();

mPowerAdjustCapabilityStruct.Value().cause = powerAdjustReason;

MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, PowerAdjustmentCapability::Id);

return CHIP_NO_ERROR;
}

CHIP_ERROR DeviceEnergyManagementDelegate::SetForecast(const DataModel::Nullable<Structs::ForecastStruct::Type> & forecast)
{
assertChipStackLockedByCurrentThread();
Expand Down Expand Up @@ -962,9 +973,9 @@ CHIP_ERROR DeviceEnergyManagementDelegate::SetOptOutState(OptOutStateEnum newVal
if (mPowerAdjustmentInProgress)
{
if ((newValue == OptOutStateEnum::kLocalOptOut &&
mPowerAdjustCapabilityStruct.Value().cause == PowerAdjustReasonEnum::kLocalOptimizationAdjustment) ||
GetPowerAdjustmentCapability().Value().cause == PowerAdjustReasonEnum::kLocalOptimizationAdjustment) ||
(newValue == OptOutStateEnum::kGridOptOut &&
mPowerAdjustCapabilityStruct.Value().cause == PowerAdjustReasonEnum::kGridOptimizationAdjustment) ||
GetPowerAdjustmentCapability().Value().cause == PowerAdjustReasonEnum::kGridOptimizationAdjustment) ||
newValue == OptOutStateEnum::kOptOut)
{
err = CancelPowerAdjustRequestAndGenerateEvent(DeviceEnergyManagement::CauseEnum::kUserOptOut);
Expand Down

0 comments on commit 81175d3

Please sign in to comment.