From 71f497ecbb4e57b232dde4182eb77a0952939fb9 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sun, 24 Mar 2024 17:17:53 +0100 Subject: [PATCH] Garmin protocol: create specific field definition for day of week --- .../service/devices/garmin/GarminSupport.java | 4 +-- .../FieldDefinitionDayOfWeek.java | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/fit/fieldDefinitions/FieldDefinitionDayOfWeek.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java index 77c250c51..7150540b6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java @@ -189,7 +189,7 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni todayDailyForecast.setFieldByName("high_temperature", weather.todayMaxTemp - 273.15); todayDailyForecast.setFieldByName("condition", FitWeatherConditions.openWeatherCodeToFitWeatherStatus(weather.currentConditionCode)); todayDailyForecast.setFieldByName("precipitation_probability", weather.precipProbability); - todayDailyForecast.setFieldByName("day_of_week", GarminTimeUtils.unixTimeToGarminDayOfWeek(weather.timestamp)); + todayDailyForecast.setFieldByName("day_of_week", weather.timestamp); weatherData.add(todayDailyForecast); @@ -204,7 +204,7 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni weatherDailyForecast.setFieldByName("high_temperature", daily.maxTemp - 273.15); weatherDailyForecast.setFieldByName("condition", FitWeatherConditions.openWeatherCodeToFitWeatherStatus(daily.conditionCode)); weatherDailyForecast.setFieldByName("precipitation_probability", daily.precipProbability); - weatherDailyForecast.setFieldByName("day_of_week", GarminTimeUtils.unixTimeToGarminDayOfWeek(ts)); + weatherDailyForecast.setFieldByName("day_of_week", ts); weatherData.add(weatherDailyForecast); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/fit/fieldDefinitions/FieldDefinitionDayOfWeek.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/fit/fieldDefinitions/FieldDefinitionDayOfWeek.java new file mode 100644 index 000000000..a1dfefeec --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/fit/fieldDefinitions/FieldDefinitionDayOfWeek.java @@ -0,0 +1,32 @@ +package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.fieldDefinitions; + +import org.threeten.bp.DayOfWeek; +import org.threeten.bp.Instant; +import org.threeten.bp.ZoneId; + +import java.nio.ByteBuffer; + +import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.FieldDefinition; +import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.baseTypes.BaseType; + +public class FieldDefinitionDayOfWeek extends FieldDefinition { + + public FieldDefinitionDayOfWeek(int localNumber, int size, BaseType baseType, String name) { + super(localNumber, size, baseType, name, 1, 0); + } + + @Override + public Object decode(ByteBuffer byteBuffer) { + int raw = (int) baseType.decode(byteBuffer, scale, offset); + return DayOfWeek.of(raw == 0 ? 7 : raw); + } + + @Override + public void encode(ByteBuffer byteBuffer, Object o) { + if (o instanceof DayOfWeek) { + baseType.encode(byteBuffer, (((DayOfWeek) o).getValue() % 7), scale, offset); + return; + } + baseType.encode(byteBuffer, (Instant.ofEpochSecond((int) o).atZone(ZoneId.systemDefault()).getDayOfWeek().getValue() % 7), scale, offset); + } +}