public List <ScadaPointResult> FetchHistoricalPointData(DashboardScadaTimeSeriesPoint dashboardScadaTimeSeriesPoint) { try { string pnt = dashboardScadaTimeSeriesPoint.ScadaPoint_.Id_; string type = dashboardScadaTimeSeriesPoint.HistoryFetchStrategy_; int nret = 0; uint s = 0; double dval = 0; DateTime timestamp = DateTime.Now; DateTime startTime = dashboardScadaTimeSeriesPoint.StartTime; DateTime endTime = dashboardScadaTimeSeriesPoint.EndTime; string status = ""; TimeSpan period = TimeSpan.FromSeconds(dashboardScadaTimeSeriesPoint.FetchPeriodSecs_); //history request initiation if (type == DashboardScadaTimeSeriesPoint.FetchStrategyRaw) { nret = History.DnaGetHistRaw(pnt, startTime, endTime, out s); } else if (type == DashboardScadaTimeSeriesPoint.FetchStrategySnap) { nret = History.DnaGetHistSnap(pnt, startTime, endTime, period, out s); } else if (type == DashboardScadaTimeSeriesPoint.FetchStrategyAverage) { nret = History.DnaGetHistAvg(pnt, startTime, endTime, period, out s); } else if (type == DashboardScadaTimeSeriesPoint.FetchStrategyMin) { nret = History.DnaGetHistMin(pnt, startTime, endTime, period, out s); } else if (type == DashboardScadaTimeSeriesPoint.FetchStrategyMax) { nret = History.DnaGetHistMax(pnt, startTime, endTime, period, out s); } // Get history values List <ScadaPointResult> historyResults = new List <ScadaPointResult>(); while (nret == 0) { nret = History.DnaGetNextHist(s, out dval, out timestamp, out status); if (status != null) { historyResults.Add(new ScadaPointResult(dval, status, timestamp)); } } return(historyResults); } catch (Exception e) { // Todo send this to console printing of the dashboard Console.WriteLine($"Error while fetching history data of point {dashboardScadaTimeSeriesPoint.ScadaPoint_.Id_}"); Console.WriteLine($"The exception is {e}"); } return(null); }
public List <ScadaPointResult> FetchHistoricalPointDataTest(DashboardScadaTimeSeriesPoint dashboardScadaTimeSeriesPoint) { // return random values for testing List <ScadaPointResult> historyResults = new List <ScadaPointResult>(); Random rand = new Random(); int numPeriods = (int)(dashboardScadaTimeSeriesPoint.GetEndTime() - dashboardScadaTimeSeriesPoint.GetStartTime()).TotalSeconds / dashboardScadaTimeSeriesPoint.FetchPeriodSecs_; for (int i = 0; i < numPeriods; i++) { historyResults.Add(new ScadaPointResult(rand.Next(0, 100), "Good", dashboardScadaTimeSeriesPoint.GetStartTime().AddSeconds(dashboardScadaTimeSeriesPoint.FetchPeriodSecs_ * i))); } return(historyResults); }
public async Task <List <ScadaPointResult> > FetchHistoricalPointDataAsync(DashboardScadaTimeSeriesPoint dashboardScadaTimeSeriesPoint, CancellationToken ct) { List <ScadaPointResult> historyResults = new List <ScadaPointResult>(); if (AppSettingsHelper.GetSetting <bool>("scada_fetch_random", false)) { // If random scada data fetch was selected historyResults = await Task.Run <List <ScadaPointResult> >(() => { return(FetchHistoricalPointDataTest(dashboardScadaTimeSeriesPoint)); }); historyResults = FetchHistoricalPointDataTest(dashboardScadaTimeSeriesPoint); } else { historyResults = FetchHistoricalPointData(dashboardScadaTimeSeriesPoint); } return(historyResults); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { List <IDashboardTimeSeriesPoint> fields = new List <IDashboardTimeSeriesPoint>(); var jsonArray = JArray.Load(reader); foreach (var item in jsonArray) { var jsonObject = item as JObject; var dashboardTimeSeriesPoint = default(IDashboardTimeSeriesPoint); switch (jsonObject["TimeSeriesType_"].Value <string>()) { case DashboardScadaTimeSeriesPoint.timeSeriesType: dashboardTimeSeriesPoint = new DashboardScadaTimeSeriesPoint(); break; } serializer.Populate(jsonObject.CreateReader(), dashboardTimeSeriesPoint); fields.Add(dashboardTimeSeriesPoint); } return(fields); }
private void AddBtn_Click(object sender, RoutedEventArgs e) { // show relavent edit window if (TimeSeriesPointTypesComboBox.SelectedIndex > -1 && TimeSeriesPointTypesComboBox.SelectedIndex <= dataPointsVM.PointTypes.Count && dataPointsVM.PointTypes[TimeSeriesPointTypesComboBox.SelectedIndex] == DashboardScadaTimeSeriesPoint.timeSeriesType) { // show scada point edit window with relavent initialisation DateTime startTime = DateTime.Now; DateTime endTime = startTime; ScadaDataPoint pnt = new ScadaDataPoint(""); if (dataPointsVM.dashboardTimeSeriesPoints.Count > 0) { startTime = dataPointsVM.dashboardTimeSeriesPoints.ElementAt(0).StartTime; endTime = dataPointsVM.dashboardTimeSeriesPoints.ElementAt(0).EndTime; } DashboardScadaTimeSeriesPoint scadaTimeSeriesPoint = new DashboardScadaTimeSeriesPoint(pnt, startTime, endTime); ScadaTimeSeriesPointEditWindow scadaTimeSeriesPointEditWindow = new ScadaTimeSeriesPointEditWindow(scadaTimeSeriesPoint); scadaTimeSeriesPointEditWindow.ShowDialog(); if (scadaTimeSeriesPointEditWindow.DialogResult == true) { // update the point dataPointsVM.dashboardTimeSeriesPoints.Add(scadaTimeSeriesPointEditWindow.scadaTimeSeriesPointVM.ScadaTimeSeriesPoint); } } }
public ScadaTimeSeriesPointVM(DashboardScadaTimeSeriesPoint scadaTimeSeriesPoint) { ScadaTimeSeriesPoint = new DashboardScadaTimeSeriesPoint(scadaTimeSeriesPoint); }
public ScadaTimeSeriesPointEditWindow(DashboardScadaTimeSeriesPoint pnt) { InitializeComponent(); scadaTimeSeriesPointVM = new ScadaTimeSeriesPointVM(pnt); ScadaPointEditForm.DataContext = scadaTimeSeriesPointVM; }
public async Task FetchAndPlotData() { // stop running fetch tasks if (cts != null) { //cts.Cancel(); return; } // ***Instantiate the CancellationTokenSource. cts = new CancellationTokenSource(); // clear the current series of the plot SeriesCollection.Clear(); // get each point data and bind it to the Series Collection // iterate through each timeseries point in the config for (int i = 0; i < LinePlotCellConfig_.TimeSeriesPoints_.Count; i++) { IDashboardTimeSeriesPoint pnt = LinePlotCellConfig_.TimeSeriesPoints_.ElementAt(i); if (pnt.GetType().FullName == typeof(DashboardScadaTimeSeriesPoint).FullName) { // handle if the point is a scada point ScadaFetcher scadaFetcher = new ScadaFetcher(); DashboardScadaTimeSeriesPoint scadaTimeseriesPnt = (DashboardScadaTimeSeriesPoint)pnt; // fetch the results from the data fetcher List <ScadaPointResult> results = new List <ScadaPointResult>(); try { // ***Send a token to carry the message if cancellation is requested. results = await scadaFetcher.FetchHistoricalPointDataAsync(scadaTimeseriesPnt, cts.Token); } // *** If cancellation is requested, an OperationCanceledException results. catch (OperationCanceledException) { AddLinesToConsole("Existing Fetch task cancelled"); } catch (Exception e) { AddLinesToConsole($"Error in running fetch task: {e.Message}"); } if (results == null) { // handle the null results by printing in the console AddLinesToConsole("No values returned after fetching"); results = new List <ScadaPointResult>(); } // Get Plot Values from ScadaResults List <double> plotVals = new List <double>(); for (int resCounter = 0; resCounter < results.Count; resCounter++) { ScadaPointResult res = results[resCounter]; plotVals.Add(res.Val_); } // change the plot values spacing in secs and the starting dateTime if (i == 0) { MeasPeriodSecs_ = scadaTimeseriesPnt.FetchPeriodSecs_; if (plotVals.Count > 0) { StartDateTime_ = new DateTime(results[0].ResultTime_.Ticks); } } // getting the required color string colorString = scadaTimeseriesPnt.ColorString_; if (colorString == null) { colorString = "NULL"; } SolidColorBrush requiredColorBrush; try { requiredColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(colorString)); } catch (Exception ex) { requiredColorBrush = null; //AddLinesToConsole($"Unable to parse the color string \"{colorString}\""); } // Add the Data Point fetch results to the Plot lines Collection SeriesCollection.Add(new GLineSeries() { Title = scadaTimeseriesPnt.ScadaPoint_.Name_, Values = new GearedValues <double>(plotVals), PointGeometry = null, Fill = Brushes.Transparent, StrokeThickness = scadaTimeseriesPnt.StrokeThickness_, LineSmoothness = 0, Stroke = requiredColorBrush }); // https://stackoverflow.com/questions/2109756/how-do-i-get-the-color-from-a-hexadecimal-color-code-using-net } } // ***Set the CancellationTokenSource to null when the download is complete. cts = null; }