Refactor timer disposal and improve code readability

Co-authored-by: erikdred <88980320+erikdred@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-27 20:17:59 +00:00
parent bd11c827da
commit ae0b2fe086

View File

@@ -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;
}
/// <summary>
/// Disposes the current movement timer if it exists
/// </summary>
private void DisposeMovementTimer()
{
if (_movementTimer != null)
{
_movementTimer.Stop();
_movementTimer.Dispose();
_movementTimer = null;
}
}
/// <summary>
/// Called when movement timer completes
/// </summary>
@@ -287,29 +286,31 @@ 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)
{
case RequestedState.Raise:
if (InUpPosition)
{
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)
else
{
Raise();
}
else if (commandToExecute == RequestedState.Lower)
break;
case RequestedState.Lower:
if (!InUpPosition)
{
Debug.LogMessage(LogEventLevel.Debug, this, $"Already in down position, ignoring banked Lower command");
}
else
{
Lower();
}
break;
}
}
}