public Object GetData(IDataReader reader) { // This is where we define the mapping between the object properties and the // data columns. The convention that should be used is that the object property // names are exactly the same as the column names. However if there is some // compelling reason for the names to be different, the mapping can be defined here. // We assume the reader has data and is already on the row that contains the data // we need. We don't need to call read. As a general rule, assume that every field must // be null checked. If a field is null then the nullvalue for that field has already // been set by the DTO constructor, we don't have to change it. if (!_isInitialized) { InitializeMapper(reader); } // Now we can load the data into the DTO object from the DB reader MeterAsset dto = new MeterAsset(); if (!reader.IsDBNull(_ordinal_MeterID)) { dto.MeterID = Convert.ToInt32(reader[_ordinal_MeterID]); } if (!reader.IsDBNull(_ordinal_AreaID)) { dto.AreaID_Internal = Convert.ToInt32(reader[_ordinal_AreaID]); } if (!reader.IsDBNull(_ordinal_LibertyArea)) { dto.AreaID_Liberty = Convert.ToInt32(reader[_ordinal_LibertyArea]); } if (!reader.IsDBNull(_ordinal_ClusterID)) { dto.PAMClusterID = Convert.ToInt32(reader[_ordinal_ClusterID]); } if (!reader.IsDBNull(_ordinal_MeterName)) { dto.MeterName = Convert.ToString(reader[_ordinal_MeterName]); } if (!reader.IsDBNull(_ordinal_Description)) { dto.MeterDescription = Convert.ToString(reader[_ordinal_Description]); } if (!reader.IsDBNull(_ordinal_MeterGroup)) { dto.MeterGroupID = Convert.ToInt32(reader[_ordinal_MeterGroup]); } if (!reader.IsDBNull(_ordinal_MeterGroupDesc)) { dto.MeterGroupDesc = Convert.ToString(reader[_ordinal_MeterGroupDesc]); } if (!reader.IsDBNull(_ordinal_MeterType)) { dto.MeterTypeID = Convert.ToInt32(reader[_ordinal_MeterType]); } if (!reader.IsDBNull(_ordinal_Latitude)) { dto.Latitude = Convert.ToSingle(reader[_ordinal_Latitude]); } if (!reader.IsDBNull(_ordinal_Longitude)) { dto.Longitude = Convert.ToSingle(reader[_ordinal_Longitude]); } return(dto); }
private Meter ToMeter(XDocument sheet) { double?ToNullableDouble(string str) { if (string.IsNullOrEmpty(str)) { return(null); } return(Convert.ToDouble(str)); } XElement rootElement = sheet.Root; Meter meter = new Meter() { MeterAssets = new List <MeterAsset>(), Channels = new List <Channel>() }; XElement meterElement = rootElement.Element("meter"); meter.ID = Convert.ToInt32((string)meterElement.Element(nameof(meter.ID))); meter.AssetKey = (string)meterElement.Element(nameof(meter.AssetKey)); meter.LocationID = Convert.ToInt32((string)meterElement.Element(nameof(meter.LocationID))); meter.Name = (string)meterElement.Element(nameof(meter.Name)); meter.Alias = (string)meterElement.Element(nameof(meter.Alias)); meter.ShortName = (string)meterElement.Element(nameof(meter.ShortName)); meter.Make = (string)meterElement.Element(nameof(meter.Make)); meter.Model = (string)meterElement.Element(nameof(meter.Model)); meter.TimeZone = (string)meterElement.Element(nameof(meter.TimeZone)); meter.Description = (string)meterElement.Element(nameof(meter.Description)); Location location = new Location() { Meters = new List <Meter>(), AssetLocations = new List <AssetLocation>() }; XElement meterLocationElement = rootElement.Element("location"); location.ID = Convert.ToInt32((string)meterLocationElement.Element(nameof(location.ID))); location.LocationKey = (string)meterLocationElement.Element(nameof(location.LocationKey)); location.Name = (string)meterLocationElement.Element(nameof(location.Name)); location.Alias = (string)meterLocationElement.Element(nameof(location.Alias)); location.ShortName = (string)meterLocationElement.Element(nameof(location.ShortName)); location.Latitude = Convert.ToDouble((string)meterLocationElement.Element(nameof(location.Latitude))); location.Longitude = Convert.ToDouble((string)meterLocationElement.Element(nameof(location.Longitude))); location.Description = (string)meterLocationElement.Element(nameof(location.Description)); location.Meters.Add(meter); meter.Location = location; // This needs to be adjusted to work for Assets of all Types Dictionary <int, Asset> assets = new Dictionary <int, Asset>(); foreach (XElement breakerElement in rootElement.Elements("breaker")) { Breaker breaker = new Breaker() { AssetLocations = new List <AssetLocation>(), MeterAssets = new List <MeterAsset>(), DirectChannels = new List <Channel>(), Connections = new List <AssetConnection>(), AssetTypeID = (int)AssetType.Breaker }; breaker.VoltageKV = Convert.ToDouble((string)breakerElement.Element(nameof(breaker.VoltageKV))); breaker.ThermalRating = Convert.ToDouble((string)breakerElement.Element(nameof(breaker.ThermalRating))); breaker.Speed = Convert.ToDouble((string)breakerElement.Element(nameof(breaker.Speed))); breaker.ID = Convert.ToInt32((string)breakerElement.Element(nameof(breaker.ID))); breaker.AssetKey = (string)breakerElement.Element(nameof(breaker.AssetKey)); breaker.Description = (string)breakerElement.Element(nameof(breaker.Description)); breaker.AssetName = (string)breakerElement.Element(nameof(breaker.AssetName)); assets.Add(breaker.ID, breaker); } foreach (XElement busElement in rootElement.Elements("bus")) { Bus bus = new Bus() { AssetLocations = new List <AssetLocation>(), MeterAssets = new List <MeterAsset>(), DirectChannels = new List <Channel>(), Connections = new List <AssetConnection>(), AssetTypeID = (int)AssetType.Bus }; bus.VoltageKV = Convert.ToDouble((string)busElement.Element(nameof(bus.VoltageKV))); bus.ID = Convert.ToInt32((string)busElement.Element(nameof(bus.ID))); bus.AssetKey = (string)busElement.Element(nameof(bus.AssetKey)); bus.Description = (string)busElement.Element(nameof(bus.Description)); bus.AssetName = (string)busElement.Element(nameof(bus.AssetName)); assets.Add(bus.ID, bus); } foreach (XElement capacitorBankElement in rootElement.Elements("capacitorBank")) { CapBank capBank = new CapBank() { AssetLocations = new List <AssetLocation>(), MeterAssets = new List <MeterAsset>(), DirectChannels = new List <Channel>(), Connections = new List <AssetConnection>(), AssetTypeID = (int)AssetType.CapacitorBank }; capBank.VoltageKV = Convert.ToDouble((string)capacitorBankElement.Element(nameof(capBank.VoltageKV))); capBank.NumberOfBanks = Convert.ToInt32((string)capacitorBankElement.Element(nameof(capBank.NumberOfBanks))); capBank.CansPerBank = Convert.ToInt32((string)capacitorBankElement.Element(nameof(capBank.CansPerBank))); capBank.CapacitancePerBank = Convert.ToDouble((string)capacitorBankElement.Element(nameof(capBank.CapacitancePerBank))); capBank.ID = Convert.ToInt32((string)capacitorBankElement.Element(nameof(capBank.ID))); capBank.AssetKey = (string)capacitorBankElement.Element(nameof(capBank.AssetKey)); capBank.Description = (string)capacitorBankElement.Element(nameof(capBank.Description)); capBank.AssetName = (string)capacitorBankElement.Element(nameof(capBank.AssetName)); assets.Add(capBank.ID, capBank); } foreach (XElement xfrElement in rootElement.Elements("transformer")) { Transformer xfr = new Transformer() { AssetLocations = new List <AssetLocation>(), MeterAssets = new List <MeterAsset>(), DirectChannels = new List <Channel>(), Connections = new List <AssetConnection>(), AssetTypeID = (int)AssetType.Transformer }; xfr.ThermalRating = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.ThermalRating))); xfr.SecondaryVoltageKV = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.SecondaryVoltageKV))); xfr.PrimaryVoltageKV = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.PrimaryVoltageKV))); xfr.Tap = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.Tap))); xfr.R0 = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.R0))); xfr.X0 = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.X0))); xfr.R1 = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.R1))); xfr.X1 = Convert.ToDouble((string)xfrElement.Element(nameof(xfr.X1))); xfr.ID = Convert.ToInt32((string)xfrElement.Element(nameof(xfr.ID))); xfr.AssetKey = (string)xfrElement.Element(nameof(xfr.AssetKey)); xfr.Description = (string)xfrElement.Element(nameof(xfr.Description)); xfr.AssetName = (string)xfrElement.Element(nameof(xfr.AssetName)); assets.Add(xfr.ID, xfr); } foreach (XElement lineElement in rootElement.Elements("line")) { Line line = new Line() { AssetLocations = new List <AssetLocation>(), MeterAssets = new List <MeterAsset>(), DirectChannels = new List <Channel>(), Connections = new List <AssetConnection>(), AssetTypeID = (int)AssetType.Line, Segments = new List <LineSegment>() }; line.VoltageKV = Convert.ToDouble((string)lineElement.Element(nameof(line.VoltageKV))); line.MaxFaultDistance = Convert.ToInt32((string)lineElement.Element(nameof(line.MaxFaultDistance))); line.MinFaultDistance = Convert.ToInt32((string)lineElement.Element(nameof(line.MinFaultDistance))); line.ID = Convert.ToInt32((string)lineElement.Element(nameof(line.ID))); line.AssetKey = (string)lineElement.Element(nameof(line.AssetKey)); line.Description = (string)lineElement.Element(nameof(line.Description)); line.AssetName = (string)lineElement.Element(nameof(line.AssetName)); assets.Add(line.ID, line); } foreach (XElement lineSegmentElement in rootElement.Elements("lineSegment")) { LineSegment lineSegment = new LineSegment() { AssetLocations = new List <AssetLocation>(), MeterAssets = new List <MeterAsset>(), DirectChannels = new List <Channel>(), Connections = new List <AssetConnection>(), AssetTypeID = (int)AssetType.LineSegement }; lineSegment.ThermalRating = Convert.ToDouble((string)lineSegmentElement.Element(nameof(lineSegment.ThermalRating))); lineSegment.R0 = Convert.ToDouble((string)lineSegmentElement.Element(nameof(lineSegment.R0))); lineSegment.X0 = Convert.ToDouble((string)lineSegmentElement.Element(nameof(lineSegment.X0))); lineSegment.R1 = Convert.ToDouble((string)lineSegmentElement.Element(nameof(lineSegment.R1))); lineSegment.X1 = Convert.ToDouble((string)lineSegmentElement.Element(nameof(lineSegment.X1))); lineSegment.Length = Convert.ToDouble((string)lineSegmentElement.Element(nameof(lineSegment.Length))); lineSegment.ID = Convert.ToInt32((string)lineSegmentElement.Element(nameof(lineSegment.ID))); lineSegment.AssetKey = (string)lineSegmentElement.Element(nameof(lineSegment.AssetKey)); lineSegment.Description = (string)lineSegmentElement.Element(nameof(lineSegment.Description)); lineSegment.AssetName = (string)lineSegmentElement.Element(nameof(lineSegment.AssetName)); assets.Add(lineSegment.ID, lineSegment); } foreach (XElement assetConnectionElement in rootElement.Elements("assetConnection")) { AssetConnection assetConnection = new AssetConnection(); assetConnection.ID = Convert.ToInt32((string)assetConnectionElement.Element(nameof(assetConnection.ID))); assetConnection.ChildID = Convert.ToInt32((string)assetConnectionElement.Element(nameof(assetConnection.ChildID))); assetConnection.ParentID = Convert.ToInt32((string)assetConnectionElement.Element(nameof(assetConnection.ParentID))); assetConnection.AssetRelationshipTypeID = Convert.ToInt32((string)assetConnectionElement.Element(nameof(assetConnection.AssetRelationshipTypeID))); assets[assetConnection.ChildID].Connections.Add(assetConnection); assets[assetConnection.ParentID].Connections.Add(assetConnection); if (assets[assetConnection.ChildID].AssetTypeID == (int)AssetType.Line && assets[assetConnection.ParentID].AssetTypeID == (int)AssetType.LineSegement) { Line line = (Line)assets[assetConnection.ChildID]; LineSegment lineSegment = (LineSegment)assets[assetConnection.ParentID]; line.Segments.Add(lineSegment); lineSegment.Line = line; assets[assetConnection.ChildID] = line; assets[assetConnection.ParentID] = lineSegment; } else if (assets[assetConnection.ParentID].AssetTypeID == (int)AssetType.Line && assets[assetConnection.ChildID].AssetTypeID == (int)AssetType.LineSegement) { Line line = (Line)assets[assetConnection.ParentID]; LineSegment lineSegment = (LineSegment)assets[assetConnection.ChildID]; line.Segments.Add(lineSegment); lineSegment.Line = line; assets[assetConnection.ParentID] = line; assets[assetConnection.ChildID] = lineSegment; } assetConnection.Child = assets[assetConnection.ChildID]; assetConnection.Parent = assets[assetConnection.ParentID]; } Dictionary <int, Channel> channels = new Dictionary <int, Channel>(); Dictionary <int, MeasurementType> measurementTypes = new Dictionary <int, MeasurementType>(); Dictionary <int, MeasurementCharacteristic> measurementCharacteristics = new Dictionary <int, MeasurementCharacteristic>(); Dictionary <int, Phase> phases = new Dictionary <int, Phase>(); foreach (XElement channelElement in rootElement.Elements("channel")) { Channel channel = new Channel() { Series = new List <Series>() }; channel.ID = Convert.ToInt32((string)channelElement.Element(nameof(channel.ID))); channel.MeterID = Convert.ToInt32((string)channelElement.Element(nameof(channel.MeterID))); channel.AssetID = Convert.ToInt32((string)channelElement.Element(nameof(channel.AssetID))); channel.MeasurementTypeID = Convert.ToInt32((string)channelElement.Element(nameof(channel.MeasurementTypeID))); channel.MeasurementCharacteristicID = Convert.ToInt32((string)channelElement.Element(nameof(channel.ID))); channel.PhaseID = Convert.ToInt32((string)channelElement.Element(nameof(channel.PhaseID))); channel.Name = (string)channelElement.Element(nameof(channel.Name)); channel.SamplesPerHour = Convert.ToDouble((string)channelElement.Element(nameof(channel.SamplesPerHour))); channel.PerUnitValue = ToNullableDouble((string)channelElement.Element(nameof(channel.PerUnitValue))); channel.HarmonicGroup = Convert.ToInt32((string)channelElement.Element(nameof(channel.HarmonicGroup))); channel.Description = (string)channelElement.Element(nameof(channel.Description)); channel.Enabled = Convert.ToBoolean((string)channelElement.Element(nameof(channel.Enabled))); channels.Add(channel.ID, channel); channel.MeasurementType = measurementTypes.GetOrAdd(channel.MeasurementTypeID, id => new MeasurementType() { ID = id, Name = (string)channelElement.Element(nameof(channel.MeasurementType)) }); channel.MeasurementCharacteristic = measurementCharacteristics.GetOrAdd(channel.MeasurementCharacteristicID, id => new MeasurementCharacteristic() { ID = id, Name = (string)channelElement.Element(nameof(channel.MeasurementCharacteristic)) }); channel.Phase = phases.GetOrAdd(channel.PhaseID, id => new Phase() { ID = id, Name = (string)channelElement.Element(nameof(channel.Phase)) }); channel.Meter = meter; meter.Channels.Add(channel); channel.Asset = assets[channel.AssetID]; channel.Asset.DirectChannels.Add(channel); } Dictionary <int, SeriesType> seriesTypes = new Dictionary <int, SeriesType>(); foreach (XElement seriesElement in rootElement.Elements("series")) { Series series = new Series(); series.ID = Convert.ToInt32((string)seriesElement.Element(nameof(series.ID))); series.ChannelID = Convert.ToInt32((string)seriesElement.Element(nameof(series.ChannelID))); series.SeriesTypeID = Convert.ToInt32((string)seriesElement.Element(nameof(series.SeriesTypeID))); series.SourceIndexes = (string)seriesElement.Element(nameof(series.SourceIndexes)); series.SeriesType = seriesTypes.GetOrAdd(series.SeriesTypeID, id => new SeriesType() { ID = id, Name = (string)seriesElement.Element(nameof(series.SeriesType)) }); series.Channel = channels[series.ChannelID]; series.Channel.Series.Add(series); } foreach (XElement assetLocationElement in rootElement.Elements("assetLocation")) { AssetLocation assetLocation = new AssetLocation(); assetLocation.ID = Convert.ToInt32((string)assetLocationElement.Element(nameof(assetLocation.ID))); assetLocation.LocationID = Convert.ToInt32((string)assetLocationElement.Element(nameof(assetLocation.LocationID))); assetLocation.AssetID = Convert.ToInt32((string)assetLocationElement.Element(nameof(assetLocation.AssetID))); assetLocation.Location = location; location.AssetLocations.Add(assetLocation); assetLocation.Asset = assets[assetLocation.AssetID]; assetLocation.Asset.AssetLocations.Add(assetLocation); } foreach (XElement meterAssetElement in rootElement.Elements("meterAsset")) { MeterAsset meterAsset = new MeterAsset(); meterAsset.ID = Convert.ToInt32((string)meterAssetElement.Element(nameof(meterAsset.ID))); meterAsset.MeterID = Convert.ToInt32((string)meterAssetElement.Element(nameof(meterAsset.MeterID))); meterAsset.AssetID = Convert.ToInt32((string)meterAssetElement.Element(nameof(meterAsset.AssetID))); meterAsset.Meter = meter; meter.MeterAssets.Add(meterAsset); meterAsset.Asset = assets[meterAsset.AssetID]; meterAsset.Asset.MeterAssets.Add(meterAsset); } return(meter); }