2025-04-25 17:33:11 -07:00

99 lines
2.7 KiB
Dart

// Functions related to parsing JSON strings and objects.
import '/var/config.dart';
import 'package:flutter/material.dart';
import 'dart:math';
String formatOpenWeatherData(var data) {
String location = pullOpenWeatherCity(data),
temp = pullOpenWeatherTemp(data),
conditions = pullOpenWeatherConditions(data),
windSpeed = pullOpenWeatherWind(data),
humidity = pullOpenWeatherHumidity(data);
String windChill = getWindChill(temp, windSpeed), heatIndex = "";
String comfort = "";
String text =
"$location is $temp$tempUnits and $conditions"
" with a wind speed of $windSpeed$windUnits"
" and humidity of $humidity$humidityUnits."
" $windChill$heatIndex"
" $comfort";
final String doubleSpace = " ", singleSpace = " ";
while (text.contains(doubleSpace)) {
text = text.replaceAll(doubleSpace, singleSpace);
}
return text;
}
String pullOpenWeatherCity(var data) {
String location = "${data['city']['name']} (${data['city']['country']})";
if (debug) {
debugPrint("DEBUG: location = '$location'");
}
return location;
}
String pullOpenWeatherTemp(var data) {
String temp = data['list'][0]['main']['temp'].toString();
if (debug) {
debugPrint("DEBUG: temp = '$temp'");
}
return temp;
}
String pullOpenWeatherConditions(var data) {
String conditions = data['list'][0]['weather'][0]['description'].toString();
if (debug) {
debugPrint("DEBUG: conditions = '$conditions'");
}
return conditions;
}
String pullOpenWeatherWind(var data) {
String wind = data['list'][0]['wind']['speed'].toString();
if (debug) {
debugPrint("DEBUG: wind = '$wind'");
}
return wind;
}
String pullOpenWeatherHumidity(var data) {
String humidity = data['list'][0]['main']['humidity'].toString();
if (debug) {
debugPrint("DEBUG: humidity = '$humidity'");
}
return humidity;
}
double calcWindChill(double temp, double windSpeed) {
// ## Wind Chill ##
// # Wind speed as noted in: https://answers.yahoo.com/question/index?qid=20091020183148AAHm3kB&guccounter=1
// # More official source: https://www.weather.gov/media/epz/wxcalc/windChill.pdf
double windChill =
35.74 +
(0.6215 * temp) -
(35.75 * pow(windSpeed, 0.16)) +
(0.4275 * temp * pow(windSpeed, 0.16));
if (debug) {
debugPrint("DEBUG: windChill = '$windChill'");
}
return windChill;
}
String getWindChill(String temp, String windSpeed) {
double temperature = double.parse(temp);
double wind = double.parse(windSpeed);
if (temperature < 50 && wind > 5) {
double windChill = calcWindChill((temperature), (wind));
return "My guess is that's a wind chill of $windChill$tempUnits";
}
return "";
}