using System;
using System.Collections.Generic;
using Serilog.Events;
namespace PepperDash.Core
{
//*********************************************************************************************************
///
/// Represents a Device
///
public class Device : IKeyName
{
///
/// Unique Key
///
public string Key { get; protected set; }
///
/// Gets or sets the Name
///
public string Name { get; protected set; }
///
///
///
public bool Enabled { get; protected set; }
///
/// A place to store reference to the original config object, if any. These values should
/// NOT be used as properties on the device as they are all publicly-settable values.
///
//public DeviceConfig Config { get; private set; }
///
/// Helper method to check if Config exists
///
//public bool HasConfig { get { return Config != null; } }
List _PreActivationActions;
List _PostActivationActions;
///
///
///
public static Device DefaultDevice { get { return _DefaultDevice; } }
static Device _DefaultDevice = new Device("Default", "Default");
///
/// Base constructor for all Devices.
///
///
public Device(string key)
{
Key = key;
if (key.Contains(".")) Debug.LogMessage(LogEventLevel.Information, "WARNING: Device key should not include '.'", this);
Name = "";
}
///
/// Constructor with key and name
///
///
///
public Device(string key, string name) : this(key)
{
Name = name;
}
//public Device(DeviceConfig config)
// : this(config.Key, config.Name)
//{
// Config = config;
//}
///
/// Adds a pre activation action
///
///
public void AddPreActivationAction(Action act)
{
if (_PreActivationActions == null)
_PreActivationActions = new List();
_PreActivationActions.Add(act);
}
///
/// Adds a post activation action
///
///
///
/// AddPostActivationAction method
///
public void AddPostActivationAction(Action act)
{
if (_PostActivationActions == null)
_PostActivationActions = new List();
_PostActivationActions.Add(act);
}
///
/// PreActivate method
///
public void PreActivate()
{
if (_PreActivationActions != null)
_PreActivationActions.ForEach(a =>
{
try
{
a.Invoke();
}
catch (Exception e)
{
Debug.LogMessage(e, "Error in PreActivationAction: " + e.Message, this);
}
});
}
///
/// Activate method
///
public bool Activate()
{
//if (_PreActivationActions != null)
// _PreActivationActions.ForEach(a => a.Invoke());
var result = CustomActivate();
//if(result && _PostActivationActions != null)
// _PostActivationActions.ForEach(a => a.Invoke());
return result;
}
///
/// PostActivate method
///
public void PostActivate()
{
if (_PostActivationActions != null)
_PostActivationActions.ForEach(a =>
{
try
{
a.Invoke();
}
catch (Exception e)
{
Debug.LogMessage(e, "Error in PostActivationAction: " + e.Message, this);
}
});
}
///
/// Called in between Pre and PostActivationActions when Activate() is called.
/// Override to provide addtitional setup when calling activation. Overriding classes
/// do not need to call base.CustomActivate()
///
/// true if device activated successfully.
///
/// CustomActivate method
///
public virtual bool CustomActivate() { return true; }
///
/// Call to deactivate device - unlink events, etc. Overriding classes do not
/// need to call base.Deactivate()
///
///
public virtual bool Deactivate() { return true; }
///
/// Call this method to start communications with a device. Overriding classes do not need to call base.Initialize()
///
public virtual void Initialize()
{
}
///
/// Helper method to check object for bool value false and fire an Action method
///
/// Should be of type bool, others will be ignored
/// Action to be run when o is false
public void OnFalse(object o, Action a)
{
if (o is bool && !(bool)o) a();
}
///
/// Returns a string representation of the object, including its key and name.
///
/// The returned string is formatted as "{Key} - {Name}". If the Name property is
/// null or empty, "---" is used in place of the name.
/// A string that represents the object, containing the key and name in the format "{Key} - {Name}".
///
/// ToString method
///
public override string ToString()
{
return string.Format("{0} - {1}", Key, string.IsNullOrEmpty(Name) ? "---" : Name);
}
}
}