mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-05 15:55:02 +00:00
Combining repos
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
using Crestron.SimplSharpPro.DeviceSupport;
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
using PepperDash.Core;
|
||||
|
||||
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class CrestronGenericBaseCommunicationMonitor : StatusMonitorBase
|
||||
{
|
||||
GenericBase Device;
|
||||
|
||||
public CrestronGenericBaseCommunicationMonitor(IKeyed parent, GenericBase device, long warningTime, long errorTime)
|
||||
: base(parent, warningTime, errorTime)
|
||||
{
|
||||
Device = device;
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
Device.OnlineStatusChange -= Device_OnlineStatusChange;
|
||||
Device.OnlineStatusChange += Device_OnlineStatusChange;
|
||||
GetStatus();
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
Device.OnlineStatusChange -= Device_OnlineStatusChange;
|
||||
}
|
||||
|
||||
void Device_OnlineStatusChange(GenericBase currentDevice, OnlineOfflineEventArgs args)
|
||||
{
|
||||
GetStatus();
|
||||
}
|
||||
|
||||
void GetStatus()
|
||||
{
|
||||
if (Device.IsOnline)
|
||||
{
|
||||
Status = MonitorStatus.IsOk;
|
||||
StopErrorTimers();
|
||||
}
|
||||
else
|
||||
StartErrorTimers();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
using Crestron.SimplSharpPro.DeviceSupport;
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
using PepperDash.Core;
|
||||
|
||||
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Used for monitoring comms that are IBasicCommunication. Will send a poll string and provide an event when
|
||||
/// statuses change.
|
||||
/// </summary>
|
||||
public class GenericCommunicationMonitor : StatusMonitorBase
|
||||
{
|
||||
public IBasicCommunication Client { get; private set; }
|
||||
|
||||
long PollTime;
|
||||
CTimer PollTimer;
|
||||
string PollString;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="pollTime">in MS, >= 5000</param>
|
||||
/// <param name="warningTime">in MS, >= 5000</param>
|
||||
/// <param name="errorTime">in MS, >= 5000</param>
|
||||
/// <param name="pollString">String to send to comm</param>
|
||||
public GenericCommunicationMonitor(IKeyed parent, IBasicCommunication client, long pollTime,
|
||||
long warningTime, long errorTime, string pollString) :
|
||||
base(parent, warningTime, errorTime)
|
||||
{
|
||||
if (pollTime > warningTime || pollTime > errorTime)
|
||||
throw new ArgumentException("pollTime must be less than warning or errorTime");
|
||||
if (pollTime < 5000)
|
||||
throw new ArgumentException("pollTime cannot be less than 5000 ms");
|
||||
|
||||
Client = client;
|
||||
PollTime = pollTime;
|
||||
PollString = pollString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build the monitor from a config object
|
||||
/// </summary>
|
||||
public GenericCommunicationMonitor(IKeyed parent, IBasicCommunication client,
|
||||
CommunicationMonitorConfig props) :
|
||||
this(parent, client, props.PollInterval, props.TimeToWarning, props.TimeToError, props.PollString)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
Client.BytesReceived += Client_BytesReceived;
|
||||
Poll();
|
||||
PollTimer = new CTimer(o => Poll(), null, PollTime, PollTime);
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
Client.BytesReceived -= this.Client_BytesReceived;
|
||||
PollTimer.Stop();
|
||||
PollTimer = null;
|
||||
StopErrorTimers();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upon any receipt of data, set everything to ok!
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
void Client_BytesReceived(object sender, GenericCommMethodReceiveBytesArgs e)
|
||||
{
|
||||
Status = MonitorStatus.IsOk;
|
||||
StopErrorTimers();
|
||||
}
|
||||
|
||||
void Poll()
|
||||
{
|
||||
StartErrorTimers();
|
||||
if (Client.IsConnected)
|
||||
{
|
||||
Debug.Console(2, Client, "Monitor, Polling");
|
||||
Client.SendText(PollString);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Console(2, Client, "Monitor, Comm not connected");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When the client connects, and we're waiting for it, respond and disconect from event
|
||||
/// </summary>
|
||||
void OneTimeConnectHandler(object o, EventArgs a)
|
||||
{
|
||||
if (Client.IsConnected)
|
||||
{
|
||||
//Client.IsConnected -= OneTimeConnectHandler;
|
||||
Debug.Console(2, Client, "Monitor, Comm connected");
|
||||
Poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class CommunicationMonitorConfig
|
||||
{
|
||||
public int PollInterval { get; set; }
|
||||
public int TimeToWarning { get; set; }
|
||||
public int TimeToError { get; set; }
|
||||
public string PollString { get; set; }
|
||||
|
||||
public CommunicationMonitorConfig()
|
||||
{
|
||||
PollInterval = 30000;
|
||||
TimeToWarning = 120000;
|
||||
TimeToError = 300000;
|
||||
PollString = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
|
||||
using System;
|
||||
|
||||
using PepperDash.Core;
|
||||
|
||||
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
public interface IStatusMonitor
|
||||
{
|
||||
IKeyed Parent { get; }
|
||||
event EventHandler<MonitorStatusChangeEventArgs> StatusChange;
|
||||
MonitorStatus Status { get; }
|
||||
string Message { get; }
|
||||
void Start();
|
||||
void Stop();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Represents a class that has a basic communication monitoring
|
||||
/// </summary>
|
||||
public interface ICommunicationMonitor
|
||||
{
|
||||
StatusMonitorBase CommunicationMonitor { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public enum MonitorStatus
|
||||
{
|
||||
IsOk, InWarning, InError, StatusUnknown
|
||||
}
|
||||
|
||||
public class MonitorStatusChangeEventArgs : EventArgs
|
||||
{
|
||||
public MonitorStatus Status { get; private set; }
|
||||
public string Message { get; private set; }
|
||||
|
||||
public MonitorStatusChangeEventArgs(MonitorStatus status)
|
||||
{
|
||||
Status = status;
|
||||
Message = status == MonitorStatus.IsOk ? "" : status.ToString();
|
||||
}
|
||||
|
||||
public MonitorStatusChangeEventArgs(MonitorStatus status, string message)
|
||||
{
|
||||
Status = status;
|
||||
Message = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
using Crestron.SimplSharpPro.DeviceSupport;
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
using PepperDash.Core;
|
||||
|
||||
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
public abstract class StatusMonitorBase : IStatusMonitor
|
||||
{
|
||||
public event EventHandler<MonitorStatusChangeEventArgs> StatusChange;
|
||||
|
||||
public IKeyed Parent { get; private set; }
|
||||
|
||||
public MonitorStatus Status
|
||||
{
|
||||
get { return _Status; }
|
||||
protected set
|
||||
{
|
||||
if (value != _Status)
|
||||
{
|
||||
_Status = value;
|
||||
OnStatusChange(value);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
MonitorStatus _Status;
|
||||
|
||||
public string Message
|
||||
{
|
||||
get { return _Message; }
|
||||
set
|
||||
{
|
||||
if (value == _Message) return;
|
||||
_Message = value;
|
||||
OnStatusChange(Status, value);
|
||||
|
||||
}
|
||||
}
|
||||
string _Message;
|
||||
|
||||
long WarningTime;
|
||||
long ErrorTime;
|
||||
CTimer WarningTimer;
|
||||
CTimer ErrorTimer;
|
||||
|
||||
public StatusMonitorBase(IKeyed parent, long warningTime, long errorTime)
|
||||
{
|
||||
Parent = parent;
|
||||
if (warningTime > errorTime)
|
||||
throw new ArgumentException("warningTime must be less than errorTime");
|
||||
if (warningTime < 5000 || errorTime < 5000)
|
||||
throw new ArgumentException("time values cannot be less that 5000 ms");
|
||||
|
||||
Status = MonitorStatus.StatusUnknown;
|
||||
WarningTime = warningTime;
|
||||
ErrorTime = errorTime;
|
||||
}
|
||||
|
||||
public abstract void Start();
|
||||
public abstract void Stop();
|
||||
|
||||
protected void OnStatusChange(MonitorStatus status)
|
||||
{
|
||||
var handler = StatusChange;
|
||||
if (handler != null)
|
||||
handler(this, new MonitorStatusChangeEventArgs(status));
|
||||
}
|
||||
|
||||
protected void OnStatusChange(MonitorStatus status, string message)
|
||||
{
|
||||
var handler = StatusChange;
|
||||
if (handler != null)
|
||||
handler(this, new MonitorStatusChangeEventArgs(status, message));
|
||||
}
|
||||
|
||||
protected void StartErrorTimers()
|
||||
{
|
||||
if (WarningTimer == null) WarningTimer = new CTimer(o => { Status = MonitorStatus.InWarning; }, WarningTime);
|
||||
if (ErrorTimer == null) ErrorTimer = new CTimer(o => { Status = MonitorStatus.InError; }, ErrorTime);
|
||||
}
|
||||
|
||||
protected void StopErrorTimers()
|
||||
{
|
||||
if (WarningTimer != null) WarningTimer.Stop();
|
||||
if (ErrorTimer != null) ErrorTimer.Stop();
|
||||
WarningTimer = null;
|
||||
ErrorTimer = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
using Crestron.SimplSharpPro.DeviceSupport;
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
using PepperDash.Core;
|
||||
|
||||
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class StatusMonitorCollection : IStatusMonitor
|
||||
{
|
||||
public IKeyed Parent { get; private set; }
|
||||
|
||||
List<IStatusMonitor> Monitors = new List<IStatusMonitor>();
|
||||
|
||||
#region IStatusMonitor Members
|
||||
|
||||
public event EventHandler<MonitorStatusChangeEventArgs> StatusChange;
|
||||
|
||||
public MonitorStatus Status { get; protected set; }
|
||||
|
||||
public string Message { get; private set; }
|
||||
|
||||
|
||||
public StatusMonitorCollection(IKeyed parent)
|
||||
{
|
||||
Parent = parent;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
foreach (var mon in Monitors)
|
||||
mon.StatusChange += mon_StatusChange;
|
||||
ProcessStatuses();
|
||||
}
|
||||
|
||||
|
||||
void ProcessStatuses()
|
||||
{
|
||||
var InError = Monitors.Where(m => m.Status == MonitorStatus.InError);
|
||||
var InWarning = Monitors.Where(m => m.Status == MonitorStatus.InWarning);
|
||||
var IsOk = Monitors.Where(m => m.Status == MonitorStatus.IsOk);
|
||||
|
||||
|
||||
MonitorStatus initialStatus;
|
||||
string prefix = "0:";
|
||||
if (InError.Count() > 0)
|
||||
{
|
||||
initialStatus = MonitorStatus.InError;
|
||||
prefix = "3:";
|
||||
}
|
||||
else if (InWarning.Count() > 0)
|
||||
{
|
||||
initialStatus = MonitorStatus.InWarning;
|
||||
prefix = "2:";
|
||||
}
|
||||
else if (InWarning.Count() > 0)
|
||||
initialStatus = MonitorStatus.IsOk;
|
||||
else
|
||||
initialStatus = MonitorStatus.StatusUnknown;
|
||||
|
||||
// Build the error message string
|
||||
if (InError.Count() > 0 || InWarning.Count() > 0)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(prefix);
|
||||
if (InError.Count() > 0)
|
||||
{
|
||||
// Do string splits and joins
|
||||
sb.Append(string.Format("{0} Errors:", InError.Count()));
|
||||
foreach (var mon in InError)
|
||||
sb.Append(string.Format("{0}, ", mon.Parent.Key));
|
||||
}
|
||||
if (InWarning.Count() > 0)
|
||||
{
|
||||
sb.Append(string.Format("{0} Warnings:", InWarning.Count()));
|
||||
foreach (var mon in InWarning)
|
||||
sb.Append(string.Format("{0}, ", mon.Parent.Key));
|
||||
}
|
||||
Message = sb.ToString();
|
||||
}
|
||||
|
||||
// Want to fire even if status doesn't change because the message may.
|
||||
Status = initialStatus;
|
||||
OnStatusChange(initialStatus, Message);
|
||||
}
|
||||
|
||||
|
||||
void mon_StatusChange(object sender, MonitorStatusChangeEventArgs e)
|
||||
{
|
||||
ProcessStatuses();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void AddMonitor(IStatusMonitor monitor)
|
||||
{
|
||||
if (!Monitors.Contains(monitor))
|
||||
Monitors.Add(monitor);
|
||||
}
|
||||
|
||||
|
||||
protected void OnStatusChange(MonitorStatus status, string message)
|
||||
{
|
||||
var handler = StatusChange;
|
||||
if (handler != null)
|
||||
handler(this, new MonitorStatusChangeEventArgs(status, message));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user