public DataGroup QueryDataGroup(int eventID, Meter meter) { string target = $"DataGroup-{eventID}"; try { bool isSafe = s_mutex.WaitOne(); if (isSafe) { DataGroup dataGroup = m_memoryCache.GetOrCreate(target, task => { task.SlidingExpiration = TimeSpan.FromMinutes(10.0D); using (AdoDataConnection connection = new AdoDataConnection(m_configuration["OpenXDA:ConnectionString"], m_configuration["OpenXDA:DataProviderString"])) { List <byte[]> data = ChannelData.DataFromEvent(eventID, connection); return(ToDataGroup(meter, data)); } }); return(dataGroup); } else { return(null); } } finally { s_mutex.ReleaseMutex(); } }
private VICycleDataGroup GetCycleData(AdoDataConnection connection, int eventID) { TableOperations <Event> eventTable = new TableOperations <Event>(connection); Event evt = eventTable.QueryRecordWhere("ID = {0}", eventID); if ((object)evt == null) { return(null); } List <byte[]> timeDomainData = ChannelData.DataFromEvent(eventID, connection); TableOperations <Meter> meterTable = new TableOperations <Meter>(connection); Meter meter = meterTable.QueryRecordWhere("ID = {0}", evt.MeterID); if ((object)meter == null) { return(null); } meter.ConnectionFactory = () => new AdoDataConnection(connection.Connection, connection.AdapterType, false); DataGroup dataGroup = new DataGroup(); dataGroup.FromData(meter, timeDomainData); return(Transform.ToVICycleDataGroup(new VIDataGroup(dataGroup), SystemFrequency)); }
public IHttpActionResult GetEventWaveformData(EventDataJSON json) { if (json != null && json.EventID != null) { try { int eventID = int.Parse(json.EventID); using (AdoDataConnection connection = new AdoDataConnection("systemSettings")) { Event evt = new TableOperations <Event>(connection).QueryRecordWhere("ID = {0}", eventID); Meter meter = new TableOperations <Meter>(connection).QueryRecordWhere("ID = {0}", evt.MeterID); meter.ConnectionFactory = () => new AdoDataConnection("systemSettings"); List <byte[]> frequencyDomainData = ChannelData.DataFromEvent(eventID, connection); DataGroup dataGroup = new DataGroup(); dataGroup.FromData(meter, frequencyDomainData); VIDataGroup vIDataGroup = new VIDataGroup(dataGroup); dataGroup = vIDataGroup.ToDataGroup(); DataTable table = new DataTable(); table.Columns.Add("Timestamp", typeof(DateTime)); foreach (var series in dataGroup.DataSeries) { table.Columns.Add(series.SeriesInfo.Channel.MeasurementType.Name + "(" + series.SeriesInfo.Channel.Phase.Name + ")", typeof(double)); } for (int i = 0; i < dataGroup.DataSeries[0].DataPoints.Count(); ++i) { DataRow row = table.NewRow(); row["Timestamp"] = dataGroup.DataSeries[0].DataPoints[i].Time; for (int j = 1; j < table.Columns.Count; ++j) { row[table.Columns[j].ColumnName] = dataGroup.DataSeries[j - 1].DataPoints[i].Value; } table.Rows.Add(row); } return(Ok(table)); } } catch (Exception ex) { return(InternalServerError(ex)); } } else { return(BadRequest("Please provide event id")); } }
private Dictionary <string, DataSeries> QueryEventData(AdoDataConnection connection, Meter meter, DateTime startTime, DateTime endTime) { Func <IEnumerable <DataSeries>, DataSeries> merge = grouping => { DataSeries mergedSeries = DataSeries.Merge(grouping); mergedSeries.SeriesInfo = grouping.First().SeriesInfo; return(mergedSeries); }; double systemFrequency = connection.ExecuteScalar <double?>("SELECT Value FROM Setting WHERE Name = 'SystemFrequency'") ?? 60.0D; DataTable dataTable = connection.RetrieveData("SELECT ID FROM Event WHERE MeterID = {0} AND EndTime >= {1} AND StartTime <= {2})", meter.ID, ToDateTime2(connection, startTime), ToDateTime2(connection, endTime)); Dictionary <string, DataSeries> dict = new Dictionary <string, DataSeries>(); IEnumerable <DataGroup> dataGroups = dataTable .Select() .Select(row => row.ConvertField <int>("ID")) .Select(id => ToDataGroup(meter, ChannelData.DataFromEvent(id, connection))) .OrderBy(subGroup => subGroup.StartTime) .ToList(); List <DataSeries> mergedSeriesList = dataGroups .SelectMany(dataGroup => dataGroup.DataSeries) .GroupBy(dataSeries => dataSeries.SeriesInfo.Channel.Name) .Select(merge) .ToList(); DataGroup mergedGroup = new DataGroup(); mergedSeriesList.ForEach(mergedSeries => mergedGroup.Add(mergedSeries)); foreach (DataSeries dataSeries in mergedGroup.DataSeries) { string key = dataSeries.SeriesInfo.Channel.Name; dict.GetOrAdd(key, _ => dataSeries.ToSubSeries(startTime, endTime)); } VICycleDataGroup viCycleDataGroup = Transform.ToVICycleDataGroup(new VIDataGroup(mergedGroup), systemFrequency); foreach (CycleDataGroup cycleDataGroup in viCycleDataGroup.CycleDataGroups) { DataGroup dg = cycleDataGroup.ToDataGroup(); string key = dg.DataSeries.First().SeriesInfo.Channel.Name; dict.GetOrAdd(key + " RMS", _ => cycleDataGroup.RMS.ToSubSeries(startTime, endTime)); dict.GetOrAdd(key + " Angle", _ => cycleDataGroup.Phase.ToSubSeries(startTime, endTime)); } return(dict); }
private VIDataGroup GetVIDataGroup() { TableOperations <Meter> meterTable = new TableOperations <Meter>(m_connection); TableOperations <Event> eventTable = new TableOperations <Event>(m_connection); Event evt = eventTable.QueryRecordWhere("ID = {0}", m_eventID); Meter meter = meterTable.QueryRecordWhere("ID = {0}", evt.MeterID); List <byte[]> timeDomainData = ChannelData.DataFromEvent(evt.ID, m_connection); //This Should start by getting multiple datasets meter.ConnectionFactory = () => new AdoDataConnection(m_connection.Connection, m_connection.AdapterType, false); DataGroup dataGroup = new DataGroup(); dataGroup.FromData(meter, timeDomainData); return(new VIDataGroup(dataGroup)); }
public static DataGroup QueryDataGroup(int eventID, Meter meter) { string target = $"DataGroup-{eventID}"; Task <DataGroup> dataGroupTask = new Task <DataGroup>(() => { List <byte[]> data = ChannelData.DataFromEvent(eventID, () => new AdoDataConnection("dbOpenXDA")); return(ToDataGroup(meter, data)); }); if (s_memoryCache.Add(target, dataGroupTask, new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(m_cacheSlidingExpiration) })) { dataGroupTask.Start(); } dataGroupTask = (Task <DataGroup>)s_memoryCache.Get(target); return(dataGroupTask.Result); }
public MeterDataSet(Event evt) { Resources = new Dictionary <Type, object>(); DataSeries = new List <DataSeries>(); Digitals = new List <DataSeries>(); ReportedDisturbances = new List <ReportedDisturbance>(); using (AdoDataConnection connection = new AdoDataConnection("systemSettings")) { Meter = (new TableOperations <Meter>(connection)).QueryRecordWhere("ID = {0}", evt.MeterID); Meter.ConnectionFactory = () => new AdoDataConnection("systemSettings"); CreateDbConnection = () => new AdoDataConnection("systemSettings"); ConnectionString = string.Empty; FilePath = (new TableOperations <DataFile>(connection)).QueryRecordWhere("FileGroupID = {0}", evt.FileGroupID).FilePath; FileGroup = (new TableOperations <FileGroup>(connection)).QueryRecordWhere("ID = {0}", evt.FileGroupID); DataGroup dataGroup = ToDataGroup(ChannelData.DataFromEvent(evt.ID, connection)); DataSeries = dataGroup.DataSeries.Where(ds => ds.SeriesInfo.Channel.MeasurementType.Name != "Digital").ToList(); Digitals = dataGroup.DataSeries.Where(ds => ds.SeriesInfo.Channel.MeasurementType.Name == "Digital").ToList(); } }
private DataGroup QueryDataGroup(int eventID, Meter meter) { string target = $"DataGroup-{eventID}"; Task <DataGroup> dataGroupTask = new Task <DataGroup>(() => { using (AdoDataConnection connection = new AdoDataConnection(SettingsCategory)) { List <byte[]> data = ChannelData.DataFromEvent(eventID, SettingsCategory); return(ToDataGroup(meter, data)); } }); if (s_memoryCache.Add(target, dataGroupTask, new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(10.0D) })) { dataGroupTask.Start(); } dataGroupTask = (Task <DataGroup>)s_memoryCache.Get(target); return(dataGroupTask.Result); }
public eventSet getSignalDataByIDAndType(string EventInstanceID, String DataType) { eventSet theset = new eventSet(); theset.data = new List <signalDetail>(); DataGroup eventDataGroup = new DataGroup(); using (AdoDataConnection connection = new AdoDataConnection("SystemSettings")) { Event evt = (new TableOperations <Event>(connection)).QueryRecordWhere("ID = {0}", Convert.ToInt32(EventInstanceID)); Meter meter = (new TableOperations <Meter>(connection)).QueryRecordWhere("ID = {0}", evt.MeterID); Line line = (new TableOperations <Line>(connection)).QueryRecordWhere("ID = {0}", evt.AssetID); EventData eventData = (new TableOperations <EventData>(connection)).QueryRecordWhere("ID = {0}", evt.EventDataID); eventDataGroup.FromData(meter, ChannelData.DataFromEvent(evt.ID, connection)); int i = -1; int datacount = eventDataGroup.DataSeries[0].DataPoints.Count(); theset.xAxis = new string[datacount]; foreach (DataSeries theseries in eventDataGroup.DataSeries) { i++; signalDetail theitem = new signalDetail(); string measurementType = "I"; // Assume Current, sorry this is ugly string phasename = theseries.SeriesInfo.Channel.Phase.Name; if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Voltage")) { measurementType = "V"; } if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Power")) { measurementType = "P"; } if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Energy")) { measurementType = "E"; } if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Digital")) { if (theseries.SeriesInfo.Channel.MeasurementCharacteristic.Name == "None") { continue; } measurementType = "D"; phasename = theseries.SeriesInfo.Channel.Description; } if (DataType != null) { if (measurementType != DataType) { continue; } } if (theseries.SeriesInfo.SeriesType.Name.Substring(0, 3) == "Min") { continue; } if (theseries.SeriesInfo.SeriesType.Name.Substring(0, 3) == "Max") { continue; } theset.Yaxis0name = "Current"; if (measurementType == "V") { theset.Yaxis0name = "Voltage"; } if (measurementType == "D") { theset.Yaxis0name = "Breakers"; } //theitem.name = theseries.SeriesInfo.SeriesType.Name.Substring(0, 3) + " " + measurementType + theseries.SeriesInfo.Channel.Phase.Name; theitem.name = measurementType + phasename; theitem.data = new double[datacount]; theitem.type = "line"; theitem.yAxis = 0; if (theitem.name.Contains("IRES")) { theitem.showInTooltip = false; theitem.visible = false; } int j = 0; DateTime beginticks = eventDataGroup.DataSeries[i].DataPoints[0].Time; foreach (FaultData.DataAnalysis.DataPoint thepoint in eventDataGroup.DataSeries[i].DataPoints) { double elapsed = thepoint.Time.Subtract(beginticks).TotalSeconds; theset.xAxis[j] = elapsed.ToString(); theitem.data[j] = thepoint.Value; j++; } theset.data.Add(theitem); } } //theset = FetchFaultSegmentDetails(EventInstanceID, theset); eventSet thereturnset = FetchMeterEventCycleData(EventInstanceID, theset.Yaxis0name, theset); return(thereturnset); }
private eventSet FetchMeterEventDataByID(string EventInstanceID) { eventSet theset = new eventSet(); theset.data = new List <signalDetail>(); DataGroup eventDataGroup = new DataGroup(); using (AdoDataConnection connection = new AdoDataConnection("SystemSettings")) { Event evt = (new TableOperations <Event>(connection)).QueryRecordWhere("ID = {0}", Convert.ToInt32(EventInstanceID)); Meter meter = (new TableOperations <Meter>(connection)).QueryRecordWhere("ID = {0}", evt.MeterID); Line line = (new TableOperations <Line>(connection)).QueryRecordWhere("ID = {0}", evt.AssetID); theset.Yaxis0name = "Voltage"; theset.Yaxis1name = "Current"; //eventDataAdapter.GetTimeDomainData(evt.EventDataID); eventDataGroup.FromData(meter, ChannelData.DataFromEvent(evt.ID, connection)); int i = 0; foreach (DataSeries theseries in eventDataGroup.DataSeries) { int datacount = eventDataGroup.DataSeries[i].DataPoints.Count(); theset.xAxis = new string[datacount]; signalDetail theitem = new signalDetail(); string measurementType = "I"; // Assume Current, sorry this is ugly if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Voltage")) { measurementType = "V"; } if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Power")) { measurementType = "P"; } if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Energy")) { measurementType = "E"; } if (theseries.SeriesInfo.SeriesType.Name.Substring(0, 3) == "Min") { continue; } if (theseries.SeriesInfo.SeriesType.Name.Substring(0, 3) == "Max") { continue; } //theitem.name = theseries.SeriesInfo.SeriesType.Name.Substring(0, 3) + " " + measurementType + theseries.SeriesInfo.Channel.Phase.Name; theitem.name = measurementType + theseries.SeriesInfo.Channel.Phase.Name; theitem.data = new double[datacount]; theitem.type = "line"; theitem.yAxis = 0; if (theitem.name.Contains("Angle")) { theitem.showInTooltip = false; theitem.visible = false; theitem.showInLegend = false; } if (theitem.name.Contains("RMS")) { theitem.showInTooltip = false; theitem.visible = false; } if (theitem.name.Contains("RES")) { theitem.showInTooltip = false; theitem.visible = false; } if (theitem.name.Contains("Peak")) { theitem.showInTooltip = false; theitem.visible = false; } if (theseries.SeriesInfo.Channel.MeasurementType.Name.Equals("Current")) { theitem.yAxis = 1; } int j = 0; DateTime beginticks = eventDataGroup.DataSeries[i].DataPoints[0].Time; foreach (DataPoint thepoint in eventDataGroup.DataSeries[i].DataPoints) { double elapsed = thepoint.Time.Subtract(beginticks).TotalSeconds; theset.xAxis[j] = elapsed.ToString(); theitem.data[j] = thepoint.Value; j++; } i++; theset.data.Add(theitem); } } return(theset); }
public List <FlotSeries> GetFlotData(int eventID, List <int> seriesIndexes) { List <FlotSeries> flotSeriesList = new List <FlotSeries>(); using (AdoDataConnection connection = CreateDbConnection()) { TableOperations <Meter> meterTable = new TableOperations <Meter>(connection); TableOperations <Event> eventTable = new TableOperations <Event>(connection); TableOperations <FaultCurve> faultCurveTable = new TableOperations <FaultCurve>(connection); Event evt = eventTable.QueryRecordWhere("ID = {0}", eventID); Meter meter = meterTable.QueryRecordWhere("ID = {0}", evt.MeterID); meter.ConnectionFactory = () => new AdoDataConnection(connection.Connection, connection.AdapterType, false); List <FlotSeries> flotInfo = GetFlotInfo(eventID); DateTime epoch = new DateTime(1970, 1, 1); Lazy <DataGroup> dataGroup = new Lazy <DataGroup>(() => ToDataGroup(meter, ChannelData.DataFromEvent(evt.ID, connection))); Dictionary <int, DataSeries> waveformData = dataGroup.Value.DataSeries.ToDictionary(dataSeries => dataSeries.SeriesInfo.ID); Lazy <DataGroup> cycleData = new Lazy <DataGroup>(() => (Transform.ToVICycleDataGroup(new VIDataGroup(dataGroup.Value), SystemFrequency).ToDataGroup())); Lazy <Dictionary <string, DataSeries> > faultCurveData = new Lazy <Dictionary <string, DataSeries> >(() => { return(faultCurveTable .QueryRecordsWhere("EventID = {0}", evt.ID) .Select(faultCurve => new { Algorithm = faultCurve.Algorithm, DataGroup = ToDataGroup(meter, new List <byte[]>(1) { faultCurve.Data }) }) .Where(obj => obj.DataGroup.DataSeries.Count > 0) .ToDictionary(obj => obj.Algorithm, obj => obj.DataGroup[0])); }); foreach (int index in seriesIndexes) { DataSeries dataSeries = null; FlotSeries flotSeries; if (index >= flotInfo.Count) { continue; } flotSeries = flotInfo[index]; if (flotSeries.FlotType == FlotSeriesType.Waveform) { if (!waveformData.TryGetValue(flotSeries.SeriesID, out dataSeries)) { continue; } } else if (flotSeries.FlotType == FlotSeriesType.Cycle) { dataSeries = cycleData.Value.DataSeries .Where(series => series.SeriesInfo.Channel.MeasurementType.Name == flotSeries.MeasurementType) .Where(series => series.SeriesInfo.Channel.Phase.Name == flotSeries.Phase) .Skip(flotSeries.SeriesID) .FirstOrDefault(); if ((object)dataSeries == null) { continue; } } else if (flotSeries.FlotType == FlotSeriesType.Fault) { string algorithm = flotSeries.ChannelName; if (!faultCurveData.Value.TryGetValue(algorithm, out dataSeries)) { continue; } } else { continue; } foreach (DataPoint dataPoint in dataSeries.DataPoints) { if (!double.IsNaN(dataPoint.Value)) { flotSeries.DataPoints.Add(new double[] { dataPoint.Time.Subtract(epoch).TotalMilliseconds, dataPoint.Value }); } } flotSeriesList.Add(flotSeries); } } return(flotSeriesList); }