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 { /// /// Base class for status monitors /// public abstract class StatusMonitorBase : IStatusMonitor, IKeyName { /// /// Event fired when status changes /// public event EventHandler StatusChange; /// /// Gets or sets the Key /// public string Key { get { return Parent.Key + "-comMonitor"; } } /// /// Gets or sets the Name /// public string Name { get { return "Comm. monitor"; } } /// /// Gets or sets the Parent /// public IKeyed Parent { get; private set; } /// /// Bool feedback for online status /// public BoolFeedback IsOnlineFeedback { get; set; } /// /// Indicates whether the monitored device is online /// public bool IsOnline; /// /// Current monitor status /// public MonitorStatus Status { get { return _Status; } protected set { if (value != _Status) { _Status = value; OnStatusChange(value); } } } MonitorStatus _Status; /// /// Current status message /// 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; /// /// Constructor /// /// parent device /// time in milliseconds before warning status /// time in milliseconds before error status 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"); IsOnlineFeedback = new BoolFeedback(() => { return IsOnline; }); Status = MonitorStatus.StatusUnknown; WarningTime = warningTime; ErrorTime = errorTime; } /// /// Starts the monitor /// public abstract void Start(); /// /// Stops the monitor /// public abstract void Stop(); /// /// Fires the StatusChange event /// /// monitor status protected void OnStatusChange(MonitorStatus status) { if (_Status == MonitorStatus.IsOk) IsOnline = true; else IsOnline = false; IsOnlineFeedback.FireUpdate(); var handler = StatusChange; if (handler != null) handler(this, new MonitorStatusChangeEventArgs(status)); } /// /// Fires the StatusChange event with message /// /// monitor status /// status message protected void OnStatusChange(MonitorStatus status, string message) { if (_Status == MonitorStatus.IsOk) IsOnline = true; else IsOnline = false; IsOnlineFeedback.FireUpdate(); var handler = StatusChange; if (handler != null) handler(this, new MonitorStatusChangeEventArgs(status, message)); } /// /// Starts the error timers /// 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); } /// /// Stops the error timers /// protected void StopErrorTimers() { if (WarningTimer != null) WarningTimer.Stop(); if (ErrorTimer != null) ErrorTimer.Stop(); WarningTimer = null; ErrorTimer = null; } /// /// Resets the error timers /// protected void ResetErrorTimers() { if(WarningTimer != null) WarningTimer.Reset(WarningTime, WarningTime); if(ErrorTimer != null) ErrorTimer.Reset(ErrorTime, ErrorTime); } } }