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,68 +80,143 @@ namespace PepperDash.Essentials.Core
} }
} }
/// <summary> public static IROutputPort GetIrOutputPort(DeviceConfig dc)
/// Returns a ready-to-go IrOutputPortController from a DeviceConfig object. {
/// </summary> var irControllerKey = dc.Key + "-ir";
public static IrOutputPortController GetIrOutputPortController(DeviceConfig devConf) if (dc.Properties == null)
{ {
var irControllerKey = devConf.Key + "-ir"; Debug.Console(0, "[{0}] WARNING: Device config does not include properties. IR will not function.", dc.Key);
if (devConf.Properties == null) return null;
{ }
Debug.Console(0, "[{0}] WARNING: Device config does not include properties. IR will not function.", devConf.Key);
return new IrOutputPortController(irControllerKey, null, "");
}
var control = devConf.Properties["control"]; var control = dc.Properties["control"];
if (control == null) if (control == null)
{ {
var c = new IrOutputPortController(irControllerKey, null, ""); Debug.Console(0,
Debug.Console(0, c, "WARNING: Device config does not include control properties. IR will not function"); "WARNING: Device config does not include control properties. IR will not function for {0}", dc.Key);
return c; return null;
} }
var portDevKey = control.Value<string>("controlPortDevKey"); var portDevKey = control.Value<string>("controlPortDevKey");
var portNum = control.Value<uint>("controlPortNumber"); var portNum = control.Value<uint>("controlPortNumber");
IIROutputPorts irDev = null; IIROutputPorts irDev = null;
if (portDevKey == null) if (portDevKey == null)
{ {
var c = new IrOutputPortController(irControllerKey, null, ""); Debug.Console(0, "WARNING: control properties is missing ir device for {0}", dc.Key);
Debug.Console(0, c, "WARNING: control properties is missing ir device"); return null;
return c; }
}
if (portNum == 0) if (portNum == 0)
{ {
var c = new IrOutputPortController(irControllerKey, null, ""); Debug.Console(0, "WARNING: control properties is missing ir port number for {0}", dc.Key);
Debug.Console(0, c, "WARNING: control properties is missing ir port number"); return null;
return c; }
}
if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase) if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase)
|| portDevKey.Equals("processor", StringComparison.OrdinalIgnoreCase)) || portDevKey.Equals("processor", StringComparison.OrdinalIgnoreCase))
irDev = Global.ControlSystem; irDev = Global.ControlSystem;
else else
irDev = DeviceManager.GetDeviceForKey(portDevKey) as IIROutputPorts; irDev = DeviceManager.GetDeviceForKey(portDevKey) as IIROutputPorts;
if (irDev == null) if (irDev == null)
{ {
var c = new IrOutputPortController(irControllerKey, null, ""); Debug.Console(0, "WARNING: device with IR ports '{0}' not found", portDevKey);
Debug.Console(0, c, "WARNING: device with IR ports '{0}' not found", portDevKey); return null;
return c; }
} if (portNum >= irDev.NumberOfIROutputPorts)
{
Debug.Console(0, "WARNING: device '{0}' IR port {1} out of range",
portDevKey, portNum);
return null;
}
if (portNum <= irDev.NumberOfIROutputPorts) // success!
return new IrOutputPortController(irControllerKey, irDev.IROutputPorts[portNum], var port = irDev.IROutputPorts[portNum];
IrDriverPathPrefix + control["irFile"].Value<string>());
else port.LoadIRDriver(Global.FilePathPrefix + "IR" + Global.DirectorySeparator + control["irFile"].Value<string>());
{
var c = new IrOutputPortController(irControllerKey, null, ""); return port;
Debug.Console(0, c, "WARNING: device '{0}' IR port {1} out of range",
portDevKey, portNum); }
return c;
} 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>
/// Returns a ready-to-go IrOutputPortController from a DeviceConfig object.
/// </summary>
public static IrOutputPortController GetIrOutputPortController(DeviceConfig devConf)
{
var irControllerKey = devConf.Key + "-ir";
if (devConf.Properties == null)
{
Debug.Console(0, "[{0}] WARNING: Device config does not include properties. IR will not function.", devConf.Key);
return new IrOutputPortController(irControllerKey, null, "");
}
var control = devConf.Properties["control"];
if (control == null)
{
var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: Device config does not include control properties. IR will not function");
return c;
}
var portDevKey = control.Value<string>("controlPortDevKey");
var portNum = control.Value<uint>("controlPortNumber");
IIROutputPorts irDev = null;
if (portDevKey == null)
{
var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: control properties is missing ir device");
return c;
}
if (portNum == 0)
{
var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: control properties is missing ir port number");
return c;
}
if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase)
|| portDevKey.Equals("processor", StringComparison.OrdinalIgnoreCase))
irDev = Global.ControlSystem;
else
irDev = DeviceManager.GetDeviceForKey(portDevKey) as IIROutputPorts;
if (irDev == null)
{
var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: device with IR ports '{0}' not found", portDevKey);
return c;
}
if (portNum <= irDev.NumberOfIROutputPorts) // success!
return new IrOutputPortController(irControllerKey, irDev.IROutputPorts[portNum],
IrDriverPathPrefix + control["irFile"].Value<string>());
else
{
var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: device '{0}' IR port {1} out of range",
portDevKey, portNum);
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,7 +42,19 @@ namespace PepperDash.Essentials.Core
LoadDriver(irDriverFilepath); LoadDriver(irDriverFilepath);
} }
/// <summary> public IrOutputPortController(string key, Func<DeviceConfig, IROutputPort> postActivationFunc,
DeviceConfig config)
: base(key)
{
AddPostActivationAction(() =>
{
IrPort = postActivationFunc(config);
});
}
/// <summary>
/// Loads the IR driver at path /// Loads the IR driver at path
/// </summary> /// </summary>
/// <param name="path"></param> /// <param name="path"></param>
@@ -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);
} }
} }
} }