fix: use .NET timers instead of CTimer

This commit is contained in:
Andrew Welker
2025-12-29 11:53:52 -06:00
parent 7d3f871460
commit 0c4aec14d1

View File

@@ -1,12 +1,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Timers;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using PepperDash.Core; using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.Core; using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.Config;
using PepperDash.Essentials.Core.CrestronIO; using PepperDash.Essentials.Core.CrestronIO;
using PepperDash.Essentials.Core.DeviceTypeInterfaces; using PepperDash.Essentials.Core.DeviceTypeInterfaces;
using Serilog.Events; using PepperDash.Essentials.Devices.Common.Displays;
namespace PepperDash.Essentials.Devices.Common.Shades namespace PepperDash.Essentials.Devices.Common.Shades
{ {
@@ -30,7 +32,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades
readonly ScreenLiftRelaysConfig LowerRelayConfig; readonly ScreenLiftRelaysConfig LowerRelayConfig;
readonly ScreenLiftRelaysConfig LatchedRelayConfig; readonly ScreenLiftRelaysConfig LatchedRelayConfig;
Displays.DisplayBase DisplayDevice; DisplayBase DisplayDevice;
ISwitchedOutput RaiseRelay; ISwitchedOutput RaiseRelay;
ISwitchedOutput LowerRelay; ISwitchedOutput LowerRelay;
ISwitchedOutput LatchedRelay; ISwitchedOutput LatchedRelay;
@@ -38,7 +40,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades
private bool _isMoving; private bool _isMoving;
private RequestedState _requestedState; private RequestedState _requestedState;
private RequestedState _currentMovement; private RequestedState _currentMovement;
private CTimer _movementTimer; private Timer _movementTimer;
/// <summary> /// <summary>
/// Gets or sets the InUpPosition /// Gets or sets the InUpPosition
@@ -95,6 +97,11 @@ namespace PepperDash.Essentials.Devices.Common.Shades
IsInUpPosition = new BoolFeedback("isInUpPosition", () => _isInUpPosition); IsInUpPosition = new BoolFeedback("isInUpPosition", () => _isInUpPosition);
// Initialize movement timer for reuse
_movementTimer = new Timer();
_movementTimer.Elapsed += OnMovementComplete;
_movementTimer.AutoReset = false;
switch (Mode) switch (Mode)
{ {
case eScreenLiftControlMode.momentary: case eScreenLiftControlMode.momentary:
@@ -144,25 +151,25 @@ namespace PepperDash.Essentials.Devices.Common.Shades
{ {
case eScreenLiftControlMode.momentary: case eScreenLiftControlMode.momentary:
{ {
Debug.LogMessage(LogEventLevel.Debug, this, $"Getting relays for {Mode}"); this.LogDebug("Getting relays for {mode}", Mode);
RaiseRelay = GetSwitchedOutputFromDevice(RaiseRelayConfig.DeviceKey); RaiseRelay = GetSwitchedOutputFromDevice(RaiseRelayConfig.DeviceKey);
LowerRelay = GetSwitchedOutputFromDevice(LowerRelayConfig.DeviceKey); LowerRelay = GetSwitchedOutputFromDevice(LowerRelayConfig.DeviceKey);
break; break;
} }
case eScreenLiftControlMode.latched: case eScreenLiftControlMode.latched:
{ {
Debug.LogMessage(LogEventLevel.Debug, this, $"Getting relays for {Mode}"); this.LogDebug("Getting relays for {mode}", Mode);
LatchedRelay = GetSwitchedOutputFromDevice(LatchedRelayConfig.DeviceKey); LatchedRelay = GetSwitchedOutputFromDevice(LatchedRelayConfig.DeviceKey);
break; break;
} }
} }
Debug.LogMessage(LogEventLevel.Debug, this, $"Getting display with key {DisplayDeviceKey}"); this.LogDebug("Getting display with key {displayKey}", DisplayDeviceKey);
DisplayDevice = GetDisplayBaseFromDevice(DisplayDeviceKey); DisplayDevice = GetDisplayBaseFromDevice(DisplayDeviceKey);
if (DisplayDevice != null) if (DisplayDevice != null)
{ {
Debug.LogMessage(LogEventLevel.Debug, this, $"Subscribing to {DisplayDeviceKey} feedbacks"); this.LogDebug("Subscribing to {displayKey} feedbacks", DisplayDeviceKey);
DisplayDevice.IsWarmingUpFeedback.OutputChange += IsWarmingUpFeedback_OutputChange; DisplayDevice.IsWarmingUpFeedback.OutputChange += IsWarmingUpFeedback_OutputChange;
DisplayDevice.IsCoolingDownFeedback.OutputChange += IsCoolingDownFeedback_OutputChange; DisplayDevice.IsCoolingDownFeedback.OutputChange += IsCoolingDownFeedback_OutputChange;
@@ -178,17 +185,17 @@ namespace PepperDash.Essentials.Devices.Common.Shades
{ {
if (RaiseRelay == null && LatchedRelay == null) return; if (RaiseRelay == null && LatchedRelay == null) return;
Debug.LogMessage(LogEventLevel.Debug, this, $"Raise called for {Type}"); this.LogDebug("Raise called for {type}", Type);
// If device is moving, bank the command // If device is moving, bank the command
if (_isMoving) if (_isMoving)
{ {
Debug.LogMessage(LogEventLevel.Debug, this, $"Device is moving, banking Raise command"); this.LogDebug("Device is moving, banking Raise command");
_requestedState = RequestedState.Raise; _requestedState = RequestedState.Raise;
return; return;
} }
Debug.LogMessage(LogEventLevel.Debug, this, $"Raising {Type}"); this.LogDebug("Raising {type}", Type);
switch (Mode) switch (Mode)
{ {
@@ -201,8 +208,12 @@ namespace PepperDash.Essentials.Devices.Common.Shades
{ {
_isMoving = true; _isMoving = true;
_currentMovement = RequestedState.Raise; _currentMovement = RequestedState.Raise;
DisposeMovementTimer(); if (_movementTimer.Enabled)
_movementTimer = new CTimer(OnMovementComplete, RaiseRelayConfig.MoveTimeInMs); {
_movementTimer.Stop();
}
_movementTimer.Interval = RaiseRelayConfig.MoveTimeInMs;
_movementTimer.Start();
} }
else else
{ {
@@ -226,17 +237,17 @@ namespace PepperDash.Essentials.Devices.Common.Shades
{ {
if (LowerRelay == null && LatchedRelay == null) return; if (LowerRelay == null && LatchedRelay == null) return;
Debug.LogMessage(LogEventLevel.Debug, this, $"Lower called for {Type}"); this.LogDebug("Lower called for {type}", Type);
// If device is moving, bank the command // If device is moving, bank the command
if (_isMoving) if (_isMoving)
{ {
Debug.LogMessage(LogEventLevel.Debug, this, $"Device is moving, banking Lower command"); this.LogDebug("Device is moving, banking Lower command");
_requestedState = RequestedState.Lower; _requestedState = RequestedState.Lower;
return; return;
} }
Debug.LogMessage(LogEventLevel.Debug, this, $"Lowering {Type}"); this.LogDebug("Lowering {type}", Type);
switch (Mode) switch (Mode)
{ {
@@ -249,8 +260,12 @@ namespace PepperDash.Essentials.Devices.Common.Shades
{ {
_isMoving = true; _isMoving = true;
_currentMovement = RequestedState.Lower; _currentMovement = RequestedState.Lower;
DisposeMovementTimer(); if (_movementTimer.Enabled)
_movementTimer = new CTimer(OnMovementComplete, LowerRelayConfig.MoveTimeInMs); {
_movementTimer.Stop();
}
_movementTimer.Interval = LowerRelayConfig.MoveTimeInMs;
_movementTimer.Start();
} }
else else
{ {
@@ -267,14 +282,12 @@ namespace PepperDash.Essentials.Devices.Common.Shades
} }
} }
/// <summary>
/// Disposes the current movement timer if it exists
/// </summary>
private void DisposeMovementTimer() private void DisposeMovementTimer()
{ {
if (_movementTimer != null) if (_movementTimer != null)
{ {
_movementTimer.Stop(); _movementTimer.Stop();
_movementTimer.Elapsed -= OnMovementComplete;
_movementTimer.Dispose(); _movementTimer.Dispose();
_movementTimer = null; _movementTimer = null;
} }
@@ -283,9 +296,9 @@ namespace PepperDash.Essentials.Devices.Common.Shades
/// <summary> /// <summary>
/// Called when movement timer completes /// Called when movement timer completes
/// </summary> /// </summary>
private void OnMovementComplete(object o) private void OnMovementComplete(object sender, ElapsedEventArgs e)
{ {
Debug.LogMessage(LogEventLevel.Debug, this, $"Movement complete"); this.LogDebug("Movement complete");
// Update position based on completed movement // Update position based on completed movement
if (_currentMovement == RequestedState.Raise) if (_currentMovement == RequestedState.Raise)
@@ -303,7 +316,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades
// Execute banked command if one exists // Execute banked command if one exists
if (_requestedState != RequestedState.None) if (_requestedState != RequestedState.None)
{ {
Debug.LogMessage(LogEventLevel.Debug, this, $"Executing banked command: {_requestedState}"); this.LogDebug("Executing next command: {command}", _requestedState);
var commandToExecute = _requestedState; var commandToExecute = _requestedState;
_requestedState = RequestedState.None; _requestedState = RequestedState.None;
@@ -322,41 +335,47 @@ namespace PepperDash.Essentials.Devices.Common.Shades
} }
} }
void PulseOutput(ISwitchedOutput output, int pulseTime) private void PulseOutput(ISwitchedOutput output, int pulseTime)
{ {
output.On(); output.On();
CTimer pulseTimer = new CTimer(new CTimerCallbackFunction((o) => output.Off()), pulseTime);
var timer = new Timer(pulseTime)
{
AutoReset = false
};
timer.Elapsed += (sender, e) =>
{
output.Off();
timer.Dispose();
};
timer.Start();
} }
/// <summary> private ISwitchedOutput GetSwitchedOutputFromDevice(string relayKey)
/// Attempts to get the port on teh specified device from config
/// </summary>
/// <param name="relayKey"></param>
/// <returns></returns>
ISwitchedOutput GetSwitchedOutputFromDevice(string relayKey)
{ {
var portDevice = DeviceManager.GetDeviceForKey(relayKey); var portDevice = DeviceManager.GetDeviceForKey<ISwitchedOutput>(relayKey);
if (portDevice != null) if (portDevice != null)
{ {
return portDevice as ISwitchedOutput; return portDevice;
} }
else else
{ {
Debug.LogMessage(LogEventLevel.Debug, this, "Error: Unable to get relay device with key '{0}'", relayKey); this.LogWarning("Error: Unable to get relay device with key '{relayKey}'", relayKey);
return null; return null;
} }
} }
Displays.DisplayBase GetDisplayBaseFromDevice(string displayKey) private DisplayBase GetDisplayBaseFromDevice(string displayKey)
{ {
var displayDevice = DeviceManager.GetDeviceForKey(displayKey); var displayDevice = DeviceManager.GetDeviceForKey<DisplayBase>(displayKey);
if (displayDevice != null) if (displayDevice != null)
{ {
return displayDevice as Displays.DisplayBase; return displayDevice;
} }
else else
{ {
Debug.LogMessage(LogEventLevel.Debug, this, "Error: Unable to get display device with key '{0}'", displayKey); this.LogWarning("Error: Unable to get display device with key '{displayKey}'", displayKey);
return null; return null;
} }
} }
@@ -364,7 +383,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades
} }
/// <summary> /// <summary>
/// Represents a ScreenLiftControllerFactory /// Factory for ScreenLiftController devices
/// </summary> /// </summary>
public class ScreenLiftControllerFactory : EssentialsDeviceFactory<RelayControlledShade> public class ScreenLiftControllerFactory : EssentialsDeviceFactory<RelayControlledShade>
{ {
@@ -376,14 +395,11 @@ namespace PepperDash.Essentials.Devices.Common.Shades
TypeNames = new List<string>() { "screenliftcontroller" }; TypeNames = new List<string>() { "screenliftcontroller" };
} }
/// <summary>
/// BuildDevice method
/// </summary>
/// <inheritdoc /> /// <inheritdoc />
public override EssentialsDevice BuildDevice(DeviceConfig dc) public override EssentialsDevice BuildDevice(DeviceConfig dc)
{ {
Debug.LogMessage(LogEventLevel.Debug, "Factory Attempting to create new Generic Comm Device"); Debug.LogDebug("Factory Attempting to create new ScreenLiftController Device");
var props = Newtonsoft.Json.JsonConvert.DeserializeObject<ScreenLiftControllerConfigProperties>(dc.Properties.ToString()); var props = dc.Properties.ToObject<ScreenLiftControllerConfigProperties>();
return new ScreenLiftController(dc.Key, dc.Name, props); return new ScreenLiftController(dc.Key, dc.Name, props);
} }