From ae0b2fe0864692fd85a8c9fffce8f02fc4b3a105 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 27 Dec 2025 20:17:59 +0000 Subject: [PATCH] Refactor timer disposal and improve code readability Co-authored-by: erikdred <88980320+erikdred@users.noreply.github.com> --- .../Displays/ScreenLiftController.cs | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs b/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs index 4494fdbf..459819af 100644 --- a/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs +++ b/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs @@ -199,14 +199,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades if (RaiseRelayConfig.RaiseTimeInMs > 0) { _isMoving = true; - - // Dispose previous timer if exists - if (_movementTimer != null) - { - _movementTimer.Stop(); - _movementTimer.Dispose(); - } - + DisposeMovementTimer(); _movementTimer = new CTimer(OnMovementComplete, RaiseRelayConfig.RaiseTimeInMs); } break; @@ -249,14 +242,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades if (LowerRelayConfig.LowerTimeInMs > 0) { _isMoving = true; - - // Dispose previous timer if exists - if (_movementTimer != null) - { - _movementTimer.Stop(); - _movementTimer.Dispose(); - } - + DisposeMovementTimer(); _movementTimer = new CTimer(OnMovementComplete, LowerRelayConfig.LowerTimeInMs); } break; @@ -270,6 +256,19 @@ namespace PepperDash.Essentials.Devices.Common.Shades InUpPosition = false; } + /// + /// Disposes the current movement timer if it exists + /// + private void DisposeMovementTimer() + { + if (_movementTimer != null) + { + _movementTimer.Stop(); + _movementTimer.Dispose(); + _movementTimer = null; + } + } + /// /// Called when movement timer completes /// @@ -287,28 +286,30 @@ namespace PepperDash.Essentials.Devices.Common.Shades var commandToExecute = _requestedState; _requestedState = RequestedState.None; - // Check if current state matches what the banked command would do - // If so, ignore it - if (commandToExecute == RequestedState.Raise && InUpPosition) + // Check if current state matches what the banked command would do and execute if different + switch (commandToExecute) { - Debug.LogMessage(LogEventLevel.Debug, this, $"Already in up position, ignoring banked Raise command"); - return; - } - - if (commandToExecute == RequestedState.Lower && !InUpPosition) - { - Debug.LogMessage(LogEventLevel.Debug, this, $"Already in down position, ignoring banked Lower command"); - return; - } - - // Execute the banked command - if (commandToExecute == RequestedState.Raise) - { - Raise(); - } - else if (commandToExecute == RequestedState.Lower) - { - Lower(); + case RequestedState.Raise: + if (InUpPosition) + { + Debug.LogMessage(LogEventLevel.Debug, this, $"Already in up position, ignoring banked Raise command"); + } + else + { + Raise(); + } + break; + + case RequestedState.Lower: + if (!InUpPosition) + { + Debug.LogMessage(LogEventLevel.Debug, this, $"Already in down position, ignoring banked Lower command"); + } + else + { + Lower(); + } + break; } } }