mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 21:06:50 +01: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:
parent
879272deb7
commit
0befc1a95e
@ -39,26 +39,21 @@ public class WeatherNotificationReceiver extends BroadcastReceiver {
|
||||
LOG.info("Wrong action");
|
||||
return;
|
||||
}
|
||||
ParcelableWeather2 weather = null;
|
||||
ParcelableWeather2 parcelableWeather2 = null;
|
||||
try {
|
||||
weather = intent.getParcelableExtra("ru.gelin.android.weather.notification.EXTRA_WEATHER");
|
||||
parcelableWeather2 = intent.getParcelableExtra("ru.gelin.android.weather.notification.EXTRA_WEATHER");
|
||||
} catch (RuntimeException e) {
|
||||
e.printStackTrace();
|
||||
LOG.error("cannot get ParcelableWeather2", e);
|
||||
}
|
||||
|
||||
if (weather != null) {
|
||||
Weather.getInstance().setWeather2(weather);
|
||||
LOG.info("weather in " + weather.location + " is " + weather.currentCondition + " (" + (weather.currentTemp - 273) + "°C)");
|
||||
if (parcelableWeather2 != null) {
|
||||
Weather weather = Weather.getInstance();
|
||||
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);
|
||||
GBApplication.deviceService().onSendWeather(weatherSpec);
|
||||
}
|
||||
|
@ -16,19 +16,13 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.model;
|
||||
|
||||
import ru.gelin.android.weather.notification.ParcelableWeather2;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Weather {
|
||||
private ParcelableWeather2 weather2 = null;
|
||||
private WeatherSpec weatherSpec = null;
|
||||
|
||||
public ParcelableWeather2 getWeather2() {
|
||||
return weather2;
|
||||
}
|
||||
|
||||
public void setWeather2(ParcelableWeather2 weather2) {
|
||||
this.weather2 = weather2;
|
||||
}
|
||||
private JSONObject reconstructedOWMWeather = null;
|
||||
private JSONObject reconstructedOWMForecast = null;
|
||||
|
||||
public WeatherSpec getWeatherSpec() {
|
||||
return weatherSpec;
|
||||
@ -38,6 +32,22 @@ public class Weather {
|
||||
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();
|
||||
public static Weather getInstance() {return weather;}
|
||||
|
||||
|
@ -37,7 +37,7 @@ public class WeatherSpec implements Parcelable {
|
||||
public int timestamp;
|
||||
public String location;
|
||||
public int currentTemp;
|
||||
public int currentConditionCode;
|
||||
public int currentConditionCode = 3200;
|
||||
public String currentCondition;
|
||||
public int todayMaxTemp;
|
||||
public int todayMinTemp;
|
||||
|
@ -28,7 +28,7 @@ import java.util.UUID;
|
||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleColor;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
|
||||
import ru.gelin.android.weather.notification.ParcelableWeather2;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
|
||||
class AppMessageHandlerPebStyle extends AppMessageHandler {
|
||||
public static final int KEY_AMPM_TEXT = 21;
|
||||
@ -92,7 +92,7 @@ class AppMessageHandlerPebStyle extends AppMessageHandler {
|
||||
|
||||
|
||||
//WEATHER
|
||||
ParcelableWeather2 weather = Weather.getInstance().getWeather2();
|
||||
WeatherSpec weather = Weather.getInstance().getWeatherSpec();
|
||||
if (weather != null) {
|
||||
//comment the same key in the general section above!
|
||||
pairs.add(new Pair<>(KEY_LOCATION_SERVICE, (Object) 0)); //0 auto, 1 manual
|
||||
|
@ -132,7 +132,7 @@ public class GBWebClient extends WebViewClient {
|
||||
|
||||
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");
|
||||
return null;
|
||||
}
|
||||
@ -142,8 +142,8 @@ public class GBWebClient extends WebViewClient {
|
||||
try {
|
||||
JSONObject resp;
|
||||
|
||||
if ("/data/2.5/weather".equals(type) && Weather.getInstance().getWeather2().reconstructedWeather != null) {
|
||||
resp = new JSONObject(Weather.getInstance().getWeather2().reconstructedWeather.toString());
|
||||
if ("/data/2.5/weather".equals(type) && Weather.getInstance().getReconstructedOWMWeather() != null) {
|
||||
resp = new JSONObject(Weather.getInstance().getReconstructedOWMWeather().toString());
|
||||
|
||||
JSONObject main = resp.getJSONObject("main");
|
||||
|
||||
@ -152,8 +152,8 @@ public class GBWebClient extends WebViewClient {
|
||||
resp.put("cod", 200);
|
||||
resp.put("coord", coordObject(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
|
||||
// resp = new JSONObject(Weather.getInstance().getWeather2().reconstructedForecast.toString());
|
||||
// } 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().reconstructedOWMForecast.toString());
|
||||
//
|
||||
// JSONObject city = resp.getJSONObject("city");
|
||||
// city.put("coord", coordObject(currentPosition));
|
||||
|
@ -26,8 +26,6 @@ import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
|
||||
@ -35,22 +33,10 @@ public class ParcelableWeather2 implements Parcelable {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class);
|
||||
|
||||
// getters and setters suck ;)
|
||||
public WeatherSpec weatherSpec = new WeatherSpec();
|
||||
|
||||
public long time = 0;
|
||||
public long queryTime = 0;
|
||||
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;
|
||||
public JSONObject reconstructedOWMWeather = null;
|
||||
public JSONObject reconstructedOWMForecast = null;
|
||||
|
||||
private ParcelableWeather2(Parcel in) {
|
||||
int version = in.readInt();
|
||||
@ -59,45 +45,46 @@ public class ParcelableWeather2 implements Parcelable {
|
||||
}
|
||||
Bundle bundle = in.readBundle();
|
||||
|
||||
location = bundle.getString("weather_location");
|
||||
time = bundle.getLong("weather_time");
|
||||
queryTime = bundle.getLong("weather_query_time");
|
||||
weatherSpec.location = bundle.getString("weather_location");
|
||||
long time = bundle.getLong("weather_time");
|
||||
long queryTime = bundle.getLong("weather_query_time");
|
||||
weatherSpec.timestamp = (int) (queryTime / 1000);
|
||||
bundle.getString("weather_forecast_url");
|
||||
int conditions = bundle.getInt("weather_conditions");
|
||||
if (conditions > 0) {
|
||||
Bundle conditionBundle = in.readBundle();
|
||||
reconstructedWeather = new JSONObject();
|
||||
reconstructedOWMWeather = new JSONObject();
|
||||
JSONArray weather = new JSONArray();
|
||||
JSONObject condition = new JSONObject();
|
||||
JSONObject main = new JSONObject();
|
||||
|
||||
currentCondition = conditionBundle.getString("weather_condition_text");
|
||||
weatherSpec.currentCondition = conditionBundle.getString("weather_condition_text");
|
||||
conditionBundle.getStringArray("weather_condition_types");
|
||||
currentTemp = conditionBundle.getInt("weather_current_temp");
|
||||
weatherSpec.currentTemp = conditionBundle.getInt("weather_current_temp");
|
||||
|
||||
currentConditionType = conditionBundle.getStringArray("weather_condition_types");
|
||||
currentConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]);
|
||||
todayLowTemp = conditionBundle.getInt("weather_low_temp");
|
||||
todayHighTemp = conditionBundle.getInt("weather_high_temp");
|
||||
String[] currentConditionType = conditionBundle.getStringArray("weather_condition_types");
|
||||
weatherSpec.currentConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]);
|
||||
weatherSpec.todayMinTemp = conditionBundle.getInt("weather_low_temp");
|
||||
weatherSpec.todayMaxTemp = conditionBundle.getInt("weather_high_temp");
|
||||
try {
|
||||
condition.put("id", currentConditionCode);
|
||||
condition.put("main", currentCondition);
|
||||
condition.put("icon", Weather.mapToOpenWeatherMapIcon(currentConditionCode));
|
||||
condition.put("id", weatherSpec.currentConditionCode);
|
||||
condition.put("main", weatherSpec.currentCondition);
|
||||
condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherSpec.currentConditionCode));
|
||||
weather.put(condition);
|
||||
|
||||
main.put("temp", currentTemp);
|
||||
main.put("temp", weatherSpec.currentTemp);
|
||||
main.put("humidity", conditionBundle.getInt("weather_humidity_value"));
|
||||
main.put("temp_min", todayLowTemp);
|
||||
main.put("temp_max", todayHighTemp);
|
||||
main.put("name", location);
|
||||
main.put("temp_min", weatherSpec.todayMinTemp);
|
||||
main.put("temp_max", weatherSpec.todayMaxTemp);
|
||||
main.put("name", weatherSpec.location);
|
||||
|
||||
reconstructedWeather.put("weather", weather);
|
||||
reconstructedWeather.put("main", main);
|
||||
reconstructedOWMWeather.put("weather", weather);
|
||||
reconstructedOWMWeather.put("main", main);
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedWeather.toString());
|
||||
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedOWMWeather.toString());
|
||||
//fetch forecasts
|
||||
int timeOffset = 0;
|
||||
|
||||
@ -114,7 +101,7 @@ public class ParcelableWeather2 implements Parcelable {
|
||||
int forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(forecastConditionType[0]);
|
||||
int forecastLowTemp = forecastBundle.getInt("weather_low_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 {
|
||||
condition.put("id", forecastConditionCode);
|
||||
condition.put("main", forecastBundle.getString("weather_condition_text"));
|
||||
@ -138,18 +125,18 @@ public class ParcelableWeather2 implements Parcelable {
|
||||
}
|
||||
try {
|
||||
//"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");
|
||||
|
||||
reconstructedForecast = new JSONObject();
|
||||
reconstructedForecast.put("city", city);
|
||||
reconstructedForecast.put("cnt", list.length());
|
||||
reconstructedForecast.put("list", list);
|
||||
reconstructedOWMForecast = new JSONObject();
|
||||
reconstructedOWMForecast.put("city", city);
|
||||
reconstructedOWMForecast.put("cnt", list.length());
|
||||
reconstructedOWMForecast.put("list", list);
|
||||
|
||||
} catch (JSONException e) {
|
||||
LOG.error("error while construction JSON", e);
|
||||
}
|
||||
LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedForecast.toString());
|
||||
LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedOWMForecast.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user