From 385686e7e00ef09ce7accf2641f49c07e17a98c2 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Tue, 17 Jul 2018 11:32:38 -0600 Subject: [PATCH] Added Bridge classes back in from on prem repo --- .gitmodules | 2 +- PepperDashEssentials/Bridges/BridgeBase.cs | 210 ++++++++++++++++++ PepperDashEssentials/Bridges/BridgeFactory.cs | 184 +++++++++++++++ 3 files changed, 395 insertions(+), 1 deletion(-) create mode 100644 PepperDashEssentials/Bridges/BridgeBase.cs create mode 100644 PepperDashEssentials/Bridges/BridgeFactory.cs diff --git a/.gitmodules b/.gitmodules index 4c8f4585..2ae0902f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "essentials-framework"] path = essentials-framework - url = https://@bitbucket.org/Pepperdash_Products/essentials-framework.git + url = https://bitbucket.org/Pepperdash_Products/essentials-framework.git diff --git a/PepperDashEssentials/Bridges/BridgeBase.cs b/PepperDashEssentials/Bridges/BridgeBase.cs new file mode 100644 index 00000000..e8536df1 --- /dev/null +++ b/PepperDashEssentials/Bridges/BridgeBase.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharpPro.EthernetCommunication; + +using PepperDash.Core; +using PepperDash.Essentials.Core; +using PepperDash.Essentials.Core.Devices; + +namespace PepperDash.Essentials.Bridges +{ + /// + /// Base class for all bridge class variants + /// + public class BridgeBase : Device + { + public BridgeApi Api { get; private set; } + + public BridgeBase(string key) : + base(key) + { + + } + + } + + /// + /// Base class for bridge API variants + /// + public abstract class BridgeApi : Device + { + public BridgeApi(string key) : + base(key) + { + + } + + } + + /// + /// Bridge API using EISC + /// + public class EiscApi : BridgeApi + { + public ThreeSeriesTcpIpEthernetIntersystemCommunications Eisc { get; private set; } + + + public EiscApi(string key, uint ipid, string hostname) : + base(key) + { + Eisc = new ThreeSeriesTcpIpEthernetIntersystemCommunications(ipid, hostname, Global.ControlSystem); + + Eisc.SigChange += new Crestron.SimplSharpPro.DeviceSupport.SigEventHandler(Eisc_SigChange); + + Eisc.Register(); + } + + /// + /// Handles incoming sig changes + /// + /// + /// + void Eisc_SigChange(object currentDevice, Crestron.SimplSharpPro.SigEventArgs args) + { + if (Debug.Level >= 1) + Debug.Console(1, this, "BridgeApi EISC change: {0} {1}={2}", args.Sig.Type, args.Sig.Number, args.Sig.StringValue); + var uo = args.Sig.UserObject; + if (uo is Action) + (uo as Action)(args.Sig.BoolValue); + else if (uo is Action) + (uo as Action)(args.Sig.UShortValue); + else if (uo is Action) + (uo as Action)(args.Sig.StringValue); + } + } + + + + + /// + /// Defines each type and it's matching API type + /// + public static class DeviceApiFactory + { + public static Dictionary TypeMap = new Dictionary + { + { typeof(DmChassisController), typeof(DmChassisControllerApi) }, + { typeof(IBasicCommunication), typeof(IBasicCommunicationApi) } + //{ typeof(SomeShittyDisplayController), typeof(SomeShittyDisplayControllerApi) } + }; + } + + public class IBasicCommunicationApi : DeviceApiBase + { + public IBasicCommunication Device { get; set; } + + SerialFeedback TextReceivedFeedback; + + public IBasicCommunicationApi(IBasicCommunication dev) + { + TextReceivedFeedback = new SerialFeedback(); + + Device = dev; + + SetupFeedbacks(); + + ActionApi = new Dictionary + { + { "connect", new Action(Device.Connect) }, + { "disconnect", new Action(Device.Disconnect) }, + { "connectstate", new Action( b => ConnectByState(b) ) }, + { "sendtext", new Action( s => Device.SendText(s) ) } + + }; + + FeedbackApi = new Dictionary + { + { "isconnected", new BoolFeedback( () => Device.IsConnected ) }, + { "textrecieved", TextReceivedFeedback } + }; + } + + /// + /// Controls connection based on state of input + /// + /// + void ConnectByState(bool state) + { + if (state) + Device.Connect(); + else + Device.Disconnect(); + } + + void SetupFeedbacks() + { + Device.TextReceived += new EventHandler(Device_TextReceived); + + if(Device is ISocketStatus) + (Device as ISocketStatus).ConnectionChange += new EventHandler(IBasicCommunicationApi_ConnectionChange); + } + + void IBasicCommunicationApi_ConnectionChange(object sender, GenericSocketStatusChageEventArgs e) + { + FeedbackApi["isconnected"].FireUpdate(); + } + + void Device_TextReceived(object sender, GenericCommMethodReceiveTextArgs e) + { + TextReceivedFeedback.FireUpdate(e.Text); + } + } + + + + public class DmChassisController : Device + { + public DmChassisController(string key) + : base(key) + { + + } + + public void SetInput(int input) + { + Debug.Console(2, this, "Dm Chassis {0}, input {1}", Key, input); + } + } + + /// + /// Each flavor of API is a static class with static properties and a static constructor that + /// links up the things to do. + /// + public class DmChassisControllerApi : DeviceApiBase + { + IntFeedback Output1Feedback; + IntFeedback Output2Feedback; + + public DmChassisControllerApi(DmChassisController dev) + { + Output1Feedback = new IntFeedback( new Func(() => 1)); + Output2Feedback = new IntFeedback( new Func(() => 2)); + + ActionApi = new Dictionary + { + + }; + + FeedbackApi = new Dictionary + { + { "Output-1/fb", Output1Feedback }, + { "Output-2/fb", Output2Feedback } + }; + } + + /// + /// Factory method + /// + /// + /// + public static DmChassisControllerApi GetActionApiForDevice(DmChassisController dev) + { + return new DmChassisControllerApi(dev); + } + } + + +} \ No newline at end of file diff --git a/PepperDashEssentials/Bridges/BridgeFactory.cs b/PepperDashEssentials/Bridges/BridgeFactory.cs new file mode 100644 index 00000000..97cc6fce --- /dev/null +++ b/PepperDashEssentials/Bridges/BridgeFactory.cs @@ -0,0 +1,184 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using PepperDash.Essentials.Core; +using PepperDash.Essentials.Core.Config; +using PepperDash.Core; +using PepperDash.Essentials.Core.Routing; +using Crestron.SimplSharpPro; +using Crestron.SimplSharpPro.EthernetCommunication; + +namespace PepperDash.Essentials +{ + public class BridgeFactory + { + public static IKeyed GetDevice(PepperDash.Essentials.Core.Config.DeviceConfig dc) + { + // ? why is this static JTA 2018-06-13? + + var key = dc.Key; + var name = dc.Name; + var type = dc.Type; + var properties = dc.Properties; + var propAnon = new { }; + JsonConvert.DeserializeAnonymousType(dc.Properties.ToString(), propAnon); + + var typeName = dc.Type.ToLower(); + var groupName = dc.Group.ToLower(); + + Debug.Console(0, "Name {0}, Key {1}, Type {2}, Properties {3}", name, key, type, properties.ToString()); + if (typeName == "dm") + { + return new DmBridge(key, name, properties); + } + else if (typeName == "comm") + { + return new CommBridge(key, name, properties); + } + else + return null; + } + } + + public class DmBridge : Device + { + public EiscBridgeProperties Properties { get; private set; } + + public PepperDash.Essentials.DM.DmChassisController DmSwitch { get; private set; } + + public DmBridge(string key, string name, JToken properties) : base(key, name) + { + Properties = JsonConvert.DeserializeObject(properties.ToString()); + } + + public override bool CustomActivate() + { + // Create EiscApis + if (Properties.Eiscs != null) + { + foreach (var eisc in Properties.Eiscs) + { + var ApiEisc = new BridgeApiEisc(eisc.IpId, eisc.Hostname); + + ApiEisc.Eisc.SetUShortSigAction(101, u => DmSwitch.ExecuteSwitch(u,1, eRoutingSignalType.Video)); + ApiEisc.Eisc.SetUShortSigAction(102, u => DmSwitch.ExecuteSwitch(u,2, eRoutingSignalType.Video)); + } + } + + foreach (var device in DeviceManager.AllDevices) + { + if (device.Key == this.Properties.ParentDeviceKey) + { + Debug.Console(0, "deviceKey {0} Matches", device.Key); + DmSwitch = DeviceManager.GetDeviceForKey(device.Key) as PepperDash.Essentials.DM.DmChassisController; + } + else + { + Debug.Console(0, "deviceKey {0} doesn't match", device.Key); + } + } + + Debug.Console(0, "Bridge {0} Activated", this.Name); + return true; + } + } + + public class CommBridge : Device + { + public CommBridgeProperties Properties { get; private set; } + + public List CommDevices { get; private set; } + + public CommBridge(string key, string name, JToken properties) + : base(key, name) + { + Properties = JsonConvert.DeserializeObject(properties.ToString()); + } + + public override bool CustomActivate() + { + // Create EiscApis + if (Properties.Eiscs != null) + { + foreach (var eisc in Properties.Eiscs) + { + var ApiEisc = new BridgeApiEisc(eisc.IpId, eisc.Hostname); + } + } + + foreach (var deviceKey in Properties.CommDevices) + { + var device = DeviceManager.GetDeviceForKey(deviceKey); + + if (device != null) + { + Debug.Console(0, "deviceKey {0} Found in Device Manager", device.Key); + CommDevices.Add(device as IBasicCommunication); + } + else + { + Debug.Console(0, "deviceKey {0} Not Found in Device Manager", deviceKey); + } + } + + // Iterate through all the CommDevices and link up their Actions and Feedbacks + + Debug.Console(0, "Bridge {0} Activated", this.Name); + + return true; + } + } + + + public class EiscBridgeProperties + { + public string ParentDeviceKey { get; set; } + public eApiType ApiType { get; set; } + public List Eiscs { get; set; } + public string ApiOverrideFilePath { get; set; } + + public class EiscProperties + { + public string IpId { get; set; } + public string Hostname { get; set; } + } + } + + public class CommBridgeProperties : EiscBridgeProperties + { + public List CommDevices { get; set; } + } + + public enum eApiType { Eisc = 0 } + + public class BridgeApiEisc + { + public uint Ipid { get; private set; } + public ThreeSeriesTcpIpEthernetIntersystemCommunications Eisc { get; private set; } + + public BridgeApiEisc(string ipid, string hostname) + { + Ipid = (UInt32)int.Parse(ipid, System.Globalization.NumberStyles.HexNumber); + Eisc = new ThreeSeriesTcpIpEthernetIntersystemCommunications(Ipid, hostname, Global.ControlSystem); + Eisc.Register(); + Eisc.SigChange += Eisc_SigChange; + Debug.Console(0, "BridgeApiEisc Created at Ipid {0}", ipid); + } + void Eisc_SigChange(object currentDevice, Crestron.SimplSharpPro.SigEventArgs args) + { + if (Debug.Level >= 1) + Debug.Console(1, "BridgeApiEisc change: {0} {1}={2}", args.Sig.Type, args.Sig.Number, args.Sig.StringValue); + var uo = args.Sig.UserObject; + if (uo is Action) + (uo as Action)(args.Sig.BoolValue); + else if (uo is Action) + (uo as Action)(args.Sig.UShortValue); + else if (uo is Action) + (uo as Action)(args.Sig.StringValue); + } + } +} \ No newline at end of file