mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
Pebble: move the specific method to mimic JSON weather response to local package
This commit is contained in:
committed by
José Rebelo
parent
9111676bb2
commit
a9cff502ab
@@ -1,14 +1,8 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.model.weather
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
object Weather {
|
||||
private val LOG = LoggerFactory.getLogger("Weather")
|
||||
|
||||
private val weatherSpecs = CopyOnWriteArrayList<WeatherSpec>()
|
||||
|
||||
private var cacheManager: WeatherCacheManager? = null
|
||||
@@ -28,45 +22,6 @@ object Weather {
|
||||
@JvmStatic
|
||||
fun getWeatherSpecs(): List<WeatherSpec> = weatherSpecs
|
||||
|
||||
@JvmStatic
|
||||
fun createReconstructedOWMWeatherReply(): JSONObject? {
|
||||
val spec = getWeatherSpec() ?: return null
|
||||
|
||||
return try {
|
||||
JSONObject().apply {
|
||||
put("weather", JSONArray().apply {
|
||||
put(JSONObject().apply {
|
||||
put("id", spec.currentConditionCode)
|
||||
put("main", spec.currentCondition)
|
||||
put("description", spec.currentCondition)
|
||||
put("icon",
|
||||
WeatherMapper.mapToOpenWeatherMapIcon(spec.currentConditionCode)
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
put("main", JSONObject().apply {
|
||||
put("temp", spec.currentTemp)
|
||||
put("humidity", spec.currentHumidity)
|
||||
put("temp_min", spec.todayMinTemp)
|
||||
put("temp_max", spec.todayMaxTemp)
|
||||
})
|
||||
|
||||
put("wind", JSONObject().apply {
|
||||
put("speed", spec.windSpeed / 3.6f)
|
||||
put("deg", spec.windDirection)
|
||||
})
|
||||
|
||||
put("name", spec.location)
|
||||
}.also {
|
||||
LOG.debug("Weather JSON for WEBVIEW: {}", it)
|
||||
}
|
||||
} catch (e: JSONException) {
|
||||
LOG.error("Error while reconstructing OWM weather reply", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun initializeCache(cacheManager: WeatherCacheManager) {
|
||||
Weather.cacheManager = cacheManager
|
||||
|
||||
+43
-1
@@ -32,6 +32,7 @@ import net.e175.klaus.solarpositioning.SPA;
|
||||
import net.e175.klaus.solarpositioning.SunriseTransitSet;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
@@ -45,6 +46,7 @@ import java.util.Map;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.weather.Weather;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.weather.WeatherMapper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.weather.WeatherSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton;
|
||||
|
||||
@@ -150,9 +152,10 @@ public class GBWebClient extends WebViewClient {
|
||||
}
|
||||
|
||||
CurrentPosition currentPosition = new CurrentPosition();
|
||||
WeatherSpec current = Weather.getWeatherSpec();
|
||||
|
||||
try {
|
||||
JSONObject resp = Weather.createReconstructedOWMWeatherReply();
|
||||
JSONObject resp = createReconstructedOWMWeatherReply(current);
|
||||
if ("/data/2.5/weather".equals(type) && resp != null) {
|
||||
JSONObject main = resp.getJSONObject("main");
|
||||
|
||||
@@ -198,6 +201,45 @@ public class GBWebClient extends WebViewClient {
|
||||
|
||||
}
|
||||
|
||||
public JSONObject createReconstructedOWMWeatherReply(WeatherSpec weatherSpec) {
|
||||
if (weatherSpec == null) {
|
||||
return null;
|
||||
}
|
||||
JSONObject reconstructedOWMWeather = new JSONObject();
|
||||
JSONArray weather = new JSONArray();
|
||||
JSONObject condition = new JSONObject();
|
||||
JSONObject main = new JSONObject();
|
||||
JSONObject wind = new JSONObject();
|
||||
|
||||
try {
|
||||
condition.put("id", weatherSpec.getCurrentConditionCode());
|
||||
condition.put("main", weatherSpec.getCurrentCondition());
|
||||
condition.put("description", weatherSpec.getCurrentCondition());
|
||||
condition.put("icon", WeatherMapper.mapToOpenWeatherMapIcon(weatherSpec.getCurrentConditionCode()));
|
||||
weather.put(condition);
|
||||
|
||||
|
||||
main.put("temp", weatherSpec.getCurrentTemp());
|
||||
main.put("humidity", weatherSpec.getCurrentHumidity());
|
||||
main.put("temp_min", weatherSpec.getTodayMinTemp());
|
||||
main.put("temp_max", weatherSpec.getTodayMaxTemp());
|
||||
|
||||
wind.put("speed", (weatherSpec.getWindSpeed() / 3.6f)); //meter per second
|
||||
wind.put("deg", weatherSpec.getWindDirection());
|
||||
|
||||
reconstructedOWMWeather.put("weather", weather);
|
||||
reconstructedOWMWeather.put("main", main);
|
||||
reconstructedOWMWeather.put("name", weatherSpec.getLocation());
|
||||
reconstructedOWMWeather.put("wind", wind);
|
||||
|
||||
} catch (JSONException e) {
|
||||
LOG.error("Error while reconstructing OWM weather reply");
|
||||
return null;
|
||||
}
|
||||
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedOWMWeather);
|
||||
return reconstructedOWMWeather;
|
||||
}
|
||||
|
||||
|
||||
private static JSONObject sysObject(CurrentPosition currentPosition) throws JSONException {
|
||||
final SunriseTransitSet sunriseTransitSet = SPA.calculateSunriseTransitSet(
|
||||
|
||||
Reference in New Issue
Block a user