/// <summary> /// Group the channels we're modifying together based the value and field that contains an existing limit value. /// </summary> /// <param name="channels">The list of channels to group.</param> /// <param name="parameters">The parameters to add.</param> /// <returns></returns> private List <ChannelLimitAnalysis> GetGroupedLimitChannels(ICollection <Channel> channels, ChannelParameter[] parameters) { Debug.Assert(channels.Select(c => c.Id).Distinct().Count() == 1, $"Channels containing more than ID were passed to {nameof(GetGroupedLimitChannels)}"); var analyzer = new ChannelLimitAnalyzer(new List <Channel>(channels)); var list = new List <ChannelLimitAnalysis>(); //Try and get the limit property that has the most amount of channels associated with it out of all remaining channels. //As we remove channels from the analyzer, each property will update the channels that should be associated with it, //eventually leaving us with the standalone channels that did not share any of their limit property values with any //other channel. while (analyzer.Channels.Count > 0) { var record = analyzer.GetProperty(); if (record.Channels == null) { break; } list.Add(record); foreach (var r in record.Channels) { analyzer.Channels.Remove(r); } } ThrowIfAnalyzerContainsRemainingChannels(analyzer, channels, parameters); return(list); }
/// <summary> /// Group the channels we're modifying together based the value and field that contains an existing limit value. /// </summary> /// <param name="channels">The list of channels to group.</param> /// <param name="channelId">The ID of all of the channels.</param> /// <param name="parameters">The parameters to add.</param> /// <returns></returns> private List <Tuple <double?, List <Channel>, ChannelProperty> > GetGroupedChannels(List <Channel> channels, int channelId, ChannelParameter[] parameters) { var analyzer = new ChannelLimitAnalyzer(new List <Channel>(channels)); var list = new List <Tuple <double?, List <Channel>, ChannelProperty> >(); while (analyzer.Channels.Count > 0) { var record = analyzer.GetProperty(); if (record.Item2 == null) { break; } list.Add(record); foreach (var r in record.Item2) { analyzer.Channels.Remove(r); } } if (analyzer.Channels.Count > 0) { var sensorIdBuilder = new StringBuilder(); for (var i = 0; i < analyzer.Channels.Count; i++) { sensorIdBuilder.Append(analyzer.Channels[i].SensorId); if (i < analyzer.Channels.Count - 2) { sensorIdBuilder.Append(", "); } else if (i == analyzer.Channels.Count - 2) { sensorIdBuilder.Append(" and "); } } var sensorIds = sensorIdBuilder.ToString(); var builder = new StringBuilder(); var propertyNames = string.Join(", ", parameters.Select(p => $"'{p.Property}'")); var values = string.Join(", ", parameters.Select(p => $"'{p.Value}'")); if (parameters.Length > 1) { builder.Append($"Cannot set properties {propertyNames} to values {values} for Channel ID {channelId}: "); } else { builder.Append($"Cannot set property {propertyNames} to value {values} for Channel ID {channelId}: "); } builder.Append($"Cannot set property {propertyNames} to value {values} for Channel ID {channelId}: "); if (analyzer.Channels.Count > 1) { builder.Append($"Sensor IDs {sensorIds} do not have a limit value defined on them. "); } else { builder.Append($"Sensor ID {sensorIds} does not have a limit value defined on it. "); } builder.Append($"Please set one of '{nameof(ChannelProperty.UpperErrorLimit)}', '{nameof(ChannelProperty.LowerErrorLimit)}', '{nameof(ChannelProperty.UpperWarningLimit)}' or '{nameof(ChannelProperty.LowerWarningLimit)}' first and then try again."); throw new InvalidOperationException(builder.ToString()); } return(list); }