Merge branch 'development' into feature/Updates-to-rf-gateways

This commit is contained in:
Andrew Welker
2020-05-21 09:46:58 -06:00
committed by GitHub
4 changed files with 429 additions and 264 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

@@ -27,22 +27,77 @@ namespace PepperDash.Essentials.Core.CrestronIO
} }
} }
public GenericDigitalInputDevice(string key, DigitalInput inputPort):
base(key) public GenericDigitalInputDevice(string key, string name, Func<IOPortConfig, DigitalInput> postActivationFunc,
IOPortConfig config)
: base(key, name)
{ {
InputStateFeedback = new BoolFeedback(InputStateFeedbackFunc);
InputPort = inputPort; AddPostActivationAction(() =>
{
InputPort = postActivationFunc(config);
InputPort.StateChange += InputPort_StateChange; InputPort.Register();
InputPort.StateChange += InputPort_StateChange;
});
} }
#region Events
void InputPort_StateChange(DigitalInput digitalInput, DigitalInputEventArgs args) void InputPort_StateChange(DigitalInput digitalInput, DigitalInputEventArgs args)
{ {
InputStateFeedback.FireUpdate(); InputStateFeedback.FireUpdate();
} }
#endregion
#region PreActivate
private static DigitalInput GetDigitalInput(IOPortConfig dc)
{
IDigitalInputPorts ioPortDevice;
if (dc.PortDeviceKey.Equals("processor"))
{
if (!Global.ControlSystem.SupportsDigitalInput)
{
Debug.Console(0, "GetDigitalInput: Processor does not support Digital Inputs");
return null;
}
ioPortDevice = Global.ControlSystem;
}
else
{
var ioPortDev = DeviceManager.GetDeviceForKey(dc.PortDeviceKey) as IDigitalInputPorts;
if (ioPortDev == null)
{
Debug.Console(0, "GetDigitalInput: Device {0} is not a valid device", dc.PortDeviceKey);
return null;
}
ioPortDevice = ioPortDev;
}
if (ioPortDevice == null)
{
Debug.Console(0, "GetDigitalInput: Device '0' is not a valid IRelayPorts Device", dc.PortDeviceKey);
return null;
}
if (dc.PortNumber > ioPortDevice.NumberOfDigitalInputPorts)
{
Debug.Console(0, "GetDigitalInput: Device {0} does not contain a port {1}", dc.PortDeviceKey, dc.PortNumber);
}
return ioPortDevice.DigitalInputPorts[dc.PortNumber];
}
#endregion
#region Bridge Linking
public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge)
{ {
var joinMap = new IDigitalInputJoinMap(joinStart); var joinMap = new IDigitalInputJoinMap(joinStart);
@@ -67,96 +122,35 @@ namespace PepperDash.Essentials.Core.CrestronIO
Debug.Console(1, this, "Error: {0}", e); Debug.Console(1, this, "Error: {0}", e);
} }
} }
}
public class GenericDigitalInputDeviceFactory : EssentialsDeviceFactory<GenericDigitalInputDevice> #endregion
{
public GenericDigitalInputDeviceFactory() #region Factory
public class GenericDigitalInputDeviceFactory : EssentialsDeviceFactory<GenericDigitalInputDevice>
{ {
TypeNames = new List<string>() { "digitalinput" }; public GenericDigitalInputDeviceFactory()
}
public override EssentialsDevice BuildDevice(DeviceConfig dc)
{
Debug.Console(1, "Factory Attempting to create new Digtal Input Device");
var props = JsonConvert.DeserializeObject<IOPortConfig>(dc.Properties.ToString());
IDigitalInputPorts portDevice;
if (props.PortDeviceKey == "processor")
portDevice = Global.ControlSystem as IDigitalInputPorts;
else
portDevice = DeviceManager.GetDeviceForKey(props.PortDeviceKey) as IDigitalInputPorts;
if (portDevice == null)
Debug.Console(0, "ERROR: Unable to add digital input device with key '{0}'. Port Device does not support digital inputs", dc.Key);
else
{ {
var cs = (portDevice as CrestronControlSystem); TypeNames = new List<string>() { "digitalinput" };
if (cs == null) }
{
Debug.Console(0, "ERROR: Port device for [{0}] is not control system", props.PortDeviceKey); public override EssentialsDevice BuildDevice(DeviceConfig dc)
return null; {
} Debug.Console(1, "Factory Attempting to create new Generic Relay Device");
if (cs.SupportsVersiport) var props = JsonConvert.DeserializeObject<IOPortConfig>(dc.Properties.ToString());
{
Debug.Console(1, "Attempting to add Digital Input device to Versiport port '{0}'", props.PortNumber); if (props == null) return null;
if (props.PortNumber > cs.NumberOfVersiPorts) var portDevice = new GenericDigitalInputDevice(dc.Key, dc.Name, GetDigitalInput, props);
{
Debug.Console(0, "WARNING: Cannot add Vesiport {0} on {1}. Out of range", return portDevice;
props.PortNumber, props.PortDeviceKey);
return null;
}
Versiport vp = cs.VersiPorts[props.PortNumber];
if (!vp.Registered)
{
var regSuccess = vp.Register();
if (regSuccess == eDeviceRegistrationUnRegistrationResponse.Success)
{
Debug.Console(1, "Successfully Created Digital Input Device on Versiport");
return new GenericVersiportDigitalInputDevice(dc.Key, vp, props);
}
else
{
Debug.Console(0, "WARNING: Attempt to register versiport {0} on device with key '{1}' failed: {2}",
props.PortNumber, props.PortDeviceKey, regSuccess);
return null;
}
}
}
else if (cs.SupportsDigitalInput)
{
Debug.Console(1, "Attempting to add Digital Input device to Digital Input port '{0}'", props.PortNumber);
if (props.PortNumber > cs.NumberOfDigitalInputPorts)
{
Debug.Console(0, "WARNING: Cannot register DIO port {0} on {1}. Out of range",
props.PortNumber, props.PortDeviceKey);
return null;
}
DigitalInput digitalInput = cs.DigitalInputPorts[props.PortNumber];
if (!digitalInput.Registered)
{
if (digitalInput.Register() == eDeviceRegistrationUnRegistrationResponse.Success)
{
Debug.Console(1, "Successfully Created Digital Input Device on Digital Input");
return new GenericDigitalInputDevice(dc.Key, digitalInput);
}
else
Debug.Console(0, "WARNING: Attempt to register digital input {0} on device with key '{1}' failed.",
props.PortNumber, props.PortDeviceKey);
}
}
} }
return null;
} }
#endregion
} }
} }

