using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.Reflection;
using PepperDash.Core;
using PepperDash.Essentials.Core.Config;
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
{
protected EssentialsDevice(string key)
: base(key)
{
SubscribeToActivateComplete();
}
protected EssentialsDevice(string key, string name)
: base(key, name)
{
SubscribeToActivateComplete();
}
private void SubscribeToActivateComplete()
{
DeviceManager.AllDevicesActivated += DeviceManagerOnAllDevicesActivated;
}
private void DeviceManagerOnAllDevicesActivated(object sender, EventArgs eventArgs)
{
CrestronInvoke.BeginInvoke((o) =>
{
try
{
Initialize();
}
catch (Exception ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Exception initializing device: {0}", ex.Message);
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stack Trace: {0}", ex.StackTrace);
}
});
}
}
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class DescriptionAttribute : Attribute
{
private string _Description;
public DescriptionAttribute(string description)
{
//Debug.Console(2, "Setting Description: {0}", description);
_Description = description;
}
public string Description
{
get { return _Description; }
}
}
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class ConfigSnippetAttribute : Attribute
{
private string _ConfigSnippet;
public ConfigSnippetAttribute(string configSnippet)
{
//Debug.Console(2, "Setting Config Snippet {0}", configSnippet);
_ConfigSnippet = configSnippet;
}
public string ConfigSnippet
{
get { return _ConfigSnippet; }
}
}
///
/// Devices the basic needs for a Device Factory
///
public abstract class EssentialsDeviceFactory : IDeviceFactory where T:EssentialsDevice
{
#region IDeviceFactory Members
///
/// A list of strings that can be used in the type property of a DeviceConfig object to build an instance of this device
///
public List TypeNames { get; protected set; }
///
/// Loads an item to the DeviceFactory.FactoryMethods dictionary for each entry in the TypeNames list
///
public void LoadTypeFactories()
{
foreach (var typeName in TypeNames)
{
//Debug.Console(2, "Getting Description Attribute from class: '{0}'", typeof(T).FullName);
var descriptionAttribute = typeof(T).GetCustomAttributes(typeof(DescriptionAttribute), true) as DescriptionAttribute[];
string description = descriptionAttribute[0].Description;
var snippetAttribute = typeof(T).GetCustomAttributes(typeof(ConfigSnippetAttribute), true) as ConfigSnippetAttribute[];
DeviceFactory.AddFactoryForType(typeName.ToLower(), description, typeof(T), BuildDevice);
}
}
///
/// The method that will build the device
///
/// The device config
/// An instance of the device
public abstract EssentialsDevice BuildDevice(DeviceConfig dc);
#endregion
}
///
/// Devices the basic needs for a Device Factory
///
public abstract class EssentialsPluginDeviceFactory : EssentialsDeviceFactory, IPluginDeviceFactory where T : EssentialsDevice
{
///
/// Specifies the minimum version of Essentials required for a plugin to run. Must use the format Major.Minor.Build (ex. "1.4.33")
///
public string MinimumEssentialsFrameworkVersion { get; protected set; }
}
}