Add the heat index as well as some error handling.

This commit is contained in:
Hyperling 2025-04-26 08:37:35 -07:00
parent 7ffcfee1d3
commit 6e2d293cdc
3 changed files with 76 additions and 22 deletions

View File

@ -10,26 +10,30 @@ import 'dart:convert';
Future<String> hitAPI(String url) async {
if (debug) debugPrint("DEBUG: URL is '$url'.");
http.Response response = await http.get(Uri.parse(url));
try {
http.Response response = await http.get(Uri.parse(url));
String data = "";
if (response.statusCode == 200) {
data = response.body;
var decodedData = jsonDecode(data);
//data = decodedData.toString();
//data = jsonEncode(decodedData);
if (debugVerbose) {
debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n");
}
} else {
if (debug) {
debugPrint(
"DEBUG: Response failed with code '${response.statusCode.toString()}'",
);
String data = "";
if (response.statusCode == 200) {
data = response.body;
var decodedData = jsonDecode(data);
//data = decodedData.toString();
//data = jsonEncode(decodedData);
if (debugVerbose) {
debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n");
}
} else {
if (debug) {
debugPrint(
"DEBUG: Response failed with code '${response.statusCode.toString()}'",
);
}
}
return data;
} catch (e) {
return "Sorry! It seems we have an issue: '${e.toString()}'.";
}
return data;
}
// How to guide: https://openweathermap.org/current#zip

View File

@ -11,7 +11,8 @@ String formatOpenWeatherData(var data) {
windSpeed = pullOpenWeatherWind(data),
humidity = pullOpenWeatherHumidity(data);
String windChill = getWindChill(temp, windSpeed), heatIndex = "";
String windChill = getWindChill(temp, windSpeed),
heatIndex = getHeatIndex(temp, humidity);
String comfort = "";
@ -19,7 +20,8 @@ String formatOpenWeatherData(var data) {
"$location is $temp$tempUnits and $conditions"
" with a wind speed of $windSpeed$windUnits"
" and humidity of $humidity$humidityUnits."
" $windChill$heatIndex"
" $windChill"
" $heatIndex"
" $comfort";
final String doubleSpace = " ", singleSpace = " ";
@ -89,10 +91,54 @@ String getWindChill(String temp, String windSpeed) {
double temperature = double.parse(temp);
double wind = double.parse(windSpeed);
if (temperature < 50 && wind > 5) {
if ((temperature < 50 && wind > 5) || debug) {
double windChill = calcWindChill((temperature), (wind));
return "My guess is that's a wind chill of $windChill$tempUnits";
return "My guess is that's a wind chill of $windChill$tempUnits.";
}
return "";
}
double calcHeatIndex(double temp, double humidity) {
// ## Heat Index ##
// # Official formula: https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
double heatIndex =
-42.379 +
2.04901523 * temp +
10.14333127 * humidity -
0.22475541 * temp * humidity -
0.00683783 * temp * temp -
0.05481717 * humidity * humidity +
0.00122874 * temp * temp * humidity +
0.00085282 * temp * humidity * humidity -
0.00000199 * temp * temp * humidity * humidity;
if (humidity < 13 && temp >= 80 && temp <= 112) {
heatIndex -= ((13 - humidity) / 4) * sqrt((17 - (temp - 95.0).abs()) / 17);
}
if (humidity > 85 && temp >= 80 && temp <= 87) {
heatIndex += ((humidity - 85) / 10) * ((87 - temp) / 5);
}
if (heatIndex < 80) {
heatIndex =
0.5 * (temp + 61.0 + ((temp - 68.0) * 1.2) + (humidity * 0.094));
heatIndex = (heatIndex + temp) / 2;
}
if (debug) {
debugPrint("DEBUG: heatIndex = '$heatIndex'");
}
return heatIndex;
}
String getHeatIndex(String temp, String humidity) {
double temperature = double.parse(temp);
double humid = double.parse(humidity);
if (temperature > 80 || debug) {
double heatIndex = calcHeatIndex((temperature), (humid));
return "My guess is that's a heat index of $heatIndex$tempUnits.";
}
return "";
}

View File

@ -97,6 +97,10 @@ class _MainAppState extends State<MainApp> {
weather = "Please enter a location.";
}
if (weather.toString().contains("Sorry!")) {
return weather;
}
if (weather != loadText) {
if (debug) {
debugPrint("DEBUG: Formatting text.");