1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-10-02 11:17:00 +02:00

Always initialize weather before sending

This commit is contained in:
Martin.JM 2024-02-15 20:31:17 +01:00 committed by José Rebelo
parent 4b70f3fcdb
commit 6a2186919c
3 changed files with 51 additions and 74 deletions

View File

@ -166,8 +166,6 @@ public class HuaweiSupportProvider {
private MusicStateSpec musicStateSpec = null; private MusicStateSpec musicStateSpec = null;
private MusicSpec musicSpec = null; private MusicSpec musicSpec = null;
private Weather.Settings weatherSettings = null;
private final HuaweiPacket.ParamsProvider paramsProvider = new HuaweiPacket.ParamsProvider(); private final HuaweiPacket.ParamsProvider paramsProvider = new HuaweiPacket.ParamsProvider();
protected ResponseManager responseManager = new ResponseManager(this); protected ResponseManager responseManager = new ResponseManager(this);
@ -1703,86 +1701,61 @@ public class HuaweiSupportProvider {
} }
public void onSendWeather(WeatherSpec weatherSpec) { public void onSendWeather(WeatherSpec weatherSpec) {
if (weatherSettings != null && weatherSettings.weatherSupported) { // Initialize weather settings and send weather
try { if (!getHuaweiCoordinator().supportsWeather()) {
SendWeatherCurrentRequest sendWeatherCurrentRequest = new SendWeatherCurrentRequest( // TODO: exception?
this, return;
weatherSettings, }
weatherSpec
);
SendGpsAndTimeToDeviceRequest sendGpsAndTimeToDeviceRequest = new SendGpsAndTimeToDeviceRequest( Weather.Settings weatherSettings = new Weather.Settings();
this
);
sendWeatherCurrentRequest.nextRequest(sendGpsAndTimeToDeviceRequest); SendWeatherStartRequest weatherStartRequest = new SendWeatherStartRequest(this, weatherSettings);
Request lastRequest = weatherStartRequest;
if (getHuaweiCoordinator().supportsWeatherUnit()) {
SendWeatherUnitRequest weatherUnitRequest = new SendWeatherUnitRequest(this);
lastRequest.nextRequest(weatherUnitRequest);
lastRequest = weatherUnitRequest;
}
if (getHuaweiCoordinator().supportsWeatherForecasts()) { SendWeatherSupportRequest weatherSupportRequest = new SendWeatherSupportRequest(this, weatherSettings);
SendWeatherForecastRequest sendWeatherForecastRequest = new SendWeatherForecastRequest( lastRequest.nextRequest(weatherSupportRequest);
this, lastRequest = weatherSupportRequest;
weatherSpec
);
sendGpsAndTimeToDeviceRequest.nextRequest(sendWeatherForecastRequest);
}
sendWeatherCurrentRequest.doPerform(); if (getHuaweiCoordinator().supportsWeatherExtended()) {
} catch (IOException e) { SendWeatherExtendedSupportRequest weatherExtendedSupportRequest = new SendWeatherExtendedSupportRequest(this, weatherSettings);
// TODO: Use translatable string lastRequest.nextRequest(weatherExtendedSupportRequest);
GB.toast(context, "Failed to send weather", Toast.LENGTH_SHORT, GB.ERROR, e); lastRequest = weatherExtendedSupportRequest;
LOG.error("Failed to send weather", e); }
}
} else {
// Initialize weather settings
if (!getHuaweiCoordinator().supportsWeather()) {
// TODO: exception?
return;
}
this.weatherSettings = new Weather.Settings(); if (getHuaweiCoordinator().supportsWeatherMoonRiseSet()) {
SendWeatherSunMoonSupportRequest weatherSunMoonSupportRequest = new SendWeatherSunMoonSupportRequest(this, weatherSettings);
lastRequest.nextRequest(weatherSunMoonSupportRequest);
lastRequest = weatherSunMoonSupportRequest;
}
RequestCallback requestCallback = new RequestCallback(this) { // End of initialization and start of actually sending weather
@Override
public void call() {
this.support.weatherSettings.weatherSupported = true;
this.support.onSendWeather(weatherSpec);
}
};
SendWeatherStartRequest weatherStartRequest = new SendWeatherStartRequest(this); SendWeatherCurrentRequest sendWeatherCurrentRequest = new SendWeatherCurrentRequest(this, weatherSettings, weatherSpec);
Request lastRequest = weatherStartRequest; lastRequest.nextRequest(sendWeatherCurrentRequest);
lastRequest = sendWeatherCurrentRequest;
if (getHuaweiCoordinator().supportsWeatherUnit()) { SendGpsAndTimeToDeviceRequest sendGpsAndTimeToDeviceRequest = new SendGpsAndTimeToDeviceRequest(this);
SendWeatherUnitRequest weatherUnitRequest = new SendWeatherUnitRequest(this); lastRequest.nextRequest(sendGpsAndTimeToDeviceRequest);
lastRequest.nextRequest(weatherUnitRequest); lastRequest = sendGpsAndTimeToDeviceRequest;
lastRequest = weatherUnitRequest;
}
SendWeatherSupportRequest weatherSupportRequest = new SendWeatherSupportRequest(this, weatherSettings); if (getHuaweiCoordinator().supportsWeatherForecasts()) {
lastRequest.nextRequest(weatherSupportRequest); SendWeatherForecastRequest sendWeatherForecastRequest = new SendWeatherForecastRequest(this, weatherSpec);
lastRequest = weatherSupportRequest; lastRequest.nextRequest(sendWeatherForecastRequest);
lastRequest = sendWeatherForecastRequest;
}
if (getHuaweiCoordinator().supportsWeatherExtended()) { try {
SendWeatherExtendedSupportRequest weatherExtendedSupportRequest = new SendWeatherExtendedSupportRequest(this, weatherSettings); weatherStartRequest.doPerform();
lastRequest.nextRequest(weatherExtendedSupportRequest); } catch (IOException e) {
lastRequest = weatherExtendedSupportRequest; // TODO: Use translatable string
} GB.toast(context, "Failed to send weather", Toast.LENGTH_SHORT, GB.ERROR, e);
LOG.error("Failed to send weather", e);
if (getHuaweiCoordinator().supportsWeatherMoonRiseSet()) {
SendWeatherSunMoonSupportRequest weatherSunMoonSupportRequest = new SendWeatherSunMoonSupportRequest(this, weatherSettings);
lastRequest.nextRequest(weatherSunMoonSupportRequest);
lastRequest = weatherSunMoonSupportRequest;
}
lastRequest.setFinalizeReq(requestCallback);
try {
weatherStartRequest.doPerform();
} catch (IOException e) {
// TODO: Use translatable string
GB.toast(context, "Failed to send initialize weather requests", Toast.LENGTH_SHORT, GB.ERROR, e);
LOG.error("Failed to send initialize weather requests", e);
}
} }
} }
} }

