Skip to content

Commit 02906e1

Browse files
committed
Valve: Disco ball - Handle automatic close
1 parent 30e6fd1 commit 02906e1

File tree

3 files changed

+557
-43
lines changed

3 files changed

+557
-43
lines changed

src/app/clusters/valve-configuration-and-control-server/valve-configuration-and-control-cluster-logic.cpp

+35-4
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,44 @@ CHIP_ERROR ClusterLogic::HandleOpenCommand(std::optional<DataModel::Nullable<Ela
457457
void ClusterLogic::HandleCloseInternal()
458458
{
459459
// TODO: call the delegate and add to tests
460-
// TODO: Handle error returns on the delegate.
461-
460+
CHIP_ERROR err;
461+
BitMask<ValveFaultBitmap> faults;
462+
if (mConformance.HasFeature(Feature::kLevel))
463+
{
464+
Percent currentLevel;
465+
err = mClusterDriver.HandleCloseValve(currentLevel, faults);
466+
if (err == CHIP_NO_ERROR)
467+
{
468+
mState.SetCurrentLevel(DataModel::Nullable<Percent>(currentLevel));
469+
if (currentLevel == 0)
470+
{
471+
mState.SetCurrentState(DataModel::Nullable<ValveStateEnum>(ValveStateEnum::kClosed));
472+
}
473+
else
474+
{
475+
mState.SetCurrentState(DataModel::Nullable<ValveStateEnum>(ValveStateEnum::kTransitioning));
476+
}
477+
}
478+
}
479+
else
480+
{
481+
ValveStateEnum state;
482+
err = mClusterDriver.HandleCloseValve(state, faults);
483+
if (err == CHIP_NO_ERROR)
484+
{
485+
mState.SetCurrentState(state);
486+
}
487+
}
488+
// If there was an error, we know nothing about the current state
489+
if (err != CHIP_NO_ERROR)
490+
{
491+
mState.SetCurrentLevel(DataModel::NullNullable);
492+
mState.SetCurrentState(DataModel::NullNullable);
493+
}
494+
mState.SetValveFault(faults);
462495
mState.SetOpenDuration(DataModel::NullNullable);
463496
mState.SetRemainingDuration(DataModel::NullNullable);
464-
mState.SetCurrentLevel(DataModel::Nullable<Percent>(0));
465497
mState.SetTargetLevel(DataModel::NullNullable);
466-
mState.SetCurrentState(DataModel::Nullable<ValveStateEnum>(ValveStateEnum::kClosed));
467498
mState.SetTargetState(DataModel::NullNullable);
468499
mState.SetAutoCloseTime(DataModel::NullNullable);
469500
}

src/app/clusters/valve-configuration-and-control-server/valve-configuration-and-control-delegate.h

+51-12
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ class DelegateBase
5959
DelegateBase(){};
6060
virtual ~DelegateBase() = default;
6161

62-
// TODO: Remove these
63-
virtual DataModel::Nullable<chip::Percent> HandleOpenValve(DataModel::Nullable<chip::Percent> level)
64-
{
65-
return DataModel::NullNullable;
66-
}
67-
virtual void HandleRemainingDurationTick(uint32_t duration) {}
68-
6962
// This delegate function will be called only for valve implementations that support the LVL feature.
7063
// Delegates for valves that do not support the LVL feature return CHIP_ERROR_NOT_IMPLEMENTED.
7164
// When this function is called, the delegate should set the valve to the target level, or begin the async process of opening
@@ -89,7 +82,7 @@ class DelegateBase
8982
// When this function is called, the delegate should open the valve, or begin the async process of opening the valve.
9083
// If the valve is able to be opened (success)
9184
// - If the valve is fully open, currentState should be set to kOpen
92-
// - If the valve is not fully opened, the delegate should set the currentLevel to the current valve level and the caller
85+
// - If the valve is not fully opened, the delegate should set the currentState to kTransitioning and the caller
9386
// will continue to query the valve level until the target level is reached or the valve is closed.
9487
// - A valve fault may be returned even if the Open command is successful, if the fault did not prevent the valve from safely
9588
// opening
@@ -103,7 +96,41 @@ class DelegateBase
10396
virtual Percent GetCurrentValveLevel() = 0;
10497
// This delegate function will be called only for valve implementations that do not support the LVL feature.
10598
virtual ValveStateEnum GetCurrentValveState() = 0;
106-
virtual CHIP_ERROR HandleCloseValve() = 0;
99+
100+
// This delegate function will be called when the valve needs to be closed either due to an explicit command or
101+
// from the expiration of the open duration. This function will be called for valves that support the LVL feature.
102+
// Delegates for valves that do not support the LVL feature should return CHIP_ERROR_NOT_IMPLEMENTED.
103+
// When this function is called, the delegate should close the valve, or begin the async process of closing.
104+
// If the valve is able to be closed (success)
105+
// - the delegate should set currentLevel
106+
// - If the valve is fully closed, currentLevel should be set to 0
107+
// - If the valve is not fully closed, the delegate should set the currentLevel to the current valve level and the caller
108+
// will continue to query the valve level until the valve reaches 0 or a command overrides.
109+
// - A valve fault may be returned even if the Open command is successful, if the fault did not prevent the valve from safely
110+
// closing
111+
// - return CHIP_NO_ERROR
112+
// If the valve cannot be closed (failure)
113+
// - The delegate should set the valveFault parameter to indicate the reason for the failure (if applicable)
114+
// - The delegate should return a CHIP_ERROR_INTERNAL
115+
virtual CHIP_ERROR HandleCloseValve(Percent & currentLevel, BitMask<ValveFaultBitmap> & valveFault) = 0;
116+
117+
// This delegate function will be called when the valve needs to be closed either due to an explicit command or
118+
// from the expiration of the open duration.
119+
// This delegate function will be called only for valve implementations that DO NOT support the LVL feature.
120+
// Delegates for valves that support the LVL feature should return CHIP_ERROR_NOT_IMPLEMENTED.
121+
// When this function is called, the delegate should close the valve, or begin the async process of closing.
122+
// If the valve is able to be closed (success)
123+
// - If the valve is fully closed, currentState should be set to kClosed
124+
// - If the valve is not fully opened, the delegate should set the currentLevel to the current valve level and the caller
125+
// will continue to query the valve level until the target level is reached or the valve is closed.
126+
// - A valve fault may be returned even if the Open command is successful, if the fault did not prevent the valve from safely
127+
// opening
128+
// - return CHIP_NO_ERROR
129+
// If the valve cannot be safely opened (failure)
130+
// - The delegate should set the valveFault parameter to indicate the reason for the failure (if applicable)
131+
// - The delegate should return a CHIP_ERROR_INTERNAL
132+
virtual CHIP_ERROR HandleCloseValve(ValveStateEnum & currentState, BitMask<ValveFaultBitmap> & valveFault) = 0;
133+
107134
virtual DelegateType GetDelegateType() { return DelegateType::kBase; };
108135
};
109136

