Skip to content

Commit 54f87e5

Browse files
committed
Improve error handling
1 parent d44f3ed commit 54f87e5

File tree

4 files changed

+138
-22
lines changed

4 files changed

+138
-22
lines changed

examples/water-heater-management-app/water-heater-management-common/include/WhmDelegate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class WaterHeaterManagementDelegate : public WaterHeaterManagement::Delegate,
174174
/**
175175
* @brief Determine whether the heating sources need to be turned on or off
176176
*/
177-
void CheckIfHeatNeedsToBeTurnedOnOrOff();
177+
Protocols::InteractionModel::Status CheckIfHeatNeedsToBeTurnedOnOrOff();
178178

179179
/**
180180
* @brief Static timer callback for when Boost timer expires.

examples/water-heater-management-app/water-heater-management-common/include/WhmManufacturer.h

+29-2
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,42 @@ class WhmManufacturer
7070
/**
7171
* @brief Turn the heating of the water tank on.
7272
*/
73-
void TurnHeatingOn();
73+
Protocols::InteractionModel::Status TurnHeatingOn();
7474

7575
/**
7676
* @brief Turn the heating of the water tank off.
7777
*/
78-
void TurnHeatingOff();
78+
Protocols::InteractionModel::Status TurnHeatingOff();
79+
80+
/**
81+
* @brief Called to handle a boost command.
82+
*
83+
* @param duration Indicates the time period in seconds for which the BOOST state is activated before it automatically reverts to the previous mode (e.g. OFF, MANUAL or TIMED).
84+
* @param oneShot Indicates whether the BOOST state should be automatically canceled once the hot water has first reached the set point temperature (or the TemporarySetpoint temperature, if specified) for the TargetPercentage (if specified).
85+
* @param emergencyBoost Indicates that the consumer wants the water to be heated as quickly as practicable. This MAY cause multiple heat sources to be activated (e.g. a heat pump and direct electric heating element).
86+
* @param temporarySetpoint Indicates the target temperature to which to heat the hot water for this Boost command. It SHALL be used instead of the normal set point temperature whilst the BOOST state is active.
87+
* @param targetPercentage If the tank supports the TankPercent feature, this field indicates the amount of water that SHALL be heated by this Boost command before the heater is switched off.
88+
* @param targetReheat If the tank supports the TankPercent feature, and the heating by this Boost command has ceased because the TargetPercentage of the water in the tank has been heated to the set point (or TemporarySetpoint if included), this field indicates the percentage to which the hot water in the tank SHALL be allowed to fall before again beginning to reheat it.
89+
*
90+
* @return Success if the boost command is successful; otherwise return the appropriate error.
91+
*/
92+
Protocols::InteractionModel::Status BoostCommandStarted(uint32_t duration, Optional<bool> oneShot, Optional<bool> emergencyBoost, Optional<int16_t> temporarySetpoint, Optional<chip::Percent> targetPercentage, Optional<chip::Percent> targetReheat);
93+
94+
/**
95+
* @brief Called when the Boost command has been cancelled.
96+
*
97+
* @return It should report SUCCESS if successful and FAILURE otherwise.
98+
*/
99+
Protocols::InteractionModel::Status BoostCommandCancelled();
100+
101+
/**
102+
* @brief Called when a boost command has completed.
103+
*/
104+
void BoostCommandFinished();
79105

80106
private:
81107
WaterHeaterManagementInstance * mWhmInstance;
108+
bool mBoostActive;
82109
};
83110

