public ScadaPointResult FetchCurrentPointData(ScadaDataPoint point)
        {
            // Fetch a realtime value of the point
            DateTime timestamp = DateTime.Now;

            //get Realtime value
            int nret = RealTime.DNAGetRTAll(point.Id_, out double dval, out timestamp, out string status, out string desc, out string units);

            ScadaPointResult scadaPointResult;

            if (nret == 0)
            {
                scadaPointResult = new ScadaPointResult(dval, status, timestamp, units);
                return(scadaPointResult);
            }
            return(null);
        }
Exemple #2
0
        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;
        }