@@ -157,69 +157,71 @@ void AppTask::ClosureButtonActionEventHandler(AppEvent * aEvent)
157157 if (aEvent->Type == AppEvent::kEventType_Button )
158158 {
159159 // Schedule work on the chip stack thread to ensure all CHIP API calls are safe
160- chip::DeviceLayer::PlatformMgr ().ScheduleWork ([](intptr_t ) {
161- // Check if an action is already in progress
162- if (ClosureManager::GetInstance ().IsClosureControlMotionInProgress ())
163- {
164- // Stop the current action
165- auto status = ClosureManager::GetInstance ().GetClosureControlLogic ().HandleStop ();
166- if (status != Protocols::InteractionModel::Status::Success)
160+ chip::DeviceLayer::PlatformMgr ().ScheduleWork (
161+ [](intptr_t ) {
162+ // Check if an action is already in progress
163+ if (ClosureManager::GetInstance ().IsClosureControlMotionInProgress ())
167164 {
168- ChipLogError (AppServer, " Failed to stop closure action: %u" , to_underlying (status));
165+ // Stop the current action
166+ auto status = ClosureManager::GetInstance ().GetClosureControlLogic ().HandleStop ();
167+ if (status != Protocols::InteractionModel::Status::Success)
168+ {
169+ ChipLogError (AppServer, " Failed to stop closure action: %u" , to_underlying (status));
170+ }
169171 }
170- }
171- else
172- {
173- DataModel::Nullable<ClosureControl::GenericOverallCurrentState> currentState;
174- CHIP_ERROR err = ClosureManager::GetInstance ().GetClosureControlLogic ().GetOverallCurrentState (currentState);
175-
176- if (err != CHIP_NO_ERROR)
177- {
178- ChipLogError (AppServer, " Failed to get current closure state: %s" , chip::ErrorStr (err));
179- return ;
180- }
181- if (currentState.IsNull ())
182- {
183- ChipLogError (AppServer, " Failed to get current closure state: currentState is null" );
184- return ;
185- }
186- if (!currentState.Value ().position .HasValue () || currentState.Value ().position .Value ().IsNull ())
187- {
188- ChipLogError (AppServer, " Failed to get current closure state: position is null" );
189- return ;
190- }
191-
192- // Get current position and determine target position (toggle)
193- auto currentPosition = currentState.Value ().position .Value ().Value ();
194- ChipLogProgress (AppServer, " Current state - Position: %d" , to_underlying (currentPosition));
195-
196- ClosureControl::TargetPositionEnum targetPosition =
197- (currentPosition == ClosureControl::CurrentPositionEnum::kFullyOpened )
198- ? ClosureControl::TargetPositionEnum::kMoveToFullyClosed
199- : ClosureControl::TargetPositionEnum::kMoveToFullyOpen ;
200- ChipLogProgress (AppServer, " Target position: %d" , to_underlying (targetPosition));
201-
202- Optional<bool > latch = chip::NullOptional;
203- if (currentState.Value ().latch .HasValue () && !currentState.Value ().latch .Value ().IsNull ())
204- {
205- latch = MakeOptional (false );
206- }
207-
208- Optional<Globals::ThreeLevelAutoEnum> speed = NullOptional;
209- if (currentState.Value ().speed .HasValue ())
210- {
211- speed = chip::MakeOptional (currentState.Value ().speed .Value ());
212- }
213-
214- // Move to the target position with latch set to false and preserved speed value
215- auto status = ClosureManager::GetInstance ().GetClosureControlLogic ().HandleMoveTo (
216- MakeOptional (targetPosition), latch, speed);
217- if (status != Protocols::InteractionModel::Status::Success)
172+ else
218173 {
219- ChipLogError (AppServer, " Failed to move closure to target position: %u" , to_underlying (status));
174+ DataModel::Nullable<ClosureControl::GenericOverallCurrentState> currentState;
175+ CHIP_ERROR err = ClosureManager::GetInstance ().GetClosureControlLogic ().GetOverallCurrentState (currentState);
176+
177+ if (err != CHIP_NO_ERROR)
178+ {
179+ ChipLogError (AppServer, " Failed to get current closure state: %s" , chip::ErrorStr (err));
180+ return ;
181+ }
182+ if (currentState.IsNull ())
183+ {
184+ ChipLogError (AppServer, " Failed to get current closure state: currentState is null" );
185+ return ;
186+ }
187+ if (!currentState.Value ().position .HasValue () || currentState.Value ().position .Value ().IsNull ())
188+ {
189+ ChipLogError (AppServer, " Failed to get current closure state: position is null" );
190+ return ;
191+ }
192+
193+ // Get current position and determine target position (toggle)
194+ auto currentPosition = currentState.Value ().position .Value ().Value ();
195+ ChipLogProgress (AppServer, " Current state - Position: %d" , to_underlying (currentPosition));
196+
197+ ClosureControl::TargetPositionEnum targetPosition =
198+ (currentPosition == ClosureControl::CurrentPositionEnum::kFullyOpened )
199+ ? ClosureControl::TargetPositionEnum::kMoveToFullyClosed
200+ : ClosureControl::TargetPositionEnum::kMoveToFullyOpen ;
201+ ChipLogProgress (AppServer, " Target position: %d" , to_underlying (targetPosition));
202+
203+ Optional<bool > latch = chip::NullOptional;
204+ if (currentState.Value ().latch .HasValue () && !currentState.Value ().latch .Value ().IsNull ())
205+ {
206+ latch = MakeOptional (false );
207+ }
208+
209+ Optional<Globals::ThreeLevelAutoEnum> speed = NullOptional;
210+ if (currentState.Value ().speed .HasValue ())
211+ {
212+ speed = chip::MakeOptional (currentState.Value ().speed .Value ());
213+ }
214+
215+ // Move to the target position with latch set to false and preserved speed value
216+ auto status = ClosureManager::GetInstance ().GetClosureControlLogic ().HandleMoveTo (MakeOptional (targetPosition),
217+ latch, speed);
218+ if (status != Protocols::InteractionModel::Status::Success)
219+ {
220+ ChipLogError (AppServer, " Failed to move closure to target position: %u" , to_underlying (status));
221+ }
220222 }
221- }
222- }, 0 );
223+ },
224+ 0 );
223225 }
224226 else
225227 {
0 commit comments