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

Weather: recreate OWM fake reply from weatherSpec, no not directly create in in Weather Notification receiver

This should bring CM weather on par with Weather Notficication when using background js
This commit is contained in:
Andreas Shimokawa 2017-12-14 14:54:09 +01:00
parent 1752b1b51e
commit 948002dc7c
6 changed files with 52 additions and 43 deletions

View File

@ -132,7 +132,6 @@ public class CMWeatherReceiver extends BroadcastReceiver implements CMWeatherMan
weatherSpec.todayMaxTemp = (int) weatherInfo.getTodaysHigh() + 273;
weatherSpec.todayMinTemp = (int) weatherInfo.getTodaysLow() + 273;
}
weatherSpec.currentConditionCode = Weather.mapToOpenWeatherMapCondition(CMtoYahooCondintion(weatherInfo.getConditionCode()));
weatherSpec.currentCondition = Weather.getConditionString(weatherSpec.currentConditionCode);

View File

@ -48,7 +48,6 @@ public class WeatherNotificationReceiver extends BroadcastReceiver {
if (parcelableWeather2 != null) {
Weather weather = Weather.getInstance();
weather.setReconstructedOWMWeather(parcelableWeather2.reconstructedOWMWeather);
weather.setReconstructedOWMForecast(parcelableWeather2.reconstructedOWMForecast);
WeatherSpec weatherSpec = parcelableWeather2.weatherSpec;

View File

@ -16,12 +16,17 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.model;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Weather {
private static final Logger LOG = LoggerFactory.getLogger(Weather.class);
private WeatherSpec weatherSpec = null;
private JSONObject reconstructedOWMWeather = null;
private JSONObject reconstructedOWMForecast = null;
public WeatherSpec getWeatherSpec() {
@ -32,12 +37,36 @@ public class Weather {
this.weatherSpec = weatherSpec;
}
public JSONObject getReconstructedOWMWeather() {
return reconstructedOWMWeather;
}
public JSONObject createReconstructedOWMWeatherReply() {
if (weatherSpec == null) {
return null;
}
JSONObject reconstructedOWMWeather = new JSONObject();
JSONArray weather = new JSONArray();
JSONObject condition = new JSONObject();
JSONObject main = new JSONObject();
public void setReconstructedOWMWeather(JSONObject reconstructedOWMWeather) {
this.reconstructedOWMWeather = reconstructedOWMWeather;
try {
condition.put("id", weatherSpec.currentConditionCode);
condition.put("main", weatherSpec.currentCondition);
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);
reconstructedOWMWeather.put("weather", weather);
reconstructedOWMWeather.put("main", main);
} catch (JSONException e) {
LOG.error("Error while reconstructing OWM weather reply");
return null;
}
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedOWMWeather.toString());
return reconstructedOWMWeather;
}
public JSONObject getReconstructedOWMForecast() {

View File

@ -39,6 +39,7 @@ public class WeatherSpec implements Parcelable {
public int currentTemp;
public int currentConditionCode = 3200;
public String currentCondition;
public int currentHumidity;
public int todayMaxTemp;
public int todayMinTemp;
public ArrayList<Forecast> forecasts = new ArrayList<>();
@ -53,6 +54,7 @@ public class WeatherSpec implements Parcelable {
currentTemp = in.readInt();
currentConditionCode = in.readInt();
currentCondition = in.readString();
currentHumidity = in.readInt();
todayMaxTemp = in.readInt();
todayMinTemp = in.readInt();
in.readList(forecasts, Forecast.class.getClassLoader());
@ -70,6 +72,7 @@ public class WeatherSpec implements Parcelable {
dest.writeInt(currentTemp);
dest.writeInt(currentConditionCode);
dest.writeString(currentCondition);
dest.writeInt(currentHumidity);
dest.writeInt(todayMaxTemp);
dest.writeInt(todayMinTemp);
dest.writeList(forecasts);
@ -90,20 +93,23 @@ public class WeatherSpec implements Parcelable {
public int minTemp;
public int maxTemp;
public int conditionCode;
public int humidity;
public Forecast() {
}
public Forecast(int minTemp, int maxTemp, int conditionCode) {
public Forecast(int minTemp, int maxTemp, int conditionCode, int humidity) {
this.minTemp = minTemp;
this.maxTemp = maxTemp;
this.conditionCode = conditionCode;
this.humidity = humidity;
}
Forecast(Parcel in) {
minTemp = in.readInt();
maxTemp = in.readInt();
conditionCode = in.readInt();
humidity = in.readInt();
}
@Override
@ -116,6 +122,7 @@ public class WeatherSpec implements Parcelable {
dest.writeInt(minTemp);
dest.writeInt(maxTemp);
dest.writeInt(conditionCode);
dest.writeInt(humidity);
}
}
}

View File

@ -140,11 +140,8 @@ public class GBWebClient extends WebViewClient {
CurrentPosition currentPosition = new CurrentPosition();
try {
JSONObject resp;
if ("/data/2.5/weather".equals(type) && Weather.getInstance().getReconstructedOWMWeather() != null) {
resp = new JSONObject(Weather.getInstance().getReconstructedOWMWeather().toString());
JSONObject resp = Weather.getInstance().createReconstructedOWMWeatherReply();
if ("/data/2.5/weather".equals(type) && resp != null) {
JSONObject main = resp.getJSONObject("main");
convertTemps(main, units); //caller might want different units

View File

@ -35,7 +35,6 @@ public class ParcelableWeather2 implements Parcelable {
// getters and setters suck ;)
public WeatherSpec weatherSpec = new WeatherSpec();
public JSONObject reconstructedOWMWeather = null;
public JSONObject reconstructedOWMForecast = null;
private ParcelableWeather2(Parcel in) {
@ -53,11 +52,6 @@ public class ParcelableWeather2 implements Parcelable {
int conditions = bundle.getInt("weather_conditions");
if (conditions > 0) {
Bundle conditionBundle = in.readBundle(getClass().getClassLoader());
reconstructedOWMWeather = new JSONObject();
JSONArray weather = new JSONArray();
JSONObject condition = new JSONObject();
JSONObject main = new JSONObject();
weatherSpec.currentCondition = conditionBundle.getString("weather_condition_text");
conditionBundle.getStringArray("weather_condition_types");
weatherSpec.currentTemp = conditionBundle.getInt("weather_current_temp");
@ -68,25 +62,8 @@ public class ParcelableWeather2 implements Parcelable {
}
weatherSpec.todayMinTemp = conditionBundle.getInt("weather_low_temp");
weatherSpec.todayMaxTemp = conditionBundle.getInt("weather_high_temp");
try {
condition.put("id", weatherSpec.currentConditionCode);
condition.put("main", weatherSpec.currentCondition);
condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherSpec.currentConditionCode));
weather.put(condition);
weatherSpec.currentHumidity = conditionBundle.getInt("weather_humidity_value");
main.put("temp", weatherSpec.currentTemp);
main.put("humidity", conditionBundle.getInt("weather_humidity_value"));
main.put("temp_min", weatherSpec.todayMinTemp);
main.put("temp_max", weatherSpec.todayMaxTemp);
main.put("name", weatherSpec.location);
reconstructedOWMWeather.put("weather", weather);
reconstructedOWMWeather.put("main", main);
} catch (JSONException e) {
e.printStackTrace();
}
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedOWMWeather.toString());
//fetch forecasts
int timeOffset = 0;
@ -95,9 +72,9 @@ public class ParcelableWeather2 implements Parcelable {
while (--conditions > 0) {
timeOffset += 86400000; //manually determined
JSONObject item = new JSONObject();
condition = new JSONObject();
main = new JSONObject();
weather = new JSONArray();
JSONObject condition = new JSONObject();
JSONObject main = new JSONObject();
JSONArray weather = new JSONArray();
Bundle forecastBundle = in.readBundle(getClass().getClassLoader());
String[] forecastConditionType = forecastBundle.getStringArray("weather_condition_types");
int forecastConditionCode = 0;
@ -106,7 +83,8 @@ public class ParcelableWeather2 implements Parcelable {
}
int forecastLowTemp = forecastBundle.getInt("weather_low_temp");
int forecastHighTemp = forecastBundle.getInt("weather_high_temp");
weatherSpec.forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode));
int forecastHumidity = forecastBundle.getInt("weather_humidity_value");
weatherSpec.forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode, forecastHumidity));
try {
condition.put("id", forecastConditionCode);
condition.put("main", forecastBundle.getString("weather_condition_text"));
@ -114,7 +92,7 @@ public class ParcelableWeather2 implements Parcelable {
weather.put(condition);
main.put("temp", forecastBundle.getInt("weather_current_temp"));
main.put("humidity", forecastBundle.getInt("weather_humidity_value"));
main.put("humidity", forecastHumidity);
main.put("temp_min", forecastLowTemp);
main.put("temp_max", forecastHighTemp);