View File

@@ -21,7 +21,8 @@ namespace PepperDash.Essentials.Core.CrestronIO
public BoolFeedback OutputIsOnFeedback { get; private set; } public BoolFeedback OutputIsOnFeedback { get; private set; }
public GenericRelayDevice(string key, Relay relay): //Maintained for compatibility with PepperDash.Essentials.Core.Devices.CrestronProcessor
public GenericRelayDevice(string key, Relay relay) :
base(key) base(key)
{ {
OutputIsOnFeedback = new BoolFeedback(new Func<bool>(() => RelayOutput.State)); OutputIsOnFeedback = new BoolFeedback(new Func<bool>(() => RelayOutput.State));
@@ -32,11 +33,75 @@ namespace PepperDash.Essentials.Core.CrestronIO
RelayOutput.StateChange += new RelayEventHandler(RelayOutput_StateChange); RelayOutput.StateChange += new RelayEventHandler(RelayOutput_StateChange);
} }
public GenericRelayDevice(string key, string name, Func<IOPortConfig, Relay> postActivationFunc,
IOPortConfig config)
: base(key, name)
{
AddPostActivationAction(() =>
{
RelayOutput = postActivationFunc(config);
RelayOutput.Register();
RelayOutput.StateChange += RelayOutput_StateChange;
});
}
#region PreActivate
private static Relay GetRelay(IOPortConfig dc)
{
IRelayPorts relayDevice;
if(dc.PortDeviceKey.Equals("processor"))
{
if (!Global.ControlSystem.SupportsRelay)
{
Debug.Console(0, "GetRelayDevice: Processor does not support relays");
return null;
}
relayDevice = Global.ControlSystem;
}
else
{
var relayDev = DeviceManager.GetDeviceForKey(dc.PortDeviceKey) as IRelayPorts;
if (relayDev == null)
{
Debug.Console(0, "GetRelayDevice: Device {0} is not a valid device", dc.PortDeviceKey);
return null;
}
relayDevice = relayDev;
}
if (relayDevice == null)
{
Debug.Console(0, "GetRelayDevice: Device '0' is not a valid IRelayPorts Device", dc.PortDeviceKey);
return null;
}
if (dc.PortNumber > relayDevice.NumberOfRelayPorts)
{
Debug.Console(0, "GetRelayDevice: Device {0} does not contain a port {1}", dc.PortDeviceKey, dc.PortNumber);
}
return relayDevice.RelayPorts[dc.PortNumber];
}
#endregion
#region Events
void RelayOutput_StateChange(Relay relay, RelayEventArgs args) void RelayOutput_StateChange(Relay relay, RelayEventArgs args)
{ {
OutputIsOnFeedback.FireUpdate(); OutputIsOnFeedback.FireUpdate();
} }
#endregion
#region Methods
public void OpenRelay() public void OpenRelay()
{ {
RelayOutput.State = false; RelayOutput.State = false;
@@ -55,6 +120,8 @@ namespace PepperDash.Essentials.Core.CrestronIO
CloseRelay(); CloseRelay();
} }
#endregion
#region ISwitchedOutput Members #region ISwitchedOutput Members
void ISwitchedOutput.On() void ISwitchedOutput.On()
@@ -69,6 +136,8 @@ namespace PepperDash.Essentials.Core.CrestronIO
#endregion #endregion
#region Bridge Linking
public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge)
{ {
var joinMap = new GenericRelayControllerJoinMap(joinStart); var joinMap = new GenericRelayControllerJoinMap(joinStart);
@@ -100,75 +169,88 @@ namespace PepperDash.Essentials.Core.CrestronIO
OutputIsOnFeedback.LinkInputSig(trilist.BooleanInput[joinMap.Relay.JoinNumber]); OutputIsOnFeedback.LinkInputSig(trilist.BooleanInput[joinMap.Relay.JoinNumber]);
} }
}
public class GenericRelayDeviceFactory : EssentialsDeviceFactory<GenericRelayDevice> #endregion
{
public GenericRelayDeviceFactory() #region Factory
public class GenericRelayDeviceFactory : EssentialsDeviceFactory<GenericRelayDevice>
{ {
TypeNames = new List<string>() { "relayoutput" }; public GenericRelayDeviceFactory()
}
public override EssentialsDevice BuildDevice(DeviceConfig dc)
{
Debug.Console(1, "Factory Attempting to create new Generic Relay Device");
var props = JsonConvert.DeserializeObject<IOPortConfig>(dc.Properties.ToString());
var key = dc.Key;
IRelayPorts portDevice;
if (props.PortDeviceKey == "processor")
portDevice = Global.ControlSystem as IRelayPorts;
else
portDevice = DeviceManager.GetDeviceForKey(props.PortDeviceKey) as IRelayPorts;
if (portDevice == null)
Debug.Console(0, "Unable to add relay device with key '{0}'. Port Device does not support relays", key);
else
{ {
var cs = (portDevice as CrestronControlSystem); TypeNames = new List<string>() { "relayoutput" };
if (cs != null)
{
// The relay is on a control system processor
if (!cs.SupportsRelay || props.PortNumber > cs.NumberOfRelayPorts)
{
Debug.Console(0, "Port Device: {0} does not support relays or does not have enough relays");
return null;
}
}
else
{
// The relay is on another device type
if (props.PortNumber > portDevice.NumberOfRelayPorts)
{
Debug.Console(0, "Port Device: {0} does not have enough relays");
return null;
}
}
Relay relay = portDevice.RelayPorts[props.PortNumber];
if (!relay.Registered)
{
if (relay.Register() == eDeviceRegistrationUnRegistrationResponse.Success)
return new GenericRelayDevice(key, relay);
else
Debug.Console(0, "Attempt to register relay {0} on device with key '{1}' failed.", props.PortNumber, props.PortDeviceKey);
}
else
{
return new GenericRelayDevice(key, relay);
}
// Future: Check if portDevice is 3-series card or other non control system that supports versiports
} }
return null; public override EssentialsDevice BuildDevice(DeviceConfig dc)
{
Debug.Console(1, "Factory Attempting to create new Generic Relay Device");
var props = JsonConvert.DeserializeObject<IOPortConfig>(dc.Properties.ToString());
if (props == null) return null;
var portDevice = new GenericRelayDevice(dc.Key, dc.Name, GetRelay, props);
return portDevice;
/*
if (props.PortDeviceKey == "processor")
portDevice = Global.ControlSystem as IRelayPorts;
else
portDevice = DeviceManager.GetDeviceForKey(props.PortDeviceKey) as IRelayPorts;
if (portDevice == null)
Debug.Console(0, "Unable to add relay device with key '{0}'. Port Device does not support relays", key);
else
{
var cs = (portDevice as CrestronControlSystem);
if (cs != null)
{
// The relay is on a control system processor
if (!cs.SupportsRelay || props.PortNumber > cs.NumberOfRelayPorts)
{
Debug.Console(0, "Port Device: {0} does not support relays or does not have enough relays");
return null;
}
}
else
{
// The relay is on another device type
if (props.PortNumber > portDevice.NumberOfRelayPorts)
{
Debug.Console(0, "Port Device: {0} does not have enough relays");
return null;
}
}
Relay relay = portDevice.RelayPorts[props.PortNumber];
if (!relay.Registered)
{
if (relay.Register() == eDeviceRegistrationUnRegistrationResponse.Success)
return new GenericRelayDevice(key, relay);
else
Debug.Console(0, "Attempt to register relay {0} on device with key '{1}' failed.", props.PortNumber, props.PortDeviceKey);
}
else
{
return new GenericRelayDevice(key, relay);
}
// Future: Check if portDevice is 3-series card or other non control system that supports versiports
}
*/
}
} }
#endregion
} }
} }

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);
} }
} }
} }