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

Weather refactoring

No longer save an instance of ParcelableWeather2, rely on our WeatherSpec instead which now has all forecast data and save reconstructed owm weather json in Weather
This commit is contained in:
Andreas Shimokawa 2017-11-30 10:24:31 +01:00
parent 879272deb7
commit 0befc1a95e
6 changed files with 68 additions and 76 deletions

View File

@ -39,26 +39,21 @@ public class WeatherNotificationReceiver extends BroadcastReceiver {
LOG.info("Wrong action"); LOG.info("Wrong action");
return; return;
} }
ParcelableWeather2 weather = null; ParcelableWeather2 parcelableWeather2 = null;
try { try {
weather = intent.getParcelableExtra("ru.gelin.android.weather.notification.EXTRA_WEATHER"); parcelableWeather2 = intent.getParcelableExtra("ru.gelin.android.weather.notification.EXTRA_WEATHER");
} catch (RuntimeException e) { } catch (RuntimeException e) {
e.printStackTrace(); LOG.error("cannot get ParcelableWeather2", e);
} }
if (weather != null) { if (parcelableWeather2 != null) {
Weather.getInstance().setWeather2(weather); Weather weather = Weather.getInstance();
LOG.info("weather in " + weather.location + " is " + weather.currentCondition + " (" + (weather.currentTemp - 273) + "°C)"); weather.setReconstructedOWMWeather(parcelableWeather2.reconstructedOWMWeather);
weather.setReconstructedOWMForecast(parcelableWeather2.reconstructedOWMForecast);
WeatherSpec weatherSpec = parcelableWeather2.weatherSpec;
LOG.info("weather in " + weatherSpec.location + " is " + weatherSpec.currentCondition + " (" + (weatherSpec.currentTemp - 273) + "°C)");
WeatherSpec weatherSpec = new WeatherSpec();
weatherSpec.timestamp = (int) (weather.queryTime / 1000);
weatherSpec.location = weather.location;
weatherSpec.currentTemp = weather.currentTemp;
weatherSpec.currentCondition = weather.currentCondition;
weatherSpec.currentConditionCode = weather.currentConditionCode;
weatherSpec.todayMaxTemp = weather.todayHighTemp;
weatherSpec.todayMinTemp = weather.todayLowTemp;
weatherSpec.forecasts = weather.forecasts;
Weather.getInstance().setWeatherSpec(weatherSpec); Weather.getInstance().setWeatherSpec(weatherSpec);
GBApplication.deviceService().onSendWeather(weatherSpec); GBApplication.deviceService().onSendWeather(weatherSpec);
} }

View File

