public async Task <ActionResult> BatteryPredictionAsync(int batteryAgeDays, double batteryRatedCycles, double lifetimeBatteryCyclesUsed, double dailyTripDuration) { var payload = new BatteryPredictionPayload(batteryAgeDays, dailyTripDuration); var httpClient = _clientFactory.CreateClient(NamedHttpClients.ScoringService); // Create the payload to send to the Logic App. var postBody = JsonConvert.SerializeObject(payload); var httpResponse = await httpClient.PostAsync(_configuration["ScoringUrl"], new StringContent(postBody, Encoding.UTF8, "application/json")); httpResponse.EnsureSuccessStatusCode(); var result = BatteryPredictionResult.FromJson(await httpResponse.Content.ReadAsStringAsync()); // The results return in an array of doubles. We only expect a single result for this prediction. var predictedDailyCyclesUsed = result.Result[0]; // Multiply the predictedCyclesConsumed * 30 (days), add that value to the lifetime cycles used, then see if it exceeds the battery's rated cycles. var predictedToFail = predictedDailyCyclesUsed * 30 + lifetimeBatteryCyclesUsed > batteryRatedCycles; return(Json(predictedToFail)); }
public async Task <ActionResult> BatteryPredictionAsync ( string vin, int batteryAgeDays, double batteryRatedCycles, double lifetimeBatteryCyclesUsed, double tripDurationMinutes ) { string scoringUrl = _configuration["ScoringUrl"]; string apiKey = _configuration["ScoringKey"]; // Azure ML Designer-deployed real time inferencing endpoint schema var payload = new { Inputs = new Dictionary <string, List <Dictionary <string, string> > >() { { "WebServiceInput0", new List <Dictionary <string, string> >() { new Dictionary <string, string>() { { "vin", vin }, { "batteryAgeDays", batteryAgeDays.ToString() }, { "batteryRatedCycles", batteryRatedCycles.ToString() }, { "lifetimeBatteryCyclesUsed", lifetimeBatteryCyclesUsed.ToString() }, { "tripDurationMinutes", tripDurationMinutes.ToString() } } } }, }, GlobalParameters = new Dictionary <string, string>() { } }; var httpClient = _clientFactory.CreateClient(WellKnown.SCORING_SVC_CLIENT); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); // Create the payload to send to the Scoring service var postBody = JsonConvert.SerializeObject(payload); var httpResponse = await httpClient.PostAsync(scoringUrl, new StringContent(postBody, Encoding.UTF8, "application/json")); httpResponse.EnsureSuccessStatusCode(); BatteryPredictionResult result = BatteryPredictionResult.FromJson(await httpResponse.Content.ReadAsStringAsync()); var predictedToFail = result.Results["WebServiceOutput0"][0]["result"]; return(Json(predictedToFail)); }