moves bridgeBase and IBridge/IBridgeAdvanced

This commit is contained in:
Andrew Welker
2020-04-13 16:01:19 -06:00
parent 3b2b69de1b
commit 31244e4294
2 changed files with 406 additions and 411 deletions

View File

@@ -1,407 +1,406 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using Crestron.SimplSharp.Reflection;
using System.Text; using Crestron.SimplSharpPro.EthernetCommunication;
using Crestron.SimplSharp;
using Crestron.SimplSharpPro.EthernetCommunication; using Newtonsoft.Json;
using Newtonsoft.Json; using PepperDash.Core;
using PepperDash.Essentials.Core.Lighting;
using PepperDash.Core; using PepperDash.Essentials.Core.Config;
using PepperDash.Essentials.Core; using PepperDash.Essentials.Core.CrestronIO;
using PepperDash.Essentials.Core.Lighting;
using PepperDash.Essentials.Core.Devices; //using PepperDash.Essentials.Devices.Common.Cameras;
using PepperDash.Essentials.Devices.Common;
using PepperDash.Essentials.Devices.Common.Cameras; namespace PepperDash.Essentials.Core.Bridges
using PepperDash.Essentials.Core.Config; {
using PepperDash.Essentials.Core.CrestronIO; /// <summary>
using PepperDash.Essentials.DM; /// Helper methods for bridges
//using PepperDash.Essentials.Devices.Common.Cameras; /// </summary>
public static class BridgeHelper
namespace PepperDash.Essentials.Bridges {
{ public static void PrintJoinMap(string command)
/// <summary> {
/// Helper methods for bridges var targets = command.Split(' ');
/// </summary>
public static class BridgeHelper var bridgeKey = targets[0].Trim();
{
public static void PrintJoinMap(string command) var bridge = DeviceManager.GetDeviceForKey(bridgeKey) as EiscApi;
{
string bridgeKey = ""; if (bridge == null)
string deviceKey = ""; {
Debug.Console(0, "Unable to find bridge with key: '{0}'", bridgeKey);
var targets = command.Split(' '); return;
}
bridgeKey = targets[0].Trim();
if (targets.Length > 1)
var bridge = DeviceManager.GetDeviceForKey(bridgeKey) as EiscApi; {
var deviceKey = targets[1].Trim();
if (bridge == null)
{ if (string.IsNullOrEmpty(deviceKey)) return;
Debug.Console(0, "Unable to find bridge with key: '{0}'", bridgeKey); bridge.PrintJoinMapForDevice(deviceKey);
return; }
} else
{
if (targets.Length > 1) bridge.PrintJoinMaps();
{ }
deviceKey = targets[1].Trim(); }
}
if (!string.IsNullOrEmpty(deviceKey))
{
bridge.PrintJoinMapForDevice(deviceKey); /// <summary>
return; /// Base class for all bridge class variants
} /// </summary>
} public class BridgeBase : EssentialsDevice
else {
{ public BridgeApi Api { get; protected set; }
bridge.PrintJoinMaps();
} public BridgeBase(string key) :
} base(key)
} {
}
/// <summary> }
/// Base class for all bridge class variants
/// </summary> /// <summary>
public class BridgeBase : Device /// Base class for bridge API variants
{ /// </summary>
public BridgeApi Api { get; private set; } public abstract class BridgeApi : EssentialsDevice
{
public BridgeBase(string key) : protected BridgeApi(string key) :
base(key) base(key)
{ {
} }
}
}
/// <summary>
/// <summary> /// Bridge API using EISC
/// Base class for bridge API variants /// </summary>
/// </summary> public class EiscApi : BridgeApi
public abstract class BridgeApi : Device {
{ public EiscApiPropertiesConfig PropertiesConfig { get; private set; }
public BridgeApi(string key) :
base(key) protected Dictionary<string, JoinMapBaseAdvanced> JoinMaps { get; private set; }
{
public ThreeSeriesTcpIpEthernetIntersystemCommunications Eisc { get; private set; }
}
} public EiscApi(DeviceConfig dc) :
base(dc.Key)
/// <summary> {
/// Bridge API using EISC JoinMaps = new Dictionary<string, JoinMapBaseAdvanced>();
/// </summary>
public class EiscApi : BridgeApi PropertiesConfig = JsonConvert.DeserializeObject<EiscApiPropertiesConfig>(dc.Properties.ToString());
{
public EiscApiPropertiesConfig PropertiesConfig { get; private set; } Eisc = new ThreeSeriesTcpIpEthernetIntersystemCommunications(PropertiesConfig.Control.IpIdInt, PropertiesConfig.Control.TcpSshProperties.Address, Global.ControlSystem);
protected Dictionary<string, JoinMapBaseAdvanced> JoinMaps { get; private set; } Eisc.SigChange += Eisc_SigChange;
public ThreeSeriesTcpIpEthernetIntersystemCommunications Eisc { get; private set; } Eisc.Register();
public EiscApi(DeviceConfig dc) : AddPostActivationAction( () =>
base(dc.Key) {
{ Debug.Console(1, this, "Linking Devices...");
JoinMaps = new Dictionary<string, JoinMapBaseAdvanced>();
foreach (var d in PropertiesConfig.Devices)
PropertiesConfig = JsonConvert.DeserializeObject<EiscApiPropertiesConfig>(dc.Properties.ToString()); {
var device = DeviceManager.GetDeviceForKey(d.DeviceKey);
Eisc = new ThreeSeriesTcpIpEthernetIntersystemCommunications(PropertiesConfig.Control.IpIdInt, PropertiesConfig.Control.TcpSshProperties.Address, Global.ControlSystem);
if (device == null) continue;
Eisc.SigChange += new Crestron.SimplSharpPro.DeviceSupport.SigEventHandler(Eisc_SigChange);
Debug.Console(1, this, "Linking Device: '{0}'", device.Key);
Eisc.Register();
if (device.GetType().GetCType().IsAssignableFrom(typeof (IBridge)))
AddPostActivationAction( () => {
{ var bridge = device as IBridge;
Debug.Console(1, this, "Linking Devices...");
if (bridge != null) bridge.LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
foreach (var d in PropertiesConfig.Devices) continue;
{ }
var device = DeviceManager.GetDeviceForKey(d.DeviceKey);
if (device.GetType().GetCType().IsAssignableFrom(typeof (IBridgeAdvanced)))
if (device != null) {
{ var bridge = device as IBridgeAdvanced;
Debug.Console(1, this, "Linking Device: '{0}'", device.Key); if (bridge != null) bridge.LinkToApi(Eisc, d.JoinStart, d.JoinMapKey, this);
}
if (device is IBridge) // Check for this first to allow bridges in plugins to override existing bridges that apply to the same type. //if (device is IBridge) // Check for this first to allow bridges in plugins to override existing bridges that apply to the same type.
{ //{
(device as IBridge).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as IBridge).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is IBridgeAdvanced) //else if (device is IBridgeAdvanced)
{ //{
Debug.Console(2, this, "'{0}' is IBridgeAdvanced", device.Key); // Debug.Console(2, this, "'{0}' is IBridgeAdvanced", device.Key);
(device as IBridgeAdvanced).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey, this); // (device as IBridgeAdvanced).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey, this);
} //}
else if (device is PepperDash.Essentials.Core.Monitoring.SystemMonitorController) //else if (device is PepperDash.Essentials.Core.Monitoring.SystemMonitorController)
{ //{
(device as PepperDash.Essentials.Core.Monitoring.SystemMonitorController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as PepperDash.Essentials.Core.Monitoring.SystemMonitorController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is GenericComm) //else if (device is GenericComm)
{ //{
(device as GenericComm).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as GenericComm).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is CameraBase) //else if (device is CameraBase)
{ //{
(device as CameraBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey, this); // (device as CameraBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey, this);
continue; // continue;
} //}
else if (device is PepperDash.Essentials.Core.DisplayBase) //else if (device is PepperDash.Essentials.Core.DisplayBase)
{ //{
(device as DisplayBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DisplayBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is DmChassisController) //else if (device is DmChassisController)
{ //{
(device as DmChassisController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DmChassisController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is DmBladeChassisController) //else if (device is DmBladeChassisController)
{ //{
(device as DmBladeChassisController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DmBladeChassisController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is DmpsRoutingController) //else if (device is DmpsRoutingController)
{ //{
(device as DmpsRoutingController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DmpsRoutingController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is DmpsAudioOutputController) //else if (device is DmpsAudioOutputController)
{ //{
(device as DmpsAudioOutputController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DmpsAudioOutputController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is DmTxControllerBase) //else if (device is DmTxControllerBase)
{ //{
(device as DmTxControllerBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DmTxControllerBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is DmRmcControllerBase) //else if (device is DmRmcControllerBase)
{ //{
(device as DmRmcControllerBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DmRmcControllerBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is GenericRelayDevice) //else if (device is GenericRelayDevice)
{ //{
(device as GenericRelayDevice).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as GenericRelayDevice).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is IRSetTopBoxBase) //else if (device is IRSetTopBoxBase)
{ //{
(device as IRSetTopBoxBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as IRSetTopBoxBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is IDigitalInput) //else if (device is IDigitalInput)
{ //{
(device as IDigitalInput).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as IDigitalInput).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is AppleTV) //else if (device is AppleTV)
{ //{
(device as AppleTV).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as AppleTV).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is HdMdxxxCEController) //else if (device is HdMdxxxCEController)
{ //{
(device as HdMdxxxCEController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as HdMdxxxCEController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is LightingBase) //else if (device is LightingBase)
{ //{
(device as LightingBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as LightingBase).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is DigitalLogger) //else if (device is DigitalLogger)
{ //{
(device as DigitalLogger).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as DigitalLogger).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is PepperDash.Essentials.Devices.Common.Occupancy.GlsOccupancySensorBaseController) //else if (device is PepperDash.Essentials.Devices.Common.Occupancy.GlsOccupancySensorBaseController)
{ //{
(device as PepperDash.Essentials.Devices.Common.Occupancy.GlsOccupancySensorBaseController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as PepperDash.Essentials.Devices.Common.Occupancy.GlsOccupancySensorBaseController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is StatusSignController) //else if (device is StatusSignController)
{ //{
(device as StatusSignController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as StatusSignController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
else if (device is C2nRthsController) //else if (device is C2nRthsController)
{ //{
(device as C2nRthsController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey); // (device as C2nRthsController).LinkToApi(Eisc, d.JoinStart, d.JoinMapKey);
continue; // continue;
} //}
} }
}
Debug.Console(1, this, "Devices Linked.");
Debug.Console(1, this, "Devices Linked."); });
}); }
}
/// <summary>
/// <summary> /// Adds a join map
/// Adds a join map /// </summary>
/// </summary> /// <param name="deviceKey"></param>
/// <param name="deviceKey"></param> /// <param name="joinMap"></param>
/// <param name="joinMap"></param> public void AddJoinMap(string deviceKey, JoinMapBaseAdvanced joinMap)
public void AddJoinMap(string deviceKey, JoinMapBaseAdvanced joinMap) {
{ if (!JoinMaps.ContainsKey(deviceKey))
if (!JoinMaps.ContainsKey(deviceKey)) {
{ JoinMaps.Add(deviceKey, joinMap);
JoinMaps.Add(deviceKey, joinMap); }
} else
else {
{ Debug.Console(2, this, "Unable to add join map with key '{0}'. Key already exists in JoinMaps dictionary", deviceKey);
Debug.Console(2, this, "Unable to add join map with key '{0}'. Key already exists in JoinMaps dictionary", deviceKey); }
} }
}
/// <summary>
/// <summary> /// Prints all the join maps on this bridge
/// Prints all the join maps on this bridge /// </summary>
/// </summary> public void PrintJoinMaps()
public void PrintJoinMaps() {
{ Debug.Console(0, this, "Join Maps for EISC IPID: {0}", Eisc.ID.ToString("X"));
Debug.Console(0, this, "Join Maps for EISC IPID: {0}", Eisc.ID.ToString("X"));
foreach (var joinMap in JoinMaps)
foreach (var joinMap in JoinMaps) {
{ Debug.Console(0, "Join map for device '{0}':", joinMap.Key);
Debug.Console(0, "Join map for device '{0}':", joinMap.Key); joinMap.Value.PrintJoinMapInfo();
joinMap.Value.PrintJoinMapInfo(); }
} }
}
/// <summary>
/// <summary> /// Prints the join map for a device by key
/// Prints the join map for a device by key /// </summary>
/// </summary> /// <param name="deviceKey"></param>
/// <param name="deviceKey"></param> public void PrintJoinMapForDevice(string deviceKey)
public void PrintJoinMapForDevice(string deviceKey) {
{ var joinMap = JoinMaps[deviceKey];
var joinMap = JoinMaps[deviceKey];
if (joinMap == null)
if (joinMap == null) {
{ Debug.Console(0, this, "Unable to find joinMap for device with key: '{0}'", deviceKey);
Debug.Console(0, this, "Unable to find joinMap for device with key: '{0}'", deviceKey); return;
return; }
}
Debug.Console(0, "Join map for device '{0}' on EISC '{1}':", deviceKey, this.Key);
Debug.Console(0, "Join map for device '{0}' on EISC '{1}':", deviceKey, this.Key); joinMap.PrintJoinMapInfo();
joinMap.PrintJoinMapInfo(); }
}
/// <summary>
/// <summary> /// Used for debugging to trigger an action based on a join number and type
/// Used for debugging to trigger an action based on a join number and type /// </summary>
/// </summary> /// <param name="join"></param>
/// <param name="join"></param> /// <param name="type"></param>
/// <param name="type"></param> public void ExecuteJoinAction(uint join, string type, object state)
public void ExecuteJoinAction(uint join, string type, object state) {
{ try
try {
{ switch (type.ToLower())
switch (type.ToLower()) {
{ case "digital":
case "digital": {
{ var uo = Eisc.BooleanOutput[join].UserObject as Action<bool>;
var uo = Eisc.BooleanOutput[join].UserObject as Action<bool>; if (uo != null)
if (uo != null) {
{ Debug.Console(1, this, "Executing Action: {0}", uo.ToString());
Debug.Console(1, this, "Executing Action: {0}", uo.ToString()); uo(Convert.ToBoolean(state));
uo(Convert.ToBoolean(state)); }
} else
else Debug.Console(1, this, "User Action is null. Nothing to Execute");
Debug.Console(1, this, "User Action is null. Nothing to Execute"); break;
break; }
} case "analog":
case "analog": {
{ var uo = Eisc.BooleanOutput[join].UserObject as Action<ushort>;
var uo = Eisc.BooleanOutput[join].UserObject as Action<ushort>; if (uo != null)
if (uo != null) {
{ Debug.Console(1, this, "Executing Action: {0}", uo.ToString());
Debug.Console(1, this, "Executing Action: {0}", uo.ToString()); uo(Convert.ToUInt16(state));
uo(Convert.ToUInt16(state)); }
} else
else Debug.Console(1, this, "User Action is null. Nothing to Execute"); break;
Debug.Console(1, this, "User Action is null. Nothing to Execute"); break; }
} case "serial":
case "serial": {
{ var uo = Eisc.BooleanOutput[join].UserObject as Action<string>;
var uo = Eisc.BooleanOutput[join].UserObject as Action<string>; if (uo != null)
if (uo != null) {
{ Debug.Console(1, this, "Executing Action: {0}", uo.ToString());
Debug.Console(1, this, "Executing Action: {0}", uo.ToString()); uo(Convert.ToString(state));
uo(Convert.ToString(state)); }
} else
else Debug.Console(1, this, "User Action is null. Nothing to Execute");
Debug.Console(1, this, "User Action is null. Nothing to Execute"); break;
break; }
} default:
default: {
{ Debug.Console(1, "Unknown join type. Use digital/serial/analog");
Debug.Console(1, "Unknown join type. Use digital/serial/analog"); break;
break; }
} }
} }
} catch (Exception e)
catch (Exception e) {
{ Debug.Console(1, this, "Error: {0}", e);
Debug.Console(1, this, "Error: {0}", e); }
}
}
}
/// <summary>
/// <summary> /// Handles incoming sig changes
/// Handles incoming sig changes /// </summary>
/// </summary> /// <param name="currentDevice"></param>
/// <param name="currentDevice"></param> /// <param name="args"></param>
/// <param name="args"></param> void Eisc_SigChange(object currentDevice, Crestron.SimplSharpPro.SigEventArgs args)
void Eisc_SigChange(object currentDevice, Crestron.SimplSharpPro.SigEventArgs args) {
{ try
try {
{ if (Debug.Level >= 1)
if (Debug.Level >= 1) Debug.Console(1, this, "EiscApi change: {0} {1}={2}", args.Sig.Type, args.Sig.Number, args.Sig.StringValue);
Debug.Console(1, this, "EiscApi change: {0} {1}={2}", args.Sig.Type, args.Sig.Number, args.Sig.StringValue); var uo = args.Sig.UserObject;
var uo = args.Sig.UserObject; if (uo != null)
if (uo != null) {
{ Debug.Console(1, this, "Executing Action: {0}", uo.ToString());
Debug.Console(1, this, "Executing Action: {0}", uo.ToString()); if (uo is Action<bool>)
if (uo is Action<bool>) (uo as Action<bool>)(args.Sig.BoolValue);
(uo as Action<bool>)(args.Sig.BoolValue); else if (uo is Action<ushort>)
else if (uo is Action<ushort>) (uo as Action<ushort>)(args.Sig.UShortValue);
(uo as Action<ushort>)(args.Sig.UShortValue); else if (uo is Action<string>)
else if (uo is Action<string>) (uo as Action<string>)(args.Sig.StringValue);
(uo as Action<string>)(args.Sig.StringValue); }
} }
} catch (Exception e)
catch (Exception e) {
{ Debug.Console(2, this, "Error in Eisc_SigChange handler: {0}", e);
Debug.Console(2, this, "Error in Eisc_SigChange handler: {0}", e); }
} }
} }
}
public class EiscApiPropertiesConfig
public class EiscApiPropertiesConfig {
{ [JsonProperty("control")]
[JsonProperty("control")] public EssentialsControlPropertiesConfig Control { get; set; }
public EssentialsControlPropertiesConfig Control { get; set; }
[JsonProperty("devices")]
[JsonProperty("devices")] public List<ApiDevicePropertiesConfig> Devices { get; set; }
public List<ApiDevicePropertiesConfig> Devices { get; set; }
public class ApiDevicePropertiesConfig
public class ApiDevicePropertiesConfig {
{ [JsonProperty("deviceKey")]
[JsonProperty("deviceKey")] public string DeviceKey { get; set; }
public string DeviceKey { get; set; }
[JsonProperty("joinStart")]
[JsonProperty("joinStart")] public uint JoinStart { get; set; }
public uint JoinStart { get; set; }
[JsonProperty("joinMapKey")]
[JsonProperty("joinMapKey")] public string JoinMapKey { get; set; }
public string JoinMapKey { get; set; } }
}
}
}
} }

View File

@@ -1,11 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharpPro.DeviceSupport; using Crestron.SimplSharpPro.DeviceSupport;
namespace PepperDash.Essentials.Bridges namespace PepperDash.Essentials.Core.Bridges
{ {
/// <summary> /// <summary>
/// Defines a device that uses the legacy JoinMapBase for its join map /// Defines a device that uses the legacy JoinMapBase for its join map