mirror of
https://github.com/MistWeatherMedia/WeatherSTAR-3000-Java.git
synced 2026-11-28 14:38:08 -08:00
122 lines
4.8 KiB
Java
122 lines
4.8 KiB
Java
package WS3000.weatherstar3000;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import util.Utilities;
|
|
|
|
import java.net.URI;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpResponse;
|
|
import java.util.ArrayList;
|
|
|
|
public class HourlyObservations extends Thread {
|
|
ArrayList<String> locName = new ArrayList<>();
|
|
ArrayList<String> temperature = new ArrayList<>();
|
|
ArrayList<String> condition = new ArrayList<>();
|
|
ArrayList<String> wind = new ArrayList<>();
|
|
Utilities utl = new Utilities();
|
|
|
|
String getLocName(String displayName) {
|
|
return utl.ljust(displayName, 14, " ").toUpperCase().substring(0, 14);
|
|
}
|
|
String getTemperature(String icaoCode, String key) {
|
|
try {
|
|
HttpClient Client = HttpClient.newBuilder().build();
|
|
HttpRequest Request = HttpRequest.newBuilder()
|
|
.uri(URI.create("https://api.weather.com/v3/wx/observations/current?icaoCode=" + icaoCode + "&units=e&language=en-US&format=json&apiKey=" + key))
|
|
.GET()
|
|
.header("Accept", "application/json")
|
|
.build();
|
|
try {
|
|
HttpResponse<String> Response = Client.send(Request, HttpResponse.BodyHandlers.ofString());
|
|
if (Response.statusCode() != 200) {
|
|
return " ";
|
|
}
|
|
String ResponseBody = Response.body();
|
|
JSONObject JSONResponse = new JSONObject(ResponseBody);
|
|
|
|
return utl.rjust(Integer.toString(JSONResponse.getInt("temperature")), 3, " ");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return " ";
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return " ";
|
|
}
|
|
}
|
|
String getCondition(String icaoCode, String key) {
|
|
try {
|
|
HttpClient Client = HttpClient.newBuilder().build();
|
|
HttpRequest Request = HttpRequest.newBuilder()
|
|
.uri(URI.create("https://api.weather.com/v3/wx/observations/current?icaoCode=" + icaoCode + "&units=e&language=en-US&format=json&apiKey=" + key))
|
|
.GET()
|
|
.header("Accept", "application/json")
|
|
.build();
|
|
try {
|
|
HttpResponse<String> Response = Client.send(Request, HttpResponse.BodyHandlers.ofString());
|
|
if (Response.statusCode() != 200) {
|
|
return "NO REPORT";
|
|
}
|
|
String ResponseBody = Response.body();
|
|
JSONObject JSONResponse = new JSONObject(ResponseBody);
|
|
|
|
return utl.getCondition(JSONResponse.getInt("iconCodeExtend"));
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return "NO REPORT";
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return "NO REPORT";
|
|
}
|
|
}
|
|
String getWind(String icaoCode, String key) {
|
|
try {
|
|
HttpClient Client = HttpClient.newBuilder().build();
|
|
HttpRequest Request = HttpRequest.newBuilder()
|
|
.uri(URI.create("https://api.weather.com/v3/wx/observations/current?icaoCode=" + icaoCode + "&units=e&language=en-US&format=json&apiKey=" + key))
|
|
.GET()
|
|
.header("Accept", "application/json")
|
|
.build();
|
|
try {
|
|
HttpResponse<String> Response = Client.send(Request, HttpResponse.BodyHandlers.ofString());
|
|
if (Response.statusCode() != 200) {
|
|
return " ";
|
|
}
|
|
String ResponseBody = Response.body();
|
|
JSONObject JSONResponse = new JSONObject(ResponseBody);
|
|
|
|
int windDir = JSONResponse.getInt("windDirection");
|
|
int windSpeed = JSONResponse.getInt("windSpeed");
|
|
|
|
return utl.formatWind(windSpeed, windDir, false);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return " ";
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return " ";
|
|
}
|
|
}
|
|
public void run() {
|
|
ArrayList<String> nearNames = new ArrayList<>();
|
|
nearNames.addAll(Main.nearNames);
|
|
ArrayList<String> nearIcaos = new ArrayList<>();;
|
|
nearIcaos.addAll(Main.nearIcaos);
|
|
String key = Main.key;
|
|
Main.hourlyObservations.locName.clear();
|
|
Main.hourlyObservations.temperature.clear();
|
|
Main.hourlyObservations.condition.clear();
|
|
Main.hourlyObservations.wind.clear();
|
|
for (int i = 0; i < nearIcaos.size(); i++) {
|
|
Main.hourlyObservations.locName.add(getLocName(nearNames.get(i)));
|
|
Main.hourlyObservations.temperature.add(getTemperature(nearIcaos.get(i), key));
|
|
Main.hourlyObservations.condition.add(getCondition(nearIcaos.get(i), key));
|
|
Main.hourlyObservations.wind.add(getWind(nearIcaos.get(i), key));
|
|
}
|
|
}
|
|
}
|