using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharpPro; using Crestron.SimplSharpPro.DeviceSupport; using PepperDash.Core; using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.Bridges; using Newtonsoft.Json; using Serilog.Events; namespace PepperDash.Essentials.Core.CrestronIO { /// /// Represents a generic digital input deviced tied to a versiport /// public class GenericVersiportDigitalOutputDevice : EssentialsBridgeableDevice, IDigitalOutput { public Versiport OutputPort { get; private set; } public BoolFeedback OutputStateFeedback { get; private set; } Func OutputStateFeedbackFunc { get { return () => OutputPort.DigitalOut; } } public GenericVersiportDigitalOutputDevice(string key, string name, Func postActivationFunc, IOPortConfig config) : base(key, name) { OutputStateFeedback = new BoolFeedback(OutputStateFeedbackFunc); AddPostActivationAction(() => { OutputPort = postActivationFunc(config); OutputPort.Register(); if (!OutputPort.SupportsDigitalOutput) { Debug.LogMessage(LogEventLevel.Information, this, "Device does not support configuration as a Digital Output"); return; } OutputPort.SetVersiportConfiguration(eVersiportConfiguration.DigitalOutput); OutputPort.VersiportChange += OutputPort_VersiportChange; }); } void OutputPort_VersiportChange(Versiport port, VersiportEventArgs args) { Debug.LogMessage(LogEventLevel.Debug, this, "Versiport change: {0}", args.Event); if(args.Event == eVersiportEvent.DigitalOutChange) OutputStateFeedback.FireUpdate(); } /// /// Set value of the versiport digital output /// /// value to set the output to public void SetOutput(bool state) { if (OutputPort.SupportsDigitalOutput) { Debug.LogMessage(LogEventLevel.Information, this, "Passed the Check"); OutputPort.DigitalOut = state; } else { Debug.LogMessage(LogEventLevel.Information, this, "Versiport does not support Digital Output Mode"); } } #region Bridge Linking public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) { var joinMap = new IDigitalOutputJoinMap(joinStart); var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey); if (!string.IsNullOrEmpty(joinMapSerialized)) joinMap = JsonConvert.DeserializeObject(joinMapSerialized); if (bridge != null) { bridge.AddJoinMap(Key, joinMap); } else { Debug.LogMessage(LogEventLevel.Information, this, "Please update config to use 'eiscapiadvanced' to get all join map features for this device."); } try { Debug.LogMessage(LogEventLevel.Debug, this, "Linking to Trilist '{0}'", trilist.ID.ToString("X")); // Link feedback for input state OutputStateFeedback.LinkInputSig(trilist.BooleanInput[joinMap.OutputState.JoinNumber]); trilist.SetBoolSigAction(joinMap.OutputState.JoinNumber, SetOutput); } catch (Exception e) { Debug.LogMessage(LogEventLevel.Debug, this, "Unable to link device '{0}'. Input is null", Key); Debug.LogMessage(LogEventLevel.Debug, this, "Error: {0}", e); } } #endregion public static Versiport GetVersiportDigitalOutput(IOPortConfig dc) { IIOPorts ioPortDevice; if (dc.PortDeviceKey.Equals("processor")) { if (!Global.ControlSystem.SupportsVersiport) { Debug.LogMessage(LogEventLevel.Information, "GetVersiportDigitalOuptut: Processor does not support Versiports"); return null; } ioPortDevice = Global.ControlSystem; } else { var ioPortDev = DeviceManager.GetDeviceForKey(dc.PortDeviceKey) as IIOPorts; if (ioPortDev == null) { Debug.LogMessage(LogEventLevel.Information, "GetVersiportDigitalOuptut: Device {0} is not a valid device", dc.PortDeviceKey); return null; } ioPortDevice = ioPortDev; } if (ioPortDevice == null) { Debug.LogMessage(LogEventLevel.Information, "GetVersiportDigitalOuptut: Device '0' is not a valid IOPorts Device", dc.PortDeviceKey); return null; } if (dc.PortNumber > ioPortDevice.NumberOfVersiPorts) { Debug.LogMessage(LogEventLevel.Information, "GetVersiportDigitalOuptut: Device {0} does not contain a port {1}", dc.PortDeviceKey, dc.PortNumber); } var port = ioPortDevice.VersiPorts[dc.PortNumber]; return port; } } public class GenericVersiportDigitalOutputDeviceFactory : EssentialsDeviceFactory { public GenericVersiportDigitalOutputDeviceFactory() { TypeNames = new List() { "versiportoutput" }; } public override EssentialsDevice BuildDevice(DeviceConfig dc) { Debug.LogMessage(LogEventLevel.Debug, "Factory Attempting to create new Generic Versiport Device"); var props = JsonConvert.DeserializeObject(dc.Properties.ToString()); if (props == null) return null; var portDevice = new GenericVersiportDigitalOutputDevice(dc.Key, dc.Name, GenericVersiportDigitalOutputDevice.GetVersiportDigitalOutput, props); return portDevice; } } }