Updated IRPortHelper

Updated IrOurputPortController

#205

Please Check my work - I'm a little less confident in this one
This commit is contained in:
Trevor Payne
2020-05-20 21:53:48 -05:00
parent 19e78d649f
commit 190697d855
2 changed files with 152 additions and 63 deletions

View File

@@ -80,6 +80,81 @@ namespace PepperDash.Essentials.Core
} }
} }
public static IROutputPort GetIrOutputPort(DeviceConfig dc)
{
var irControllerKey = dc.Key + "-ir";
if (dc.Properties == null)
{
Debug.Console(0, "[{0}] WARNING: Device config does not include properties. IR will not function.", dc.Key);
return null;
}
var control = dc.Properties["control"];
if (control == null)
{
Debug.Console(0,
"WARNING: Device config does not include control properties. IR will not function for {0}", dc.Key);
return null;
}
var portDevKey = control.Value<string>("controlPortDevKey");
var portNum = control.Value<uint>("controlPortNumber");
IIROutputPorts irDev = null;
if (portDevKey == null)
{
Debug.Console(0, "WARNING: control properties is missing ir device for {0}", dc.Key);
return null;
}
if (portNum == 0)
{
Debug.Console(0, "WARNING: control properties is missing ir port number for {0}", dc.Key);
return null;
}
if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase)
|| portDevKey.Equals("processor", StringComparison.OrdinalIgnoreCase))
irDev = Global.ControlSystem;
else
irDev = DeviceManager.GetDeviceForKey(portDevKey) as IIROutputPorts;
if (irDev == null)
{
Debug.Console(0, "WARNING: device with IR ports '{0}' not found", portDevKey);
return null;
}
if (portNum >= irDev.NumberOfIROutputPorts)
{
Debug.Console(0, "WARNING: device '{0}' IR port {1} out of range",
portDevKey, portNum);
return null;
}
var port = irDev.IROutputPorts[portNum];
port.LoadIRDriver(Global.FilePathPrefix + "IR" + Global.DirectorySeparator + control["irFile"].Value<string>());
return port;
}
public static IrOutputPortController GetIrOutputPortController(DeviceConfig config)
{
Debug.Console(1, "Attempting to create new Ir Port Controller");
if (config == null)
{
return null;
}
var irDevice = new IrOutputPortController(config.Key, GetIrOutputPort, config);
return irDevice;
}
/*
/// <summary> /// <summary>
/// Returns a ready-to-go IrOutputPortController from a DeviceConfig object. /// Returns a ready-to-go IrOutputPortController from a DeviceConfig object.
/// </summary> /// </summary>
@@ -141,7 +216,7 @@ namespace PepperDash.Essentials.Core
portDevKey, portNum); portDevKey, portNum);
return c; return c;
} }
} }*/
} }
/// <summary> /// <summary>

View File

@@ -4,6 +4,9 @@ using System.Linq;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharpPro; using Crestron.SimplSharpPro;
using Crestron.SimplSharpPro.DeviceSupport; using Crestron.SimplSharpPro.DeviceSupport;
using Newtonsoft.Json.Linq;
using PepperDash.Essentials.Core.Config;
using PepperDash.Core; using PepperDash.Core;
@@ -39,6 +42,18 @@ namespace PepperDash.Essentials.Core
LoadDriver(irDriverFilepath); LoadDriver(irDriverFilepath);
} }
public IrOutputPortController(string key, Func<DeviceConfig, IROutputPort> postActivationFunc,
DeviceConfig config)
: base(key)
{
AddPostActivationAction(() =>
{
IrPort = postActivationFunc(config);
});
}
/// <summary> /// <summary>
/// Loads the IR driver at path /// Loads the IR driver at path
/// </summary> /// </summary>
@@ -119,6 +134,5 @@ namespace PepperDash.Essentials.Core
Debug.Console(2, this, "Device {0}: IR Driver {1} does not contain command {2}", Debug.Console(2, this, "Device {0}: IR Driver {1} does not contain command {2}",
Key, IrPort.IRDriverFileNameByIRDriverId(IrPortUid), command); Key, IrPort.IRDriverFileNameByIRDriverId(IrPortUid), command);
} }
} }
} }