Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
rlanning2
OpenWeather REST and file connector
Commits
077a681c
Commit
077a681c
authored
Nov 07, 2021
by
Christopher Bohn
🤔
Browse files
Can report current data from "onecall" dataset
parent
00a3d60b
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/main/java/edu/unl/cse/soft160/json_connections/Demonstration.java
View file @
077a681c
...
...
@@ -16,18 +16,7 @@ import java.util.List;
import
java.util.Scanner
;
public
class
Demonstration
{
public
static
void
main
(
String
...
args
)
{
String
apiKey
=
null
;
try
{
apiKey
=
RestConnection
.
getApiKey
(
"openweathermap"
);
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"IO Exception: "
+
ioException
.
getClass
());
System
.
err
.
println
(
"\t"
+
ioException
.
getMessage
());
System
.
err
.
println
(
"Caused by: "
+
ioException
.
getCause
());
System
.
err
.
println
(
"\t"
+
ioException
.
getCause
().
getMessage
());
}
Scanner
scanner
=
new
Scanner
(
System
.
in
);
private
static
String
getDataSet
(
Scanner
scanner
)
{
List
<
String
>
dataSets
=
new
ArrayList
<>(
OpenWeatherConnector
.
allowableDataSets
);
for
(
int
i
=
0
;
i
<
dataSets
.
size
();
++
i
)
{
System
.
out
.
println
((
i
+
1
)
+
". "
+
dataSets
.
get
(
i
));
...
...
@@ -36,15 +25,17 @@ public class Demonstration {
int
choice
=
scanner
.
nextInt
();
scanner
.
nextLine
();
String
dataSet
=
dataSets
.
get
(
choice
-
1
);
OpenWeatherConnector
weather
=
new
OpenWeatherConnector
(
dataSet
,
apiKey
);
Instant
now
=
Instant
.
now
();
String
query
;
// this is a might be a candidate for a switch expression
switch
(
dataSet
)
{
// (https://docs.oracle.com/en/java/javase/17/language/switch-expressions.html)
case
"weather"
:
// but Java 11 doesn't have that feature
return
dataSets
.
get
(
choice
-
1
);
}
private
static
String
getData
(
OpenWeatherConnector
weather
,
String
dataSet
,
Instant
now
)
{
String
query
;
switch
(
dataSet
)
{
case
"weather"
:
case
"forecast"
:
query
=
"zip=68588"
;
break
;
case
"onecall"
:
case
"air_pollution"
:
case
"air_pollution/forecast"
:
query
=
"lat=40.81506358&lon=-96.7048613"
;
...
...
@@ -72,9 +63,137 @@ public class Demonstration {
System
.
err
.
println
(
"\t"
+
ioException
.
getCause
().
getMessage
());
}
}
return
data
;
}
System
.
out
.
println
(
"Raw JSON: "
+
data
);
private
static
void
optionallySaveDataToFile
(
String
data
,
String
dataSet
,
Scanner
scanner
)
{
System
.
out
.
print
(
System
.
lineSeparator
()
+
"Do you wish to save this data to a file Y/(N)? "
);
String
answer
=
scanner
.
nextLine
();
if
(
answer
.
length
()
>
0
&&
answer
.
toUpperCase
().
charAt
(
0
)
==
'Y'
)
{
System
.
out
.
print
(
"What shall the file's name be? "
);
String
filename
=
scanner
.
nextLine
();
if
(!
filename
.
endsWith
(
".json"
))
{
filename
+=
".json"
;
}
FileConnection
fileConnection
=
new
FileConnection
(
dataSet
);
try
{
System
.
out
.
println
(
"Saving..."
);
fileConnection
.
save
(
filename
,
(
JSONObject
)
new
JSONParser
().
parse
(
data
));
System
.
out
.
println
(
"\t"
+
filename
+
" saved"
);
}
catch
(
ParseException
|
IOException
exception
)
{
exception
.
printStackTrace
();
}
}
else
{
System
.
out
.
println
(
"Not saving data."
);
}
}
private
static
void
reportAirPollutionHistoryOrForecastData
(
OpenWeatherConnector
weather
,
Instant
now
)
{
Date
timestamp
;
timestamp
=
weather
.
getTimestamps
().
get
(
weather
.
getTimestamps
().
size
()
/
2
);
System
.
out
.
println
(
"The air quality at "
+
timestamp
+
(
timestamp
.
toInstant
().
isBefore
(
now
)
?
" was "
:
" will be "
)
+
weather
.
getAirQualityIndex
(
timestamp
)
+
" pollutant concentration"
);
System
.
out
.
println
(
"CO: "
+
weather
.
getCarbonMonoxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"NO: "
+
weather
.
getNitrogenMonoxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"NO2: "
+
weather
.
getNitrogenDioxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"O3: "
+
weather
.
getOzone
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"SO2: "
+
weather
.
getSulfurDioxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"NH3: "
+
weather
.
getAmmonia
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"Fine particulate matter: "
+
weather
.
getFineParticulateMatter
(
timestamp
)
+
" "
+
"μg/m3"
);
System
.
out
.
println
(
"Coarse particulate matter: "
+
weather
.
getCoarseParticulateMatter
(
timestamp
)
+
" "
+
"μg/m3"
);
}
private
static
void
reportAirPollutionData
(
OpenWeatherConnector
weather
)
{
System
.
out
.
println
(
"The current air quality is "
+
weather
.
getAirQualityIndex
()
+
" pollutant concentration"
);
System
.
out
.
println
(
"CO: "
+
weather
.
getCarbonMonoxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"NO: "
+
weather
.
getNitrogenMonoxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"NO2: "
+
weather
.
getNitrogenDioxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"O3: "
+
weather
.
getOzone
()
+
" μg/m3"
);
System
.
out
.
println
(
"SO2: "
+
weather
.
getSulfurDioxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"NH3: "
+
weather
.
getAmmonia
()
+
" μg/m3"
);
System
.
out
.
println
(
"Fine particulate matter: "
+
weather
.
getFineParticulateMatter
()
+
" μg/m3"
);
System
.
out
.
println
(
"Coarse particulate matter: "
+
weather
.
getCoarseParticulateMatter
()
+
" μg/m3"
);
}
private
static
void
reportOneCallData
(
OpenWeatherConnector
weather
)
{
System
.
out
.
println
(
"It is currently "
+
weather
.
getWeatherCategories
());
System
.
out
.
println
(
"Specifically, it is "
+
weather
.
getWeatherDescriptions
());
System
.
out
.
println
(
"The temperature is "
+
weather
.
getTemperature
()
+
"K"
);
System
.
out
.
println
(
"Factoring in the relative humidity of "
+
weather
.
getHumidity
()
+
"%, the dew "
+
"point is "
+
weather
.
getDewPoint
()
+
"K, and it feels like "
+
weather
.
getFeelsLike
()
+
"K"
);
System
.
out
.
println
(
"That probably also factored in winds, which are "
+
weather
.
getWindSpeed
()
+
"m/s"
+
" from "
+
weather
.
getWindDirection
()
+
"˚, gusting to "
+
weather
.
getWindGust
()
+
"m/s"
);
System
.
out
.
println
(
"Visibility is "
+
weather
.
getVisibility
()
+
"m"
);
System
.
out
.
println
(
"The pressure is "
+
weather
.
getPressure
()
+
"hPa"
);
System
.
out
.
println
(
"Cloud cover is "
+
weather
.
getCloudCover
()
+
"%"
);
System
.
out
.
println
(
"The UV Index is "
+
weather
.
getUltravioletIndex
());
System
.
out
.
println
(
"Precipitation in the last hour has been "
+
weather
.
getOneHourRainfall
()
+
"mm of"
+
" rain and "
+
weather
.
getOneHourSnowfall
()
+
"mm of snow."
);
System
.
out
.
println
(
"In 30 minutes... (standby)"
);
System
.
out
.
println
(
"In 12 hours... (standby)"
);
System
.
out
.
println
(
"In 4 days... (standby)"
);
}
private
static
void
reportForecastData
(
OpenWeatherConnector
weather
)
{
Date
timestamp
;
timestamp
=
weather
.
getTimestamps
().
get
(
weather
.
getTimestamps
().
size
()
/
2
);
System
.
out
.
println
(
"At "
+
timestamp
+
" it will be "
+
weather
.
getWeatherCategories
(
timestamp
));
System
.
out
.
println
(
"Specifically, it will be "
+
weather
.
getWeatherDescriptions
(
timestamp
));
System
.
out
.
println
(
"The temperature will be "
+
weather
.
getTemperature
(
timestamp
)
+
"K"
);
System
.
out
.
println
(
"Factoring in the relative humidity of "
+
weather
.
getHumidity
(
timestamp
)
+
"%, "
+
"it will feel like "
+
weather
.
getFeelsLike
(
timestamp
)
+
"K"
);
System
.
out
.
println
(
"That probably also factored in winds, which will be "
+
weather
.
getWindSpeed
(
timestamp
)
+
"m/s from "
+
weather
.
getWindDirection
(
timestamp
)
+
"˚, gusting to "
+
weather
.
getWindGust
(
timestamp
)
+
"m/s"
);
System
.
out
.
println
(
"Visibility will be "
+
weather
.
getVisibility
(
timestamp
)
+
"m"
);
System
.
out
.
println
(
"The pressure will be "
+
weather
.
getPressure
(
timestamp
)
+
"hPa"
);
System
.
out
.
println
(
"Cloud cover will be "
+
weather
.
getCloudCover
(
timestamp
)
+
"%"
);
System
.
out
.
println
(
"There will be a "
+
weather
.
getProbabilityOfPrecipitation
(
timestamp
)
*
100
+
"% "
+
"chance of precipitation, resulting in "
+
weather
.
getThreeHourRainfall
(
timestamp
)
+
"mm of "
+
"rain and "
+
weather
.
getThreeHourSnowfall
(
timestamp
)
+
"mm of snow in the three hours before"
+
" "
+
timestamp
);
}
private
static
void
reportWeatherData
(
OpenWeatherConnector
weather
)
{
System
.
out
.
println
(
"It is currently "
+
weather
.
getWeatherCategories
());
System
.
out
.
println
(
"Specifically, it is "
+
weather
.
getWeatherDescriptions
());
System
.
out
.
println
(
"The temperature is "
+
weather
.
getTemperature
()
+
"K"
);
System
.
out
.
println
(
"Factoring in the relative humidity of "
+
weather
.
getHumidity
()
+
"%, it feels "
+
"like "
+
weather
.
getFeelsLike
()
+
"K"
);
System
.
out
.
println
(
"That probably also factored in winds, which are "
+
weather
.
getWindSpeed
()
+
"m/s"
+
" from "
+
weather
.
getWindDirection
()
+
"˚, gusting to "
+
weather
.
getWindGust
()
+
"m/s"
);
System
.
out
.
println
(
"Visibility is "
+
weather
.
getVisibility
()
+
"m"
);
System
.
out
.
println
(
"The pressure is "
+
weather
.
getPressure
()
+
"hPa"
);
System
.
out
.
println
(
"Cloud cover is "
+
weather
.
getCloudCover
()
+
"%"
);
System
.
out
.
println
(
"Precipitation in the last hour has been "
+
weather
.
getOneHourRainfall
()
+
"mm of"
+
" rain and "
+
weather
.
getOneHourSnowfall
()
+
"mm of snow."
);
System
.
out
.
println
(
"If three-hour precipitation data is reported, it's "
+
weather
.
getThreeHourRainfall
()
+
"mm of rain and "
+
weather
.
getThreeHourSnowfall
()
+
"mm of "
+
"snow. (You might not want to trust those values if they match the 1-hour precipitation.)"
);
}
public
static
void
main
(
String
...
args
)
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
Instant
now
=
Instant
.
now
();
String
apiKey
=
null
;
try
{
apiKey
=
RestConnection
.
getApiKey
(
"openweathermap"
);
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"IO Exception: "
+
ioException
.
getClass
());
System
.
err
.
println
(
"\t"
+
ioException
.
getMessage
());
System
.
err
.
println
(
"Caused by: "
+
ioException
.
getCause
());
System
.
err
.
println
(
"\t"
+
ioException
.
getCause
().
getMessage
());
}
String
dataSet
=
getDataSet
(
scanner
);
OpenWeatherConnector
weather
=
new
OpenWeatherConnector
(
dataSet
,
apiKey
);
String
data
=
getData
(
weather
,
dataSet
,
now
);
System
.
out
.
println
(
"Raw JSON: "
+
data
);
System
.
out
.
println
(
"Location: "
+
weather
.
getLatitude
()
+
" latitude, "
+
weather
.
getLongitude
()
+
" "
+
"longitude"
);
Date
currentTimeStamp
=
weather
.
getTimestamp
();
...
...
@@ -82,93 +201,28 @@ public class Demonstration {
(
currentTimeStamp
==
OpenWeatherConnector
.
IMPOSSIBLE_DATE
?
"not present"
:
currentTimeStamp
));
System
.
out
.
println
(
"Available timestamps: "
+
weather
.
getTimestamps
());
Date
timestamp
;
switch
(
dataSet
)
{
case
"weather"
:
System
.
out
.
println
(
"It is currently "
+
weather
.
getWeatherCategories
());
System
.
out
.
println
(
"Specifically, it is "
+
weather
.
getWeatherDescriptions
());
System
.
out
.
println
(
"The temperature is "
+
weather
.
getTemperature
()
+
"K"
);
System
.
out
.
println
(
"Factoring in the relative humidity of "
+
weather
.
getHumidity
()
+
"%, it feels "
+
"like "
+
weather
.
getFeelsLike
()
+
"K"
);
System
.
out
.
println
(
"That probably also factored in winds, which are "
+
weather
.
getWindSpeed
()
+
"m/s"
+
" from "
+
weather
.
getWindDirection
()
+
"˚, gusting to "
+
weather
.
getWindGust
()
+
"m/s"
);
System
.
out
.
println
(
"Visibility is "
+
weather
.
getVisibility
()
+
"m"
);
System
.
out
.
println
(
"The pressure is "
+
weather
.
getPressure
()
+
"hPa"
);
System
.
out
.
println
(
"Cloud cover is "
+
weather
.
getCloudCover
()
+
"%"
);
System
.
out
.
println
(
"Precipitation in the last hour has been "
+
weather
.
getOneHourRainfall
()
+
"mm of"
+
" rain and "
+
weather
.
getOneHourSnowfall
()
+
"mm of snow."
);
System
.
out
.
println
(
"If three-hour precipitation data is reported, it's "
+
weather
.
getThreeHourRainfall
()
+
"mm of rain and "
+
weather
.
getThreeHourSnowfall
()
+
"mm of "
+
"snow. (You might not want to trust those values if they match the 1-hour precipitation.)"
);
reportWeatherData
(
weather
);
break
;
case
"forecast"
:
timestamp
=
weather
.
getTimestamps
().
get
(
weather
.
getTimestamps
().
size
()
/
2
);
System
.
out
.
println
(
"At "
+
timestamp
+
" it will be "
+
weather
.
getWeatherCategories
(
timestamp
));
System
.
out
.
println
(
"Specifically, it will be "
+
weather
.
getWeatherDescriptions
(
timestamp
));
System
.
out
.
println
(
"The temperature will be "
+
weather
.
getTemperature
(
timestamp
)
+
"K"
);
System
.
out
.
println
(
"Factoring in the relative humidity of "
+
weather
.
getHumidity
(
timestamp
)
+
"%, "
+
"it will feel like "
+
weather
.
getFeelsLike
(
timestamp
)
+
"K"
);
System
.
out
.
println
(
"That probably also factored in winds, which will be "
+
weather
.
getWindSpeed
(
timestamp
)
+
"m/s from "
+
weather
.
getWindDirection
(
timestamp
)
+
"˚, gusting to "
+
weather
.
getWindGust
(
timestamp
)
+
"m/s"
);
System
.
out
.
println
(
"Visibility will be "
+
weather
.
getVisibility
(
timestamp
)
+
"m"
);
System
.
out
.
println
(
"The pressure will be "
+
weather
.
getPressure
(
timestamp
)
+
"hPa"
);
System
.
out
.
println
(
"Cloud cover will be "
+
weather
.
getCloudCover
(
timestamp
)
+
"%"
);
System
.
out
.
println
(
"There will be a "
+
weather
.
getProbabilityOfPrecipitation
(
timestamp
)
*
100
+
"% "
+
"chance of precipitation, resulting in "
+
weather
.
getThreeHourRainfall
(
timestamp
)
+
"mm of "
+
"rain and "
+
weather
.
getThreeHourSnowfall
(
timestamp
)
+
"mm of snow in the three hours before"
+
" "
+
timestamp
);
reportForecastData
(
weather
);
break
;
case
"onecall"
:
reportOneCallData
(
weather
);
break
;
case
"air_pollution"
:
System
.
out
.
println
(
"The current air quality is "
+
weather
.
getAirQualityIndex
()
+
" pollutant concentration"
);
System
.
out
.
println
(
"CO: "
+
weather
.
getCarbonMonoxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"NO: "
+
weather
.
getNitrogenMonoxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"NO2: "
+
weather
.
getNitrogenDioxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"O3: "
+
weather
.
getOzone
()
+
" μg/m3"
);
System
.
out
.
println
(
"SO2: "
+
weather
.
getSulfurDioxide
()
+
" μg/m3"
);
System
.
out
.
println
(
"NH3: "
+
weather
.
getAmmonia
()
+
" μg/m3"
);
System
.
out
.
println
(
"Fine particulate matter: "
+
weather
.
getFineParticulateMatter
()
+
" μg/m3"
);
System
.
out
.
println
(
"Coarse particulate matter: "
+
weather
.
getCoarseParticulateMatter
()
+
" μg/m3"
);
reportAirPollutionData
(
weather
);
break
;
case
"air_pollution/history"
:
case
"air_pollution/forecast"
:
timestamp
=
weather
.
getTimestamps
().
get
(
weather
.
getTimestamps
().
size
()
/
2
);
System
.
out
.
println
(
"The air quality at "
+
timestamp
+
(
timestamp
.
toInstant
().
isBefore
(
now
)
?
" was "
:
" will be "
)
+
weather
.
getAirQualityIndex
(
timestamp
)
+
" pollutant concentration"
);
System
.
out
.
println
(
"CO: "
+
weather
.
getCarbonMonoxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"NO: "
+
weather
.
getNitrogenMonoxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"NO2: "
+
weather
.
getNitrogenDioxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"O3: "
+
weather
.
getOzone
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"SO2: "
+
weather
.
getSulfurDioxide
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"NH3: "
+
weather
.
getAmmonia
(
timestamp
)
+
" μg/m3"
);
System
.
out
.
println
(
"Fine particulate matter: "
+
weather
.
getFineParticulateMatter
(
timestamp
)
+
" "
+
"μg/m3"
);
System
.
out
.
println
(
"Coarse particulate matter: "
+
weather
.
getCoarseParticulateMatter
(
timestamp
)
+
" "
+
"μg/m3"
);
reportAirPollutionHistoryOrForecastData
(
weather
,
now
);
break
;
default
:
System
.
out
.
println
(
"No examples yet for this dataset."
);
}
System
.
out
.
print
(
System
.
lineSeparator
()
+
"Do you wish to save this data to a file Y/(N)? "
);
String
answer
=
scanner
.
nextLine
();
if
(
answer
.
length
()
>
0
&&
answer
.
toUpperCase
().
charAt
(
0
)
==
'Y'
)
{
System
.
out
.
print
(
"What shall the file's name be? "
);
String
filename
=
scanner
.
nextLine
();
if
(!
filename
.
endsWith
(
".json"
))
{
filename
+=
".json"
;
}
FileConnection
fileConnection
=
new
FileConnection
(
dataSet
);
try
{
System
.
out
.
println
(
"Saving..."
);
fileConnection
.
save
(
filename
,
(
JSONObject
)
new
JSONParser
().
parse
(
data
));
System
.
out
.
println
(
"\t"
+
filename
+
" saved"
);
}
catch
(
ParseException
|
IOException
exception
)
{
exception
.
printStackTrace
();
}
}
else
{
System
.
out
.
println
(
"Not saving data."
);
}
optionallySaveDataToFile
(
data
,
dataSet
,
scanner
);
scanner
.
close
();
}
}
src/main/java/edu/unl/cse/soft160/json_connections/connector/OpenWeatherConnector.java
View file @
077a681c
...
...
@@ -351,7 +351,11 @@ public class OpenWeatherConnector {
if
(
data
.
containsKey
(
"city"
))
{
locationContainer
=
(
JSONObject
)
data
.
get
(
"city"
);
}
return
extractDoubleFromJSON
(
locationContainer
,
"coord"
,
"lat"
,
Double
.
NaN
);
if
(
dataSet
.
startsWith
(
"onecall"
))
{
return
extractDoubleFromJSON
(
locationContainer
,
"lat"
,
Double
.
NaN
);
}
else
{
return
extractDoubleFromJSON
(
locationContainer
,
"coord"
,
"lat"
,
Double
.
NaN
);
}
}
/**
...
...
@@ -368,7 +372,11 @@ public class OpenWeatherConnector {
if
(
data
.
containsKey
(
"city"
))
{
locationContainer
=
(
JSONObject
)
data
.
get
(
"city"
);
}
return
extractDoubleFromJSON
(
locationContainer
,
"coord"
,
"lon"
,
Double
.
NaN
);
if
(
dataSet
.
startsWith
(
"onecall"
))
{
return
extractDoubleFromJSON
(
locationContainer
,
"lon"
,
Double
.
NaN
);
}
else
{
return
extractDoubleFromJSON
(
locationContainer
,
"coord"
,
"lon"
,
Double
.
NaN
);
}
}
/**
...
...
@@ -448,14 +456,14 @@ public class OpenWeatherConnector {
* <p>Provides the broad categories for the current weather. If there are more than one category provided, the
* "primary" category will at the head of the list. The weather categories are guaranteed to be in
* the same order as the corresponding descriptions provided by {@link #getWeatherDescriptions()}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return list of weather categories
* @see #getWeatherDescriptions()
*/
public
List
<
WeatherCategory
>
getWeatherCategories
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
getWeatherCategories
(
data
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
return
getWeatherCategories
(
dataSet
.
startsWith
(
"onecall"
)
?
(
JSONObject
)
data
.
get
(
"current"
)
:
data
);
}
/**
...
...
@@ -502,14 +510,14 @@ public class OpenWeatherConnector {
* <p>Provides descriptions of the current weather. If there are more than one weather description provided, the
* "primary" description will be at the head of the list. The weather descriptions are guaranteed to be in the
* same order as the corresponding categories provided by {@link #getWeatherCategories()}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return list of weather descriptions
* @see #getWeatherCategories()
*/
public
List
<
String
>
getWeatherDescriptions
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
getWeatherDescriptions
(
data
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
return
getWeatherDescriptions
(
dataSet
.
startsWith
(
"onecall"
)
?
(
JSONObject
)
data
.
get
(
"current"
)
:
data
);
}
/**
...
...
@@ -530,13 +538,14 @@ public class OpenWeatherConnector {
/**
* <p>Provides the current visibility in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current visibility, or {@link Long#MIN_VALUE} if visibility is not in current observation
*/
public
long
getVisibility
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractLongFromJSON
(
data
,
"visibility"
,
Long
.
MIN_VALUE
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
return
extractLongFromJSON
(
dataSet
.
startsWith
(
"onecall"
)
?
(
JSONObject
)
data
.
get
(
"current"
)
:
data
,
"visibility"
,
Long
.
MIN_VALUE
);
}
/**
...
...
@@ -555,13 +564,14 @@ public class OpenWeatherConnector {
/**
* <p>Provides the current temperature in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current temperature, or {@link Double#NaN} if temperature is not in current observation
*/
public
double
getTemperature
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractDoubleFromJSON
(
data
,
"main"
,
"temp"
,
Double
.
NaN
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
return
extractDoubleFromJSON
(
data
,
dataSet
.
startsWith
(
"onecall"
)
?
"current"
:
"main"
,
"temp"
,
Double
.
NaN
);
}
/**
...
...
@@ -578,20 +588,19 @@ public class OpenWeatherConnector {
}
/**
* <p>Provides the current humidity in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Provides the current relative humidity (percent of saturation).</p>
* <p>Available for the "weather" and "onecall" datasets.</p>
*
* @return the current humidity, or {@link Long#MIN_VALUE} if visibility is not in current observation
*/
public
long
getHumidity
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractLongFromJSON
(
data
,
"main"
,
"humidity"
,
Long
.
MIN_VALUE
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
return
extractLongFromJSON
(
data
,
dataSet
.
startsWith
(
"onecall"
)
?
"current"
:
"main"
,
"humidity"
,
Long
.
MIN_VALUE
);
}
/**
* <p>Provides the forecasted humidity in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Provides the forecasted relative humidity (percent of saturation).</p>
* <p>Available for the "forecast" dataset.</p>
*
* @param timestamp the date/time corresponding to the desired data
...
...
@@ -602,18 +611,31 @@ public class OpenWeatherConnector {
return
extractLongFromJSON
(
getListEntry
(
"list"
,
timestamp
),
"main"
,
"humidity"
,
Long
.
MIN_VALUE
);
}
/**
* <p>Provides the current dew point in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "onecall" dataset.</p>
*
* @return the current dew point, or {@link Double#NaN} if dew point is not in current observation
*/
public
double
getDewPoint
()
{
checkForDataReadiness
(
Set
.
of
(
"onecall"
));
return
extractDoubleFromJSON
(
data
,
"current"
,
"dew_point"
,
Double
.
NaN
);
}
/**
* <p>Provides the current "feels like" temperature in the default
* <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current "feels like" temperature, or {@link Double#NaN} if "feels like" temperature is not in
* current observation
*/
public
double
getFeelsLike
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractDoubleFromJSON
(
data
,
"main"
,
"feels_like"
,
Double
.
NaN
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
return
extractDoubleFromJSON
(
data
,
dataSet
.
startsWith
(
"onecall"
)
?
"current"
:
"main"
,
"feels_like"
,
Double
.
NaN
);
}
/**
...
...
@@ -634,13 +656,14 @@ public class OpenWeatherConnector {
/**
* <p>Provides the current pressure in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current pressure, or {@link Long#MIN_VALUE} if pressure is not in current observation
*/
public
long
getPressure
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractLongFromJSON
(
data
,
"main"
,
"pressure"
,
Long
.
MIN_VALUE
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
return
extractLongFromJSON
(
data
,
dataSet
.
startsWith
(
"onecall"
)
?
"current"
:
"main"
,
"pressure"
,
Long
.
MIN_VALUE
);
}
/**
...
...
@@ -658,13 +681,17 @@ public class OpenWeatherConnector {
/**
* <p>Provides the current wind direction, in degrees.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current wind direction, or {@link Long#MIN_VALUE} if winds are not in current observation
*/
public
long
getWindDirection
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractLongFromJSON
(
data
,
"wind"
,
"deg"
,
Long
.
MIN_VALUE
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
if
(
dataSet
.
startsWith
(
"onecall"
))
{
return
extractLongFromJSON
(
data
,
"current"
,
"wind_deg"
,
Long
.
MIN_VALUE
);
}
else
{
return
extractLongFromJSON
(
data
,
"wind"
,
"deg"
,
Long
.
MIN_VALUE
);
}
}
/**
...
...
@@ -682,13 +709,17 @@ public class OpenWeatherConnector {
/**
* <p>Provides the current wind speed in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current wind speed, or 0.0 if winds are not in current observation
*/
public
double
getWindSpeed
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractDoubleFromJSON
(
data
,
"wind"
,
"speed"
,
0.0
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
if
(
dataSet
.
startsWith
(
"onecall"
))
{
return
extractDoubleFromJSON
(
data
,
"current"
,
"wind_speed"
,
0.0
);
}
else
{
return
extractDoubleFromJSON
(
data
,
"wind"
,
"speed"
,
0.0
);
}
}
/**
...
...
@@ -707,13 +738,17 @@ public class OpenWeatherConnector {
/**
* <p>Provides the current wind gust in the default <a href="https://openweathermap.org/current#data">units</a>
* or those specified in the call to {@link #retrieveData(String)}.</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current wind gust, or current wind speed if wind gust is not in current observation
*/
public
double
getWindGust
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractDoubleFromJSON
(
data
,
"wind"
,
"gust"
,
getWindSpeed
());
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
if
(
dataSet
.
startsWith
(
"onecall"
))
{
return
extractDoubleFromJSON
(
data
,
"current"
,
"wind_gust"
,
getWindSpeed
());
}
else
{
return
extractDoubleFromJSON
(
data
,
"wind"
,
"gust"
,
getWindSpeed
());
}
}
/**
...
...
@@ -729,15 +764,30 @@ public class OpenWeatherConnector {
return
extractDoubleFromJSON
(
getListEntry
(
"list"
,
timestamp
),
"wind"
,
"gust"
,
getWindSpeed
(
timestamp
));
}
/**
* <p>Provides the current UV Index.</p>
* <p>Available for the "onecall" dataset.</p>
*
* @return the current UV Index, or {@link Double#NaN} if the UV Index is not in current observation
*/
public
double
getUltravioletIndex
()
{
checkForDataReadiness
(
Set
.
of
(
"onecall"
));
return
extractDoubleFromJSON
(
data
,
"current"
,
"uvi"
,
Double
.
NaN
);
}
/**
* <p>Provides the current cloud cover percentage (cloudiness).</p>
* <p>Available for the "weather" dataset.</p>
* <p>Available for the "weather"
and "onecall"
dataset
s
.</p>
*
* @return the current cloudiness, or 0 if cloudiness is not in current observation
*/
public
long
getCloudCover
()
{
checkForDataReadiness
(
Set
.
of
(
"weather"
));
return
extractLongFromJSON
(
data
,
"clouds"
,
"all"
,
0
);
checkForDataReadiness
(
Set
.
of
(
"weather"
,
"onecall"
));
if
(
dataSet
.
startsWith
(
"onecall"
))
{
return
extractLongFromJSON
(
data
,
"clouds"
,
0
);
}
else
{
return
extractLongFromJSON
(
data
,
"clouds"
,
"all"
,
0
);
}