Successfully pulling a good weather report. Now to add the heat index and wind chill calculations.

This commit is contained in:
Hyperling 2025-04-25 17:10:22 -07:00
parent b1d01c6915
commit 7e53a2b4e2
3 changed files with 135 additions and 12 deletions

View File

@ -8,7 +8,7 @@ import 'dart:convert';
// Generic method to hit a GET request and return the response.
Future<String> hitAPI(String url) async {
if (debug) debugPrint(url);
if (debug) debugPrint("DEBUG: URL is '$url'.");
http.Response response = await http.get(Uri.parse(url));
@ -16,10 +16,17 @@ Future<String> hitAPI(String url) async {
if (response.statusCode == 200) {
data = response.body;
var decodedData = jsonDecode(data);
if (debugVerbose) debugPrint(decodedData.toString());
data = decodedData.toString();
//data = decodedData.toString();
//data = jsonEncode(decodedData);
if (debugVerbose) {
debugPrint("DEBUG-VERBOSE: Response data: \n\n$data\n");
}
} else {
if (debug) debugPrint(response.statusCode.toString());
if (debug) {
debugPrint(
"DEBUG: Response failed with code '${response.statusCode.toString()}'",
);
}
}
return data;
@ -38,8 +45,3 @@ Future<String> hitOpenWeatherZip(String zipCode, String? countryCode) async {
String data = await hitAPI(url);
return data;
}
// Convert the Future value into a usable value.
loadWeather() async {
return await hitOpenWeatherZip("47630", "US");
}

View File

@ -1 +1,72 @@
// Functions related to parsing JSON strings and objects.
import '/var/config.dart';
import 'package:flutter/material.dart';
String formatOpenWeatherData(var data) {
String location = pullOpenWeatherCity(data),
temp = pullOpenWeatherTemp(data),
conditions = pullOpenWeatherConditions(data),
windSpeed = pullOpenWeatherWind(data),
humidity = pullOpenWeatherHumidity(data),
windChill = "",
heatIndex = "",
comfort = "";
// Always ask for Imperial units in API request
final String tempUnits = 'F', windUnits = 'mph', humidityUnits = '%';
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;
}

View File

@ -1,7 +1,10 @@
// Local
import 'dart:convert';
import '/var/secrets.dart';
import '/var/config.dart';
import 'helpers/http.dart';
import 'helpers/json.dart';
// Flutter / Dart
import 'package:flutter/material.dart';
@ -74,10 +77,51 @@ class _MainAppState extends State<MainApp> {
);
}
// Call the
// Convert the Future value into a usable value.
pullWeather({
String? country,
String? zip,
String? city,
String? lat,
String? long,
}) async {
country ??= "US";
if (lat != null && long != null) {
weather = "Lat / Long Not Yet Implemented";
} else if (zip != null) {
weather = await hitOpenWeatherZip(zip, country);
} else if (city != null) {
weather = "City Weather Not Yet Implemented";
} else {
weather = "Please enter a location.";
}
if (weather != loadText) {
if (debug) {
debugPrint("DEBUG: Formatting text.");
}
var weatherObject = jsonDecode(weather);
String weatherString = formatOpenWeatherData(weatherObject);
weather = weatherString;
if (debug) {
debugPrint("DEBUG: Set to formatted weather string.");
}
} else {
if (debug) {
debugPrint("DEBUG: Skipping text formatting.");
}
}
}
// Call the API and put the desired information into the screen variable.
_refreshWeather() {
var lastReloadSeconds = DateTime.now().difference(lastLoadTime).inSeconds;
if (debug) debugPrint("DEBUG: Refresh was $lastReloadSeconds seconds ago.");
if (debug) {
debugPrint("DEBUG: Refresh was '$lastReloadSeconds' seconds ago.");
}
if (lastReloadSeconds < limitRefreshSeconds) {
debugPrint("DEBUG: Skipping reload.");
@ -86,8 +130,14 @@ class _MainAppState extends State<MainApp> {
return;
}
if (debug) {
debugPrint("DEBUG: Pulling weather...");
}
weather = loadText;
weather = loadWeather();
pullWeather(country: "US", zip: "47630");
lastLoadTime = DateTime.now();
if (debug) {
debugPrint("DEBUG: Weather pulled and date is set.");
}
}
}