wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*  Copyright (C) 2022-2024 Daniele Gobbetti, Enrico Brambilla, José Rebelo,
    TylerWilliamson
 
    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 <https://www.gnu.org/licenses/>. */
 
package nodomain.freeyourgadget.gadgetbridge.externalevents;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.Objects;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
 
public class GenericWeatherReceiver extends BroadcastReceiver {
    private static final Logger LOG = LoggerFactory.getLogger(GenericWeatherReceiver.class);
 
    public final static String ACTION_GENERIC_WEATHER = "nodomain.freeyourgadget.gadgetbridge.ACTION_GENERIC_WEATHER";
    public final static String EXTRA_WEATHER_JSON = "WeatherJson";
    public final static String EXTRA_WEATHER_SECONDARY_JSON = "WeatherSecondaryJson";
 
    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent == null) {
            LOG.warn("Intent is null");
            return;
        }
 
        if (!ACTION_GENERIC_WEATHER.equals(intent.getAction())) {
            LOG.warn("Unknown action {}", intent.getAction());
            return;
        }
 
        final Bundle bundle = intent.getExtras();
        if (bundle == null) {
            LOG.warn("Intent has no extras");
            return;
        }
 
        if (!bundle.containsKey(EXTRA_WEATHER_JSON)) {
            LOG.warn("Bundle key {} not found", EXTRA_WEATHER_JSON);
            return;
        }
 
        try {
            final JSONObject primaryWeatherJson = new JSONObject(Objects.requireNonNull(bundle.getString(EXTRA_WEATHER_JSON)));
            final WeatherSpec primaryWeather = weatherFromJson(primaryWeatherJson);
 
            final ArrayList<WeatherSpec> weathers = new ArrayList<>();
            weathers.add(primaryWeather);
 
            if (bundle.containsKey(EXTRA_WEATHER_SECONDARY_JSON)) {
                final JSONArray secondaryWeatherJson = new JSONArray(bundle.getString(EXTRA_WEATHER_SECONDARY_JSON, "[]"));
 
                for (int i = 0; i < secondaryWeatherJson.length(); i++) {
                    weathers.add(weatherFromJson(secondaryWeatherJson.getJSONObject(i)));
                }
            }
 
            LOG.info("Got generic weather for {} locations", weathers.size());
            Weather.getInstance().setWeatherSpec(weathers);
            GBApplication.deviceService().onSendWeather(weathers);
        } catch (final Exception e) {
            GB.toast("Gadgetbridge received broken or incompatible weather data", Toast.LENGTH_SHORT, GB.ERROR, e);
        }
    }
 
    private WeatherSpec weatherFromJson(final JSONObject weatherJson) throws JSONException {
        final WeatherSpec weatherSpec = new WeatherSpec();
 
        weatherSpec.timestamp = safelyGet(weatherJson, Integer.class, "timestamp", (int) (System.currentTimeMillis() / 1000));
        weatherSpec.location = safelyGet(weatherJson, String.class, "location", "");
        weatherSpec.currentTemp = safelyGet(weatherJson, Integer.class, "currentTemp", 0);
        weatherSpec.todayMinTemp = safelyGet(weatherJson, Integer.class, "todayMinTemp", 0);
        weatherSpec.todayMaxTemp = safelyGet(weatherJson, Integer.class, "todayMaxTemp", 0);
        weatherSpec.currentCondition = safelyGet(weatherJson, String.class, "currentCondition", "");
        weatherSpec.currentConditionCode = safelyGet(weatherJson, Integer.class, "currentConditionCode", 0);
        weatherSpec.currentHumidity = safelyGet(weatherJson, Integer.class, "currentHumidity", 0);
        weatherSpec.windSpeed = safelyGet(weatherJson, Number.class, "windSpeed", 0d).floatValue();
        weatherSpec.windDirection = safelyGet(weatherJson, Integer.class, "windDirection", 0);
        weatherSpec.uvIndex = safelyGet(weatherJson, Number.class, "uvIndex", 0d).floatValue();
        weatherSpec.precipProbability = safelyGet(weatherJson, Integer.class, "precipProbability", 0);
        weatherSpec.dewPoint = safelyGet(weatherJson, Integer.class, "dewPoint", 0);
        weatherSpec.pressure = safelyGet(weatherJson, Number.class, "pressure", 0).floatValue();
        weatherSpec.cloudCover = safelyGet(weatherJson, Integer.class, "cloudCover", 0);
        weatherSpec.visibility = safelyGet(weatherJson, Number.class, "visibility", 0).floatValue();
        weatherSpec.sunRise = safelyGet(weatherJson, Integer.class, "sunRise", 0);
        weatherSpec.sunSet = safelyGet(weatherJson, Integer.class, "sunSet", 0);
        weatherSpec.moonRise = safelyGet(weatherJson, Integer.class, "moonRise", 0);
        weatherSpec.moonSet = safelyGet(weatherJson, Integer.class, "moonSet", 0);
        weatherSpec.moonPhase = safelyGet(weatherJson, Integer.class, "moonPhase", 0);
        weatherSpec.latitude = safelyGet(weatherJson, Number.class, "latitude", 0).floatValue();
        weatherSpec.longitude = safelyGet(weatherJson, Number.class, "longitude", 0).floatValue();
        weatherSpec.feelsLikeTemp = safelyGet(weatherJson, Integer.class, "feelsLikeTemp", 0);
        weatherSpec.isCurrentLocation = safelyGet(weatherJson, Integer.class, "isCurrentLocation", -1);
 
        if (weatherJson.has("airQuality")) {
            weatherSpec.airQuality = toAirQuality(weatherJson.getJSONObject("airQuality"));
        }
 
        if (weatherJson.has("forecasts")) {
            final JSONArray forecastArray = weatherJson.getJSONArray("forecasts");
            weatherSpec.forecasts = new ArrayList<>();
 
            for (int i = 0, l = forecastArray.length(); i < l; i++) {
                final JSONObject forecastJson = forecastArray.getJSONObject(i);
 
                final WeatherSpec.Daily forecast = new WeatherSpec.Daily();
 
                forecast.conditionCode = safelyGet(forecastJson, Integer.class, "conditionCode", 0);
                forecast.humidity = safelyGet(forecastJson, Integer.class, "humidity", 0);
                forecast.maxTemp = safelyGet(forecastJson, Integer.class, "maxTemp", 0);
                forecast.minTemp = safelyGet(forecastJson, Integer.class, "minTemp", 0);
                forecast.windSpeed = safelyGet(forecastJson, Number.class, "windSpeed", 0).floatValue();
                forecast.windDirection = safelyGet(forecastJson, Integer.class, "windDirection", 0);
                forecast.uvIndex = safelyGet(forecastJson, Number.class, "uvIndex", 0d).floatValue();
                forecast.precipProbability = safelyGet(forecastJson, Integer.class, "precipProbability", 0);
                forecast.sunRise = safelyGet(forecastJson, Integer.class, "sunRise", 0);
                forecast.sunSet = safelyGet(forecastJson, Integer.class, "sunSet", 0);
                forecast.moonRise = safelyGet(forecastJson, Integer.class, "moonRise", 0);
                forecast.moonSet = safelyGet(forecastJson, Integer.class, "moonSet", 0);
                forecast.moonPhase = safelyGet(forecastJson, Integer.class, "moonPhase", 0);
 
                if (forecastJson.has("airQuality")) {
                    forecast.airQuality = toAirQuality(forecastJson.getJSONObject("airQuality"));
                }
 
                weatherSpec.forecasts.add(forecast);
            }
        }
 
        if (weatherJson.has("hourly")) {
            final JSONArray forecastArray = weatherJson.getJSONArray("hourly");
            weatherSpec.hourly = new ArrayList<>();
 
            for (int i = 0, l = forecastArray.length(); i < l; i++) {
                final JSONObject forecastJson = forecastArray.getJSONObject(i);
 
                final WeatherSpec.Hourly forecast = new WeatherSpec.Hourly();
 
                forecast.timestamp = safelyGet(forecastJson, Integer.class, "timestamp", 0);
                forecast.temp = safelyGet(forecastJson, Integer.class, "temp", 0);
                forecast.conditionCode = safelyGet(forecastJson, Integer.class, "conditionCode", 0);
                forecast.humidity = safelyGet(forecastJson, Integer.class, "humidity", 0);
                forecast.windSpeed = safelyGet(forecastJson, Number.class, "windSpeed", 0).floatValue();
                forecast.windDirection = safelyGet(forecastJson, Integer.class, "windDirection", 0);
                forecast.uvIndex = safelyGet(forecastJson, Number.class, "uvIndex", 0d).floatValue();
                forecast.precipProbability = safelyGet(forecastJson, Integer.class, "precipProbability", 0);
 
                weatherSpec.hourly.add(forecast);
            }
        }
 
        return weatherSpec;
    }
 
    private WeatherSpec.AirQuality toAirQuality(final JSONObject jsonObject) {
        final WeatherSpec.AirQuality airQuality = new WeatherSpec.AirQuality();
        airQuality.aqi = safelyGet(jsonObject, Integer.class, "aqi", -1);
        airQuality.co = safelyGet(jsonObject, Number.class, "co", -1).floatValue();
        airQuality.no2 = safelyGet(jsonObject, Number.class, "no2", -1).floatValue();
        airQuality.o3 = safelyGet(jsonObject, Number.class, "o3", -1).floatValue();
        airQuality.pm10 = safelyGet(jsonObject, Number.class, "pm10", -1).floatValue();
        airQuality.pm25 = safelyGet(jsonObject, Number.class, "pm25", -1).floatValue();
        airQuality.so2 = safelyGet(jsonObject, Number.class, "so2", -1).floatValue();
        airQuality.coAqi = safelyGet(jsonObject, Integer.class, "coAqi", -1);
        airQuality.no2Aqi = safelyGet(jsonObject, Integer.class, "no2Aqi", -1);
        airQuality.o3Aqi = safelyGet(jsonObject, Integer.class, "o3Aqi", -1);
        airQuality.pm10Aqi = safelyGet(jsonObject, Integer.class, "pm10Aqi", -1);
        airQuality.pm25Aqi = safelyGet(jsonObject, Integer.class, "pm25Aqi", -1);
        airQuality.so2Aqi = safelyGet(jsonObject, Integer.class, "so2Aqi", -1);
 
        return airQuality;
    }
 
    private <T> T safelyGet(JSONObject jsonObject, Class<T> tClass, String name, T defaultValue) {
        try {
            if (jsonObject.has(name)) {
                Object value = jsonObject.get(name);
 
                if (tClass.isInstance(value)) {
                    return (T) value;
                }
            }
        } catch (Exception e) {
            //
        }
        return defaultValue;
    }
}