Add unit support and some cleanup
This commit is contained in:
parent
043f839a4d
commit
242f3c6dbc
|
@ -251,6 +251,17 @@ public class Weather {
|
|||
}
|
||||
}
|
||||
|
||||
public enum HuaweiTemperatureFormat {
|
||||
CELSIUS,
|
||||
FAHRENHEIT
|
||||
}
|
||||
|
||||
private static byte temperatureFormatToByte(HuaweiTemperatureFormat temperatureFormat) {
|
||||
if (temperatureFormat == HuaweiTemperatureFormat.FAHRENHEIT)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static class CurrentWeatherRequest extends HuaweiPacket {
|
||||
public static final byte id = 0x01;
|
||||
|
||||
|
@ -265,7 +276,7 @@ public class Weather {
|
|||
Short pm25, // TODO: might be float?
|
||||
String locationName,
|
||||
Byte currentTemperature,
|
||||
Byte temperatureUnit,
|
||||
HuaweiTemperatureFormat temperatureUnit,
|
||||
Short airQualityIndex,
|
||||
Integer observationTime,
|
||||
String sourceName
|
||||
|
@ -314,7 +325,7 @@ public class Weather {
|
|||
if (currentTemperature != null && settings.currentTemperatureSupported)
|
||||
this.tlv.put(0x09, currentTemperature);
|
||||
if (temperatureUnit != null && settings.unitSupported)
|
||||
this.tlv.put(0x0a, temperatureUnit);
|
||||
this.tlv.put(0x0a, temperatureFormatToByte(temperatureUnit));
|
||||
if (airQualityIndex != null && settings.airQualityIndexSupported)
|
||||
this.tlv.put(0x0b, airQualityIndex);
|
||||
if (observationTime != null && settings.timeSupported)
|
||||
|
@ -382,12 +393,12 @@ public class Weather {
|
|||
public static class WeatherUnitRequest extends HuaweiPacket {
|
||||
public static final byte id = 0x05;
|
||||
|
||||
public WeatherUnitRequest(ParamsProvider paramsProvider) {
|
||||
public WeatherUnitRequest(ParamsProvider paramsProvider, HuaweiTemperatureFormat temperatureFormat) {
|
||||
super(paramsProvider);
|
||||
|
||||
this.serviceId = Weather.id;
|
||||
this.commandId = id;
|
||||
this.tlv = new HuaweiTLV().put(0x01, (byte) 0); // TODO: find out what unit is what
|
||||
this.tlv = new HuaweiTLV().put(0x01, temperatureFormatToByte(temperatureFormat));
|
||||
this.isEncrypted = true;
|
||||
this.complete = true;
|
||||
}
|
||||
|
@ -507,7 +518,6 @@ public class Weather {
|
|||
}
|
||||
this.tlv.put(0x81, timeDataTlv);
|
||||
}
|
||||
// this.tlv.put(0x81);
|
||||
|
||||
if (dayDataList != null && !dayDataList.isEmpty()) {
|
||||
HuaweiTLV dayDataTlv = new HuaweiTLV();
|
||||
|
@ -527,7 +537,6 @@ public class Weather {
|
|||
}
|
||||
this.tlv.put(0x90, dayDataTlv);
|
||||
}
|
||||
// this.tlv.put(0x90);
|
||||
|
||||
this.isEncrypted = true;
|
||||
this.isSliced = true;
|
||||
|
|
|
@ -389,7 +389,7 @@ public class AsynchronousResponse {
|
|||
SendWeatherDeviceRequest sendWeatherDeviceRequest = new SendWeatherDeviceRequest(this.support);
|
||||
sendWeatherDeviceRequest.doPerform();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // TODO: Change
|
||||
LOG.error("Could not send weather device request", e);
|
||||
}
|
||||
|
||||
// TODO: send back weather?
|
||||
|
|
|
@ -1703,7 +1703,7 @@ public class HuaweiSupportProvider {
|
|||
public void onSendWeather(WeatherSpec weatherSpec) {
|
||||
// Initialize weather settings and send weather
|
||||
if (!getHuaweiCoordinator().supportsWeather()) {
|
||||
// TODO: exception?
|
||||
LOG.error("onSendWeather called while weather is not supported.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,9 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
|
@ -38,8 +41,11 @@ public class SendWeatherCurrentRequest extends Request {
|
|||
|
||||
@Override
|
||||
protected List<byte[]> createRequest() throws RequestCreationException {
|
||||
Weather.HuaweiTemperatureFormat temperatureFormat = Weather.HuaweiTemperatureFormat.CELSIUS;
|
||||
String unit = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
||||
if (unit.equals(GBApplication.getContext().getString(R.string.p_unit_imperial)))
|
||||
temperatureFormat = Weather.HuaweiTemperatureFormat.FAHRENHEIT;
|
||||
try {
|
||||
// TODO: support multiple units
|
||||
Short pm25 = null;
|
||||
Short aqi = null;
|
||||
if (weatherSpec.airQuality != null) {
|
||||
|
@ -57,7 +63,7 @@ public class SendWeatherCurrentRequest extends Request {
|
|||
pm25,
|
||||
weatherSpec.location,
|
||||
(byte) (weatherSpec.currentTemp - 273),
|
||||
(byte) 0,
|
||||
temperatureFormat,
|
||||
aqi,
|
||||
weatherSpec.timestamp,
|
||||
"Gadgetbridge"
|
||||
|
|
|
@ -38,18 +38,16 @@ public class SendWeatherForecastRequest extends Request {
|
|||
@Override
|
||||
protected List<byte[]> createRequest() throws RequestCreationException {
|
||||
// TODO: Weather settings
|
||||
ArrayList<WeatherForecastData.TimeData> timeDataArrayList = new ArrayList<>(
|
||||
this.weatherSpec.hourly.size() // TODO: wrong size
|
||||
);
|
||||
ArrayList<WeatherForecastData.DayData> dayDataArrayList = new ArrayList<>(
|
||||
this.weatherSpec.forecasts.size() // TODO: wrong size
|
||||
);
|
||||
// for (WeatherSpec.Hourly hourly : weatherSpec.hourly) {
|
||||
for (int i = 0; i < Math.min(weatherSpec.hourly.size(), 24); i++) { // TODO: min?
|
||||
int hourlyCount = Math.min(weatherSpec.hourly.size(), 24);
|
||||
int dayCount = Math.min(weatherSpec.forecasts.size(), 8);
|
||||
|
||||
ArrayList<WeatherForecastData.TimeData> timeDataArrayList = new ArrayList<>(hourlyCount);
|
||||
ArrayList<WeatherForecastData.DayData> dayDataArrayList = new ArrayList<>(dayCount);
|
||||
for (int i = 0; i < hourlyCount; i++) {
|
||||
WeatherSpec.Hourly hourly = weatherSpec.hourly.get(i);
|
||||
WeatherForecastData.TimeData timeData = new WeatherForecastData.TimeData();
|
||||
timeData.timestamp = hourly.timestamp;
|
||||
timeData.icon = supportProvider.openWeatherMapConditionCodeToHuaweiIcon(hourly.conditionCode); // TODO: hourly.conditionCode conversion
|
||||
timeData.icon = supportProvider.openWeatherMapConditionCodeToHuaweiIcon(hourly.conditionCode);
|
||||
timeData.temperature = (byte) (hourly.temp - 273);
|
||||
timeDataArrayList.add(timeData);
|
||||
}
|
||||
|
@ -67,7 +65,7 @@ public class SendWeatherForecastRequest extends Request {
|
|||
today.moonPhase = (byte) 4; // weatherSpec.moonPhase; // TODO: check
|
||||
dayDataArrayList.add(today);
|
||||
|
||||
for (int i = 0; i < Math.min(weatherSpec.forecasts.size(), 7); i++) { // TODO: min?
|
||||
for (int i = 0; i < dayCount - 1; i++) {
|
||||
WeatherSpec.Daily daily = weatherSpec.forecasts.get(i);
|
||||
WeatherForecastData.DayData dayData = new WeatherForecastData.DayData();
|
||||
dayData.timestamp = weatherSpec.timestamp + (60*60*24 * (i + 1));
|
||||
|
|
|
@ -18,8 +18,12 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
|
||||
|
||||
public class SendWeatherUnitRequest extends Request {
|
||||
|
@ -32,8 +36,12 @@ public class SendWeatherUnitRequest extends Request {
|
|||
|
||||
@Override
|
||||
protected List<byte[]> createRequest() throws RequestCreationException {
|
||||
Weather.HuaweiTemperatureFormat temperatureFormat = Weather.HuaweiTemperatureFormat.CELSIUS;
|
||||
String unit = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
||||
if (unit.equals(GBApplication.getContext().getString(R.string.p_unit_imperial)))
|
||||
temperatureFormat = Weather.HuaweiTemperatureFormat.FAHRENHEIT;
|
||||
try {
|
||||
return new Weather.WeatherUnitRequest(this.paramsProvider).serialize();
|
||||
return new Weather.WeatherUnitRequest(this.paramsProvider, temperatureFormat).serialize();
|
||||
} catch (HuaweiPacket.CryptoException e) {
|
||||
throw new RequestCreationException(e);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue