public PredictionDocument Generate(BalloonDocument balloonDocument, ILogger log) { HabHubMessage habHubMessage = new HabHubMessage(); habHubMessage.alt = balloonDocument.Altitude; habHubMessage.burst = balloonDocument.BurstAltitude; habHubMessage.lat = balloonDocument.Latitude; habHubMessage.lon = balloonDocument.Longitude; habHubMessage.ascent = balloonDocument.AveAscent; return(CreateHabHubPrediction(habHubMessage).Result); }
private static async Task <PredictionDocument> CreateHabHubPrediction(HabHubMessage habHubMessage) { // First step, tell HabHub.org to run a prediction. This will give us a Uuid to identify the prediction var content = new FormUrlEncodedContent(habHubMessage.ToParameterDictionary()); var response = await client.PostAsync("http://predict.habhub.org/ajax.php?action=submitForm", content); if (response.IsSuccessStatusCode) { var responseString = await response.Content.ReadAsStringAsync(); var predictionResponse = JsonConvert.DeserializeObject <HabHubPredictionResponse>(responseString); // get uuid var uuid = predictionResponse.uuid; if (String.IsNullOrEmpty(uuid)) { throw new Exception($"Error getting uuid from habhub. No prediction available. HabHubResponse: {responseString}"); } // now check the status of generating the prediction var progressUrl = BuildProgressUrl(uuid); var isComplete = false; var counter = 0; do { var progressResponse = await client.GetAsync(progressUrl); var result = progressResponse.Content.ReadAsStringAsync().Result; if (result.ToLower().Contains("\"pred_complete\": true")) { isComplete = true; } else { counter++; System.Threading.Thread.Sleep(50); } }while (isComplete == false && counter < 5); // wait a max of 250 ms // now get the result records var url = BuildResultsUrl(uuid); response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { var csv = getSanitizedCsv(response).Result; var landingRecord = getLastRecord(csv); var predictionMessage = generatePrediction(landingRecord); if (predictionMessage != null) { return(predictionMessage); } } } return(null); }