diff --git a/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs b/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs index aad8f54a..f0e57de2 100644 --- a/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs +++ b/src/PepperDash.Essentials.Devices.Common/Displays/ScreenLiftController.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; +using System.Timers; using Crestron.SimplSharp; using PepperDash.Core; +using PepperDash.Core.Logging; using PepperDash.Essentials.Core; using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.CrestronIO; using PepperDash.Essentials.Core.DeviceTypeInterfaces; -using Serilog.Events; +using PepperDash.Essentials.Devices.Common.Displays; namespace PepperDash.Essentials.Devices.Common.Shades { @@ -30,7 +32,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades readonly ScreenLiftRelaysConfig LowerRelayConfig; readonly ScreenLiftRelaysConfig LatchedRelayConfig; - Displays.DisplayBase DisplayDevice; + DisplayBase DisplayDevice; ISwitchedOutput RaiseRelay; ISwitchedOutput LowerRelay; ISwitchedOutput LatchedRelay; @@ -38,7 +40,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades private bool _isMoving; private RequestedState _requestedState; private RequestedState _currentMovement; - private CTimer _movementTimer; + private Timer _movementTimer; /// /// Gets or sets the InUpPosition @@ -95,6 +97,11 @@ namespace PepperDash.Essentials.Devices.Common.Shades IsInUpPosition = new BoolFeedback("isInUpPosition", () => _isInUpPosition); + // Initialize movement timer for reuse + _movementTimer = new Timer(); + _movementTimer.Elapsed += OnMovementComplete; + _movementTimer.AutoReset = false; + switch (Mode) { case eScreenLiftControlMode.momentary: @@ -144,25 +151,25 @@ namespace PepperDash.Essentials.Devices.Common.Shades { case eScreenLiftControlMode.momentary: { - Debug.LogMessage(LogEventLevel.Debug, this, $"Getting relays for {Mode}"); + this.LogDebug("Getting relays for {mode}", Mode); RaiseRelay = GetSwitchedOutputFromDevice(RaiseRelayConfig.DeviceKey); LowerRelay = GetSwitchedOutputFromDevice(LowerRelayConfig.DeviceKey); break; } case eScreenLiftControlMode.latched: { - Debug.LogMessage(LogEventLevel.Debug, this, $"Getting relays for {Mode}"); + this.LogDebug("Getting relays for {mode}", Mode); LatchedRelay = GetSwitchedOutputFromDevice(LatchedRelayConfig.DeviceKey); break; } } - Debug.LogMessage(LogEventLevel.Debug, this, $"Getting display with key {DisplayDeviceKey}"); + this.LogDebug("Getting display with key {displayKey}", DisplayDeviceKey); DisplayDevice = GetDisplayBaseFromDevice(DisplayDeviceKey); 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.IsCoolingDownFeedback.OutputChange += IsCoolingDownFeedback_OutputChange; @@ -178,31 +185,35 @@ namespace PepperDash.Essentials.Devices.Common.Shades { 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 (_isMoving) { - Debug.LogMessage(LogEventLevel.Debug, this, $"Device is moving, banking Raise command"); + this.LogDebug("Device is moving, banking Raise command"); _requestedState = RequestedState.Raise; return; } - Debug.LogMessage(LogEventLevel.Debug, this, $"Raising {Type}"); - + this.LogDebug("Raising {type}", Type); + switch (Mode) { case eScreenLiftControlMode.momentary: { PulseOutput(RaiseRelay, RaiseRelayConfig.PulseTimeInMs); - + // Set moving flag and start timer if movement time is configured if (RaiseRelayConfig.MoveTimeInMs > 0) { _isMoving = true; _currentMovement = RequestedState.Raise; - DisposeMovementTimer(); - _movementTimer = new CTimer(OnMovementComplete, RaiseRelayConfig.MoveTimeInMs); + if (_movementTimer.Enabled) + { + _movementTimer.Stop(); + } + _movementTimer.Interval = RaiseRelayConfig.MoveTimeInMs; + _movementTimer.Start(); } else { @@ -226,31 +237,35 @@ namespace PepperDash.Essentials.Devices.Common.Shades { 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 (_isMoving) { - Debug.LogMessage(LogEventLevel.Debug, this, $"Device is moving, banking Lower command"); + this.LogDebug("Device is moving, banking Lower command"); _requestedState = RequestedState.Lower; return; } - Debug.LogMessage(LogEventLevel.Debug, this, $"Lowering {Type}"); + this.LogDebug("Lowering {type}", Type); switch (Mode) { case eScreenLiftControlMode.momentary: { PulseOutput(LowerRelay, LowerRelayConfig.PulseTimeInMs); - + // Set moving flag and start timer if movement time is configured if (LowerRelayConfig.MoveTimeInMs > 0) { _isMoving = true; _currentMovement = RequestedState.Lower; - DisposeMovementTimer(); - _movementTimer = new CTimer(OnMovementComplete, LowerRelayConfig.MoveTimeInMs); + if (_movementTimer.Enabled) + { + _movementTimer.Stop(); + } + _movementTimer.Interval = LowerRelayConfig.MoveTimeInMs; + _movementTimer.Start(); } else { @@ -267,14 +282,12 @@ namespace PepperDash.Essentials.Devices.Common.Shades } } - /// - /// Disposes the current movement timer if it exists - /// private void DisposeMovementTimer() { if (_movementTimer != null) { _movementTimer.Stop(); + _movementTimer.Elapsed -= OnMovementComplete; _movementTimer.Dispose(); _movementTimer = null; } @@ -283,10 +296,10 @@ namespace PepperDash.Essentials.Devices.Common.Shades /// /// Called when movement timer completes /// - 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 if (_currentMovement == RequestedState.Raise) { @@ -296,25 +309,25 @@ namespace PepperDash.Essentials.Devices.Common.Shades { InUpPosition = false; } - + _isMoving = false; _currentMovement = RequestedState.None; - + // Execute banked command if one exists if (_requestedState != RequestedState.None) { - Debug.LogMessage(LogEventLevel.Debug, this, $"Executing banked command: {_requestedState}"); - + this.LogDebug("Executing next command: {command}", _requestedState); + var commandToExecute = _requestedState; _requestedState = RequestedState.None; - + // Check if current state matches what the banked command would do and execute if different switch (commandToExecute) { case RequestedState.Raise: Raise(); break; - + case RequestedState.Lower: Lower(); break; @@ -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(); - 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(); } - /// - /// Attempts to get the port on teh specified device from config - /// - /// - /// - ISwitchedOutput GetSwitchedOutputFromDevice(string relayKey) + private ISwitchedOutput GetSwitchedOutputFromDevice(string relayKey) { - var portDevice = DeviceManager.GetDeviceForKey(relayKey); + var portDevice = DeviceManager.GetDeviceForKey(relayKey); if (portDevice != null) { - return portDevice as ISwitchedOutput; + return portDevice; } 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; } } - Displays.DisplayBase GetDisplayBaseFromDevice(string displayKey) + private DisplayBase GetDisplayBaseFromDevice(string displayKey) { - var displayDevice = DeviceManager.GetDeviceForKey(displayKey); + var displayDevice = DeviceManager.GetDeviceForKey(displayKey); if (displayDevice != null) { - return displayDevice as Displays.DisplayBase; + return displayDevice; } 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; } } @@ -364,7 +383,7 @@ namespace PepperDash.Essentials.Devices.Common.Shades } /// - /// Represents a ScreenLiftControllerFactory + /// Factory for ScreenLiftController devices /// public class ScreenLiftControllerFactory : EssentialsDeviceFactory { @@ -376,14 +395,11 @@ namespace PepperDash.Essentials.Devices.Common.Shades TypeNames = new List() { "screenliftcontroller" }; } - /// - /// BuildDevice method - /// /// public override EssentialsDevice BuildDevice(DeviceConfig dc) { - Debug.LogMessage(LogEventLevel.Debug, "Factory Attempting to create new Generic Comm Device"); - var props = Newtonsoft.Json.JsonConvert.DeserializeObject(dc.Properties.ToString()); + Debug.LogDebug("Factory Attempting to create new ScreenLiftController Device"); + var props = dc.Properties.ToObject(); return new ScreenLiftController(dc.Key, dc.Name, props); }