using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Essentials.Core;
namespace PepperDash.Essentials.AppServer.Messengers
{
///
/// Represents a ICommunicationMonitorMessenger
///
public class ICommunicationMonitorMessenger : MessengerBase
{
private readonly ICommunicationMonitor _communicationMonitor;
public ICommunicationMonitorMessenger(string key, string messagePath, ICommunicationMonitor device) : base(key, messagePath, device as IKeyName)
{
_communicationMonitor = device;
}
protected override void RegisterActions()
{
base.RegisterActions();
AddAction("/fullStatus", (id, content) =>
{
SendFullStatus(id);
});
AddAction("/commStatus", (id, content) =>
{
SendFullStatus(id);
});
_communicationMonitor.CommunicationMonitor.StatusChange += (sender, args) =>
{
PostStatusMessage(JToken.FromObject(new
{
commMonitor = new CommunicationMonitorProps
{
IsOnline = _communicationMonitor.CommunicationMonitor.IsOnline,
Status = _communicationMonitor.CommunicationMonitor.Status
}
}));
};
}
private void SendFullStatus(string id = null)
{
PostStatusMessage(new CommunicationMonitorState
{
CommunicationMonitor = new CommunicationMonitorProps
{
IsOnline = _communicationMonitor.CommunicationMonitor.IsOnline,
Status = _communicationMonitor.CommunicationMonitor.Status
},
}, id);
}
}
///
/// Represents a CommunicationMonitorState
///
public class CommunicationMonitorState : DeviceStateMessageBase
{
[JsonProperty("commMonitor", NullValueHandling = NullValueHandling.Ignore)]
///
/// Gets or sets the CommunicationMonitor
///
public CommunicationMonitorProps CommunicationMonitor { get; set; }
}
public class CommunicationMonitorProps
{ ///
/// For devices that implement ICommunicationMonitor, reports the online status of the device
///
[JsonProperty("isOnline", NullValueHandling = NullValueHandling.Ignore)]
public bool? IsOnline { get; set; }
///
/// For devices that implement ICommunicationMonitor, reports the online status of the device
///
[JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(StringEnumConverter))]
public MonitorStatus Status { get; set; }
}
}