using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using PepperDash.Core; namespace PepperDash.Core { /// /// Controls the ability to disable/enable debugging of TX/RX data sent to/from a device with a built in timer to disable /// public class CommunicationStreamDebugging { /// /// Device Key that this instance configures /// public string ParentDeviceKey { get; private set; } /// /// Timer to disable automatically if not manually disabled /// private CTimer DebugExpiryPeriod; /// /// Gets or sets the DebugSetting /// public eStreamDebuggingSetting DebugSetting { get; private set; } private uint _DebugTimeoutInMs; private const uint _DefaultDebugTimeoutMin = 30; /// /// Timeout in Minutes /// public uint DebugTimeoutMinutes { get { return _DebugTimeoutInMs / 60000; } } /// /// Gets or sets the RxStreamDebuggingIsEnabled /// public bool RxStreamDebuggingIsEnabled { get; private set; } /// /// Indicates that transmit stream debugging is enabled /// public bool TxStreamDebuggingIsEnabled { get; private set; } /// /// Constructor /// /// public CommunicationStreamDebugging(string parentDeviceKey) { ParentDeviceKey = parentDeviceKey; } /// /// Sets the debugging setting and if not setting to off, assumes the default of 30 mintues /// /// /// /// SetDebuggingWithDefaultTimeout method /// public void SetDebuggingWithDefaultTimeout(eStreamDebuggingSetting setting) { if (setting == eStreamDebuggingSetting.Off) { DisableDebugging(); return; } SetDebuggingWithSpecificTimeout(setting, _DefaultDebugTimeoutMin); } /// /// Sets the debugging setting for the specified number of minutes /// /// /// /// /// SetDebuggingWithSpecificTimeout method /// public void SetDebuggingWithSpecificTimeout(eStreamDebuggingSetting setting, uint minutes) { if (setting == eStreamDebuggingSetting.Off) { DisableDebugging(); return; } _DebugTimeoutInMs = minutes * 60000; StopDebugTimer(); DebugExpiryPeriod = new CTimer((o) => DisableDebugging(), _DebugTimeoutInMs); if ((setting & eStreamDebuggingSetting.Rx) == eStreamDebuggingSetting.Rx) RxStreamDebuggingIsEnabled = true; if ((setting & eStreamDebuggingSetting.Tx) == eStreamDebuggingSetting.Tx) TxStreamDebuggingIsEnabled = true; Debug.SetDeviceDebugSettings(ParentDeviceKey, setting); } /// /// Disabled debugging /// private void DisableDebugging() { StopDebugTimer(); Debug.SetDeviceDebugSettings(ParentDeviceKey, eStreamDebuggingSetting.Off); } private void StopDebugTimer() { RxStreamDebuggingIsEnabled = false; TxStreamDebuggingIsEnabled = false; if (DebugExpiryPeriod == null) { return; } DebugExpiryPeriod.Stop(); DebugExpiryPeriod.Dispose(); DebugExpiryPeriod = null; } } }