using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using PepperDash.Core; using PepperDash.Essentials.Core.Config; using Newtonsoft.Json; namespace PepperDash.Essentials.Core { public static class JoinMapHelper { /// /// Attempts to get the serialized join map from config /// /// /// public static string GetJoinMapForDevice(string joinMapKey) { if (string.IsNullOrEmpty(joinMapKey)) return null; var joinMap = ConfigReader.ConfigObject.JoinMaps[joinMapKey]; if (joinMap != null) { return joinMap; } else return null; } } public abstract class JoinMapBase { /// /// Modifies all the join numbers by adding the offset. This should never be called twice /// /// public abstract void OffsetJoinNumbers(uint joinStart); /// /// The collection of joins and associated metadata /// public Dictionary Joins = new Dictionary(); /// /// Prints the join information to console /// public void PrintJoinMapInfo() { Debug.Console(0, "{0}:\n", this.GetType().Name); // Get the joins of each type and print them Debug.Console(0, "Digitals:"); var digitals = Joins.Where(j => (j.Value.JoinType & eJoinType.Digital) == eJoinType.Digital).ToDictionary(j => j.Key, j => j.Value); PrintJoinList(GetSortedJoins(digitals)); Debug.Console(0, "Analogs:"); var analogs = Joins.Where(j => (j.Value.JoinType & eJoinType.Analog) == eJoinType.Analog).ToDictionary(j => j.Key, j => j.Value); PrintJoinList(GetSortedJoins(analogs)); Debug.Console(0, "Serials:"); var serials = Joins.Where(j => (j.Value.JoinType & eJoinType.Serial) == eJoinType.Serial).ToDictionary(j => j.Key, j => j.Value); PrintJoinList(GetSortedJoins(serials)); } /// /// Returns a sorted list by JoinNumber /// /// /// List> GetSortedJoins(Dictionary joins) { var sortedJoins = joins.ToList(); sortedJoins.Sort((pair1, pair2) => pair1.Value.JoinNumber.CompareTo(pair2.Value.JoinNumber)); return sortedJoins; } void PrintJoinList(List> joins) { foreach (var join in joins) { Debug.Console(0, @"Join Number: {0} | Label: '{1}' | JoinSpan: '{2}' | Type: '{3}' | Capabilities: '{4}'", join.Value.JoinNumber, join.Value.Label, join.Value.JoinSpan, join.Value.JoinType.ToString(), join.Value.JoinCapabilities.ToString()); } } /// /// Returns the join number for the join with the specified key /// /// /// public uint GetJoinForKey(string key) { if (Joins.ContainsKey(key)) return Joins[key].JoinNumber; else return 0; } /// /// Returns the join span for the join with the specified key /// /// /// public uint GetJoinSpanForKey(string key) { if (Joins.ContainsKey(key)) return Joins[key].JoinSpan; else return 0; } } /// /// Read = Provides feedback to SIMPL /// Write = Responds to sig values from SIMPL /// [Flags] public enum eJoinCapabilities { None = 0, ToSIMPL = 1, FromSIMPL = 2, ToFromSIMPL = ToSIMPL | FromSIMPL } [Flags] public enum eJoinType { None = 0, Digital = 1, Analog = 2, Serial = 4, DigitalAnalog = Digital | Analog, DigitalSerial = Digital | Serial, AnalogSerial = Analog | Serial, DigitalAnalogSerial = Digital | Analog | Serial } /// /// Data describing the join /// public class JoinMetadata { /// /// A label for the join to better describe it's usage /// [JsonProperty("label")] public string Label { get; set; } /// /// Signal type(s) /// [JsonProperty("joinType")] public eJoinType JoinType { get; set; } /// /// Join number (based on join offset value) /// [JsonProperty("joinNumber")] public uint JoinNumber { get; set; } /// /// Join range span. If join indicates the start of a range of joins, this indicated the maximum number of joins in the range /// [JsonProperty("joinSpan")] public uint JoinSpan { get; set; } /// /// Indicates whether the join is read and/or write /// [JsonProperty("joinCapabilities")] public eJoinCapabilities JoinCapabilities { get; set; } } public enum eBiologicalSex { Unknown = 0, Female = 1, Male = 2, Intersex = Male | Female } public class Human { eBiologicalSex BiologicalSex; public bool IsMale { get { return (BiologicalSex & eBiologicalSex.Male) == eBiologicalSex.Male; } } public bool IsFemale { get { return (BiologicalSex & eBiologicalSex.Female) == eBiologicalSex.Female; } } public bool IsIntersex { get { return (BiologicalSex & eBiologicalSex.Intersex) == eBiologicalSex.Intersex; } } public bool IsDeservingOfBasicHumanRights { get { return this is Human; } } } }