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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*  Copyright (C) 2016-2024 Andreas Shimokawa, Arjan Schrijver, beardhatcode,
    Carsten Pfeiffer, Daniele Gobbetti, Enrico Brambilla, José Rebelo, Taavi
    Eomäe
 
    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.model;
 
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;
 
import androidx.annotation.Nullable;
 
import java.io.Serializable;
import java.util.ArrayList;
 
// FIXME: document me and my fields, including units
public class WeatherSpec implements Parcelable, Serializable {
 
    public static final Creator<WeatherSpec> CREATOR = new Creator<WeatherSpec>() {
        @Override
        public WeatherSpec createFromParcel(Parcel in) {
            return new WeatherSpec(in);
        }
 
        @Override
        public WeatherSpec[] newArray(int size) {
            return new WeatherSpec[size];
        }
    };
    public static final int VERSION = 4;
    private static final long serialVersionUID = VERSION;
    public int timestamp; // unix epoch timestamp, in seconds
    public String location;
    public int currentTemp; // kelvin
    public int currentConditionCode = 3200; // OpenWeatherMap condition code
    public String currentCondition;
    public int currentHumidity;
    public int todayMaxTemp; // kelvin
    public int todayMinTemp; // kelvin
    public float windSpeed; // km per hour
    public int windDirection; // deg
    public float uvIndex; // 0.0 to 15.0
    public int precipProbability; // %
    public int dewPoint; // kelvin
    public float pressure; // mb
    public int cloudCover; // %
    public float visibility; // m
    public int sunRise; // unix epoch timestamp, in seconds
    public int sunSet; // unix epoch timestamp, in seconds
    public int moonRise; // unix epoch timestamp, in seconds
    public int moonSet; // unix epoch timestamp, in seconds
    public int moonPhase; // deg [0, 360[
    public float latitude;
    public float longitude;
    public int feelsLikeTemp; // kelvin
    public int isCurrentLocation = -1; // 0 for false, 1 for true, -1 for unknown
    public AirQuality airQuality;
 
    // Forecasts from the next day onward, in chronological order, one entry per day.
    // It should not include the current or previous days
    public ArrayList<Daily> forecasts = new ArrayList<>();
 
    // Hourly forecasts
    public ArrayList<Hourly> hourly = new ArrayList<>();
 
    public WeatherSpec() {
 
    }
 
    // Lower bounds of beaufort regions 1 to 12
    // Values from https://en.wikipedia.org/wiki/Beaufort_scale
    static final float[] beaufort = new float[] { 2, 6, 12, 20, 29, 39, 50, 62, 75, 89, 103, 118 };
    //                                    level: 0 1  2   3   4   5   6   7   8   9   10   11   12
 
    public static int toBeaufort(final float speed) {
        int l = 0;
        while (l < beaufort.length && beaufort[l] < speed) {
            l++;
        }
        return l;
    }
 
    public int windSpeedAsBeaufort() {
        return toBeaufort(this.windSpeed);
    }
 
    @Nullable
    public Location getLocation() {
        if (latitude == 0 && longitude == 0) {
            return null;
        }
        final Location location = new Location("weatherSpec");
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        return location;
    }
 
    protected WeatherSpec(Parcel in) {
        int version = in.readInt();
        if (version >= 2) {
            timestamp = in.readInt();
            location = in.readString();
            currentTemp = in.readInt();
            currentConditionCode = in.readInt();
            currentCondition = in.readString();
            currentHumidity = in.readInt();
            todayMaxTemp = in.readInt();
            todayMinTemp = in.readInt();
            windSpeed = in.readFloat();
            windDirection = in.readInt();
            if (version < 4) {
                // Deserialize the old Forecast list and convert them to Daily
                final ArrayList<Forecast> oldForecasts = new ArrayList<>();
                in.readList(oldForecasts, Forecast.class.getClassLoader());
                for (final Forecast forecast : oldForecasts) {
                    final Daily d = new Daily();
                    d.minTemp = forecast.minTemp;
                    d.maxTemp = forecast.maxTemp;
                    d.conditionCode = forecast.conditionCode;
                    d.humidity = forecast.humidity;
                    forecasts.add(d);
                }
            } else {
                in.readList(forecasts, Daily.class.getClassLoader());
            }
        }
        if (version >= 3) {
            uvIndex = in.readFloat();
            precipProbability = in.readInt();
        }
        if (version >= 4) {
            dewPoint = in.readInt();
            pressure = in.readFloat();
            cloudCover = in.readInt();
            visibility = in.readFloat();
            sunRise = in.readInt();
            sunSet = in.readInt();
            moonRise = in.readInt();
            moonSet = in.readInt();
            moonPhase = in.readInt();
            latitude = in.readFloat();
            longitude = in.readFloat();
            feelsLikeTemp = in.readInt();
            isCurrentLocation = in.readInt();
            airQuality = in.readParcelable(AirQuality.class.getClassLoader());
            in.readList(hourly, Hourly.class.getClassLoader());
        }
    }
 
