2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2015-2017 Andreas Shimokawa, Daniele Gobbetti
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-12-28 20:55:59 +01:00
|
|
|
package ru.gelin.android.weather.notification;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
|
2017-07-26 18:12:12 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2015-12-28 20:55:59 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2017-11-29 23:57:36 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2017-07-26 18:12:12 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
|
2017-11-29 23:57:36 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
2017-07-26 18:12:12 +02:00
|
|
|
|
2015-12-28 20:55:59 +01:00
|
|
|
public class ParcelableWeather2 implements Parcelable {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class);
|
|
|
|
|
|
|
|
// getters and setters suck ;)
|
|
|
|
|
|
|
|
public long time = 0;
|
|
|
|
public long queryTime = 0;
|
|
|
|
public int version = 0;
|
|
|
|
public String location = "";
|
|
|
|
public int currentTemp = 0;
|
2015-12-29 22:10:38 +01:00
|
|
|
public String currentCondition = "";
|
2015-12-28 20:55:59 +01:00
|
|
|
|
2017-07-26 18:12:12 +02:00
|
|
|
private String[] currentConditionType = null;
|
2016-01-03 18:28:32 +01:00
|
|
|
public int currentConditionCode = 3200;
|
2016-12-30 19:58:56 +01:00
|
|
|
public int todayLowTemp = 0;
|
|
|
|
public int todayHighTemp = 0;
|
2017-11-29 23:57:36 +01:00
|
|
|
public ArrayList<WeatherSpec.Forecast> forecasts = new ArrayList<>();
|
2016-01-03 18:28:32 +01:00
|
|
|
|
2017-07-26 18:12:12 +02:00
|
|
|
public JSONObject reconstructedWeather = null;
|
|
|
|
public JSONObject reconstructedForecast = null;
|
2016-01-03 18:28:32 +01:00
|
|
|
|
2015-12-28 20:55:59 +01:00
|
|
|
private ParcelableWeather2(Parcel in) {
|
|
|
|
int version = in.readInt();
|
|
|
|
if (version != 2) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-29 22:10:38 +01:00
|
|
|
Bundle bundle = in.readBundle();
|
2016-12-30 19:58:56 +01:00
|
|
|
|
2015-12-28 20:55:59 +01:00
|
|
|
location = bundle.getString("weather_location");
|
|
|
|
time = bundle.getLong("weather_time");
|
|
|
|
queryTime = bundle.getLong("weather_query_time");
|
|
|
|
bundle.getString("weather_forecast_url");
|
|
|
|
int conditions = bundle.getInt("weather_conditions");
|
|
|
|
if (conditions > 0) {
|
2015-12-29 22:10:38 +01:00
|
|
|
Bundle conditionBundle = in.readBundle();
|
2017-07-26 18:12:12 +02:00
|
|
|
reconstructedWeather = new JSONObject();
|
|
|
|
JSONArray weather = new JSONArray();
|
|
|
|
JSONObject condition = new JSONObject();
|
|
|
|
JSONObject main = new JSONObject();
|
|
|
|
|
2015-12-29 22:10:38 +01:00
|
|
|
currentCondition = conditionBundle.getString("weather_condition_text");
|
2015-12-28 20:55:59 +01:00
|
|
|
conditionBundle.getStringArray("weather_condition_types");
|
|
|
|
currentTemp = conditionBundle.getInt("weather_current_temp");
|
2016-01-03 18:28:32 +01:00
|
|
|
|
|
|
|
currentConditionType = conditionBundle.getStringArray("weather_condition_types");
|
|
|
|
currentConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]);
|
2016-12-30 19:58:56 +01:00
|
|
|
todayLowTemp = conditionBundle.getInt("weather_low_temp");
|
|
|
|
todayHighTemp = conditionBundle.getInt("weather_high_temp");
|
2017-07-26 18:12:12 +02:00
|
|
|
try {
|
|
|
|
condition.put("id", currentConditionCode);
|
|
|
|
condition.put("main", currentCondition);
|
|
|
|
condition.put("icon", Weather.mapToOpenWeatherMapIcon(currentConditionCode));
|
|
|
|
weather.put(condition);
|
|
|
|
|
|
|
|
main.put("temp", currentTemp);
|
|
|
|
main.put("humidity", conditionBundle.getInt("weather_humidity_value"));
|
|
|
|
main.put("temp_min", todayLowTemp);
|
|
|
|
main.put("temp_max", todayHighTemp);
|
|
|
|
main.put("name", location);
|
|
|
|
|
|
|
|
reconstructedWeather.put("weather", weather);
|
|
|
|
reconstructedWeather.put("main", main);
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedWeather.toString());
|
2017-11-29 23:57:36 +01:00
|
|
|
//fetch forecasts
|
|
|
|
int timeOffset = 0;
|
|
|
|
|
|
|
|
JSONArray list = new JSONArray();
|
|
|
|
JSONObject city = new JSONObject();
|
|
|
|
while (--conditions > 0) {
|
|
|
|
timeOffset += 86400000; //manually determined
|
2017-07-26 18:12:12 +02:00
|
|
|
JSONObject item = new JSONObject();
|
|
|
|
condition = new JSONObject();
|
|
|
|
main = new JSONObject();
|
|
|
|
weather = new JSONArray();
|
2016-01-03 18:28:32 +01:00
|
|
|
Bundle forecastBundle = in.readBundle();
|
2017-11-29 23:57:36 +01:00
|
|
|
String[] forecastConditionType = forecastBundle.getStringArray("weather_condition_types");
|
|
|
|
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));
|
2017-07-26 18:12:12 +02:00
|
|
|
try {
|
|
|
|
condition.put("id", forecastConditionCode);
|
|
|
|
condition.put("main", forecastBundle.getString("weather_condition_text"));
|
|
|
|
condition.put("icon", Weather.mapToOpenWeatherMapIcon(forecastConditionCode));
|
|
|
|
weather.put(condition);
|
|
|
|
|
|
|
|
main.put("temp", forecastBundle.getInt("weather_current_temp"));
|
|
|
|
main.put("humidity", forecastBundle.getInt("weather_humidity_value"));
|
|
|
|
main.put("temp_min", forecastLowTemp);
|
|
|
|
main.put("temp_max", forecastHighTemp);
|
|
|
|
|
|
|
|
//forecast
|
|
|
|
|
|
|
|
item.put("dt", (time / 1000) + timeOffset);
|
|
|
|
item.put("main", main);
|
|
|
|
item.put("weather", weather);
|
|
|
|
list.put(item);
|
|
|
|
} catch (JSONException e) {
|
2017-11-29 23:57:36 +01:00
|
|
|
LOG.error("error while construction JSON", e);
|
2017-07-26 18:12:12 +02:00
|
|
|
}
|
2017-11-29 23:57:36 +01:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
//"city":{"id":3181913,"name":"Bolzano","coord":{"lat":46.4927,"lon":11.3336},"country":"IT"}
|
|
|
|
city.put("name", location);
|
|
|
|
city.put("country", "World");
|
2017-07-26 18:12:12 +02:00
|
|
|
|
2017-11-29 23:57:36 +01:00
|
|
|
reconstructedForecast = new JSONObject();
|
|
|
|
reconstructedForecast.put("city", city);
|
|
|
|
reconstructedForecast.put("cnt", list.length());
|
|
|
|
reconstructedForecast.put("list", list);
|
2017-07-26 18:12:12 +02:00
|
|
|
|
2017-11-29 23:57:36 +01:00
|
|
|
} catch (JSONException e) {
|
|
|
|
LOG.error("error while construction JSON", e);
|
2016-01-03 18:28:32 +01:00
|
|
|
}
|
2017-11-29 23:57:36 +01:00
|
|
|
LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedForecast.toString());
|
2015-12-29 22:10:38 +01:00
|
|
|
}
|
2015-12-28 20:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final Creator<ParcelableWeather2> CREATOR = new Creator<ParcelableWeather2>() {
|
|
|
|
@Override
|
|
|
|
public ParcelableWeather2 createFromParcel(Parcel in) {
|
|
|
|
return new ParcelableWeather2(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ParcelableWeather2[] newArray(int size) {
|
|
|
|
return new ParcelableWeather2[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
// we do not really want to use this at all
|
|
|
|
}
|
2016-01-03 18:28:32 +01:00
|
|
|
|
|
|
|
private int weatherConditionTypesToOpenWeatherMapIds(String weather_condition_type) {
|
|
|
|
switch (weather_condition_type) {
|
|
|
|
case "THUNDERSTORM_RAIN_LIGHT":
|
|
|
|
return 200;
|
|
|
|
case "THUNDERSTORM_RAIN":
|
|
|
|
return 201;
|
|
|
|
case "THUNDERSTORM_RAIN_HEAVY":
|
|
|
|
return 202;
|
|
|
|
case "THUNDERSTORM_LIGHT":
|
|
|
|
return 210;
|
|
|
|
case "THUNDERSTORM":
|
|
|
|
return 211;
|
|
|
|
case "THUNDERSTORM_HEAVY":
|
|
|
|
return 212;
|
|
|
|
case "THUNDERSTORM_RAGGED":
|
|
|
|
return 221;
|
|
|
|
case "THUNDERSTORM_DRIZZLE_LIGHT":
|
|
|
|
return 230;
|
|
|
|
case "THUNDERSTORM_DRIZZLE":
|
|
|
|
return 231;
|
|
|
|
case "THUNDERSTORM_DRIZZLE_HEAVY":
|
|
|
|
return 232;
|
|
|
|
|
|
|
|
case "DRIZZLE_LIGHT":
|
|
|
|
return 300;
|
|
|
|
case "DRIZZLE":
|
|
|
|
return 301;
|
|
|
|
case "DRIZZLE_HEAVY":
|
|
|
|
return 302;
|
|
|
|
case "DRIZZLE_RAIN_LIGHT":
|
|
|
|
return 310;
|
|
|
|
case "DRIZZLE_RAIN":
|
|
|
|
return 311;
|
|
|
|
case "DRIZZLE_RAIN_HEAVY":
|
|
|
|
return 312;
|
|
|
|
case "DRIZZLE_SHOWER":
|
|
|
|
return 321;
|
|
|
|
|
|
|
|
case "RAIN_LIGHT":
|
|
|
|
return 500;
|
|
|
|
case "RAIN":
|
|
|
|
return 501;
|
|
|
|
case "RAIN_HEAVY":
|
|
|
|
return 502;
|
|
|
|
case "RAIN_VERY_HEAVY":
|
|
|
|
return 503;
|
|
|
|
case "RAIN_EXTREME":
|
|
|
|
return 504;
|
|
|
|
case "RAIN_FREEZING":
|
|
|
|
return 511;
|
|
|
|
case "RAIN_SHOWER_LIGHT":
|
|
|
|
return 520;
|
|
|
|
case "RAIN_SHOWER":
|
|
|
|
return 521;
|
|
|
|
case "RAIN_SHOWER_HEAVY":
|
|
|
|
return 522;
|
|
|
|
|
|
|
|
case "SNOW_LIGHT":
|
|
|
|
return 600;
|
|
|
|
case "SNOW":
|
|
|
|
return 601;
|
|
|
|
case "SNOW_HEAVY":
|
|
|
|
return 602;
|
|
|
|
case "SLEET":
|
|
|
|
return 611;
|
|
|
|
case "SNOW_SHOWER":
|
|
|
|
return 621;
|
|
|
|
|
|
|
|
case "MIST":
|
|
|
|
return 701;
|
|
|
|
case "SMOKE":
|
|
|
|
return 711;
|
|
|
|
case "HAZE":
|
|
|
|
return 721;
|
|
|
|
case "SAND_WHIRLS":
|
|
|
|
return 731;
|
|
|
|
case "FOG":
|
|
|
|
return 741;
|
|
|
|
|
|
|
|
case "CLOUDS_CLEAR":
|
|
|
|
return 800;
|
|
|
|
case "CLOUDS_FEW":
|
|
|
|
return 801;
|
|
|
|
case "CLOUDS_SCATTERED":
|
|
|
|
return 802;
|
|
|
|
case "CLOUDS_BROKEN":
|
|
|
|
return 803;
|
|
|
|
case "CLOUDS_OVERCAST":
|
|
|
|
return 804;
|
|
|
|
|
|
|
|
case "TORNADO":
|
|
|
|
return 900;
|
|
|
|
case "TROPICAL_STORM":
|
|
|
|
return 901;
|
|
|
|
case "HURRICANE":
|
|
|
|
return 902;
|
|
|
|
case "COLD":
|
|
|
|
return 903;
|
|
|
|
case "HOT":
|
|
|
|
return 904;
|
|
|
|
case "WINDY":
|
|
|
|
return 905;
|
|
|
|
case "HAIL":
|
|
|
|
return 906;
|
|
|
|
}
|
|
|
|
return 3200;
|
|
|
|
}
|
2017-11-29 23:57:36 +01:00
|
|
|
|
2015-12-28 20:55:59 +01:00
|
|
|
}
|