using Crestron.SimplSharpPro;
using Crestron.SimplSharpPro.DeviceSupport;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
using PepperDash.Essentials.Core.Presets;
using Serilog.Events;
namespace PepperDash.Essentials.Devices.Common
{
///
/// Represents a IRSetTopBoxBase
/// Wrapper class for an IR Set Top Box
///
[Description("Wrapper class for an IR Set Top Box")]
public class IRSetTopBoxBase : EssentialsBridgeableDevice, ISetTopBoxControls, IRoutingSource, IRoutingOutputs, IUsageTracking, IHasPowerControl, ITvPresetsProvider
{
///
/// Gets or sets the IrPort
///
public IrOutputPortController IrPort { get; private set; }
///
/// Gets or sets the DisplayUiType
///
public uint DisplayUiType { get { return DisplayUiConstants.TypeDirecTv; } }
///
/// Gets or sets the IrPulseTime
///
public ushort IrPulseTime { get; set; }
///
/// Gets or sets the HasPresets
///
public bool HasPresets { get; set; }
///
/// Gets or sets the HasDvr
///
public bool HasDvr { get; set; }
///
/// Gets or sets the HasDpad
///
public bool HasDpad { get; set; }
///
/// Gets or sets the HasNumeric
///
public bool HasNumeric { get; set; }
///
/// Gets or sets the TvPresets
///
public DevicePresetsModel TvPresets { get; private set; }
///
/// Initializes a new instance of the class
///
/// The unique identifier for the device
/// The name of the device
/// The IR output port controller
/// The properties configuration
public IRSetTopBoxBase(string key, string name, IrOutputPortController portCont,
SetTopBoxPropertiesConfig props)
: base(key, name)
{
IrPort = portCont;
IrPulseTime = 200;
if (props.IrPulseTime > 0)
{
IrPulseTime = (ushort)props.IrPulseTime;
}
DeviceManager.AddDevice(portCont);
HasPresets = props.HasPresets;
HasDvr = props.HasDvr;
HasDpad = props.HasDpad;
HasNumeric = props.HasNumeric;
HasKeypadAccessoryButton1 = true;
KeypadAccessoryButton1Command = "Dash";
KeypadAccessoryButton1Label = "-";
HasKeypadAccessoryButton2 = true;
KeypadAccessoryButton2Command = "KEYPAD_ENTER";
KeypadAccessoryButton2Label = "Enter";
AnyVideoOut = new RoutingOutputPort(RoutingPortNames.AnyVideoOut, eRoutingSignalType.Audio | eRoutingSignalType.Video,
eRoutingPortConnectionType.Hdmi, null, this);
AnyAudioOut = new RoutingOutputPort(RoutingPortNames.AnyAudioOut, eRoutingSignalType.Audio,
eRoutingPortConnectionType.DigitalAudio, null, this);
OutputPorts = new RoutingPortCollection { AnyVideoOut, AnyAudioOut };
}
///
/// LoadPresets method
///
public void LoadPresets(string filePath)
{
TvPresets = new DevicePresetsModel(Key + "-presets", this, filePath);
DeviceManager.AddDevice(TvPresets);
}
#region ISetTopBoxControls Members
///
/// DvrList method
///
public void DvrList(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_DVR, pressRelease);
}
///
/// Replay method
///
public void Replay(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_REPLAY, pressRelease);
}
#endregion
#region IDPad Members
///
/// Up method
///
public void Up(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_UP_ARROW, pressRelease);
}
///
/// Down method
///
public void Down(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_DN_ARROW, pressRelease);
}
///
/// Left method
///
public void Left(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_LEFT_ARROW, pressRelease);
}
///
/// Right method
///
public void Right(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_RIGHT_ARROW, pressRelease);
}
///
/// Select method
///
public void Select(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_ENTER, pressRelease);
}
///
/// Menu method
///
public void Menu(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_MENU, pressRelease);
}
///
/// Exit method
///
public void Exit(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_EXIT, pressRelease);
}
#endregion
#region INumericKeypad Members
///
/// Digit0 method
///
public void Digit0(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_0, pressRelease);
}
///
/// Digit1 method
///
public void Digit1(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_1, pressRelease);
}
///
/// Digit2 method
///
public void Digit2(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_2, pressRelease);
}
///
/// Digit3 method
///
public void Digit3(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_3, pressRelease);
}
///
/// Digit4 method
///
public void Digit4(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_4, pressRelease);
}
///
/// Digit5 method
///
public void Digit5(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_5, pressRelease);
}
///
/// Digit6 method
///
public void Digit6(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_6, pressRelease);
}
///
/// Digit7 method
///
public void Digit7(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_7, pressRelease);
}
///
/// Digit8 method
///
public void Digit8(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_8, pressRelease);
}
///
/// Digit9 method
///
public void Digit9(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_9, pressRelease);
}
///
/// Gets or sets the HasKeypadAccessoryButton1
///
public bool HasKeypadAccessoryButton1 { get; set; }
///
/// Defaults to "-"
///
public string KeypadAccessoryButton1Label { get; set; }
///
/// Defaults to "Dash"
///
public string KeypadAccessoryButton1Command { get; set; }
///
/// Presses the KeypadAccessoryButton1
///
public void KeypadAccessoryButton1(bool pressRelease)
{
IrPort.PressRelease(KeypadAccessoryButton1Command, pressRelease);
}
///
/// Gets or sets the HasKeypadAccessoryButton2
///
public bool HasKeypadAccessoryButton2 { get; set; }
///
/// Defaults to "Enter"
///
public string KeypadAccessoryButton2Label { get; set; }
///
/// Defaults to "Enter"
///
public string KeypadAccessoryButton2Command { get; set; }
///
/// Presses the KeypadAccessoryButton2
///
public void KeypadAccessoryButton2(bool pressRelease)
{
IrPort.PressRelease(KeypadAccessoryButton2Command, pressRelease);
}
#endregion
#region ISetTopBoxNumericKeypad Members
///
/// Dash method
///
public void Dash(bool pressRelease)
{
IrPort.PressRelease("dash", pressRelease);
}
///
/// Corresponds to "numericEnter" IR command
///
public void KeypadEnter(bool pressRelease)
{
IrPort.PressRelease("numericEnter", pressRelease);
}
#endregion
#region IChannelFunctions Members
///
/// ChannelUp method
///
public void ChannelUp(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_CH_PLUS, pressRelease);
}
///
/// ChannelDown method
///
public void ChannelDown(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_CH_MINUS, pressRelease);
}
///
/// LastChannel method
///
public void LastChannel(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_LAST, pressRelease);
}
///
/// Guide method
///
public void Guide(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_GUIDE, pressRelease);
}
///
/// Info method
///
public void Info(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_INFO, pressRelease);
}
#endregion
#region IColorFunctions Members
///
/// Red method
///
public void Red(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_RED, pressRelease);
}
///
/// Green method
///
public void Green(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_GREEN, pressRelease);
}
///
/// Yellow method
///
public void Yellow(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_YELLOW, pressRelease);
}
///
/// Blue method
///
public void Blue(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_BLUE, pressRelease);
}
#endregion
#region IRoutingOutputs Members
///
/// Gets or sets the AnyVideoOut
///
public RoutingOutputPort AnyVideoOut { get; private set; }
///
/// Gets or sets the AnyAudioOut
///
public RoutingOutputPort AnyAudioOut { get; private set; }
///
/// Gets or sets the OutputPorts
///
public RoutingPortCollection OutputPorts { get; private set; }
#endregion
#region ITransport Members
///
/// ChapMinus method
///
public void ChapMinus(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_REPLAY, pressRelease);
}
///
/// ChapPlus method
///
public void ChapPlus(bool pressRelease)
{
}
///
/// FFwd method
///
public void FFwd(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_FSCAN, pressRelease);
}
///
/// Pause method
///
public void Pause(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_RSCAN, pressRelease);
}
///
/// Play method
///
public void Play(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_PLAY, pressRelease);
}
///
/// Record method
///
public void Record(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_RECORD, pressRelease);
}
///
/// Rewind method
///
public void Rewind(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_RSCAN, pressRelease);
}
///
/// Stop method
///
public void Stop(bool pressRelease)
{
IrPort.PressRelease(IROutputStandardCommands.IROut_STOP, pressRelease);
}
#endregion
#region IUsageTracking Members
///
/// Gets or sets the UsageTracker
///
public UsageTracking UsageTracker { get; set; }
#endregion
#region IPower Members
///
/// PowerOn method
///
public void PowerOn()
{
IrPort.Pulse(IROutputStandardCommands.IROut_POWER_ON, IrPulseTime);
}
///
/// PowerOff method
///
public void PowerOff()
{
IrPort.Pulse(IROutputStandardCommands.IROut_POWER_OFF, IrPulseTime);
}
///
/// PowerToggle method
///
public void PowerToggle()
{
IrPort.Pulse(IROutputStandardCommands.IROut_POWER, IrPulseTime);
}
#endregion
///
/// LinkToApi method
///
///
public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge)
{
var joinMap = new SetTopBoxControllerJoinMap(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 SetTopBox: {0}", Name);
trilist.OnlineStatusChange += new OnlineStatusChangeEventHandler((o, a) =>
{
if (a.DeviceOnLine)
{
trilist.StringInput[joinMap.Name.JoinNumber].StringValue = Name;
}
});
if (this is ISetTopBoxControls stbBase)
{
trilist.BooleanInput[joinMap.HasDpad.JoinNumber].BoolValue = stbBase.HasDpad;
trilist.BooleanInput[joinMap.HasNumeric.JoinNumber].BoolValue = stbBase.HasNumeric;
trilist.BooleanInput[joinMap.HasDvr.JoinNumber].BoolValue = stbBase.HasDvr;
trilist.BooleanInput[joinMap.HasPresets.JoinNumber].BoolValue = stbBase.HasPresets;
trilist.SetBoolSigAction(joinMap.DvrList.JoinNumber, stbBase.DvrList);
trilist.SetBoolSigAction(joinMap.Replay.JoinNumber, stbBase.Replay);
trilist.SetStringSigAction(joinMap.LoadPresets.JoinNumber, stbBase.LoadPresets);
}
if (this is IHasPowerControl stbPower)
{
trilist.SetSigTrueAction(joinMap.PowerOn.JoinNumber, stbPower.PowerOn);
trilist.SetSigTrueAction(joinMap.PowerOff.JoinNumber, stbPower.PowerOff);
trilist.SetSigTrueAction(joinMap.PowerToggle.JoinNumber, stbPower.PowerToggle);
}
if (this is IDPad stbDPad)
{
trilist.SetBoolSigAction(joinMap.Up.JoinNumber, stbDPad.Up);
trilist.SetBoolSigAction(joinMap.Down.JoinNumber, stbDPad.Down);
trilist.SetBoolSigAction(joinMap.Left.JoinNumber, stbDPad.Left);
trilist.SetBoolSigAction(joinMap.Right.JoinNumber, stbDPad.Right);
trilist.SetBoolSigAction(joinMap.Select.JoinNumber, stbDPad.Select);
trilist.SetBoolSigAction(joinMap.Menu.JoinNumber, stbDPad.Menu);
trilist.SetBoolSigAction(joinMap.Exit.JoinNumber, stbDPad.Exit);
}
if (this is IChannel stbChannel)
{
trilist.SetBoolSigAction(joinMap.ChannelUp.JoinNumber, stbChannel.ChannelUp);
trilist.SetBoolSigAction(joinMap.ChannelDown.JoinNumber, stbChannel.ChannelDown);
trilist.SetBoolSigAction(joinMap.LastChannel.JoinNumber, stbChannel.LastChannel);
trilist.SetBoolSigAction(joinMap.Guide.JoinNumber, stbChannel.Guide);
trilist.SetBoolSigAction(joinMap.Info.JoinNumber, stbChannel.Info);
trilist.SetBoolSigAction(joinMap.Exit.JoinNumber, stbChannel.Exit);
}
if (this is IColor stbColor)
{
trilist.SetBoolSigAction(joinMap.Red.JoinNumber, stbColor.Red);
trilist.SetBoolSigAction(joinMap.Green.JoinNumber, stbColor.Green);
trilist.SetBoolSigAction(joinMap.Yellow.JoinNumber, stbColor.Yellow);
trilist.SetBoolSigAction(joinMap.Blue.JoinNumber, stbColor.Blue);
}
if (this is ISetTopBoxNumericKeypad stbKeypad)
{
trilist.StringInput[joinMap.KeypadAccessoryButton1Label.JoinNumber].StringValue = stbKeypad.KeypadAccessoryButton1Label;
trilist.StringInput[joinMap.KeypadAccessoryButton2Label.JoinNumber].StringValue = stbKeypad.KeypadAccessoryButton2Label;
trilist.BooleanInput[joinMap.HasKeypadAccessoryButton1.JoinNumber].BoolValue = stbKeypad.HasKeypadAccessoryButton1;
trilist.BooleanInput[joinMap.HasKeypadAccessoryButton2.JoinNumber].BoolValue = stbKeypad.HasKeypadAccessoryButton2;
trilist.SetBoolSigAction(joinMap.Digit0.JoinNumber, stbKeypad.Digit0);
trilist.SetBoolSigAction(joinMap.Digit1.JoinNumber, stbKeypad.Digit1);
trilist.SetBoolSigAction(joinMap.Digit2.JoinNumber, stbKeypad.Digit2);
trilist.SetBoolSigAction(joinMap.Digit3.JoinNumber, stbKeypad.Digit3);
trilist.SetBoolSigAction(joinMap.Digit4.JoinNumber, stbKeypad.Digit4);
trilist.SetBoolSigAction(joinMap.Digit5.JoinNumber, stbKeypad.Digit5);
trilist.SetBoolSigAction(joinMap.Digit6.JoinNumber, stbKeypad.Digit6);
trilist.SetBoolSigAction(joinMap.Digit7.JoinNumber, stbKeypad.Digit7);
trilist.SetBoolSigAction(joinMap.Digit8.JoinNumber, stbKeypad.Digit8);
trilist.SetBoolSigAction(joinMap.Digit9.JoinNumber, stbKeypad.Digit9);
trilist.SetBoolSigAction(joinMap.KeypadAccessoryButton1Press.JoinNumber, stbKeypad.KeypadAccessoryButton1);
trilist.SetBoolSigAction(joinMap.KeypadAccessoryButton2Press.JoinNumber, stbKeypad.KeypadAccessoryButton1);
trilist.SetBoolSigAction(joinMap.Dash.JoinNumber, stbKeypad.Dash);
trilist.SetBoolSigAction(joinMap.KeypadEnter.JoinNumber, stbKeypad.KeypadEnter);
}
if (this is ITransport stbTransport)
{
trilist.SetBoolSigAction(joinMap.Play.JoinNumber, stbTransport.Play);
trilist.SetBoolSigAction(joinMap.Pause.JoinNumber, stbTransport.Pause);
trilist.SetBoolSigAction(joinMap.Rewind.JoinNumber, stbTransport.Rewind);
trilist.SetBoolSigAction(joinMap.FFwd.JoinNumber, stbTransport.FFwd);
trilist.SetBoolSigAction(joinMap.ChapMinus.JoinNumber, stbTransport.ChapMinus);
trilist.SetBoolSigAction(joinMap.ChapPlus.JoinNumber, stbTransport.ChapPlus);
trilist.SetBoolSigAction(joinMap.Stop.JoinNumber, stbTransport.Stop);
trilist.SetBoolSigAction(joinMap.Record.JoinNumber, stbTransport.Record);
}
}
}
}