@@ -112,26 +139,38 @@ class LevelControlDelegate : public DelegateBase
112139
virtual CHIP_ERROR HandleOpenValve(const Percent targetLevel, Percent & currentLevel,
113140
BitMask<ValveFaultBitmap> & valveFault) = 0;
114141

115-
virtual Percent GetCurrentValveLevel() = 0;
142+
virtual Percent GetCurrentValveLevel() = 0;
143+
virtual CHIP_ERROR HandleCloseValve(Percent & currentLevel, BitMask<ValveFaultBitmap> & valveFault) = 0;
116144

117145
// Final overrides - the driver should not implement these classes
118146
CHIP_ERROR HandleOpenValve(ValveStateEnum & currentState, BitMask<ValveFaultBitmap> & valveFault) final
119147
{
120148
return CHIP_ERROR_NOT_IMPLEMENTED;
121149
}
150+
CHIP_ERROR HandleCloseValve(ValveStateEnum & currentState, BitMask<ValveFaultBitmap> & valveFault) final
151+
{
152+
return CHIP_ERROR_NOT_IMPLEMENTED;
153+
}
154+
122155
ValveStateEnum GetCurrentValveState() final { return ValveStateEnum::kUnknownEnumValue; }
123156
DelegateType GetDelegateType() final { return DelegateType::kLevel; };
124157
};
125158

126159
class NonLevelControlDelegate : public DelegateBase
127160
{
128-
virtual CHIP_ERROR HandleOpenValve(ValveStateEnum & currentState, BitMask<ValveFaultBitmap> & valveFault) = 0;
129-
virtual ValveStateEnum GetCurrentValveState() = 0;
161+
virtual CHIP_ERROR HandleOpenValve(ValveStateEnum & currentState, BitMask<ValveFaultBitmap> & valveFault) = 0;
162+
virtual ValveStateEnum GetCurrentValveState() = 0;
163+
virtual CHIP_ERROR HandleCloseValve(ValveStateEnum & currentState, BitMask<ValveFaultBitmap> & valveFault) = 0;
130164

131165
CHIP_ERROR HandleOpenValve(const Percent targetLevel, Percent & currentLevel, BitMask<ValveFaultBitmap> & valveFault) final
132166
{
133167
return CHIP_ERROR_NOT_IMPLEMENTED;
134168
}
169+
CHIP_ERROR HandleCloseValve(Percent & currentLevel, BitMask<ValveFaultBitmap> & valveFault) final
170+
{
171+
return CHIP_ERROR_NOT_IMPLEMENTED;
172+
}
173+
135174
Percent GetCurrentValveLevel() final { return 0; }
136175
DelegateType GetDelegateType() final { return DelegateType::kNonLevel; };
137176
};

0 commit comments

Comments
 (0)