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) } }; } /// /// API class for IBasicCommunication devices /// 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); } } }