private void PerformRequest() { UsftClient client = new UsftClient(txtUser.Text, txtPassword.Text, UsftClient.AuthenticationMode.Basic); Output("Testing connection..."); var testResult = client.TestConnection(); if (!testResult) { Output("Test FAILED. Please check your user name and password, and try again."); return; } Output("Test succeeded. Requesting and parsing data..."); var requestResult = GetResults(client, dtpFrom.Value, dtpTo.Value); Output("Data retrieved and parsed. " + requestResult.Count + " rows found."); Output("Formatting..."); try { Formatter.Save(requestResult, txtFileLocation.Text).Wait(); } catch (Exception exc) { Output("Error encountered while saving file: " + exc.Message); return; } Output("File saved."); }
private List <DeviceLocation> GetResults(UsftClient client, DateTime requestedStart, DateTime requestedEnd) { bool breakDownRequestByHour = false; if (requestedEnd - requestedStart > TimeSpan.FromHours(1)) { var deviceList = client.GetDeviceLocations(); // retrieve the number of devices with recent updates if (breakDownRequestByHour = deviceList.Count > 50) { Output("Large account detected! Breaking request down hour by hour. This may take a little while."); } } if (breakDownRequestByHour) { var start = requestedStart; var limit = requestedEnd.AddHours(-1); List <DeviceLocation> ret = new List <DeviceLocation>(); while (start < limit) { var end = start.AddHours(1).AddSeconds(-1); Output("Requesting " + start.ToString() + " to " + end.ToString()); ret.AddRange(client.GetHistoryFromTo(null, start, end)); start = end.AddSeconds(1); } Output("Requesting " + start.ToString() + " to " + requestedEnd.ToString()); ret.AddRange(client.GetHistoryFromTo(null, start, requestedEnd)); return(ret); } else { return(client.GetHistoryFromTo(null, requestedStart, requestedEnd)); } }
async Task PrimaryLoop() { while (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(APIKey)) { if (!working) { return; } await Task.Delay(MILLISECONDS_BETWEEN_ITERATION_CHECKS); ConfigurationManager.RefreshSection("appSettings"); } var client = new UsftClient(UserName, APIKey); DateTime lastRetrieval; TimeSpan betweenRetrievals = TimeSpan.FromSeconds(SecondsBetweenRetrievals); while (working) { lastRetrieval = DateTime.UtcNow; List <DeviceLocation> results = null; try { results = client.GetDeviceLocations(); } catch (RestException exc) { ExceptionHandler.Output("The RestClient could not retrieve the device location data", exc); } catch (Exception exc) { ExceptionHandler.Output("The RestClient encountered an unexpected exception while retrieving device location data", exc); } if (results != null) { await Formatter.Save(results); } while ((DateTime.UtcNow - lastRetrieval) < betweenRetrievals && working) { await Task.Delay(MILLISECONDS_BETWEEN_ITERATION_CHECKS); } } }