84111
/** @brief Helper function to return the singleton WhmManufacturer instance

examples/water-heater-management-app/water-heater-management-common/src/WhmDelegateImpl.cpp

+68-17
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ void WaterHeaterManagementDelegate::SetBoostState(BoostStateEnum boostState)
169169
*/
170170
Status WaterHeaterManagementDelegate::HandleBoost(uint32_t durationS, Optional<bool> oneShot, Optional<bool> emergencyBoost, Optional<int16_t> temporarySetpoint, Optional<chip::Percent> targetPercentage, Optional<chip::Percent> targetReheat)
171171
{
172-
ChipLogProgress(AppServer, "WaterHeaterManagementDelegate::HandleBoost");
172+
Status status = Status::Success;
173+
174+
ChipLogProgress(AppServer, "HandleBoost");
173175

174176
// Keep track of the boost command parameters
175177
mBoostOneShot = oneShot;
@@ -189,7 +191,7 @@ Status WaterHeaterManagementDelegate::HandleBoost(uint32_t durationS, Optional<b
189191
CHIP_ERROR err = DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(durationS), BoostTimerExpiry, this);
190192
if (err != CHIP_NO_ERROR)
191193
{
192-
ChipLogError(AppServer, "Unable to start a Boost timer: %" CHIP_ERROR_FORMAT, err.Format());
194+
ChipLogError(AppServer, "HandleBoost: Unable to start a Boost timer: %" CHIP_ERROR_FORMAT, err.Format());
193195

194196
// Not a lot we can do -> just set the boost state to inactive
195197
SetBoostState(BoostStateEnum::kInactive);
@@ -200,10 +202,23 @@ Status WaterHeaterManagementDelegate::HandleBoost(uint32_t durationS, Optional<b
200202
// Now running a boost command
201203
SetBoostState(BoostStateEnum::kActive);
202204

203-
// See if the heat needs to be turned on or off as a result of this boost command
204-
CheckIfHeatNeedsToBeTurnedOnOrOff();
205+
if (mpWhmManufacturer != nullptr)
206+
{
207+
status = mpWhmManufacturer->BoostCommandStarted(durationS, oneShot, emergencyBoost, temporarySetpoint, targetPercentage, targetReheat);
208+
}
209+
else
210+
{
211+
status = Status::InvalidInState;
212+
ChipLogError(AppServer, "HandleBoost: mpWhmManufacturer == nullptr");
213+
}
214+
215+
if (status == Status::Success)
216+
{
217+
// See if the heat needs to be turned on or off as a result of this boost command
218+
status = CheckIfHeatNeedsToBeTurnedOnOrOff();
219+
}
205220

206-
return Status::Success;
221+
return status;
207222
}
208223

209224
void WaterHeaterManagementDelegate::BoostTimerExpiry(System::Layer * systemLayer, void * delegate)
@@ -218,11 +233,20 @@ void WaterHeaterManagementDelegate::BoostTimerExpiry(System::Layer * systemLayer
218233
*/
219234
void WaterHeaterManagementDelegate::HandleBoostTimerExpiry()
220235
{
221-
ChipLogError(AppServer, "WaterHeaterManagementDelegate::HandleBoostTimerExpiry");
236+
ChipLogError(AppServer, "HandleBoostTimerExpiry");
222237

223238
// The PowerAdjustment is no longer in progress
224239
SetBoostState(BoostStateEnum::kInactive);
225240

241+
if (mpWhmManufacturer != nullptr)
242+
{
243+
mpWhmManufacturer->BoostCommandFinished();
244+
}
245+
else
246+
{
247+
ChipLogError(AppServer, "HandleBoostTimerExpiry: mpWhmManufacturer == nullptr");
248+
}
249+
226250
CheckIfHeatNeedsToBeTurnedOnOrOff();
227251
}
228252

@@ -233,18 +257,30 @@ void WaterHeaterManagementDelegate::HandleBoostTimerExpiry()
233257
*/
234258
Status WaterHeaterManagementDelegate::HandleCancelBoost()
235259
{
236-
ChipLogProgress(AppServer, "WaterHeaterManagementDelegate::HandleCancelBoost");
260+
Status status = Status::Success;
261+
262+
ChipLogProgress(AppServer, "HandleCancelBoost");
237263

238264
if (mBoostState == BoostStateEnum::kActive)
239265
{
240266
SetBoostState(BoostStateEnum::kInactive);
241267

242268
DeviceLayer::SystemLayer().CancelTimer(BoostTimerExpiry, this);
243269

244-
CheckIfHeatNeedsToBeTurnedOnOrOff();
270+
if (mpWhmManufacturer != nullptr)
271+
{
272+
status = mpWhmManufacturer->BoostCommandCancelled();
273+
}
274+
else
275+
{
276+
status = Status::InvalidInState;
277+
ChipLogError(AppServer, "HandleCancelBoost: mpWhmManufacturer == nullptr");
278+
}
279+
280+
status = CheckIfHeatNeedsToBeTurnedOnOrOff();
245281
}
246282

247-
return Status::Success;
283+
return status;
248284
}
249285

250286
/*********************************************************************************
@@ -387,8 +423,9 @@ bool WaterHeaterManagementDelegate::HasWaterTemperatureReachedTarget() const
387423
return (mTankPercentage >= targetPercentage) && (mHotWaterTemperature >= targetTemperature);
388424
}
389425

390-
void WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff()
426+
Status WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff()
391427
{
428+
Status status = Status::Success;
392429
bool turningHeatOff = false;
393430

394431
if (!HasWaterTemperatureReachedTarget())
@@ -416,19 +453,20 @@ void WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff()
416453
SetHeatDemand(heaterDemand);
417454

418455
// And turn the heating of the water tank on.
419-
mpWhmManufacturer->TurnHeatingOn();
456+
status = mpWhmManufacturer->TurnHeatingOn();
420457
}
421458
else
422459
{
423-
ChipLogError(Zcl, "WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff: Failed as mpWhmManufacturer == nullptr");
460+
status = Status::InvalidInState;
461+
ChipLogError(AppServer, "CheckIfHeatNeedsToBeTurnedOnOrOff: Failed as mpWhmManufacturer == nullptr");
424462
}
425463
}
426464
}
427465
else if (mBoostState == BoostStateEnum::kInactive && mode == ModeOff)
428466
{
429467
// The water temperature is not at the target temperature but there is no boost command in progress and the mode is Off
430468
// so need to ensure the heating is turned off.
431-
ChipLogError(Zcl, "WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff turning heating off due to mode");
469+
ChipLogError(AppServer, "CheckIfHeatNeedsToBeTurnedOnOrOff turning heating off due to mode");
432470

433471
SetHeatDemand(BitMask<WaterHeaterDemandBitmap>(0));
434472

@@ -455,32 +493,45 @@ void WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff()
455493
SetBoostState(BoostStateEnum::kInactive);
456494

457495
DeviceLayer::SystemLayer().CancelTimer(BoostTimerExpiry, this);
496+
497+
if (mpWhmManufacturer != nullptr)
498+
{
499+
status = mpWhmManufacturer->BoostCommandCancelled();
500+
}
501+
else
502+
{
503+
status = Status::InvalidInState;
504+
ChipLogError(AppServer, "CheckIfHeatNeedsToBeTurnedOnOrOff: mpWhmManufacturer == nullptr");
505+
}
458506
}
459507

460508
// Turn the heating off
461509
if (mpWhmManufacturer != nullptr)
462510
{
463-
mpWhmManufacturer->TurnHeatingOff();
511+
status = mpWhmManufacturer->TurnHeatingOff();
464512
}
465513
else
466514
{
467-
ChipLogError(Zcl, "WaterHeaterManagementDelegate::CheckIfHeatNeedsToBeTurnedOnOrOff: Failed to turn the heating off as mpWhmManufacturer == nullptr");
515+
status = Status::InvalidInState;
516+
ChipLogError(AppServer, "CheckIfHeatNeedsToBeTurnedOnOrOff: Failed to turn the heating off as mpWhmManufacturer == nullptr");
468517
}
469518
}
519+
520+
return status;
470521
}
471522

472523
void WaterHeaterManagementDelegate::SetWaterHeaterMode(uint8_t modeValue)
473524
{
474525
if (!mInstance->IsSupportedMode(modeValue))
475526
{
476-
ChipLogError(Zcl, "WaterHeaterManagementDelegate::SetWaterHeaterMode bad mode");
527+
ChipLogError(AppServer, "SetWaterHeaterMode bad mode");
477528
return;
478529
}
479530

480531
Status status = mInstance->UpdateCurrentMode(modeValue);
481532
if (status != Status::Success)
482533
{
483-
ChipLogError(Zcl, "WaterHeaterManagementDelegate::SetWaterHeaterMode updateMode failed");
534+
ChipLogError(AppServer, "SetWaterHeaterMode updateMode failed");
484535
return;
485536
}
486537

examples/water-heater-management-app/water-heater-management-common/src/WhmManufacturer.cpp

+40-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
using namespace chip;
2828
using namespace chip::app::Clusters::WaterHeaterManagement;
2929

30+
using Protocols::InteractionModel::Status;
31+
3032
CHIP_ERROR WhmManufacturer::Init()
3133
{
3234
WaterHeaterManagementDelegate * dg = GetWhmManufacturer()->GetWhmDelegate();
@@ -36,6 +38,8 @@ CHIP_ERROR WhmManufacturer::Init()
3638
return CHIP_ERROR_UNINITIALIZED;
3739
}
3840

41+
mBoostActive = false;
42+
3943
return CHIP_NO_ERROR;
4044
}
4145

@@ -90,14 +94,48 @@ BitMask<WaterHeaterDemandBitmap> WhmManufacturer::DetermineHeatingSources()
9094
return BitMask<WaterHeaterDemandBitmap>(heaterDemandMask);
9195
}
9296

93-
void WhmManufacturer::TurnHeatingOn()
97+
Status WhmManufacturer::TurnHeatingOn()
9498
{
99+
Status status = Status::Success;
100+
95101
ChipLogProgress(AppServer, "WhmManufacturer::TurnHeatingOn");
102+
103+
WaterHeaterManagementDelegate * dg = GetWhmDelegate();
104+
105+
if (dg->GetBoostState() == BoostStateEnum::kActive)
106+
{
107+
mBoostActive = true;
108+
}
109+
110+
return status;
96111
}
97112

98-
void WhmManufacturer::TurnHeatingOff()
113+
Status WhmManufacturer::TurnHeatingOff()
99114
{
115+
Status status = Status::Success;
116+
100117
ChipLogProgress(AppServer, "WhmManufacturer::TurnHeatingOff");
118+
119+
if (mBoostActive)
120+
{
121+
mBoostActive = false;
122+
}
123+
124+
return status;
125+
}
126+
127+
Status WhmManufacturer::BoostCommandStarted(uint32_t duration, Optional<bool> oneShot, Optional<bool> emergencyBoost, Optional<int16_t> temporarySetpoint, Optional<chip::Percent> targetPercentage, Optional<chip::Percent> targetReheat)
128+
{
129+
return Status::Success;
130+
}
131+
132+
Status WhmManufacturer::BoostCommandCancelled()
133+
{
134+
return Status::Success;
135+
}
136+
137+
void WhmManufacturer::BoostCommandFinished()
138+
{
101139
}
102140

103141
WaterHeaterManagementDelegate * GetWhmDelegate()

0 commit comments

Comments
 (0)