// Gets the series info data for the series with the given identifier. // Optimized to prevent loading series data for irrelevant channels. private Series GetSeriesInfo(Meter meter, int seriesID) { List <Channel> channels = meter.Channels; // Disable lazy loading var connectionFactory = meter.ConnectionFactory; meter.ConnectionFactory = null; // Search for a series that has already been loaded Series seriesInfo = channels .Where(channel => (object)channel.Series != null) .SelectMany(channel => channel.Series) .FirstOrDefault(series => series.ID == seriesID); // Restore lazy loading meter.ConnectionFactory = connectionFactory; // If the series was found without // lazy loading, return that series if ((object)seriesInfo != null) { return(seriesInfo); } int channelID; using (AdoDataConnection connection = meter.ConnectionFactory()) { // Get the channel ID of the series so we can skip lazy loading series collections for irrelevant channels channelID = connection.ExecuteScalar <int>("SELECT ChannelID FROM Series WHERE ID = {0}", seriesID); } // Now filter to the appropriate channel and search its series collection return(channels .Where(channel => channel.ID == channelID) .SelectMany(channel => channel.Series) .FirstOrDefault(series => series.ID == seriesID)); }
// Static Methods private static Series GetSeriesInfo(Meter meter, DataGroup dataGroup, string measurementTypeName, string phaseName) { int lineID = dataGroup.Line.ID; string measurementCharacteristicName = "Instantaneous"; string seriesTypeName = "Values"; char typeDesignation = (measurementTypeName == "Current") ? 'I' : measurementTypeName[0]; string phaseDesignation = (phaseName == "RES") ? "R" : phaseName.TrimEnd('N'); string channelName = string.Concat(typeDesignation, phaseDesignation); ChannelKey channelKey = new ChannelKey(lineID, 0, channelName, measurementTypeName, measurementCharacteristicName, phaseName); SeriesKey seriesKey = new SeriesKey(channelKey, seriesTypeName); Series dbSeries = meter.Channels .SelectMany(channel => channel.Series) .FirstOrDefault(series => seriesKey.Equals(new SeriesKey(series))); if ((object)dbSeries == null) { using (AdoDataConnection connection = meter.ConnectionFactory()) { Channel dbChannel = meter.Channels .FirstOrDefault(channel => channelKey.Equals(new ChannelKey(channel))); if ((object)dbChannel == null) { TableOperations <Channel> channelTable = new TableOperations <Channel>(connection); TableOperations <MeasurementType> measurementTypeTable = new TableOperations <MeasurementType>(connection); TableOperations <MeasurementCharacteristic> measurementCharacteristicTable = new TableOperations <MeasurementCharacteristic>(connection); TableOperations <Phase> phaseTable = new TableOperations <Phase>(connection); MeasurementType measurementType = measurementTypeTable.GetOrAdd(measurementTypeName); MeasurementCharacteristic measurementCharacteristic = measurementCharacteristicTable.GetOrAdd(measurementCharacteristicName); Phase phase = phaseTable.GetOrAdd(phaseName); dbChannel = new Channel() { MeterID = meter.ID, LineID = lineID, MeasurementTypeID = measurementType.ID, MeasurementCharacteristicID = measurementCharacteristic.ID, PhaseID = phase.ID, Name = channelKey.Name, SamplesPerHour = dataGroup.SamplesPerHour, Description = string.Concat(measurementCharacteristicName, " ", measurementTypeName, " ", phaseName), Enabled = true }; channelTable.AddNewRecord(dbChannel); dbChannel.ID = connection.ExecuteScalar <int>("SELECT @@IDENTITY"); meter.Channels = null; } TableOperations <Series> seriesTable = new TableOperations <Series>(connection); TableOperations <SeriesType> seriesTypeTable = new TableOperations <SeriesType>(connection); SeriesType seriesType = seriesTypeTable.GetOrAdd(seriesTypeName); dbSeries = new Series() { ChannelID = dbChannel.ID, SeriesTypeID = seriesType.ID, SourceIndexes = string.Empty }; seriesTable.AddNewRecord(dbSeries); dbSeries.ID = connection.ExecuteScalar <int>("SELECT @@IDENTITY"); dbChannel.Series = null; dbSeries = meter.Channels .SelectMany(channel => channel.Series) .First(series => seriesKey.Equals(new SeriesKey(series))); } } return(dbSeries); }
private Series GetSeriesInfo(Meter meter, ChannelKey channelKey, SeriesKey seriesKey) { Series dbSeries = meter.Channels .SelectMany(channel => channel.Series) .FirstOrDefault(series => seriesKey.Equals(new SeriesKey(series))); if ((object)dbSeries == null) { using (AdoDataConnection connection = meter.ConnectionFactory()) { Channel dbChannel = meter.Channels .FirstOrDefault(channel => channelKey.Equals(new ChannelKey(channel))); if ((object)dbChannel == null) { TableOperations <Channel> channelTable = new TableOperations <Channel>(connection); TableOperations <MeasurementType> measurementTypeTable = new TableOperations <MeasurementType>(connection); TableOperations <MeasurementCharacteristic> measurementCharacteristicTable = new TableOperations <MeasurementCharacteristic>(connection); TableOperations <Phase> phaseTable = new TableOperations <Phase>(connection); MeasurementType measurementType = measurementTypeTable.GetOrAdd(channelKey.MeasurementType); MeasurementCharacteristic measurementCharacteristic = measurementCharacteristicTable.GetOrAdd(channelKey.MeasurementCharacteristic); Phase phase = phaseTable.GetOrAdd(channelKey.Phase); dbChannel = new Channel() { MeterID = meter.ID, LineID = channelKey.LineID, MeasurementTypeID = measurementType.ID, MeasurementCharacteristicID = measurementCharacteristic.ID, PhaseID = phase.ID, Name = channelKey.Name, SamplesPerHour = 0, Description = string.Concat(channelKey.MeasurementCharacteristic, " ", channelKey.MeasurementType, " ", channelKey.Phase), Enabled = true }; channelTable.AddNewRecord(dbChannel); dbChannel.ID = connection.ExecuteScalar <int>("SELECT @@IDENTITY"); meter.Channels = null; } TableOperations <Series> seriesTable = new TableOperations <Series>(connection); TableOperations <SeriesType> seriesTypeTable = new TableOperations <SeriesType>(connection); SeriesType seriesType = seriesTypeTable.GetOrAdd(seriesKey.SeriesType); dbSeries = new Series() { ChannelID = dbChannel.ID, SeriesTypeID = seriesType.ID, SourceIndexes = string.Empty }; seriesTable.AddNewRecord(dbSeries); dbSeries.ID = connection.ExecuteScalar <int>("SELECT @@IDENTITY"); dbChannel.Series = null; dbSeries = meter.Channels .SelectMany(channel => channel.Series) .First(series => seriesKey.Equals(new SeriesKey(series))); } } return(dbSeries); }