@ -16,19 +16,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.model; package nodomain.freeyourgadget.gadgetbridge.model;
import ru.gelin.android.weather.notification.ParcelableWeather2; import org.json.JSONObject;
public class Weather { public class Weather {
private ParcelableWeather2 weather2 = null;
private WeatherSpec weatherSpec = null; private WeatherSpec weatherSpec = null;
public ParcelableWeather2 getWeather2() { private JSONObject reconstructedOWMWeather = null;
return weather2; private JSONObject reconstructedOWMForecast = null;
}
public void setWeather2(ParcelableWeather2 weather2) {
this.weather2 = weather2;
}
public WeatherSpec getWeatherSpec() { public WeatherSpec getWeatherSpec() {
return weatherSpec; return weatherSpec;
@ -38,6 +32,22 @@ public class Weather {
this.weatherSpec = weatherSpec; this.weatherSpec = weatherSpec;
} }
public JSONObject getReconstructedOWMWeather() {
return reconstructedOWMWeather;
}
public void setReconstructedOWMWeather(JSONObject reconstructedOWMWeather) {
this.reconstructedOWMWeather = reconstructedOWMWeather;
}
public JSONObject getReconstructedOWMForecast() {
return reconstructedOWMForecast;
}
public void setReconstructedOWMForecast(JSONObject reconstructedOWMForecast) {
this.reconstructedOWMForecast = reconstructedOWMForecast;
}
private static final Weather weather = new Weather(); private static final Weather weather = new Weather();
public static Weather getInstance() {return weather;} public static Weather getInstance() {return weather;}

View File

@ -37,7 +37,7 @@ public class WeatherSpec implements Parcelable {
public int timestamp; public int timestamp;
public String location; public String location;
public int currentTemp; public int currentTemp;
public int currentConditionCode; public int currentConditionCode = 3200;
public String currentCondition; public String currentCondition;
public int todayMaxTemp; public int todayMaxTemp;
public int todayMinTemp; public int todayMinTemp;

View File

@ -28,7 +28,7 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleColor; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleColor;
import nodomain.freeyourgadget.gadgetbridge.model.Weather; import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import ru.gelin.android.weather.notification.ParcelableWeather2; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
class AppMessageHandlerPebStyle extends AppMessageHandler { class AppMessageHandlerPebStyle extends AppMessageHandler {
public static final int KEY_AMPM_TEXT = 21; public static final int KEY_AMPM_TEXT = 21;
@ -92,7 +92,7 @@ class AppMessageHandlerPebStyle extends AppMessageHandler {
//WEATHER //WEATHER
ParcelableWeather2 weather = Weather.getInstance().getWeather2(); WeatherSpec weather = Weather.getInstance().getWeatherSpec();
if (weather != null) { if (weather != null) {
//comment the same key in the general section above! //comment the same key in the general section above!
pairs.add(new Pair<>(KEY_LOCATION_SERVICE, (Object) 0)); //0 auto, 1 manual pairs.add(new Pair<>(KEY_LOCATION_SERVICE, (Object) 0)); //0 auto, 1 manual

View File

@ -132,7 +132,7 @@ public class GBWebClient extends WebViewClient {
private static WebResourceResponse mimicOpenWeatherMapResponse(String type, String units) { private static WebResourceResponse mimicOpenWeatherMapResponse(String type, String units) {
if (Weather.getInstance() == null || Weather.getInstance().getWeather2() == null) { if (Weather.getInstance() == null) {
LOG.warn("WEBVIEW - Weather instance is null, cannot update weather"); LOG.warn("WEBVIEW - Weather instance is null, cannot update weather");
return null; return null;
} }
@ -142,8 +142,8 @@ public class GBWebClient extends WebViewClient {
try { try {
JSONObject resp; JSONObject resp;
if ("/data/2.5/weather".equals(type) && Weather.getInstance().getWeather2().reconstructedWeather != null) { if ("/data/2.5/weather".equals(type) && Weather.getInstance().getReconstructedOWMWeather() != null) {
resp = new JSONObject(Weather.getInstance().getWeather2().reconstructedWeather.toString()); resp = new JSONObject(Weather.getInstance().getReconstructedOWMWeather().toString());
JSONObject main = resp.getJSONObject("main"); JSONObject main = resp.getJSONObject("main");
@ -152,8 +152,8 @@ public class GBWebClient extends WebViewClient {
resp.put("cod", 200); resp.put("cod", 200);
resp.put("coord", coordObject(currentPosition)); resp.put("coord", coordObject(currentPosition));
resp.put("sys", sysObject(currentPosition)); resp.put("sys", sysObject(currentPosition));
// } else if ("/data/2.5/forecast".equals(type) && Weather.getInstance().getWeather2().reconstructedForecast != null) { //this is wrong, as we only have daily data. Unfortunately it looks like daily forecasts cannot be reconstructed // } else if ("/data/2.5/forecast".equals(type) && Weather.getInstance().getWeather2().reconstructedOWMForecast != null) { //this is wrong, as we only have daily data. Unfortunately it looks like daily forecasts cannot be reconstructed
// resp = new JSONObject(Weather.getInstance().getWeather2().reconstructedForecast.toString()); // resp = new JSONObject(Weather.getInstance().getWeather2().reconstructedOWMForecast.toString());
// //
// JSONObject city = resp.getJSONObject("city"); // JSONObject city = resp.getJSONObject("city");
// city.put("coord", coordObject(currentPosition)); // city.put("coord", coordObject(currentPosition));

View File

@ -26,8 +26,6 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import nodomain.freeyourgadget.gadgetbridge.model.Weather; import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
@ -35,22 +33,10 @@ public class ParcelableWeather2 implements Parcelable {
private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class); private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class);
// getters and setters suck ;) // getters and setters suck ;)
public WeatherSpec weatherSpec = new WeatherSpec();
public long time = 0; public JSONObject reconstructedOWMWeather = null;
public long queryTime = 0; public JSONObject reconstructedOWMForecast = null;
public int version = 0;
public String location = "";
public int currentTemp = 0;
public String currentCondition = "";
private String[] currentConditionType = null;
public int currentConditionCode = 3200;
public int todayLowTemp = 0;
public int todayHighTemp = 0;
public ArrayList<WeatherSpec.Forecast> forecasts = new ArrayList<>();
public JSONObject reconstructedWeather = null;
public JSONObject reconstructedForecast = null;
private ParcelableWeather2(Parcel in) { private ParcelableWeather2(Parcel in) {
int version = in.readInt(); int version = in.readInt();
@ -59,45 +45,46 @@ public class ParcelableWeather2 implements Parcelable {
} }
Bundle bundle = in.readBundle(); Bundle bundle = in.readBundle();
location = bundle.getString("weather_location"); weatherSpec.location = bundle.getString("weather_location");
time = bundle.getLong("weather_time"); long time = bundle.getLong("weather_time");
queryTime = bundle.getLong("weather_query_time"); long queryTime = bundle.getLong("weather_query_time");
weatherSpec.timestamp = (int) (queryTime / 1000);
bundle.getString("weather_forecast_url"); bundle.getString("weather_forecast_url");
int conditions = bundle.getInt("weather_conditions"); int conditions = bundle.getInt("weather_conditions");
if (conditions > 0) { if (conditions > 0) {
Bundle conditionBundle = in.readBundle(); Bundle conditionBundle = in.readBundle();
reconstructedWeather = new JSONObject(); reconstructedOWMWeather = new JSONObject();
JSONArray weather = new JSONArray(); JSONArray weather = new JSONArray();
JSONObject condition = new JSONObject(); JSONObject condition = new JSONObject();
JSONObject main = new JSONObject(); JSONObject main = new JSONObject();
currentCondition = conditionBundle.getString("weather_condition_text"); weatherSpec.currentCondition = conditionBundle.getString("weather_condition_text");
conditionBundle.getStringArray("weather_condition_types"); conditionBundle.getStringArray("weather_condition_types");
currentTemp = conditionBundle.getInt("weather_current_temp"); weatherSpec.currentTemp = conditionBundle.getInt("weather_current_temp");
currentConditionType = conditionBundle.getStringArray("weather_condition_types"); String[] currentConditionType = conditionBundle.getStringArray("weather_condition_types");
currentConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]); weatherSpec.currentConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]);
todayLowTemp = conditionBundle.getInt("weather_low_temp"); weatherSpec.todayMinTemp = conditionBundle.getInt("weather_low_temp");
todayHighTemp = conditionBundle.getInt("weather_high_temp"); weatherSpec.todayMaxTemp = conditionBundle.getInt("weather_high_temp");
try { try {
condition.put("id", currentConditionCode); condition.put("id", weatherSpec.currentConditionCode);
condition.put("main", currentCondition); condition.put("main", weatherSpec.currentCondition);
condition.put("icon", Weather.mapToOpenWeatherMapIcon(currentConditionCode)); condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherSpec.currentConditionCode));
weather.put(condition); weather.put(condition);
main.put("temp", currentTemp); main.put("temp", weatherSpec.currentTemp);
main.put("humidity", conditionBundle.getInt("weather_humidity_value")); main.put("humidity", conditionBundle.getInt("weather_humidity_value"));
main.put("temp_min", todayLowTemp); main.put("temp_min", weatherSpec.todayMinTemp);
main.put("temp_max", todayHighTemp); main.put("temp_max", weatherSpec.todayMaxTemp);
main.put("name", location); main.put("name", weatherSpec.location);
reconstructedWeather.put("weather", weather); reconstructedOWMWeather.put("weather", weather);
reconstructedWeather.put("main", main); reconstructedOWMWeather.put("main", main);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedWeather.toString()); LOG.debug("Weather JSON for WEBVIEW: " + reconstructedOWMWeather.toString());
//fetch forecasts //fetch forecasts
int timeOffset = 0; int timeOffset = 0;
@ -114,7 +101,7 @@ public class ParcelableWeather2 implements Parcelable {
int forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(forecastConditionType[0]); int forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(forecastConditionType[0]);
int forecastLowTemp = forecastBundle.getInt("weather_low_temp"); int forecastLowTemp = forecastBundle.getInt("weather_low_temp");
int forecastHighTemp = forecastBundle.getInt("weather_high_temp"); int forecastHighTemp = forecastBundle.getInt("weather_high_temp");
forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode)); weatherSpec.forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode));
try { try {
condition.put("id", forecastConditionCode); condition.put("id", forecastConditionCode);
condition.put("main", forecastBundle.getString("weather_condition_text")); condition.put("main", forecastBundle.getString("weather_condition_text"));
@ -138,18 +125,18 @@ public class ParcelableWeather2 implements Parcelable {
} }
try { try {
//"city":{"id":3181913,"name":"Bolzano","coord":{"lat":46.4927,"lon":11.3336},"country":"IT"} //"city":{"id":3181913,"name":"Bolzano","coord":{"lat":46.4927,"lon":11.3336},"country":"IT"}
city.put("name", location); city.put("name", weatherSpec.location);
city.put("country", "World"); city.put("country", "World");
reconstructedForecast = new JSONObject(); reconstructedOWMForecast = new JSONObject();
reconstructedForecast.put("city", city); reconstructedOWMForecast.put("city", city);
reconstructedForecast.put("cnt", list.length()); reconstructedOWMForecast.put("cnt", list.length());
reconstructedForecast.put("list", list); reconstructedOWMForecast.put("list", list);
} catch (JSONException e) { } catch (JSONException e) {
LOG.error("error while construction JSON", e); LOG.error("error while construction JSON", e);
} }
LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedForecast.toString()); LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedOWMForecast.toString());
} }
} }