Skip to content

Commit 44c333f

Browse files
committed
Address further review comments from JamesH
1 parent 5964de9 commit 44c333f

File tree

3 files changed

+78
-49
lines changed

3 files changed

+78
-49
lines changed

examples/all-clusters-app/all-clusters-common/include/WhmDelegate.h

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ namespace WaterHeaterManagement {
3030

3131
class WhmManufacturer;
3232

33+
enum HeatingOp
34+
{
35+
TurnHeatingOn,
36+
TurnHeatingOff,
37+
LeaveHeatingUnchanged
38+
};
39+
3340
// This is an application level delegate to handle operational state commands according to the specific business logic.
3441
class WaterHeaterManagementDelegate : public WaterHeaterManagement::Delegate
3542
{
@@ -162,6 +169,16 @@ class WaterHeaterManagementDelegate : public WaterHeaterManagement::Delegate
162169
*/
163170
void DrawOffHotWater(uint8_t percentageReplaced, uint16_t replacedWaterTemperature);
164171

172+
private:
173+
/**
174+
* @brief Determine whether heating needs to be turned on or off or left as is.
175+
*
176+
* @param heatingOp[out] Set as determined whether heating needs to be turned on/off or left unchanged.
177+
*
178+
* @return Success if the heating operation could be determined otherwise returns with appropriate error.
179+
*/
180+
Protocols::InteractionModel::Status DetermineIfChangingHeatingState(HeatingOp & heatingOp);
181+
165182
private:
166183
/*********************************************************************************
167184
*

examples/all-clusters-app/all-clusters-common/src/WhmDelegateImpl.cpp

+47-49
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ void WaterHeaterManagementDelegate::HandleBoostTimerExpiry()
246246
*/
247247
Status WaterHeaterManagementDelegate::HandleCancelBoost()
248248
{
249-
Status status = Status::Success;
250-
251249
ChipLogProgress(AppServer, "HandleCancelBoost");
252250

253251
if (mBoostState == BoostStateEnum::kActive)
@@ -257,20 +255,16 @@ Status WaterHeaterManagementDelegate::HandleCancelBoost()
257255

258256
DeviceLayer::SystemLayer().CancelTimer(BoostTimerExpiry, this);
259257

260-
if (mpWhmManufacturer != nullptr)
261-
{
262-
status = mpWhmManufacturer->BoostCommandCancelled();
263-
}
264-
else
265-
{
266-
status = Status::InvalidInState;
267-
ChipLogError(AppServer, "HandleCancelBoost: mpWhmManufacturer == nullptr");
268-
}
258+
VerifyOrReturnValue(mpWhmManufacturer != nullptr, Status::InvalidInState);
259+
260+
Status status = mpWhmManufacturer->BoostCommandCancelled();
261+
VerifyOrReturnValue(status == Status::Success, status);
269262

270263
status = CheckIfHeatNeedsToBeTurnedOnOrOff();
264+
VerifyOrReturnValue(status == Status::Success, status);
271265
}
272266

273-
return status;
267+
return Status::Success;
274268
}
275269

276270
/*********************************************************************************
@@ -365,11 +359,44 @@ bool WaterHeaterManagementDelegate::HasWaterTemperatureReachedTarget() const
365359

366360
Status WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff()
367361
{
368-
Status status = Status::Success;
369-
bool turningHeatOff = false;
370-
371362
VerifyOrReturnError(mpWhmManufacturer != nullptr, Status::InvalidInState);
372363

364+
HeatingOp heatingOp = HeatingOp::LeaveHeatingUnchanged;
365+
366+
Status status = DetermineIfChangingHeatingState(heatingOp);
367+
368+
VerifyOrReturnError(status == Status::Success, status);
369+
370+
if (heatingOp == HeatingOp::TurnHeatingOn)
371+
{
372+
status = mpWhmManufacturer->TurnHeatingOn(mBoostEmergencyBoost.HasValue() ? mBoostEmergencyBoost.Value() : false);
373+
}
374+
else if (heatingOp == HeatingOp::TurnHeatingOff)
375+
{
376+
// If running a boost command with the oneShot parameter and turning heat off, then must have
377+
// reached the boost command target temperature -> that's the boost command complete.
378+
if (mBoostState == BoostStateEnum::kActive && mBoostOneShot.HasValue() && mBoostOneShot.Value())
379+
{
380+
SetBoostState(BoostStateEnum::kInactive);
381+
382+
DeviceLayer::SystemLayer().CancelTimer(BoostTimerExpiry, this);
383+
384+
mBoostEmergencyBoost.ClearValue();
385+
386+
status = mpWhmManufacturer->BoostCommandCancelled();
387+
}
388+
389+
// Turn the heating off
390+
status = mpWhmManufacturer->TurnHeatingOff();
391+
}
392+
393+
return status;
394+
}
395+
396+
Status WaterHeaterManagementDelegate::DetermineIfChangingHeatingState(HeatingOp & heatingOp)
397+
{
398+
heatingOp = LeaveHeatingUnchanged;
399+
373400
if (!HasWaterTemperatureReachedTarget())
374401
{
375402
VerifyOrReturnError(WaterHeaterMode::Instance() != nullptr, Status::InvalidInState);
@@ -389,57 +416,28 @@ Status WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff()
389416
// If a boost command is in progress or in manual mode, find a heating source and "turn it on".
390417
if (mBoostState == BoostStateEnum::kActive || mode == WaterHeaterMode::kModeManual)
391418
{
392-
// Find out from the manufacturer object the heating sources to use.
393-
BitMask<WaterHeaterDemandBitmap> heaterDemand = mpWhmManufacturer->DetermineHeatingSources();
394-
395-
SetHeatDemand(heaterDemand);
396-
397-
// And turn the heating of the water tank on.
398-
status = mpWhmManufacturer->TurnHeatingOn(mBoostEmergencyBoost.HasValue() ? mBoostEmergencyBoost.Value() : false);
419+
heatingOp = HeatingOp::TurnHeatingOn;
399420
}
400421
}
401422
else if (mBoostState == BoostStateEnum::kInactive && mode == WaterHeaterMode::kModeOff)
402423
{
403424
// The water temperature is not at the target temperature but there is no boost command in progress and the mode is Off
404425
// so need to ensure the heating is turned off.
405-
ChipLogError(AppServer, "CheckIfHeatNeedsToBeTurnedOnOrOff turning heating off due to no boost cmd and kModeOff");
426+
ChipLogError(AppServer, "DetermineIfChangingHeatingState turning heating off due to no boost cmd and kModeOff");
406427

407-
SetHeatDemand(BitMask<WaterHeaterDemandBitmap>(0));
408-
409-
turningHeatOff = true;
428+
heatingOp = HeatingOp::TurnHeatingOff;
410429
}
411430
}
412431
else if (mHeatDemand.Raw() != 0)
413432
{
414433
// The water in the tank has reached the target temperature - need to turn the heating off
415-
SetHeatDemand(BitMask<WaterHeaterDemandBitmap>(0));
416-
417-
turningHeatOff = true;
434+
heatingOp = HeatingOp::TurnHeatingOff;
418435

419436
// If a boost command is in progress, record that the target temperature has been reached.
420437
mBoostTargetTemperatureReached = (mBoostState == BoostStateEnum::kActive);
421438
}
422439

423-
if (turningHeatOff)
424-
{
425-
// If running a boost command with the oneShot parameter and turning heat off, then must have
426-
// reached the boost command target temperature -> that's the boost command complete.
427-
if (mBoostState == BoostStateEnum::kActive && mBoostOneShot.HasValue() && mBoostOneShot.Value())
428-
{
429-
SetBoostState(BoostStateEnum::kInactive);
430-
431-
DeviceLayer::SystemLayer().CancelTimer(BoostTimerExpiry, this);
432-
433-
mBoostEmergencyBoost.ClearValue();
434-
435-
status = mpWhmManufacturer->BoostCommandCancelled();
436-
}
437-
438-
// Turn the heating off
439-
status = mpWhmManufacturer->TurnHeatingOff();
440-
}
441-
442-
return status;
440+
return Status::Success;
443441
}
444442

445443
Status WaterHeaterManagementDelegate::SetWaterHeaterMode(uint8_t modeValue)

examples/all-clusters-app/all-clusters-common/src/WhmManufacturer.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ Status WhmManufacturer::TurnHeatingOn(bool emergencyBoost)
114114
mBoostActive = true;
115115
}
116116

117+
if (emergencyBoost)
118+
{
119+
dg->SetHeatDemand(BitMask<WaterHeaterDemandBitmap>(WaterHeaterDemandBitmap::kImmersionElement1,
120+
WaterHeaterDemandBitmap::kImmersionElement2));
121+
}
122+
else
123+
{
124+
dg->SetHeatDemand(BitMask<WaterHeaterDemandBitmap>(WaterHeaterDemandBitmap::kImmersionElement1));
125+
}
126+
117127
return status;
118128
}
119129

@@ -128,6 +138,10 @@ Status WhmManufacturer::TurnHeatingOff()
128138
mBoostActive = false;
129139
}
130140

141+
WaterHeaterManagementDelegate * dg = GetWhmDelegate();
142+
143+
dg->SetHeatDemand(BitMask<WaterHeaterDemandBitmap>(0));
144+
131145
return status;
132146
}
133147

0 commit comments

Comments
 (0)