1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-09 07:01:33 +02:00

Weather (and Pebble Webview): fix name and add wind information to weather data

Wind information are stored and put in the reconstructed OWM response.
A long standing bug (having the "name" field inside "main" instead of at
the root level of the json) has been fixed
Lineage OS receiver and if possible weather notification app receiver will
be added in further commits.

See #482
This commit is contained in:
Daniele Gobbetti 2018-04-14 19:42:05 +02:00
parent 19c6c4c88a
commit cc83021ae3
4 changed files with 32 additions and 2 deletions

View File

@ -60,7 +60,9 @@ public class OmniJawsObserver extends ContentObserver {
"forecast_condition",
"forecast_condition_code",
"time_stamp",
"forecast_date"
"forecast_date",
"wind_speed",
"wind_direction"
};
private final String[] SETTINGS_PROJECTION = new String[]{
@ -116,6 +118,9 @@ public class OmniJawsObserver extends ContentObserver {
weatherSpec.currentTemp = toKelvin(c.getFloat(3));
weatherSpec.currentHumidity = (int) c.getFloat(4);
weatherSpec.windSpeed = toKmh(c.getFloat(11));
weatherSpec.windDirection = c.getInt(12);
weatherSpec.timestamp = (int) (Long.valueOf(c.getString(9)) / 1000);
} else if (i == 1) {
weatherSpec.todayMinTemp = toKelvin(c.getFloat(5));
@ -177,4 +182,10 @@ public class OmniJawsObserver extends ContentObserver {
return (int) ((temperature - 32) * 0.5555555555555556D + 273.15);
}
private float toKmh(float speed) {
if (mMetric) {
return speed;
}
return (speed * 1.61f);
}
}

View File

@ -45,6 +45,7 @@ public class Weather {
JSONArray weather = new JSONArray();
JSONObject condition = new JSONObject();
JSONObject main = new JSONObject();
JSONObject wind = new JSONObject();
try {
condition.put("id", weatherSpec.currentConditionCode);
@ -53,14 +54,19 @@ public class Weather {
condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherSpec.currentConditionCode));
weather.put(condition);
main.put("temp", weatherSpec.currentTemp);
main.put("humidity", weatherSpec.currentHumidity);
main.put("temp_min", weatherSpec.todayMinTemp);
main.put("temp_max", weatherSpec.todayMaxTemp);
main.put("name", weatherSpec.location);
wind.put("speed", (weatherSpec.windSpeed / 3.6f)); //meter per second
wind.put("deg", weatherSpec.windDirection);
reconstructedOWMWeather.put("weather", weather);
reconstructedOWMWeather.put("main", main);
reconstructedOWMWeather.put("name", weatherSpec.location);
reconstructedOWMWeather.put("wind", wind);
} catch (JSONException e) {
LOG.error("Error while reconstructing OWM weather reply");

View File

@ -42,6 +42,9 @@ public class WeatherSpec implements Parcelable {
public int currentHumidity;
public int todayMaxTemp;
public int todayMinTemp;
public float windSpeed; //km per hour
public int windDirection; //deg
public ArrayList<Forecast> forecasts = new ArrayList<>();
public WeatherSpec() {

View File

@ -160,6 +160,8 @@ public class GBWebClient extends WebViewClient {
JSONObject main = resp.getJSONObject("main");
convertTemps(main, units); //caller might want different units
JSONObject wind = resp.getJSONObject("wind");
convertSpeeds(wind, units);
resp.put("cod", 200);
resp.put("coord", coordObject(currentPosition));
@ -215,6 +217,14 @@ public class GBWebClient extends WebViewClient {
return sys;
}
private static void convertSpeeds(JSONObject wind, String units) throws JSONException {
if ("metric".equals(units)) {
wind.put("speed", (wind.getDouble("speed") * 3.6f) );
} else if ("imperial".equals(units)) { //it's 2018... this is so sad
wind.put("speed", (wind.getDouble("speed") * 2.237f) );
}
}
private static void convertTemps(JSONObject main, String units) throws JSONException {
if ("metric".equals(units)) {
main.put("temp", (int) main.get("temp") - 273);