/// <summary> /// Gets a trend bundle containing the trend of the first channel. /// </summary> private TrendBundle GetFirstTrend(TimeRange timeRange, int[] cnlNums) { try { stopwatch.Restart(); conn.Open(); TrendBundle trendBundle = new TrendBundle(cnlNums, 0); TrendBundle.CnlDataList trend = trendBundle.Trends[0]; NpgsqlCommand cmd = CreateTrendCommand(timeRange, cnlNums[0]); NpgsqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { trendBundle.Timestamps.Add(reader.GetDateTimeUtc(0)); trend.Add(new CnlData( reader.GetDouble(1), reader.GetInt32(2))); } stopwatch.Stop(); arcLog?.WriteAction(ServerPhrases.ReadingTrendCompleted, trendBundle.Timestamps.Count, stopwatch.ElapsedMilliseconds); return(trendBundle); } finally { conn.Close(); } }
/// <summary> /// Requests historical data from the server. /// </summary> private HistData RequestHistData(int archiveBit, TimeRange timeRange, IList <int> cnlNums) { if (cnlNums == null) { cnlNums = Array.Empty <int>(); } int cnlCnt = cnlNums.Count; HistData.RecordList[] trends = new HistData.RecordList[cnlCnt]; HistData histData = new() { CnlNums = cnlNums, Timestamps = Array.Empty <TimeRecord>(), Trends = trends }; if (cnlCnt > 0) { // request trends TrendBundle trendBundle = clientAccessor.ScadaClient.GetTrends( archiveBit, timeRange, cnlNums.ToArray()); // copy timestamps int pointCount = trendBundle.Timestamps.Count; TimeRecord[] timestamps = new TimeRecord[pointCount]; histData.Timestamps = timestamps; for (int i = 0; i < pointCount; i++) { timestamps[i] = TimeRecord.Create(trendBundle.Timestamps[i], userContext.TimeZone); } // copy channel data CnlDataFormatter formatter = CreateFormatter(); for (int cnlIdx = 0; cnlIdx < cnlCnt; cnlIdx++) { int cnlNum = cnlNums[cnlIdx]; Cnl cnl = webContext.ConfigDatabase.CnlTable.GetItem(cnlNum); HistData.RecordList records = trends[cnlIdx] = new(pointCount); TrendBundle.CnlDataList cnlDataList = trendBundle.Trends[cnlIdx]; for (int ptIdx = 0; ptIdx < pointCount; ptIdx++) { CnlData cnlData = cnlDataList[ptIdx]; records.Add(new HistDataRecord { D = cnlData, Df = formatter.FormatCnlData(cnlData, cnl, false) }); } } } return(histData); }
/// <summary> /// Creates a new trend bundle containing the specified trend. /// </summary> private static TrendBundle CreateTrendBundle(Trend trend) { TrendBundle trendBundle = new TrendBundle(new int[] { trend.CnlNum }, trend.Points.Count); TrendBundle.CnlDataList destTrend = trendBundle.Trends[0]; foreach (TrendPoint trendPoint in trend.Points) { trendBundle.Timestamps.Add(trendPoint.Timestamp); destTrend.Add(new CnlData(trendPoint.Val, trendPoint.Stat)); } return(trendBundle); }