1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-15 14:39:26 +01:00

Garmin protocol: create custom GBDeviceEvent for weather request

This commit is contained in:
Daniele Gobbetti 2024-03-27 11:29:01 +01:00
parent 4054326535
commit 8524426b70
3 changed files with 51 additions and 31 deletions

View File

@ -16,9 +16,11 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.proto.vivomovehr.GdiDeviceStatus; import nodomain.freeyourgadget.gadgetbridge.proto.vivomovehr.GdiDeviceStatus;
import nodomain.freeyourgadget.gadgetbridge.proto.vivomovehr.GdiFindMyWatch; import nodomain.freeyourgadget.gadgetbridge.proto.vivomovehr.GdiFindMyWatch;
@ -29,8 +31,10 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.communicator.ICommunicator; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.communicator.ICommunicator;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.communicator.v1.CommunicatorV1; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.communicator.v1.CommunicatorV1;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.communicator.v2.CommunicatorV2; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.communicator.v2.CommunicatorV2;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.deviceevents.WeatherRequestDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.GlobalDefinitionsEnum; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.GlobalDefinitionsEnum;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordData; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordData;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordDefinition;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.ConfigurationMessage; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.ConfigurationMessage;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.GFDIMessage; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.GFDIMessage;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.MusicControlEntityUpdateMessage; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.MusicControlEntityUpdateMessage;
@ -134,6 +138,18 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
} }
} }
@Override
public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) {
if (deviceEvent instanceof WeatherRequestDeviceEvent) {
WeatherSpec weather = Weather.getInstance().getWeatherSpec();
if (weather != null) {
sendWeatherConditions(weather);
}
}
super.evaluateGBDeviceEvent(deviceEvent);
}
@Override @Override
public void onSendWeather(final ArrayList<WeatherSpec> weatherSpecs) { public void onSendWeather(final ArrayList<WeatherSpec> weatherSpecs) {
sendWeatherConditions(weatherSpecs.get(0)); sendWeatherConditions(weatherSpecs.get(0));
@ -142,6 +158,13 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
private void sendWeatherConditions(WeatherSpec weather) { private void sendWeatherConditions(WeatherSpec weather) {
List<RecordData> weatherData = new ArrayList<>(); List<RecordData> weatherData = new ArrayList<>();
List<RecordDefinition> weatherDefinitions = new ArrayList<>(3);
weatherDefinitions.add(GlobalDefinitionsEnum.TODAY_WEATHER_CONDITIONS.getRecordDefinition());
weatherDefinitions.add(GlobalDefinitionsEnum.HOURLY_WEATHER_FORECAST.getRecordDefinition());
weatherDefinitions.add(GlobalDefinitionsEnum.DAILY_WEATHER_FORECAST.getRecordDefinition());
communicator.sendMessage(new nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.FitDefinitionMessage(weatherDefinitions).getOutgoingMessage());
try { try {
RecordData today = new RecordData(GlobalDefinitionsEnum.TODAY_WEATHER_CONDITIONS.getRecordDefinition()); RecordData today = new RecordData(GlobalDefinitionsEnum.TODAY_WEATHER_CONDITIONS.getRecordDefinition());
today.setFieldByName("weather_report", 0); // 0 = current, 1 = hourly_forecast, 2 = daily_forecast today.setFieldByName("weather_report", 0); // 0 = current, 1 = hourly_forecast, 2 = daily_forecast

View File

@ -0,0 +1,18 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.deviceevents;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
public class WeatherRequestDeviceEvent extends GBDeviceEvent {
private final int format;
private final int latitude;
private final int longitude;
private final int hoursOfForecast;
public WeatherRequestDeviceEvent(int format, int latitude, int longitude, int hoursOfForecast) {
this.format = format;
this.latitude = latitude;
this.longitude = longitude;
this.hoursOfForecast = hoursOfForecast;
}
}

View File

@ -1,33 +1,13 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages; package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages;
import java.util.ArrayList; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.deviceevents.WeatherRequestDeviceEvent;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.GlobalDefinitionsEnum;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordDefinition;
public class WeatherMessage extends GFDIMessage { public class WeatherMessage extends GFDIMessage {
private final int format; private final WeatherRequestDeviceEvent weatherRequestDeviceEvent;
private final int latitude;
private final int longitude;
private final int hoursOfForecast;
private final List<RecordDefinition> weatherDefinitions;
public WeatherMessage(int format, int latitude, int longitude, int hoursOfForecast, GarminMessage garminMessage) { public WeatherMessage(int format, int latitude, int longitude, int hoursOfForecast, GarminMessage garminMessage) {
this.format = format;
this.latitude = latitude;
this.longitude = longitude;
this.hoursOfForecast = hoursOfForecast;
this.garminMessage = garminMessage; this.garminMessage = garminMessage;
weatherRequestDeviceEvent = new WeatherRequestDeviceEvent(format, latitude, longitude, hoursOfForecast);
weatherDefinitions = new ArrayList<>(3);
weatherDefinitions.add(GlobalDefinitionsEnum.TODAY_WEATHER_CONDITIONS.getRecordDefinition());
weatherDefinitions.add(GlobalDefinitionsEnum.HOURLY_WEATHER_FORECAST.getRecordDefinition());
weatherDefinitions.add(GlobalDefinitionsEnum.DAILY_WEATHER_FORECAST.getRecordDefinition());
this.statusMessage = this.getStatusMessage(); this.statusMessage = this.getStatusMessage();
} }
@ -41,14 +21,13 @@ public class WeatherMessage extends GFDIMessage {
return new WeatherMessage(format, latitude, longitude, hoursOfForecast, garminMessage); return new WeatherMessage(format, latitude, longitude, hoursOfForecast, garminMessage);
} }
public WeatherRequestDeviceEvent getGBDeviceEvent() {
return weatherRequestDeviceEvent;
}
@Override @Override
protected boolean generateOutgoing() { protected boolean generateOutgoing() {
final MessageWriter writer = new MessageWriter(response); return false;
writer.writeShort(0); // packet size will be filled below
writer.writeShort(GarminMessage.FIT_DEFINITION.getId());
for (RecordDefinition definition : weatherDefinitions) {
definition.generateOutgoingPayload(writer);
}
return true;
} }
} }