/// <summary> /// Gets the historic data. /// Example: /// XivelyData historicData = XivelyData.GetHistoricData (apiKey, feedId, "6hours", "500"); /// </summary> /// <returns>The historic data.</returns> /// <param name="xivelyDataApiKey">Xively data API key.</param> /// <param name="feedId">Feed identifier.</param> /// <param name="duration">Duration.</param> /// <param name="limit">Limit.</param> public static XivelyData GetHistoricData(string xivelyDataApiKey, string feedId, string duration, string limit) { try { // send request to specified feedId WebRequest request = WebRequest.Create("https://api.xively.com/v2/feeds/" + feedId + "?duration=" + duration + "&limit=" + limit + "&function=average"); // add api key, for verification request.Headers.Add("X-ApiKey", xivelyDataApiKey); // get response WebResponse response = request.GetResponse(); // translate data to string Stream rawdata = response.GetResponseStream(); StreamReader reader = new StreamReader(rawdata); string jsonData = reader.ReadToEnd(); // translate string (JSON) into new weather info object XivelyData data = JsonConvert.DeserializeObject <XivelyData> (jsonData); // validation complete (no exeption) UserSettings.CorrectKey = true; // return data return(data); } catch { UserSettings.CorrectKey = false; return(null); } }
/// <summary> /// Gets the current data. /// Example: /// XivelyData data = XivelyData.GetCurrentData (apiKey, feedId); /// </summary> /// <returns>The current data.</returns> /// <param name="xivelyDataApiKey">Xively data API key.</param> /// <param name="feedId">Feed identifier.</param> public static XivelyData GetCurrentData(string xivelyDataApiKey, string feedId) { try { // send request to specified feedId WebRequest request = WebRequest.Create("https://api.xively.com/v2/feeds/" + feedId + ".json"); // debug Console.WriteLine(xivelyDataApiKey); // add api key, for verification request.Headers.Add("X-ApiKey", xivelyDataApiKey); // get response WebResponse response = request.GetResponse(); Stream rawdata = response.GetResponseStream(); StreamReader reader = new StreamReader(rawdata); string jsonData = reader.ReadToEnd(); // translate string (JSON) into new weather info object XivelyData data = JsonConvert.DeserializeObject <XivelyData> (jsonData); // sets up correct key UserSettings.CorrectKey = true; // return data return(data); } catch { UserSettings.CorrectKey = false; return(null); } }
/// <summary> /// Retrieves the data. /// </summary> public void RetrieveData() { if (CurrentData.HistoricDataStored) { XivelyData newData = CurrentData.HistroicData; if (newData.datastreams.Count < 1) { Console.WriteLine("Datastream Not Found"); } else { for (int i = 0; i < newData.datastreams.Count; i++) { if (newData.datastreams [i].id == DataStreamId) { if (newData.datastreams [i].datapoints != null) { int graph_dataP = newData.datastreams [i].datapoints.Count; _totalDataPoints = graph_dataP; _graphData = new double[_totalDataPoints]; p1Xcoordinates = new double[_totalDataPoints]; p1Ycoordinates = new double[_totalDataPoints]; for (int j = 0; j < _totalDataPoints; j++) { double d; string str = newData.datastreams [i].datapoints [j].value.ToString(); if (Double.TryParse(str, out d)) // if done, then is a number { _graphData [j] = Convert.ToDouble(newData.datastreams [i].datapoints [j].value); } // if (newData.datastreams [i].datapoints [j].value.ToString() != "nan"){ // // _graphData [j] = Convert.ToDouble (newData.datastreams [i].datapoints [j].value); // // } else { // _graphData [j] = 0; // } } } else { Console.WriteLine("no dataspoints"); } } } } } }
/// <summary> /// Collects the historic data. /// </summary> private static void CollectHistoric() { if (UserSettings.UserSetupCompleted) { if (!HistoricDataIsReceived) { HistroicData = XivelyData.GetHistoricData(UserSettings.XivelyApiKey, UserSettings.XivelyFeedId, "6hours", "500"); if (HistroicData != null) { HistoricDataIsReceived = true; HistoricDataStored = true; } } else { HistoricDataIsReceived = false; } } }
/// <summary> /// Collect the data. /// </summary> private static void Collect() { if (UserSettings.UserSetupCompleted) { if (!DataIsReceived) { Data = XivelyData.GetCurrentData(UserSettings.XivelyApiKey, UserSettings.XivelyFeedId); if (Data != null) { DataIsReceived = true; DataStored = true; } } else { DataIsReceived = false; } } }