    @Override
    public int describeContents() {
        return 0;
    }
 
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(VERSION);
        dest.writeInt(timestamp);
        dest.writeString(location);
        dest.writeInt(currentTemp);
        dest.writeInt(currentConditionCode);
        dest.writeString(currentCondition);
        dest.writeInt(currentHumidity);
        dest.writeInt(todayMaxTemp);
        dest.writeInt(todayMinTemp);
        dest.writeFloat(windSpeed);
        dest.writeInt(windDirection);
        dest.writeList(forecasts);
        dest.writeFloat(uvIndex);
        dest.writeInt(precipProbability);
        dest.writeInt(dewPoint);
        dest.writeFloat(pressure);
        dest.writeInt(cloudCover);
        dest.writeFloat(visibility);
        dest.writeInt(sunRise);
        dest.writeInt(sunSet);
        dest.writeInt(moonRise);
        dest.writeInt(moonSet);
        dest.writeInt(moonPhase);
        dest.writeFloat(latitude);
        dest.writeFloat(longitude);
        dest.writeInt(feelsLikeTemp);
        dest.writeInt(isCurrentLocation);
        dest.writeParcelable(airQuality, 0);
        dest.writeList(hourly);
    }
 
    /**
     * Convert the current day's forecast to a {@link Daily} object.
     */
    public Daily todayAsDaily() {
        final Daily daily = new Daily();
 
        daily.minTemp = this.todayMinTemp;
        daily.maxTemp = this.todayMaxTemp;
        daily.conditionCode = this.currentConditionCode;
        daily.humidity = this.currentHumidity;
        daily.windSpeed = this.windSpeed;
        daily.windDirection = this.windDirection;
        daily.uvIndex = this.uvIndex;
        daily.precipProbability = this.precipProbability;
        daily.sunRise = this.sunRise;
        daily.sunSet = this.sunSet;
        daily.moonRise = this.moonRise;
        daily.moonSet = this.moonSet;
        daily.moonPhase = this.moonPhase;
        daily.airQuality = this.airQuality;
 
        return daily;
    }
 
    @Deprecated // kept for backwards compatibility with old weather apps
    public static class Forecast implements Parcelable, Serializable {
        private static final long serialVersionUID = 1L;
 
        public static final Creator<Forecast> CREATOR = new Creator<Forecast>() {
            @Override
            public Forecast createFromParcel(Parcel in) {
                return new Forecast(in);
            }
 
            @Override
            public Forecast[] newArray(int size) {
                return new Forecast[size];
            }
        };
        public int minTemp; // Kelvin
        public int maxTemp; // Kelvin
        public int conditionCode; // OpenWeatherMap condition code
        public int humidity;
 
        public Forecast() {
        }
 
        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
        public int describeContents() {
            return 0;
        }
 
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(minTemp);
            dest.writeInt(maxTemp);
            dest.writeInt(conditionCode);
            dest.writeInt(humidity);
        }
    }
 
    public static class AirQuality implements Parcelable, Serializable {
        public static final int VERSION = 1;
        private static final long serialVersionUID = VERSION;
 
        public static final Creator<AirQuality> CREATOR = new Creator<AirQuality>() {
            @Override
            public AirQuality createFromParcel(final Parcel in) {
                return new AirQuality(in);
            }
 
            @Override
            public AirQuality[] newArray(final int size) {
                return new AirQuality[size];
            }
        };
 
        public int aqi = -1; // Air Quality Index - usually the max across all AQI values for pollutants
 
        public float co = -1; // Carbon Monoxide, mg/m^3
        public float no2 = -1; // Nitrogen Dioxide, ug/m^3
        public float o3 = -1; // Ozone, ug/m^3
        public float pm10 = -1; // Particulate Matter, 10 microns or less in diameter, ug/m^3
        public float pm25 = -1; // Particulate Matter, 2.5 microns or less in diameter, ug/m^3
        public float so2 = -1; // Sulphur Dioxide, ug/m^3
 
        // Air Quality Index values per pollutant
        // These are expected to be in the Plume scale (see https://plumelabs.files.wordpress.com/2023/06/plume_aqi_2023.pdf)
        // Some apps such as Breezy Weather fallback to the WHO 2021 AQI for pollutants that are not mapped in the Plume AQI
        // https://www.who.int/news-room/fact-sheets/detail/ambient-(outdoor)-air-quality-and-health
        //
        // Breezy Weather implementation for reference:
        // - https://github.com/breezy-weather/breezy-weather/blob/main/app/src/main/java/org/breezyweather/common/basic/models/weather/AirQuality.kt
        // - https://github.com/breezy-weather/breezy-weather/blob/main/app/src/main/java/org/breezyweather/common/basic/models/options/index/PollutantIndex.kt
 
        public int coAqi = -1;
        public int no2Aqi = -1;
        public int o3Aqi = -1;
        public int pm10Aqi = -1;
        public int pm25Aqi = -1;
        public int so2Aqi = -1;
 
        public AirQuality() {
        }
 
        AirQuality(final Parcel in) {
            in.readInt(); // version
            aqi = in.readInt();
            co = in.readFloat();
            no2 = in.readFloat();
            o3 = in.readFloat();
            pm10 = in.readFloat();
            pm25 = in.readFloat();
            so2 = in.readFloat();
            coAqi = in.readInt();
            no2Aqi = in.readInt();
            o3Aqi = in.readInt();
            pm10Aqi = in.readInt();
            pm25Aqi = in.readInt();
            so2Aqi = in.readInt();
        }
 
        @Override
        public int describeContents() {
            return 0;
        }
 
        @Override
        public void writeToParcel(final Parcel dest, final int flags) {
            dest.writeInt(VERSION);
            dest.writeInt(aqi);
            dest.writeFloat(co);
            dest.writeFloat(no2);
            dest.writeFloat(o3);
            dest.writeFloat(pm10);
            dest.writeFloat(pm25);
            dest.writeFloat(so2);
            dest.writeInt(coAqi);
            dest.writeInt(no2Aqi);
            dest.writeInt(o3Aqi);
            dest.writeInt(pm10Aqi);
            dest.writeInt(pm25Aqi);
            dest.writeInt(so2Aqi);
        }
    }
 
    public static class Daily implements Parcelable, Serializable {
        public static final int VERSION = 1;
        private static final long serialVersionUID = VERSION;
 
        public static final Creator<Daily> CREATOR = new Creator<Daily>() {
            @Override
            public Daily createFromParcel(final Parcel in) {
                return new Daily(in);
            }
 
            @Override
            public Daily[] newArray(final int size) {
                return new Daily[size];
            }
        };
        public int minTemp; // Kelvin
        public int maxTemp; // Kelvin
        public int conditionCode; // OpenWeatherMap condition code
        public int humidity;
        public float windSpeed; // km per hour
        public int windDirection; // deg
        public float uvIndex; // 0.0 to 15.0
        public int precipProbability; // %
        public int sunRise;
        public int sunSet;
        public int moonRise;
        public int moonSet;
        public int moonPhase;
        public AirQuality airQuality;
 
        public Daily() {
        }
 
        Daily(final Parcel in) {
            in.readInt(); // version
            minTemp = in.readInt();
            maxTemp = in.readInt();
            conditionCode = in.readInt();
            humidity = in.readInt();
            windSpeed = in.readFloat();
            windDirection = in.readInt();
            uvIndex = in.readFloat();
            precipProbability = in.readInt();
            sunRise = in.readInt();
            sunSet = in.readInt();
            moonRise = in.readInt();
            moonSet = in.readInt();
            moonPhase = in.readInt();
            airQuality = in.readParcelable(AirQuality.class.getClassLoader());
        }
 
        @Override
        public int describeContents() {
            return 0;
        }
 
        @Override
        public void writeToParcel(final Parcel dest, final int flags) {
            dest.writeInt(VERSION);
            dest.writeInt(minTemp);
            dest.writeInt(maxTemp);
            dest.writeInt(conditionCode);
            dest.writeInt(humidity);
            dest.writeFloat(windSpeed);
            dest.writeInt(windDirection);
            dest.writeFloat(uvIndex);
            dest.writeInt(precipProbability);
            dest.writeInt(sunRise);
            dest.writeInt(sunSet);
            dest.writeInt(moonRise);
            dest.writeInt(moonSet);
            dest.writeInt(moonPhase);
            dest.writeParcelable(airQuality, 0);
        }
 
        public int windSpeedAsBeaufort() {
            return toBeaufort(this.windSpeed);
        }
    }
 
    public static class Hourly implements Parcelable, Serializable {
        public static final int VERSION = 1;
        private static final long serialVersionUID = VERSION;
 
        public static final Creator<Hourly> CREATOR = new Creator<Hourly>() {
            @Override
            public Hourly createFromParcel(final Parcel in) {
                return new Hourly(in);
            }
 
            @Override
            public Hourly[] newArray(final int size) {
                return new Hourly[size];
            }
        };
 
        public int timestamp; // unix epoch timestamp, in seconds
        public int temp; // Kelvin
        public int conditionCode; // OpenWeatherMap condition code
        public int humidity;
        public float windSpeed; // km per hour
        public int windDirection; // deg
        public float uvIndex; // 0.0 to 15.0
        public int precipProbability; // %
 
        public Hourly() {
        }
 
        Hourly(final Parcel in) {
            in.readInt(); // version
            timestamp = in.readInt();
            temp = in.readInt();
            conditionCode = in.readInt();
            humidity = in.readInt();
            windSpeed = in.readFloat();
            windDirection = in.readInt();
            uvIndex = in.readFloat();
            precipProbability = in.readInt();
        }
 
        @Override
        public int describeContents() {
            return 0;
        }
 
        @Override
        public void writeToParcel(final Parcel dest, final int flags) {
            dest.writeInt(VERSION);
            dest.writeInt(timestamp);
            dest.writeInt(temp);
            dest.writeInt(conditionCode);
            dest.writeInt(humidity);
            dest.writeFloat(windSpeed);
            dest.writeInt(windDirection);
            dest.writeFloat(uvIndex);
            dest.writeInt(precipProbability);
        }
 
        public int windSpeedAsBeaufort() {
            return toBeaufort(this.windSpeed);
        }
    }
}