Add the heat index as well as some error handling.
This commit is contained in:
parent
7ffcfee1d3
commit
6e2d293cdc
@ -10,26 +10,30 @@ import 'dart:convert';
|
|||||||
Future<String> hitAPI(String url) async {
|
Future<String> hitAPI(String url) async {
|
||||||
if (debug) debugPrint("DEBUG: URL is '$url'.");
|
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 = "";
|
String data = "";
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
data = response.body;
|
data = response.body;
|
||||||
var decodedData = jsonDecode(data);
|
var decodedData = jsonDecode(data);
|
||||||
//data = decodedData.toString();
|
//data = decodedData.toString();
|
||||||
//data = jsonEncode(decodedData);
|
//data = jsonEncode(decodedData);
|
||||||
if (debugVerbose) {
|
if (debugVerbose) {
|
||||||
debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n");
|
debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (debug) {
|
if (debug) {
|
||||||
debugPrint(
|
debugPrint(
|
||||||
"DEBUG: Response failed with code '${response.statusCode.toString()}'",
|
"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
|
// How to guide: https://openweathermap.org/current#zip
|
||||||
|
@ -11,7 +11,8 @@ String formatOpenWeatherData(var data) {
|
|||||||
windSpeed = pullOpenWeatherWind(data),
|
windSpeed = pullOpenWeatherWind(data),
|
||||||
humidity = pullOpenWeatherHumidity(data);
|
humidity = pullOpenWeatherHumidity(data);
|
||||||
|
|
||||||
String windChill = getWindChill(temp, windSpeed), heatIndex = "";
|
String windChill = getWindChill(temp, windSpeed),
|
||||||
|
heatIndex = getHeatIndex(temp, humidity);
|
||||||
|
|
||||||
String comfort = "";
|
String comfort = "";
|
||||||
|
|
||||||
@ -19,7 +20,8 @@ String formatOpenWeatherData(var data) {
|
|||||||
"$location is $temp$tempUnits and $conditions"
|
"$location is $temp$tempUnits and $conditions"
|
||||||
" with a wind speed of $windSpeed$windUnits"
|
" with a wind speed of $windSpeed$windUnits"
|
||||||
" and humidity of $humidity$humidityUnits."
|
" and humidity of $humidity$humidityUnits."
|
||||||
" $windChill$heatIndex"
|
" $windChill"
|
||||||
|
" $heatIndex"
|
||||||
" $comfort";
|
" $comfort";
|
||||||
|
|
||||||
final String doubleSpace = " ", singleSpace = " ";
|
final String doubleSpace = " ", singleSpace = " ";
|
||||||
@ -89,10 +91,54 @@ String getWindChill(String temp, String windSpeed) {
|
|||||||
double temperature = double.parse(temp);
|
double temperature = double.parse(temp);
|
||||||
double wind = double.parse(windSpeed);
|
double wind = double.parse(windSpeed);
|
||||||
|
|
||||||
if (temperature < 50 && wind > 5) {
|
if ((temperature < 50 && wind > 5) || debug) {
|
||||||
double windChill = calcWindChill((temperature), (wind));
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,10 @@ class _MainAppState extends State<MainApp> {
|
|||||||
weather = "Please enter a location.";
|
weather = "Please enter a location.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (weather.toString().contains("Sorry!")) {
|
||||||
|
return weather;
|
||||||
|
}
|
||||||
|
|
||||||
if (weather != loadText) {
|
if (weather != loadText) {
|
||||||
if (debug) {
|
if (debug) {
|
||||||
debugPrint("DEBUG: Formatting text.");
|
debugPrint("DEBUG: Formatting text.");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user