1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 03:16:51 +01:00

Add outgoing parsing

This commit is contained in:
Martin.JM 2024-02-01 19:52:04 +01:00 committed by José Rebelo
parent 2b1c5b5819
commit 0b64408b33
3 changed files with 129 additions and 52 deletions

View File

@ -553,6 +553,13 @@ public class HuaweiPacket {
default:
return this;
}
case Weather.id:
switch (this.commandId) {
case Weather.WeatherForecastData.id:
return new Weather.WeatherForecastData.OutgoingRequest(paramsProvider).fromPacket(this);
default:
return this;
}
default:
return this;
}

View File

@ -16,6 +16,9 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
@ -204,13 +207,24 @@ public class Weather {
}
}
public static class WeatherForecastDataRequest extends HuaweiPacket {
public static class WeatherForecastData {
public static final byte id = 0x08;
public static class TimeData {
public int timestamp;
public byte icon;
public byte temperature;
@Override
public String toString() {
String timestampStr = new Date(timestamp * 1000L).toString();
return "TimeData{" +
"timestamp=" + timestamp +
", timestamp=" + timestampStr +
", icon=" + icon +
", temperature=" + temperature +
'}';
}
}
public static class DayData {
@ -223,56 +237,112 @@ public class Weather {
public int moonRiseTime;
public int moonSetTime;
public byte moonPhase; // TODO: probably enum
@Override
public String toString() {
String timestampStr = new Date(timestamp * 1000L).toString();
return "DayData{" +
"timestamp=" + timestamp +
", timestamp=" + timestampStr +
", icon=" + icon +
", highTemperature=" + highTemperature +
", lowTemperature=" + lowTemperature +
", sunriseTime=" + sunriseTime +
", sunsetTime=" + sunsetTime +
", moonRiseTime=" + moonRiseTime +
", moonSetTime=" + moonSetTime +
", moonPhase=" + moonPhase +
'}';
}
}
public WeatherForecastDataRequest(
ParamsProvider paramsProvider,
List<TimeData> timeDataList,
List<DayData> dayDataList
) {
super(paramsProvider);
public static class Request extends HuaweiPacket {
public Request(
ParamsProvider paramsProvider,
List<TimeData> timeDataList,
List<DayData> dayDataList
) {
super(paramsProvider);
this.serviceId = Weather.id;
this.commandId = id;
this.tlv = new HuaweiTLV();
this.serviceId = Weather.id;
this.commandId = id;
this.tlv = new HuaweiTLV();
if (timeDataList != null && !timeDataList.isEmpty()) {
HuaweiTLV timeDataTlv = new HuaweiTLV();
for (TimeData timeData : timeDataList) {
// TODO: NULLs?
timeDataTlv.put(0x82, new HuaweiTLV()
.put(0x03, timeData.timestamp)
.put(0x04, timeData.icon)
.put(0x05, timeData.temperature)
);
if (timeDataList != null && !timeDataList.isEmpty()) {
HuaweiTLV timeDataTlv = new HuaweiTLV();
for (TimeData timeData : timeDataList) {
// TODO: NULLs?
timeDataTlv.put(0x82, new HuaweiTLV()
.put(0x03, timeData.timestamp)
.put(0x04, timeData.icon)
.put(0x05, timeData.temperature)
);
}
this.tlv.put(0x81, timeDataTlv);
}
this.tlv.put(0x81, timeDataTlv);
}
// this.tlv.put(0x81);
// this.tlv.put(0x81);
if (dayDataList != null && !dayDataList.isEmpty()) {
HuaweiTLV dayDataTlv = new HuaweiTLV();
for (DayData dayData : dayDataList) {
// TODO: NULLs?
dayDataTlv.put(0x91, new HuaweiTLV()
.put(0x12, dayData.timestamp)
.put(0x13, dayData.icon)
.put(0x14, dayData.highTemperature)
.put(0x15, dayData.lowTemperature)
.put(0x16, dayData.sunriseTime)
.put(0x17, dayData.sunsetTime)
.put(0x1a, dayData.moonRiseTime)
.put(0x1b, dayData.moonSetTime)
.put(0x1e, dayData.moonPhase)
);
if (dayDataList != null && !dayDataList.isEmpty()) {
HuaweiTLV dayDataTlv = new HuaweiTLV();
for (DayData dayData : dayDataList) {
// TODO: NULLs?
dayDataTlv.put(0x91, new HuaweiTLV()
.put(0x12, dayData.timestamp)
.put(0x13, dayData.icon)
.put(0x14, dayData.highTemperature)
.put(0x15, dayData.lowTemperature)
.put(0x16, dayData.sunriseTime)
.put(0x17, dayData.sunsetTime)
.put(0x1a, dayData.moonRiseTime)
.put(0x1b, dayData.moonSetTime)
.put(0x1e, dayData.moonPhase)
);
}
this.tlv.put(0x90, dayDataTlv);
}
this.tlv.put(0x90, dayDataTlv);
}
// this.tlv.put(0x90);
// this.tlv.put(0x90);
this.isEncrypted = true;
this.isSliced = true;
this.complete = true;
this.isEncrypted = true;
this.isSliced = true;
this.complete = true;
}
}
public static class OutgoingRequest extends HuaweiPacket {
List<TimeData> timeDataList;
List<DayData> dayDataList;
public OutgoingRequest(ParamsProvider paramsProvider) {
super(paramsProvider);
this.complete = false;
}
@Override
public void parseTlv() throws ParseException {
timeDataList = new ArrayList<>(this.tlv.getObject(0x81).getObjects(0x82).size());
for (HuaweiTLV timeTlv : this.tlv.getObject(0x81).getObjects(0x82)) {
TimeData timeData = new TimeData();
timeData.timestamp = timeTlv.getInteger(0x03);
timeData.icon = timeTlv.getByte(0x04);
timeData.temperature = timeTlv.getByte(0x05);
timeDataList.add(timeData);
}
dayDataList = new ArrayList<>(this.tlv.getObject(0x90).getObjects(0x91).size());
for (HuaweiTLV dayTlv : this.tlv.getObject(0x90).getObjects(0x91)) {
DayData dayData = new DayData();
dayData.timestamp = dayTlv.getInteger(0x12);
dayData.icon = dayTlv.getByte(0x13);
dayData.highTemperature = dayTlv.getByte(0x14);
dayData.lowTemperature = dayTlv.getByte(0x15);
dayData.sunriseTime = dayTlv.getInteger(0x16);
dayData.sunsetTime = dayTlv.getInteger(0x17);
dayData.moonRiseTime = dayTlv.getInteger(0x1a);
dayData.moonSetTime = dayTlv.getInteger(0x1b);
dayData.moonPhase = dayTlv.getByte(0x1e);
dayDataList.add(dayData);
}
}
}
}

View File

@ -23,7 +23,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather.WeatherForecastDataRequest;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather.WeatherForecastData;
public class SendWeatherForecastRequest extends Request {
WeatherSpec weatherSpec;
@ -31,23 +31,23 @@ public class SendWeatherForecastRequest extends Request {
public SendWeatherForecastRequest(HuaweiSupportProvider support, WeatherSpec weatherSpec) {
super(support);
this.serviceId = Weather.id;
this.commandId = Weather.WeatherForecastDataRequest.id;
this.commandId = Weather.WeatherForecastData.id;
this.weatherSpec = weatherSpec;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
// TODO: Weather settings
ArrayList<WeatherForecastDataRequest.TimeData> timeDataArrayList = new ArrayList<>(
ArrayList<WeatherForecastData.TimeData> timeDataArrayList = new ArrayList<>(
this.weatherSpec.hourly.size() // TODO: wrong size
);
ArrayList<WeatherForecastDataRequest.DayData> dayDataArrayList = new ArrayList<>(
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?
for (int i = Math.min(weatherSpec.hourly.size(), 24) - 1; i >= 0; i--) { // TODO: min?
WeatherSpec.Hourly hourly = weatherSpec.hourly.get(i);
WeatherForecastDataRequest.TimeData timeData = new WeatherForecastDataRequest.TimeData();
WeatherForecastData.TimeData timeData = new WeatherForecastData.TimeData();
timeData.timestamp = hourly.timestamp;
timeData.icon = 1; // TODO: hourly.conditionCode conversion
timeData.temperature = (byte) (hourly.temp - 273);
@ -55,7 +55,7 @@ public class SendWeatherForecastRequest extends Request {
}
// Add today as well
WeatherForecastDataRequest.DayData today = new WeatherForecastDataRequest.DayData();
WeatherForecastData.DayData today = new WeatherForecastData.DayData();
today.timestamp = weatherSpec.sunRise;
today.icon = 1; // TODO
today.highTemperature = (byte) (weatherSpec.todayMaxTemp - 273);
@ -69,7 +69,7 @@ public class SendWeatherForecastRequest extends Request {
for (int i = 0; i < Math.min(weatherSpec.forecasts.size(), 7); i++) { // TODO: min?
WeatherSpec.Daily daily = weatherSpec.forecasts.get(i);
WeatherForecastDataRequest.DayData dayData = new WeatherForecastDataRequest.DayData();
WeatherForecastData.DayData dayData = new WeatherForecastData.DayData();
dayData.timestamp = daily.sunRise;
dayData.icon = 1; // TODO: daily.conditionCode conversion
dayData.highTemperature = (byte) (daily.maxTemp - 273);
@ -82,7 +82,7 @@ public class SendWeatherForecastRequest extends Request {
dayDataArrayList.add(dayData);
}
try {
return new WeatherForecastDataRequest(
return new WeatherForecastData.Request(
this.paramsProvider,
timeDataArrayList,
dayDataArrayList