public IHttpActionResult PostNewAssetForLocation([FromBody] JObject record, int lineID) { if (PostRoles == string.Empty || User.IsInRole(PostRoles)) { try { using (TransactionScope scope = new TransactionScope()) { using (AdoDataConnection connection = new AdoDataConnection(Connection)) { JToken asset = record["Asset"]; int assetTypeID = connection.ExecuteScalar <int>("SELECT ID FROM AssetType WHERE Name = 'LineSegment'"); LineSegment lineSegment = new LineSegment(); lineSegment.VoltageKV = asset["VoltageKV"].ToObject <double>(); lineSegment.AssetKey = asset["AssetKey"].ToString(); lineSegment.Description = asset["Description"].ToString(); lineSegment.AssetName = asset["AssetName"].ToString(); lineSegment.R0 = asset["R0"].ToObject <double>(); lineSegment.X0 = asset["X0"].ToObject <double>(); lineSegment.R1 = asset["R1"].ToObject <double>(); lineSegment.X1 = asset["X1"].ToObject <double>(); lineSegment.Length = asset["Length"].ToObject <double>(); lineSegment.ThermalRating = asset["ThermalRating"].ToObject <double>(); new TableOperations <LineSegment>(connection).AddNewRecord(lineSegment); int assetID = connection.ExecuteScalar <int>("SELECT ID FROM Asset WHERE AssetKey = {0}", asset["AssetKey"].ToString()); AssetConnection assetConnection = new AssetConnection() { AssetRelationshipTypeID = connection.ExecuteScalar <int>("SELECT ID FROM AssetRelationShipType WHERE Name = 'Line-LineSegment'"), ChildID = lineID, ParentID = assetID }; (new TableOperations <AssetConnection>(connection)).AddNewRecord(assetConnection); } scope.Complete(); return(Ok("Completed without errors")); } } catch (Exception ex) { return(InternalServerError(ex)); } } else { return(Unauthorized()); } }
public override IHttpActionResult Post([FromBody] JObject record) { if (PostRoles == string.Empty || User.IsInRole(PostRoles)) { base.Post(record).ExecuteAsync(new System.Threading.CancellationToken()).Result.Content.ReadAsAsync <int>().Wait(); Line lineRecord = record.ToObject <Line>(); using (AdoDataConnection connection = new AdoDataConnection(Connection)) { lineRecord = (new TableOperations <Line>(connection)).QueryRecordWhere("AssetKey = {0}", lineRecord.AssetKey); LineSegment lineSegment = new LineSegment() { VoltageKV = record["VoltageKV"].ToObject <double>(), AssetKey = record["AssetKey"].ToString() + "LineSegment", Description = record["Description"].ToString(), AssetName = record["AssetName"].ToString(), R0 = 0, X0 = 0, R1 = 0, X1 = 0, Length = 0, }; lineSegment.AssetTypeID = connection.ExecuteScalar <int>("SELECT ID FROM AssetType WHERE Name = 'LineSegment'"); new TableOperations <LineSegment>(connection).AddNewRecord(lineSegment); lineSegment.ID = connection.ExecuteScalar <int>("SELECT ID FROM Asset WHERE AssetKey = {0}", lineSegment.AssetKey); AssetConnection assetConnection = new AssetConnection() { AssetRelationshipTypeID = 0, ParentID = lineRecord.ID, ChildID = lineSegment.ID }; assetConnection.AssetRelationshipTypeID = connection.ExecuteScalar <int>("SELECT ID FROM AssetRelationshipType WHERE Name = 'Line-LineSegment'"); new TableOperations <AssetConnection>(connection).AddNewRecord(assetConnection); } return(Ok(lineRecord)); } else { return(Unauthorized()); } }
public IHttpActionResult AddLineSegmentToLine(int segmentID, int lineID) { if (PatchRoles == string.Empty || User.IsInRole(PatchRoles)) { using (AdoDataConnection connection = new AdoDataConnection(Connection)) { AssetConnection assetConnection = new AssetConnection() { AssetRelationshipTypeID = connection.ExecuteScalar <int>("SELECT ID FROM AssetRelationShipType WHERE Name = 'Line-LineSegment'"), ChildID = lineID, ParentID = segmentID }; (new TableOperations <AssetConnection>(connection)).AddNewRecord(assetConnection); return(Ok(1)); } } else { return(Unauthorized()); } }
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); }