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

@@ -78,70 +78,145 @@ namespace PepperDash.Essentials.Core
portDevKey, portNum); portDevKey, portNum);
return port; return port;
} }
} }
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>
/// 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, "");
}
/// <summary> var control = devConf.Properties["control"];
/// Returns a ready-to-go IrOutputPortController from a DeviceConfig object. if (control == null)
/// </summary> {
public static IrOutputPortController GetIrOutputPortController(DeviceConfig devConf) var c = new IrOutputPortController(irControllerKey, null, "");
{ Debug.Console(0, c, "WARNING: Device config does not include control properties. IR will not function");
var irControllerKey = devConf.Key + "-ir"; return c;
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"]; var portDevKey = control.Value<string>("controlPortDevKey");
if (control == null) var portNum = control.Value<uint>("controlPortNumber");
{ IIROutputPorts irDev = 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"); if (portDevKey == null)
var portNum = control.Value<uint>("controlPortNumber"); {
IIROutputPorts irDev = null; var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: control properties is missing ir device");
return c;
}
if (portDevKey == null) if (portNum == 0)
{ {
var c = new IrOutputPortController(irControllerKey, null, ""); var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: control properties is missing ir device"); Debug.Console(0, c, "WARNING: control properties is missing ir port number");
return c; return c;
} }
if (portNum == 0) if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase)
{ || portDevKey.Equals("processor", StringComparison.OrdinalIgnoreCase))
var c = new IrOutputPortController(irControllerKey, null, ""); irDev = Global.ControlSystem;
Debug.Console(0, c, "WARNING: control properties is missing ir port number"); else
return c; irDev = DeviceManager.GetDeviceForKey(portDevKey) as IIROutputPorts;
}
if (portDevKey.Equals("controlSystem", StringComparison.OrdinalIgnoreCase) if (irDev == null)
|| portDevKey.Equals("processor", StringComparison.OrdinalIgnoreCase)) {
irDev = Global.ControlSystem; var c = new IrOutputPortController(irControllerKey, null, "");
else Debug.Console(0, c, "WARNING: device with IR ports '{0}' not found", portDevKey);
irDev = DeviceManager.GetDeviceForKey(portDevKey) as IIROutputPorts; return c;
}
if (irDev == null) if (portNum <= irDev.NumberOfIROutputPorts) // success!
{ return new IrOutputPortController(irControllerKey, irDev.IROutputPorts[portNum],
var c = new IrOutputPortController(irControllerKey, null, ""); IrDriverPathPrefix + control["irFile"].Value<string>());
Debug.Console(0, c, "WARNING: device with IR ports '{0}' not found", portDevKey); else
return c; {
} var c = new IrOutputPortController(irControllerKey, null, "");
Debug.Console(0, c, "WARNING: device '{0}' IR port {1} out of range",
if (portNum <= irDev.NumberOfIROutputPorts) // success! portDevKey, portNum);
return new IrOutputPortController(irControllerKey, irDev.IROutputPorts[portNum], return c;
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

@@ -3,7 +3,10 @@ using System.Collections.Generic;
using System.Linq; 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;
@@ -37,9 +40,21 @@ namespace PepperDash.Essentials.Core
return; return;
} }
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>
@@ -118,7 +133,6 @@ 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);
} }
} }
} }