using System; using System.Collections.Generic; using System.Linq; namespace DatabaseImport { public class TestGraph //: BasePropertyChanged { private const string SEPARATOR = "§"; public void SetThresholdsFromSQL(string sThresholds) { //for now we take an easy parse sThresholds = sThresholds.Replace("{", "").Replace("}", ""); var sTokens = sThresholds.Split(new[] { SEPARATOR }, StringSplitOptions.None); foreach (var token in sTokens) { double d; if (double.TryParse(token, out d)) { _thresholds.Add(d); } } } public void SetChannelsFromSQL(string sChannels) { var lookup = new Dictionary(); if (null == AvailableChannels) { return; } foreach (var channel in AvailableChannels) { lookup[channel.GetGraphId()] = channel; } sChannels = sChannels.Replace("{", "").Replace("}", ""); var tokens = sChannels.Split(new[] { SEPARATOR }, StringSplitOptions.None); foreach (var token in tokens) { var sChannelId = token.Replace("(", "").Replace(")", ""); if (lookup.ContainsKey(sChannelId)) { AddChannel(lookup[sChannelId]); } } } private List _channels = new List(); private readonly Dictionary _lookup = new Dictionary(); private void AddGroup(TestTestObject group) { _groups.Add(group); } private void AddAddedGroup(TestTestObject addedGroup) { _addedGroups.Add(addedGroup); } private void AddChannel(TestObjectChannel channel) { _channels.Add(channel); _lookup[channel.GetGraphId()] = channel; //OnPropertyChanged("Channels"); //OnPropertyChanged("AvailableChannels"); } private bool ContainsChannel(TestObjectChannel channel) { return _lookup.ContainsKey(channel.GetGraphId()); } private bool ContainsCurrentChannel(TestObjectChannel channel) { if (channel.SquibChannelType == TestObjectChannel.SquibChannelTypes.Current) { return ContainsChannel(channel); } else { return _lookup.ContainsKey(channel.GetGraphId() + DTS.Common.Constants.CURRENT_SUFFIX); } } private bool ContainsVoltageChannel(TestObjectChannel channel) { return ContainsChannel(channel); } private const int MAX_CHANNELS_PER_GRAPH = 8; public TestObjectChannel[] AvailableChannels { get { _lookup.Clear(); var channels = new List(); if (_channels.Count >= MAX_CHANNELS_PER_GRAPH) return channels.ToArray(); foreach (var setChannel in _channels) { _lookup[setChannel.GetGraphId()] = setChannel; } if (_groups.Exists(to => !AddChannels(to, channels))) { return null; } if (_addedGroups.Where(to => to.Hardware.Any()).Any(to => !AddChannels(to, channels))) { return null; } channels.Sort(); return channels.ToArray(); } } private bool AddChannels(TestTestObject to, List channels) { var isoTestObject = to.GetISOTestObject(); if (null == isoTestObject) { return false; } foreach (var channel in isoTestObject.AllChannels) { if (!channel.Required || channel.Disabled) { continue; } if (string.IsNullOrWhiteSpace(channel.SensorSerialNumber)) { continue; } var sd = SensorsCollection.SensorsList.GetSensorBySerialNumber(channel.SensorSerialNumber); if (null == sd || !sd.IsDigitalOutput()) { if ((sd != null) && (sd.Bridge == Test.Module.Channel.Sensor.BridgeType.SQUIB)) { //Put squib channels (Current and/or Voltage) in the //Available list if and only if they are not in the graph if (!ContainsCurrentChannel(channel)) { var currentChannel = new TestObjectChannel(channel, isoTestObject, new TestObjectTemplate().ToISOTestObjectTemplate()) { SquibChannelType = TestObjectChannel.SquibChannelTypes.Current, NameOfTheChannel = channel.Name + " " + "Graph_SquibCurrent" }; channels.Add(currentChannel); } if (!ContainsVoltageChannel(channel)) { channel.SquibChannelType = TestObjectChannel.SquibChannelTypes.Voltage; channel.NameOfTheChannel = channel.Name + " " + "Graph_SquibVoltage"; channels.Add(channel); } } else { if (ContainsChannel(channel)) { continue; } channels.Add(channel); } } } return true; } public string GraphName { get; set; } = ""; public string GraphDescription { get; set; } = ""; public bool UseDomainMin { get; set; } public double DomainMin { get; set; } = double.MinValue; public bool UseDomainMax { get; set; } public double DomainMax { get; set; } = double.MaxValue; public bool UseRangeMin { get; set; } public double RangeMin { get; set; } = double.MinValue; public bool UseRangeMax { get; set; } public double RangeMax { get; set; } = double.MaxValue; private List _thresholds = new List(); private readonly TestTemplate _template; private List _groups = new List(); private List _addedGroups = new List(); public TestGraph(TestTemplate template) { _template = template; foreach (var group in _template.TestObjectsWithChannels) { AddGroup(new TestTestObject(group)); } foreach (var addedGroup in _template.AddedGroups) { AddAddedGroup(new TestTestObject(addedGroup)); } } } }