/// <summary> /// Get the data values for the specific site, variable and time range as a list of Series objects /// </summary> /// <param name="siteCode">the full site code (networkPrefix:siteCode)</param> /// <param name="variableCode">the full variable code (vocabularyPrefix:variableCode)</param> /// <param name="startTime">the start date/time</param> /// <param name="endTime">the end date/time</param> /// <returns>The data series. Each data series object includes a list of data values, /// site, variable, method, source and quality control level information.</returns> /// <remarks>Usually the list of Series returned will only contain one series. However in some /// cases, it will contain two or more series with the same site code and variable code, but /// with a different method or quality control level</remarks> public IList <Series> GetValues(string siteCode, string variableCode, DateTime startTime, DateTime endTime) { IList <Series> result; var req = WebServiceHelper.CreateGetValuesRequest(_serviceURL, siteCode, variableCode, startTime, endTime); using (var resp = (HttpWebResponse)req.GetResponse()) { using (var stream = resp.GetResponseStream()) { result = _parser.ParseGetValues(stream); } } return(result); }
/// <summary> /// Executes same methods as GetValues only with TAP Task interface support for including in an asynchronous pipeline. /// </summary> /// <param name="siteCode"></param> /// <param name="variableCode"></param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <see cref="http://stackoverflow.com/questions/10565090/getting-the-response-of-a-asynchronous-httpwebrequest"/> /// <returns></returns> public async Task <IList <Series> > GetValuesAsync(string siteCode, string variableCode, DateTime startTime, DateTime endTime) { await waitAsyncSemaphore(_serviceURL); //Wait for access to the semaphore!! try { //Create request... HttpWebRequest req = WebServiceHelper.CreateGetValuesRequest(_serviceURL, siteCode, variableCode, startTime, endTime); //req.Timeout = 60000; //Timeout not used in asynchronous tasks?? //Create task... Task <WebResponse> getTask = Task.Factory.FromAsync <WebResponse>(req.BeginGetResponse, req.EndGetResponse, (object)null); //Wait for task completion... await getTask; //Check for task exception... if (getTask.IsFaulted) { Exception ex = getTask.Exception.InnerExceptions.First(); throw ex; } return(_parser.ParseGetValues(ReadStreamFromResponse(getTask.Result))); } catch (Exception ex) { LogHelper.LogGetAsyncDataValuesException(_serviceURL, siteCode, variableCode, startTime, endTime, ex); return(new List <Series>()); } finally { //ALWAYS release the semaphore... releaseSemaphore(_serviceURL);; } }
/// <summary> /// TAP-interface method for acquiring parsed and stream versions of WaterOneFlow document. /// </summary> /// <param name="siteCode"></param> /// <param name="variableCode"></param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="waterOneflowTimeoutMilliseconds">The number of milliseconds to spend waiting for WaterOneFlow service response before declaring it unavailable.</param> /// <returns></returns> public async Task <Tuple <Stream, IList <Series> > > GetValuesAndRawStreamAsync(string siteCode, string variableCode, DateTime startTime, DateTime endTime, int waterOneflowTimeoutMilliseconds) { await waitAsyncSemaphore(_serviceURL); try { //Create request... HttpWebRequest req = WebServiceHelper.CreateGetValuesRequest(_serviceURL, siteCode, variableCode, startTime, endTime); //req.Timeout = waterOneflowTimeoutMilliseconds; //Timeout not used in asynchronous tasks?? //Create task... Task <WebResponse> getTask = Task.Factory.FromAsync <WebResponse>(req.BeginGetResponse, req.EndGetResponse, (object)null); //Wait for task completion... await getTask; //Check for task exception... if (getTask.IsFaulted) { Exception ex = getTask.Exception.InnerExceptions.First(); throw ex; } Stream s = ReadStreamFromResponse(getTask.Result); IList <Series> listSeries = _parser.ParseGetValues(s); #if (DEBUG) //BCC - 28-Sep-2016 - TO DO - Code not working - research needed... //Write Series list to a file... //if (EnvironmentContext.LocalEnvironment()) //{ //using (System.IO.StreamWriter swSeries = System.IO.File.CreateText(@"C:\CUAHSI\Series.json")) //{ // JsonSerializer jsonser = new JsonSerializer(); // swSeries.Write('['); //Write start of array... // if ( listSeries.Count > 0 ) // { // for (int i = 0; i < listSeries.Count; i++) // { // try // { // jsonser.Serialize(swSeries, listSeries[i]); // } // catch (Exception ex) // { // if ((i + 1) < listSeries.Count) // { // swSeries.Write(','); // } // continue; // } // if ( (i + 1) < listSeries.Count) // { // swSeries.Write(','); // } // } // } // swSeries.Write(']'); //Write end of array... //} //} if (1 < listSeries.Count) { int n = 5; ++n; } #endif return(new Tuple <Stream, IList <Series> >(s, listSeries)); } catch (Exception ex) { LogHelper.LogGetAsyncDataValuesException(_serviceURL, siteCode, variableCode, startTime, endTime, ex); throw ex; } finally { //ALWAYS release the semaphore... releaseSemaphore(_serviceURL); } }