public InterpolationHistory(double startTimestamp, ValueType startValue) { history = new TimeValuePair[arraySize]; currentIndex = 0; nextIndex = 1; history[currentIndex].Time = startTimestamp; history[currentIndex].Value = startValue; cacheValid = false; }
public void ConsumeEvent(VMSEventArgs e) { var newValue = (e as VMSPidValueEventArgs).segment.StandardValue; var newMetricValue = (e as VMSPidValueEventArgs).segment.MetricValue; counter++; TimeValuePair recordStruct = new TimeValuePair(counter, newMetricValue, newValue); if (recordStruct.value > MaxValue) { return; } if (recordStruct.value >= ValueArray[Right].value) { ValueArray[Left] = recordStruct; Right = Left; Value = ValueArray[Right].value; MetricValue = ValueArray[Right].valueMetric; PublishValues(); return; } bool roomForMore = (ValueArray[Left].value < recordStruct.value); while (ValueArray[Left].value < recordStruct.value) { Constants.SafeIndexAdd(ref Left, 1, BufferSize); } Constants.SafeIndexAdd(ref Left, -1, BufferSize); if (Left != Right) { roomForMore = true; } if (roomForMore) { ValueArray[Left] = recordStruct; } else { Constants.SafeIndexAdd(ref Left, 1, BufferSize); if (ValueArray[Left].time < (counter - TimeSpan)) { ValueArray[Left] = recordStruct; } } if (counter > TimeSpan) { while (ValueArray[Right].time < (counter - TimeSpan)) { Constants.SafeIndexAdd(ref Right, -1, BufferSize); } } }
/* * This is the function responsible for sending the request to the API. * The url that other functions have created is taken as an argument. * Boolean flag is just for warning display. * This function is called multiple times if the time period is longer than a week. * Returns a list of plots. */ public static async Task <List <IDataSeries> > GetSingleData(string url, bool is_first_chunk = false) { var httpResponse = await _client.GetAsync(url); var bytes = await httpResponse.Content.ReadAsByteArrayAsync(); var body = System.Text.Encoding.Default.GetString(bytes); var doc = new XmlDocument(); doc.LoadXml(body); XmlNamespaceManager mng = CreateManager(doc); // Handling erroneous parameters XmlElement Root = doc.DocumentElement; if (Root.Name == "ExceptionReport") { XmlNode ExcTextNode = Root.FirstChild.FirstChild; if (ExcTextNode != null) { string ExceptionMessage = ExcTextNode.InnerText; throw new Exception(ExceptionMessage); } } // Handling missing data parameters else if (Root.Attributes != null && Root.Attributes["numberMatched"] != null) { string num_of_datasets = Root.Attributes["numberMatched"].Value; if (num_of_datasets == "0") { throw new Exception("None of the selected datatypes is available for this area!"); } } var result_nodes = doc.SelectNodes("//om:result", mng); List <IDataSeries> plots = new List <IDataSeries>(); // List of formats to display which graphs are missing List <string> missing_graphs = new List <string>(); List <string> found_graphs = new List <string>(); foreach (XmlNode result in result_nodes) { // Creating the series and setting all the necessary data to plot List <Tuple <DateTime, IData> > series = new List <Tuple <DateTime, IData> >(); TypeFormat typeformat = GetTypeFormat(result, mng); DataFormat format = GetFormat(typeformat); // Sorting plots by associated parameter description string plot_parameter = GetParameter(result, mng); string plot_name = PARAMETERS[plot_parameter]; // With the above setting we can create an instance of DataSeries var plot = DataSeriesFactory.CreateDataSeries(plot_name, format, series);; // VALUETIMEPAIR var PairLst = result.SelectNodes(".//wml2:MeasurementTVP", mng); // Flag checks if there are non-NaN values, this is a very lazy solution but it works bool all_NaN = true; foreach (XmlNode TimeValuePair in PairLst) { DateTime time = Convert.ToDateTime(TimeValuePair.SelectSingleNode(".//wml2:time", mng).InnerText); double.TryParse(TimeValuePair.SelectSingleNode(".//wml2:value", mng).InnerText, NumberStyles.Any, CultureInfo.InvariantCulture, out double value); if (Double.IsNaN(value)) { continue; } // If this is reached then at least 1 value isn't NaN all_NaN = false; // Getting the correct weather class, i.e. Temperature, Humidity etc based on parameter dynamic data = GetType(typeformat, value); Tuple <DateTime, IData> plotPoint = new Tuple <DateTime, IData>(time, data); series.Add(plotPoint); } if (all_NaN) { if (!missing_graphs.Contains(plot_name)) { missing_graphs.Add(plot_name); } } else { if (!found_graphs.Contains(plot_name)) { found_graphs.Add(plot_name); } plots.Add(plot); } } // Showing the user which graphs are missing (if any) if (missing_graphs.Any() && is_first_chunk) { TellAboutGraphs(missing_graphs, found_graphs); } return(plots); }