using System.Linq;
using System.Reflection;
using Crestron.SimplSharpPro.DeviceSupport;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Bridges;
using Serilog.Events;
namespace PepperDash.Essentials.Devices.Common
{
///
/// Represents a AppleTV
/// Wrapper class for an IR-Controlled AppleTV
///
[Description("Wrapper class for an IR-Controlled AppleTV")]
public class AppleTV : EssentialsBridgeableDevice, IDPad, ITransport, IUiDisplayInfo, IRoutingSource, IRoutingOutputs
{
///
/// Gets or sets the IrPort
///
public IrOutputPortController IrPort { get; private set; }
///
/// Standard Driver Name
///
public const string StandardDriverName = "Apple_AppleTV_4th_Gen_Essentials.ir";
///
/// Gets or sets the DisplayUiType
///
public uint DisplayUiType { get { return DisplayUiConstants.TypeAppleTv; } }
///
/// Initializes a new instance of the class
///
/// The device key
/// The device name
/// The IR output port controller
public AppleTV(string key, string name, IrOutputPortController portCont)
: base(key, name)
{
IrPort = portCont;
DeviceManager.AddDevice(portCont);
HdmiOut = new RoutingOutputPort(RoutingPortNames.HdmiOut, eRoutingSignalType.Audio | eRoutingSignalType.Video,
eRoutingPortConnectionType.Hdmi, null, this);
AnyAudioOut = new RoutingOutputPort(RoutingPortNames.AnyAudioOut, eRoutingSignalType.Audio,
eRoutingPortConnectionType.DigitalAudio, null, this);
OutputPorts = new RoutingPortCollection { HdmiOut, AnyAudioOut };
PrintExpectedIrCommands();
}
///
/// PrintExpectedIrCommands method
///
public void PrintExpectedIrCommands()
{
var cmds = typeof(AppleTvIrCommands).GetFields(BindingFlags.Public | BindingFlags.Static);
foreach (var value in cmds.Select(cmd => cmd.GetValue(null)).OfType())
{
Debug.LogMessage(LogEventLevel.Verbose, this, "Expected IR Function Name: {0}", value);
}
}
#region IDPad Members
///
/// Up method
///
public void Up(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.Up, pressRelease);
}
///
/// Down method
///
public void Down(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.Down, pressRelease);
}
///
/// Left method
///
public void Left(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.Left, pressRelease);
}
///
/// Right method
///
public void Right(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.Right, pressRelease);
}
///
/// Select method
///
public void Select(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.Enter, pressRelease);
}
///
/// Menu method
///
public void Menu(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.Menu, pressRelease);
}
///
/// Exit method
///
public void Exit(bool pressRelease)
{
}
#endregion
#region ITransport Members
///
/// Play method
///
public void Play(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.PlayPause, pressRelease);
}
///
/// Pause method
///
public void Pause(bool pressRelease)
{
IrPort.PressRelease(AppleTvIrCommands.PlayPause, pressRelease);
}
///
/// Not implemented
///
///
///
/// Rewind method
///
public void Rewind(bool pressRelease)
{
}
///
/// Not implemented
///
///
public void FFwd(bool pressRelease)
{
}
///
/// Not implemented
///
///
public void ChapMinus(bool pressRelease)
{
}
///
/// Not implemented
///
///
public void ChapPlus(bool pressRelease)
{
}
///
/// Not implemented
///
///
public void Stop(bool pressRelease)
{
}
///
/// Not implemented
///
///
public void Record(bool pressRelease)
{
}
#endregion
#region IRoutingOutputs Members
///
/// Gets the HdmiOut
///
public RoutingOutputPort HdmiOut { get; private set; }
///
/// Gets the AnyAudioOut
///
public RoutingOutputPort AnyAudioOut { get; private set; }
///
/// Gets or sets the OutputPorts
///
public RoutingPortCollection OutputPorts { get; private set; }
#endregion
///
/// LinkToApi method
///
public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge)
{
var joinMap = new AppleTvJoinMap(joinStart);
var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey);
if (!string.IsNullOrEmpty(joinMapSerialized))
joinMap = JsonConvert.DeserializeObject(joinMapSerialized);
if (bridge != null)
{
bridge.AddJoinMap(Key, joinMap);
}
else
{
Debug.LogMessage(LogEventLevel.Information, this, "Please update config to use 'eiscapiadvanced' to get all join map features for this device.");
}
Debug.LogMessage(LogEventLevel.Debug, "Linking to Trilist '{0}'", trilist.ID.ToString("X"));
Debug.LogMessage(LogEventLevel.Information, "Linking to Bridge Type {0}", GetType().Name);
trilist.SetBoolSigAction(joinMap.UpArrow.JoinNumber, Up);
trilist.SetBoolSigAction(joinMap.DnArrow.JoinNumber, Down);
trilist.SetBoolSigAction(joinMap.LeftArrow.JoinNumber, Left);
trilist.SetBoolSigAction(joinMap.RightArrow.JoinNumber, Right);
trilist.SetBoolSigAction(joinMap.Select.JoinNumber, Select);
trilist.SetBoolSigAction(joinMap.Menu.JoinNumber, Menu);
trilist.SetBoolSigAction(joinMap.PlayPause.JoinNumber, Play);
}
}
}