Weather - Android#
This document provides a step-by-step implementation guide for integrating the Weather Module using the Android SDK. The Weather Module allows PED applications to fetch current and forecasted weather conditions.
Step 1: Weather Service Initialization#
The WeatherV1 class provides functionality to retrieve current and forecasted weather information for configured regions or specific airports. Below are the steps to initialize and use this service.
a. Initialize the Weather Service#
Initialize the WeatherV1 service using the initService method. This is an asynchronous method that requires an IInFlightCallback listener to handle the service initialization result.
Method Signature:
public static void initService(Context context, IInFlightCallback inFlightCallback, InFlight inFlight)
Parameters:
-
context: The Android Context to initialize the service.
-
inFlightCallback: An IInFlightCallback listener to receive the initialized service object or an error.
-
inFlight: An InFlight instance to initialize the service.
Example Usage:
WeatherV1.initService(this, new IInFlightCallback() {
@Override
public void onInitServiceComplete(Object mServiceObject, String serviceName) {
WeatherV1 weatherService = (WeatherV1) mServiceObject;
if (weatherService != null) {
Log.v("WeatherService", "Weather service initialized successfully");
}
}
@Override
public void onInitServiceFailed(String serviceName, InFlight.Error error) {
Log.e("WeatherService", "Service initialization failed: " + error);
}
}, new InFlight());
Step 2: Request Current Weather Data#
The WeatherV1 class provides methods to fetch current weather data for all configured regions or a specific airport using its ICAO code.
a. Fetch Current Weather for All Configured Regions
Method Signature:
Parameters:
- listener: A WeatherV1.CurrentWeatherRequestListener callback invoked when the weather data is available.
Callback Definition:
public interface CurrentWeatherRequestListener {
void onCurrentWeatherReceived(List<CurrentWeatherV1> weathers);
void onWeatherError(WeatherV1.Error error);
}
Callback Parameters:
-
weathers: A List
containing weather data for all configured regions. -
error: A WeatherV1.Error object describing any errors (e.g., WEATHER_ERROR_BAD_REQUEST, WEATHER_ERROR_CONNECTION_ERROR).
Properties of CurrentWeatherV1:
| Property | Type | Description |
|---|---|---|
| getCondition() | String | The weather condition description (e.g., "Sunny", "Cloudy"). Refer to WeatherCondition. |
| getConditionCode() | int | A numeric code for the condition (e.g., 1001 for Sunny, 3009 for Cloudy). Refer to WeatherConditionCode. |
| getCurrentTemp() | int | The current temperature in degrees Celsius. |
| getIcaoCode() | String | The 4-letter ICAO airport code (e.g., "KLAX" for Los Angeles, "VABB" for Mumbai). |
| getIcon() | String | A string key for the weather condition icon (e.g., "sunny", "cloudy"). Refer to WeatherIcon. Developers must provide custom icons. |
| getName() | String | The name of the city associated with the weather data. |
| getRegionId() | String | The unique identifier for the region (e.g., "CCA #73"). |
| getRegionName() | String | The name of the region (e.g., "Coastal Plain"). |
| getTimestamp() | String | The timestamp of the weather data in RFC 2822 format. |
Example Usage:
weatherService.getCurrentWeather(new WeatherV1.CurrentWeatherRequestListener() {
@Override
public void onCurrentWeatherReceived(List<CurrentWeatherV1> weathers) {
Log.v("WeatherService", "Received current weather for " + weathers.size() + " regions");
for (CurrentWeatherV1 weather : weathers) {
Log.v("WeatherService", "City: " + weather.getName() + ", Condition: " + weather.getCondition() +
", Temp: " + weather.getCurrentTemp() + "°C, ICAO: " + weather.getIcaoCode());
}
}
@Override
public void onWeatherError(WeatherV1.Error error) {
Log.e("WeatherService", "Error fetching current weather: " + error);
}
});
b. Fetch Current Weather for a Specific Airport
Method Signature:
Parameters:
-
icaoCode: The 4-letter ICAO airport code (e.g., "KJFK" for John F. Kennedy International Airport).
-
listener: A WeatherV1.CurrentWeatherRequestListener callback invoked when the weather data is available.
Example Usage:
String icaoCode = "VABB"; // ICAO code for Mumbai International Airport
weatherService.getCurrentWeather(icaoCode, new WeatherV1.CurrentWeatherRequestListener() {
@Override
public void onCurrentWeatherReceived(List<CurrentWeatherV1> weathers) {
if (!weathers.isEmpty()) {
CurrentWeatherV1 weather = weathers.get(0);
Log.v("WeatherService", "Weather for " + weather.getName() + ": " + weather.getCondition() +
", " + weather.getCurrentTemp() + "°C");
}
}
@Override
public void onWeatherError(WeatherV1.Error error) {
Log.e("WeatherService", "Error fetching weather for " + icaoCode + ": " + error);
}
});
Step 3: Weather Forecast Request for a Specific Airport#
The WeatherV1 class enables the retrieval of both current and forecasted weather information for a specific airport using its ICAO code.
Method Signature:
Parameters:
-
icaoCode: The 4-letter ICAO airport code (e.g., "KJFK").
-
listener: A WeatherV1.ForecastWeatherRequestListener callback invoked when the forecast data is available.
Callback Definition:
public interface ForecastWeatherRequestListener {
void onForecastWeatherReceived(List<ForecastWeatherV1> forecastWeathers);
void onWeatherError(WeatherV1.Error error);
}
Callback Parameters:
-
forecastWeathers: A List
containing current and forecasted weather data for the specified airport. -
error: A WeatherV1.Error object describing any errors (e.g., WEATHER_ERROR_BAD_RESPONSE, WEATHER_ERROR_INTERNAL_ERROR).
Properties of ForecastWeatherV1:
| Property | Type | Description |
|---|---|---|
| getCondition() | String | The weather condition description (e.g., "Sunny", "Cloudy"). Refer to WeatherCondition. |
| getConditionCode() | int | A numeric code for the condition (e.g., 1001 for Sunny, 3009 for Cloudy). Refer to WeatherConditionCode. |
| getCurrentTemp() | int | The current temperature in degrees Celsius (nullable for forecast items). |
| getHighTemp() | int | The expected high temperature in degrees Celsius (nullable for current weather). |
| getLowTemp() | int | The expected low temperature in degrees Celsius (nullable for current weather). |
| getIcon() | String | A string key for the weather condition icon (e.g., "sunny", "rainy"). Refer to WeatherIcon. Developers must provide custom icons. |
| getTimestamp() | String | The timestamp of the forecast data in RFC 2822 format. |
Example Usage:
String icaoCode = "VABB"; // ICAO code for Mumbai International Airport
weatherService.getForecastWeather(icaoCode, new WeatherV1.ForecastWeatherRequestListener() {
@Override
public void onForecastWeatherReceived(List<ForecastWeatherV1> forecastWeathers) {
Log.v("WeatherService", "Received forecast for " + icaoCode + ": " + forecastWeathers.size() + " items");
for (ForecastWeatherV1 forecast : forecastWeathers) {
Log.v("WeatherService", "Forecast at " + forecast.getTimestamp() + ": " +
forecast.getCondition() + ", High: " + forecast.getHighTemp() + "°C, Low: " +
forecast.getLowTemp() + "°C");
}
}
@Override
public void onWeatherError(WeatherV1.Error error) {
Log.e("WeatherService", "Error fetching forecast for " + icaoCode + ": " + error);
}
});
Step 4: Handling Errors#
The Weather Module defines several error types that may occur during requests:
| Error Type | Description |
|---|---|
| WEATHER_ERROR_BAD_REQUEST | The request has missing or incorrect parameters. |
| WEATHER_ERROR_BAD_RESPONSE | The server returned an unexpected response. |
| WEATHER_ERROR_CONNECTION_ERROR | The request failed due to a network connection error. |
| WEATHER_ERROR_INTERNAL_ERROR | The request failed due to an unexpected internal error. |
| WEATHER_ERROR_SERVICE_NOT_FOUND | The service is unavailable. |
| WEATHER_ERROR_UNKNOWN | The request failed due to an unknown error. |
Example Error Handling:
weatherService.getCurrentWeather(new WeatherV1.CurrentWeatherRequestListener() {
@Override
public void onCurrentWeatherReceived(List<CurrentWeatherV1> weathers) {
// Handle successful response
}
@Override
public void onWeatherError(WeatherV1.Error error) {
switch (error) {
case WEATHER_ERROR_BAD_REQUEST:
Log.e("WeatherService", "Bad request: Check parameters");
break;
case WEATHER_ERROR_CONNECTION_ERROR:
Log.e("WeatherService", "Network connection error");
break;
default:
Log.e("WeatherService", "Error: " + error);
}
}
});
Step 5: Weather Icon Mapping#
The WeatherIcon class provides distinct weather condition names for mapping to custom icon assets. Developers must provide their own image assets.
Available Weather Icons:
| Icon Name | Description |
|---|---|
| BLIZZARD | Blizzard conditions |
| CLOUDS | Cloudy conditions |
| DUST | Dusty conditions |
| FLOOD | Flood conditions |
| FLURRIES | Light snow flurries |
| FOG | Foggy conditions |
| HAIL | Hail conditions |
| HAZE | Hazy conditions |
| HURRICANE | Hurricane conditions |
| ICY | Icy conditions |
| LIGHT_RAIN | Light rain conditions |
| RAIN | Rain conditions |
| SLEET | Sleet conditions |
| SMOKE | Smoky conditions |
| SNOW | Snow conditions |
| SUNNY | Sunny conditions |
| THUNDERSTORM | Thunderstorm conditions |
| TORNADO | Tornado conditions |
| UNKNOWN | Unknown weather condition |
Example Icon Mapping:
public class WeatherIconMap {
public static int getIcon(WeatherCondition condition) {
switch (condition.getName()) {
case WeatherIcon.SUNNY:
return R.drawable.sunny_icon;
case WeatherIcon.CLOUDS:
return R.drawable.cloudy_icon;
case WeatherIcon.RAIN:
return R.drawable.rain_icon;
// Add other cases as needed
default:
return R.drawable.unknown_icon;
}
}
}
Version 1.0