View File

@ -27,8 +27,8 @@ public class SendGpsAndTimeToDeviceRequest extends Request {
return new GpsAndTime.CurrentGPSRequest( return new GpsAndTime.CurrentGPSRequest(
this.paramsProvider, this.paramsProvider,
(int) (Calendar.getInstance().getTime().getTime() / 1000L) - 60, (int) (Calendar.getInstance().getTime().getTime() / 1000L) - 60,
prefs.getFloat("location_latitude", 0), prefs.getFloat("location_latitude", 0.0F),
prefs.getFloat("location_longitude", 0) prefs.getFloat("location_longitude", 0.0F)
).serialize(); ).serialize();
} catch (HuaweiPacket.CryptoException e) { } catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e); throw new RequestCreationException(e);

View File

@ -32,11 +32,13 @@ public class SendWeatherStartRequest extends Request {
private static final Logger LOG = LoggerFactory.getLogger(SendWeatherStartRequest.class); private static final Logger LOG = LoggerFactory.getLogger(SendWeatherStartRequest.class);
public int response = -1; public int response = -1;
private Weather.Settings weatherSettings;
public SendWeatherStartRequest(HuaweiSupportProvider support) { public SendWeatherStartRequest(HuaweiSupportProvider support, Weather.Settings weatherSettings) {
super(support); super(support);
this.serviceId = Weather.id; this.serviceId = Weather.id;
this.commandId = Weather.WeatherStart.id; this.commandId = Weather.WeatherStart.id;
this.weatherSettings = weatherSettings;
} }
@Override @Override
@ -55,6 +57,8 @@ public class SendWeatherStartRequest extends Request {
this.stopChain(); this.stopChain();
GB.toast(supportProvider.getContext(), "Received non-ok status for WeatherStart response", Toast.LENGTH_SHORT, GB.INFO); GB.toast(supportProvider.getContext(), "Received non-ok status for WeatherStart response", Toast.LENGTH_SHORT, GB.INFO);
LOG.info("Received non-ok status for WeatherStart response"); LOG.info("Received non-ok status for WeatherStart response");
} else {
weatherSettings.weatherSupported = true;
} }
} else { } else {
this.stopChain(); this.stopChain();