Add weather settings to forecast

This commit is contained in:
Martin.JM 2024-02-18 13:45:27 +01:00 committed by José Rebelo
parent b231104a88
commit 5ebfe9b39f
3 changed files with 48 additions and 37 deletions

View File

@ -263,7 +263,6 @@ public class Weather {
} }
public enum MoonPhase { public enum MoonPhase {
UNKNOWN, // Good to have probably
NEW_MOON, NEW_MOON,
WAXING_CRESCENT, WAXING_CRESCENT,
FIRST_QUARTER, FIRST_QUARTER,
@ -277,7 +276,7 @@ public class Weather {
public static MoonPhase degreesToMoonPhase(int degrees) { public static MoonPhase degreesToMoonPhase(int degrees) {
final int leeway = 6; // Give some leeway for the new moon, first quarter, full moon, and third quarter final int leeway = 6; // Give some leeway for the new moon, first quarter, full moon, and third quarter
if (degrees < 0 || degrees > 360) if (degrees < 0 || degrees > 360)
return MoonPhase.UNKNOWN; return null;
else if (degrees >= 360 - leeway || degrees <= leeway) else if (degrees >= 360 - leeway || degrees <= leeway)
return MoonPhase.NEW_MOON; return MoonPhase.NEW_MOON;
else if (degrees < 90) else if (degrees < 90)
@ -315,7 +314,7 @@ public class Weather {
case WANING_CRESCENT: case WANING_CRESCENT:
return 8; return 8;
default: default:
return 0; return -1;
} }
} }
@ -338,7 +337,7 @@ public class Weather {
case 8: case 8:
return MoonPhase.WANING_CRESCENT; return MoonPhase.WANING_CRESCENT;
default: default:
return MoonPhase.UNKNOWN; return null;
} }
} }
@ -531,7 +530,7 @@ public class Weather {
public static class TimeData { public static class TimeData {
public int timestamp; public int timestamp;
public WeatherIcon icon; public WeatherIcon icon;
public byte temperature; public Byte temperature;
@Override @Override
public String toString() { public String toString() {
@ -548,12 +547,12 @@ public class Weather {
public static class DayData { public static class DayData {
public int timestamp; public int timestamp;
public WeatherIcon icon; public WeatherIcon icon;
public byte highTemperature; public Byte highTemperature;
public byte lowTemperature; public Byte lowTemperature;
public int sunriseTime; public Integer sunriseTime;
public int sunsetTime; public Integer sunsetTime;
public int moonRiseTime; public Integer moonRiseTime;
public int moonSetTime; public Integer moonSetTime;
public MoonPhase moonPhase; public MoonPhase moonPhase;
@Override @Override
@ -577,6 +576,7 @@ public class Weather {
public static class Request extends HuaweiPacket { public static class Request extends HuaweiPacket {
public Request( public Request(
ParamsProvider paramsProvider, ParamsProvider paramsProvider,
Settings settings,
List<TimeData> timeDataList, List<TimeData> timeDataList,
List<DayData> dayDataList List<DayData> dayDataList
) { ) {
@ -589,12 +589,13 @@ public class Weather {
if (timeDataList != null && !timeDataList.isEmpty()) { if (timeDataList != null && !timeDataList.isEmpty()) {
HuaweiTLV timeDataTlv = new HuaweiTLV(); HuaweiTLV timeDataTlv = new HuaweiTLV();
for (TimeData timeData : timeDataList) { for (TimeData timeData : timeDataList) {
// TODO: NULLs? HuaweiTLV timeTlv = new HuaweiTLV();
timeDataTlv.put(0x82, new HuaweiTLV() timeTlv.put(0x03, timeData.timestamp);
.put(0x03, timeData.timestamp) if (timeData.icon != null && settings.weatherIconSupported)
.put(0x04, iconToByte(timeData.icon)) timeTlv.put(0x04, iconToByte(timeData.icon));
.put(0x05, timeData.temperature) if (timeData.temperature != null && (settings.temperatureSupported || settings.currentTemperatureSupported))
); timeTlv.put(0x05, timeData.temperature);
timeDataTlv.put(0x82, timeTlv);
} }
this.tlv.put(0x81, timeDataTlv); this.tlv.put(0x81, timeDataTlv);
} }
@ -602,18 +603,29 @@ public class Weather {
if (dayDataList != null && !dayDataList.isEmpty()) { if (dayDataList != null && !dayDataList.isEmpty()) {
HuaweiTLV dayDataTlv = new HuaweiTLV(); HuaweiTLV dayDataTlv = new HuaweiTLV();
for (DayData dayData : dayDataList) { for (DayData dayData : dayDataList) {
// TODO: NULLs? HuaweiTLV dayTlv = new HuaweiTLV();
dayDataTlv.put(0x91, new HuaweiTLV() dayTlv.put(0x12, dayData.timestamp);
.put(0x12, dayData.timestamp) if (dayData.icon != null && settings.weatherIconSupported)
.put(0x13, iconToByte(dayData.icon)) dayTlv.put(0x13, iconToByte(dayData.icon));
.put(0x14, dayData.highTemperature) if (settings.temperatureSupported) {
.put(0x15, dayData.lowTemperature) if (dayData.highTemperature != null)
.put(0x16, dayData.sunriseTime) dayTlv.put(0x14, dayData.highTemperature);
.put(0x17, dayData.sunsetTime) if (dayData.lowTemperature != null)
.put(0x1a, dayData.moonRiseTime) dayTlv.put(0x15, dayData.lowTemperature);
.put(0x1b, dayData.moonSetTime) }
.put(0x1e, moonPhaseToByte(dayData.moonPhase)) if (settings.sunRiseSetSupported) {
); if (dayData.sunriseTime != null)
dayTlv.put(0x16, dayData.sunriseTime);
if (dayData.sunsetTime != null)
dayTlv.put(0x17, dayData.sunsetTime);
if (dayData.moonRiseTime != null)
dayTlv.put(0x1a, dayData.moonRiseTime);
if (dayData.moonSetTime != null)
dayTlv.put(0x1b, dayData.moonSetTime);
}
if (dayData.moonPhase != null && settings.moonPhaseSupported)
dayTlv.put(0x1e, moonPhaseToByte(dayData.moonPhase));
dayDataTlv.put(0x91, dayTlv);
} }
this.tlv.put(0x90, dayDataTlv); this.tlv.put(0x90, dayDataTlv);
} }
@ -712,7 +724,7 @@ public class Weather {
} }
public static class Response extends HuaweiPacket { public static class Response extends HuaweiPacket {
public byte supportedBitmap = 0; public int supportedBitmap = 0;
public boolean sunRiseSetSupported = false; public boolean sunRiseSetSupported = false;
public boolean moonPhaseSupported = false; public boolean moonPhaseSupported = false;
@ -727,7 +739,7 @@ public class Weather {
public void parseTlv() throws ParseException { public void parseTlv() throws ParseException {
if (!this.tlv.contains(0x01)) if (!this.tlv.contains(0x01))
throw new MissingTagException(0x01); throw new MissingTagException(0x01);
this.supportedBitmap = this.tlv.getByte(0x01); this.supportedBitmap = this.tlv.getInteger(0x01);
this.sunRiseSetSupported = (this.supportedBitmap & 0x01) != 0; this.sunRiseSetSupported = (this.supportedBitmap & 0x01) != 0;
this.moonPhaseSupported = (this.supportedBitmap & 0x02) != 0; this.moonPhaseSupported = (this.supportedBitmap & 0x02) != 0;

View File

@ -1745,7 +1745,7 @@ public class HuaweiSupportProvider {
lastRequest = sendGpsAndTimeToDeviceRequest; lastRequest = sendGpsAndTimeToDeviceRequest;
if (getHuaweiCoordinator().supportsWeatherForecasts()) { if (getHuaweiCoordinator().supportsWeatherForecasts()) {
SendWeatherForecastRequest sendWeatherForecastRequest = new SendWeatherForecastRequest(this, weatherSpec); SendWeatherForecastRequest sendWeatherForecastRequest = new SendWeatherForecastRequest(this, weatherSettings, weatherSpec);
lastRequest.nextRequest(sendWeatherForecastRequest); lastRequest.nextRequest(sendWeatherForecastRequest);
lastRequest = sendWeatherForecastRequest; lastRequest = sendWeatherForecastRequest;
} }

View File

@ -16,12 +16,9 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests; package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
@ -29,18 +26,19 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupport
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather.WeatherForecastData; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather.WeatherForecastData;
public class SendWeatherForecastRequest extends Request { public class SendWeatherForecastRequest extends Request {
Weather.Settings weatherSettings;
WeatherSpec weatherSpec; WeatherSpec weatherSpec;
public SendWeatherForecastRequest(HuaweiSupportProvider support, WeatherSpec weatherSpec) { public SendWeatherForecastRequest(HuaweiSupportProvider support, Weather.Settings weatherSettings, WeatherSpec weatherSpec) {
super(support); super(support);
this.serviceId = Weather.id; this.serviceId = Weather.id;
this.commandId = Weather.WeatherForecastData.id; this.commandId = Weather.WeatherForecastData.id;
this.weatherSettings = weatherSettings;
this.weatherSpec = weatherSpec; this.weatherSpec = weatherSpec;
} }
@Override @Override
protected List<byte[]> createRequest() throws RequestCreationException { protected List<byte[]> createRequest() throws RequestCreationException {
// TODO: Weather settings
int hourlyCount = Math.min(weatherSpec.hourly.size(), 24); int hourlyCount = Math.min(weatherSpec.hourly.size(), 24);
int dayCount = Math.min(weatherSpec.forecasts.size(), 8); int dayCount = Math.min(weatherSpec.forecasts.size(), 8);
@ -85,6 +83,7 @@ public class SendWeatherForecastRequest extends Request {
try { try {
return new WeatherForecastData.Request( return new WeatherForecastData.Request(
this.paramsProvider, this.paramsProvider,
this.weatherSettings,
timeDataArrayList, timeDataArrayList,
dayDataArrayList dayDataArrayList
).serialize(); ).serialize();