private XDocument ToSheet(Meter meter) { XDocument sheet = new XDocument(); XElement rootElement = new XElement("meterSettingsSheet"); sheet.Add(rootElement); XElement meterElement = new XElement("meter"); meterElement.Add(new XElement(nameof(meter.ID), meter.ID)); meterElement.Add(new XElement(nameof(meter.AssetKey), meter.AssetKey)); meterElement.Add(new XElement(nameof(meter.LocationID), meter.LocationID)); meterElement.Add(new XElement(nameof(meter.Name), meter.Name)); meterElement.Add(new XElement(nameof(meter.Alias), meter.Alias)); meterElement.Add(new XElement(nameof(meter.ShortName), meter.ShortName)); meterElement.Add(new XElement(nameof(meter.Make), meter.Make)); meterElement.Add(new XElement(nameof(meter.Model), meter.Model)); meterElement.Add(new XElement(nameof(meter.TimeZone), meter.TimeZone)); meterElement.Add(new XElement(nameof(meter.Description), meter.Description)); rootElement.Add(meterElement); XElement locationElement = new XElement("location"); locationElement.Add(new XElement(nameof(meter.Location.ID), meter.Location.ID)); locationElement.Add(new XElement(nameof(meter.Location.LocationKey), meter.Location.LocationKey)); locationElement.Add(new XElement(nameof(meter.Location.Name), meter.Location.Name)); locationElement.Add(new XElement(nameof(meter.Location.Alias), meter.Location.Alias)); locationElement.Add(new XElement(nameof(meter.Location.ShortName), meter.Location.ShortName)); locationElement.Add(new XElement(nameof(meter.Location.Latitude), meter.Location.Latitude)); locationElement.Add(new XElement(nameof(meter.Location.Longitude), meter.Location.Longitude)); locationElement.Add(new XElement(nameof(meter.Location.Description), meter.Location.Description)); rootElement.Add(locationElement); // Start With Assets IEnumerable <Asset> assets = meter.MeterAssets .Select(meterLine => meterLine.Asset) .OrderBy(asset => asset.ID); //Save individual Assets (Except LineSegments) List <LineSegment> lineSegments = new List <LineSegment>(); List <AssetConnection> assetConnections = new List <AssetConnection>(); foreach (Asset asset in assets) { XElement assetElement = new XElement("asset");; switch (asset.AssetTypeID) { case ((int)AssetType.Breaker): assetElement = new XElement("breaker"); Breaker breaker = Breaker.DetailedBreaker(asset, asset.ConnectionFactory?.Invoke()); assetElement.Add(new XElement(nameof(breaker.VoltageKV), breaker.VoltageKV)); assetElement.Add(new XElement(nameof(breaker.ThermalRating), breaker.ThermalRating)); assetElement.Add(new XElement(nameof(breaker.Speed), breaker.Speed)); assetElement.Add(new XElement(nameof(asset.ID), asset.ID)); assetElement.Add(new XElement(nameof(asset.AssetKey), asset.AssetKey)); assetElement.Add(new XElement(nameof(asset.Description), asset.Description)); assetElement.Add(new XElement(nameof(asset.AssetName), asset.AssetName)); break; case ((int)AssetType.Bus): assetElement = new XElement("bus"); Bus bus = Bus.DetailedBus(asset, asset.ConnectionFactory?.Invoke()); assetElement.Add(new XElement(nameof(bus.VoltageKV), bus.VoltageKV)); assetElement.Add(new XElement(nameof(asset.ID), asset.ID)); assetElement.Add(new XElement(nameof(asset.AssetKey), asset.AssetKey)); assetElement.Add(new XElement(nameof(asset.Description), asset.Description)); assetElement.Add(new XElement(nameof(asset.AssetName), asset.AssetName)); break; case ((int)AssetType.CapacitorBank): assetElement = new XElement("capacitorBank"); CapBank capBank = CapBank.DetailedCapBank(asset, asset.ConnectionFactory?.Invoke()); assetElement.Add(new XElement(nameof(capBank.VoltageKV), capBank.VoltageKV)); assetElement.Add(new XElement(nameof(capBank.NumberOfBanks), capBank.NumberOfBanks)); assetElement.Add(new XElement(nameof(capBank.CansPerBank), capBank.CansPerBank)); assetElement.Add(new XElement(nameof(capBank.CapacitancePerBank), capBank.CapacitancePerBank)); assetElement.Add(new XElement(nameof(asset.ID), asset.ID)); assetElement.Add(new XElement(nameof(asset.AssetKey), asset.AssetKey)); assetElement.Add(new XElement(nameof(asset.Description), asset.Description)); assetElement.Add(new XElement(nameof(asset.AssetName), asset.AssetName)); break; case ((int)AssetType.Transformer): assetElement = new XElement("transformer"); Transformer xfr = Transformer.DetailedTransformer(asset, asset.ConnectionFactory?.Invoke()); assetElement.Add(new XElement(nameof(xfr.ThermalRating), xfr.ThermalRating)); assetElement.Add(new XElement(nameof(xfr.SecondaryVoltageKV), xfr.SecondaryVoltageKV)); assetElement.Add(new XElement(nameof(xfr.PrimaryVoltageKV), xfr.PrimaryVoltageKV)); assetElement.Add(new XElement(nameof(xfr.Tap), xfr.Tap)); assetElement.Add(new XElement(nameof(xfr.X0), xfr.X0)); assetElement.Add(new XElement(nameof(xfr.R0), xfr.R0)); assetElement.Add(new XElement(nameof(xfr.X1), xfr.X1)); assetElement.Add(new XElement(nameof(xfr.R1), xfr.R1)); assetElement.Add(new XElement(nameof(asset.ID), asset.ID)); assetElement.Add(new XElement(nameof(asset.AssetKey), asset.AssetKey)); assetElement.Add(new XElement(nameof(asset.Description), asset.Description)); assetElement.Add(new XElement(nameof(asset.AssetName), asset.AssetName)); break; case ((int)AssetType.Line): assetElement = new XElement("line"); Line line = Line.DetailedLine(asset); assetElement.Add(new XElement(nameof(line.VoltageKV), line.VoltageKV)); assetElement.Add(new XElement(nameof(line.MaxFaultDistance), line.MaxFaultDistance)); assetElement.Add(new XElement(nameof(line.MinFaultDistance), line.MinFaultDistance)); assetElement.Add(new XElement(nameof(asset.ID), asset.ID)); assetElement.Add(new XElement(nameof(asset.AssetKey), asset.AssetKey)); assetElement.Add(new XElement(nameof(asset.Description), asset.Description)); assetElement.Add(new XElement(nameof(asset.AssetName), asset.AssetName)); lineSegments.AddRange(line.Segments); break; case ((int)AssetType.LineSegement): lineSegments.Add(LineSegment.DetailedLineSegment(asset, asset.ConnectionFactory?.Invoke())); break; default: assetElement = new XElement("asset"); assetElement.Add(new XElement(nameof(asset.ID), asset.ID)); assetElement.Add(new XElement(nameof(asset.AssetKey), asset.AssetKey)); assetElement.Add(new XElement(nameof(asset.Description), asset.Description)); assetElement.Add(new XElement(nameof(asset.AssetName), asset.AssetName)); break; } assetConnections.AddRange(asset.Connections.Where(item => item.ParentID == asset.ID)); rootElement.Add(assetElement); } //Deal with special Cases... If it is a line we need to keep the Line Segments... foreach (LineSegment lineSegment in lineSegments) { XElement lineSegmentElement = new XElement("lineSegment"); lineSegmentElement.Add(new XElement(nameof(lineSegment.ThermalRating), lineSegment.ThermalRating)); lineSegmentElement.Add(new XElement(nameof(lineSegment.X0), lineSegment.X0)); lineSegmentElement.Add(new XElement(nameof(lineSegment.R0), lineSegment.R0)); lineSegmentElement.Add(new XElement(nameof(lineSegment.X1), lineSegment.X1)); lineSegmentElement.Add(new XElement(nameof(lineSegment.R1), lineSegment.R1)); lineSegmentElement.Add(new XElement(nameof(lineSegment.ID), lineSegment.ID)); lineSegmentElement.Add(new XElement(nameof(lineSegment.Length), lineSegment.Length)); lineSegmentElement.Add(new XElement(nameof(lineSegment.AssetKey), lineSegment.AssetKey)); lineSegmentElement.Add(new XElement(nameof(lineSegment.Description), lineSegment.Description)); lineSegmentElement.Add(new XElement(nameof(lineSegment.AssetName), lineSegment.AssetName)); assetConnections.AddRange(lineSegment.Connections.Where(item => item.ParentID == lineSegment.ID)); rootElement.Add(lineSegmentElement); } //Deal with Asset -> Asset Connections..... foreach (AssetConnection assetConnection in assetConnections) { if (assets.Select(item => item.ID).Contains(assetConnection.ChildID) || lineSegments.Select(item => item.ID).Contains(assetConnection.ChildID)) { XElement assetConnectionElement = new XElement("assetConnection"); assetConnectionElement.Add(new XElement(nameof(assetConnection.ID), assetConnection.ID)); assetConnectionElement.Add(new XElement(nameof(assetConnection.ChildID), assetConnection.ChildID)); assetConnectionElement.Add(new XElement(nameof(assetConnection.ParentID), assetConnection.ParentID)); assetConnectionElement.Add(new XElement(nameof(assetConnection.AssetRelationshipTypeID), assetConnection.AssetRelationshipTypeID)); rootElement.Add(assetConnectionElement); } } // That is it for Assets IEnumerable <Channel> channels = meter.Channels .OrderBy(channel => channel.ID); foreach (Channel channel in channels) { if (channel.ID == 0) { continue; } XElement channelElement = new XElement("channel"); channelElement.Add(new XElement(nameof(channel.ID), channel.ID)); channelElement.Add(new XElement(nameof(channel.MeterID), channel.MeterID)); channelElement.Add(new XElement(nameof(channel.AssetID), channel.AssetID)); channelElement.Add(new XElement(nameof(channel.MeasurementTypeID), channel.MeasurementTypeID)); channelElement.Add(new XElement(nameof(channel.MeasurementType), channel.MeasurementType.Name)); channelElement.Add(new XElement(nameof(channel.MeasurementCharacteristicID), channel.MeasurementCharacteristicID)); channelElement.Add(new XElement(nameof(channel.MeasurementCharacteristic), channel.MeasurementCharacteristic.Name)); channelElement.Add(new XElement(nameof(channel.PhaseID), channel.PhaseID)); channelElement.Add(new XElement(nameof(channel.Phase), channel.Phase.Name)); channelElement.Add(new XElement(nameof(channel.Name), channel.Name)); channelElement.Add(new XElement(nameof(channel.SamplesPerHour), channel.SamplesPerHour)); channelElement.Add(new XElement(nameof(channel.PerUnitValue), channel.PerUnitValue)); channelElement.Add(new XElement(nameof(channel.HarmonicGroup), channel.HarmonicGroup)); channelElement.Add(new XElement(nameof(channel.Description), channel.Description)); channelElement.Add(new XElement(nameof(channel.Enabled), channel.Enabled)); rootElement.Add(channelElement); } IEnumerable <Series> seriesList = meter.Channels .SelectMany(channel => channel.Series) .OrderBy(series => series.ID); foreach (Series series in seriesList) { if (series.ID == 0) { continue; } XElement seriesElement = new XElement("series"); seriesElement.Add(new XElement(nameof(series.ID), series.ID)); seriesElement.Add(new XElement(nameof(series.ChannelID), series.ChannelID)); seriesElement.Add(new XElement(nameof(series.SeriesTypeID), series.SeriesTypeID)); seriesElement.Add(new XElement(nameof(series.SeriesType), series.SeriesType.Name)); seriesElement.Add(new XElement(nameof(series.SourceIndexes), series.SourceIndexes)); rootElement.Add(seriesElement); } IEnumerable <AssetLocation> assetLocations = meter.Location.AssetLocations .OrderBy(assetLocation => assetLocation.ID); foreach (AssetLocation assetLocation in assetLocations) { XElement meterLocationLineElement = new XElement("assetLocation"); meterLocationLineElement.Add(new XElement(nameof(assetLocation.ID), assetLocation.ID)); meterLocationLineElement.Add(new XElement(nameof(assetLocation.LocationID), assetLocation.LocationID)); meterLocationLineElement.Add(new XElement(nameof(assetLocation.AssetID), assetLocation.AssetID)); rootElement.Add(meterLocationLineElement); } IEnumerable <MeterAsset> meterAssets = meter.MeterAssets .OrderBy(meterAsset => meterAsset.ID); foreach (MeterAsset meterAsset in meterAssets) { XElement meterLineElement = new XElement("meterAsset"); meterLineElement.Add(new XElement(nameof(meterAsset.ID), meterAsset.ID)); meterLineElement.Add(new XElement(nameof(meterAsset.MeterID), meterAsset.MeterID)); meterLineElement.Add(new XElement(nameof(meterAsset.AssetID), meterAsset.AssetID)); rootElement.Add(meterLineElement); } return(sheet); }