using System;
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;
///
/// The current debug setting
///
public eStreamDebuggingSetting DebugSetting { get; private set; }
private uint _DebugTimeoutInMs;
private const uint _DefaultDebugTimeoutMin = 30;
///
/// Timeout in Minutes
///
public uint DebugTimeoutMinutes
{
get
{
return _DebugTimeoutInMs/60000;
}
}
///
/// Indicates that receive stream debugging is enabled
///
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
///
///
public void SetDebuggingWithDefaultTimeout(eStreamDebuggingSetting setting)
{
if (setting == eStreamDebuggingSetting.Off)
{
DisableDebugging();
return;
}
SetDebuggingWithSpecificTimeout(setting, _DefaultDebugTimeoutMin);
}
///
/// Sets the debugging setting for the specified number of minutes
///
///
///
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;
}
}
///
/// The available settings for stream debugging
///
[Flags]
public enum eStreamDebuggingSetting
{
///
/// Debug off
///
Off = 0,
///
/// Debug received data
///
Rx = 1,
///
/// Debug transmitted data
///
Tx = 2,
///
/// Debug both received and transmitted data
///
Both = Rx | Tx
}
///
/// The available settings for stream debugging response types
///
[Flags]
public enum eStreamDebuggingDataTypeSettings
{
///
/// Debug data in byte format
///
Bytes = 0,
///
/// Debug data in text format
///
Text = 1,
///
/// Debug data in both byte and text formats
///
Both = Bytes | Text,
}
}