using System; using System.Collections.Generic; using Crestron.SimplSharpPro; using Crestron.SimplSharpPro.DeviceSupport; using Newtonsoft.Json; using PepperDash.Core; using PepperDash.Core.Logging; using PepperDash.Essentials.Core.Bridges; using PepperDash.Essentials.Core.Config; namespace PepperDash.Essentials.Core.CrestronIO { /// /// Represents a generic digital input deviced tied to a versiport /// public class GenericVersiportDigitalInputDevice : EssentialsBridgeableDevice, IDigitalInput, IPartitionStateProvider, IHasFeedback { private Versiport inputPort; /// /// Gets or sets the InputStateFeedback /// public BoolFeedback InputStateFeedback { get; private set; } /// public FeedbackCollection Feedbacks { get; private set; } = new FeedbackCollection(); /// /// Gets or sets the PartitionPresentFeedback /// public BoolFeedback PartitionPresentFeedback { get; } /// /// Get partition state /// public bool PartitionPresent => !inputPort.DigitalIn; /// /// Initializes a new instance of the class. /// /// key for device /// name for device /// function to call after activation. Should return the Versiport /// config for device public GenericVersiportDigitalInputDevice(string key, string name, Func postActivationFunc, IOPortConfig config) : base(key, name) { InputStateFeedback = new BoolFeedback("inputState", () => inputPort.DigitalIn); PartitionPresentFeedback = new BoolFeedback("partitionPresent", () => !inputPort.DigitalIn); AddPostActivationAction(() => { inputPort = postActivationFunc(config); inputPort.Register(); inputPort.SetVersiportConfiguration(eVersiportConfiguration.DigitalInput); if (config.DisablePullUpResistor) inputPort.DisablePullUpResistor = true; inputPort.VersiportChange += InputPort_VersiportChange; InputStateFeedback.FireUpdate(); PartitionPresentFeedback.FireUpdate(); this.LogDebug("Created GenericVersiportDigitalInputDevice for port {port}. DisablePullUpResistor: {pullUpResistorDisable}", config.PortNumber, inputPort.DisablePullUpResistor); }); Feedbacks.Add(InputStateFeedback); Feedbacks.Add(PartitionPresentFeedback); } void InputPort_VersiportChange(Versiport port, VersiportEventArgs args) { this.LogDebug("Versiport change: {0}", args.Event); if (args.Event == eVersiportEvent.DigitalInChange) { InputStateFeedback.FireUpdate(); PartitionPresentFeedback.FireUpdate(); } } #region Bridge Linking /// /// LinkToApi method /// /// public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) { var joinMap = new IDigitalInputJoinMap(joinStart); var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey); if (!string.IsNullOrEmpty(joinMapSerialized)) joinMap = JsonConvert.DeserializeObject(joinMapSerialized); if (bridge != null) { bridge.AddJoinMap(Key, joinMap); } else { this.LogWarning("Please update config to use 'eiscapiadvanced' to get all join map features for this device."); } try { this.LogDebug("Linking to Trilist '{0}'", trilist.ID.ToString("X")); // Link feedback for input state InputStateFeedback.LinkInputSig(trilist.BooleanInput[joinMap.InputState.JoinNumber]); } catch (Exception e) { this.LogError("Unable to link device {key}. Input is null. {message}", Key, e.Message); this.LogDebug(e, "Stack Trace: "); } } #endregion /// /// GetVersiportDigitalInput method /// public static Versiport GetVersiportDigitalInput(IOPortConfig dc) { if (dc.PortDeviceKey.Equals("processor")) { if (!Global.ControlSystem.SupportsVersiport) { Debug.LogError("GetVersiportDigitalInput: Processor does not support Versiports"); return null; } return Global.ControlSystem.VersiPorts[dc.PortNumber]; } if (!(DeviceManager.GetDeviceForKey(dc.PortDeviceKey) is IIOPorts ioPortDevice)) { Debug.LogError("GetVersiportDigitalInput: Device {key} is not a valid device", dc.PortDeviceKey); return null; } if (dc.PortNumber > ioPortDevice.NumberOfVersiPorts) { Debug.LogError("GetVersiportDigitalInput: Device {key} does not contain versiport {port}", dc.PortDeviceKey, dc.PortNumber); return null; } return ioPortDevice.VersiPorts[dc.PortNumber]; } } /// /// Factory class for GenericVersiportDigitalInputDevice /// public class GenericVersiportDigitalInputDeviceFactory : EssentialsDeviceFactory { /// /// Constructor for GenericVersiportDigitalInputDeviceFactory /// public GenericVersiportDigitalInputDeviceFactory() { TypeNames = new List() { "versiportinput" }; } /// public override EssentialsDevice BuildDevice(DeviceConfig dc) { Debug.LogDebug("Factory Attempting to create new Generic Versiport Device"); var props = JsonConvert.DeserializeObject(dc.Properties.ToString()); if (props == null) return null; var portDevice = new GenericVersiportDigitalInputDevice(dc.Key, dc.Name, GenericVersiportDigitalInputDevice.GetVersiportDigitalInput, props); return portDevice; } } }