using System;
using System.Threading.Tasks;
using PepperDash.Core;
using Serilog.Events;
namespace PepperDash.Essentials.Core
{
///
/// Defines the basic needs for an EssentialsDevice to enable it to be build by an IDeviceFactory class
///
[Description("The base Essentials Device Class")]
public abstract class EssentialsDevice : Device
{
///
/// Event raised when the device is initialized.
///
public event EventHandler Initialized;
private bool _isInitialized;
///
/// Gets a value indicating whether the device is initialized.
///
public bool IsInitialized
{
get { return _isInitialized; }
private set
{
if (_isInitialized == value) return;
_isInitialized = value;
if (_isInitialized)
{
Initialized?.Invoke(this, new EventArgs());
}
}
}
///
/// Initializes a new instance of the EssentialsDevice class.
///
/// The unique identifier for the device.
protected EssentialsDevice(string key)
: base(key)
{
SubscribeToActivateComplete();
}
///
/// Initializes a new instance of the EssentialsDevice class.
///
/// The unique identifier for the device.
/// The name of the device.
protected EssentialsDevice(string key, string name)
: base(key, name)
{
SubscribeToActivateComplete();
}
private void SubscribeToActivateComplete()
{
DeviceManager.AllDevicesActivated += DeviceManagerOnAllDevicesActivated;
}
private void DeviceManagerOnAllDevicesActivated(object sender, EventArgs eventArgs)
{
Task.Run(() =>
{
try
{
Initialize();
IsInitialized = true;
}
catch (Exception ex)
{
Debug.LogMessage(LogEventLevel.Error, this, "Exception initializing device: {0}", ex.Message);
Debug.LogMessage(LogEventLevel.Debug, this, "Stack Trace: {0}", ex.StackTrace);
}
});
}
///
/// CustomActivate method
///
///
public override bool CustomActivate()
{
CreateMobileControlMessengers();
return base.CustomActivate();
}
///
/// Override this method to build and create custom Mobile Control Messengers during the Activation phase
///
protected virtual void CreateMobileControlMessengers()
{
}
}
}