From a6cb73e843e50b83fa0081bc9c4431a2f01c2b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Wed, 21 Sep 2022 21:30:23 +0100 Subject: [PATCH] Amazfit GTS 3: Fix battery drain due to unanswered weather requests - Reply with HTTP 404 to unknown weather endpoints - Add some missing fields to weather responses The official Zepp app itself gets a 404 when calling a /weather/tide endpoint, so we don't know what the watch is supposed to receive. Weather also seems to still not work correctly on the GTS 3, but this at least fixes the request spam that was coming from the watch on the tide endpoint. --- .../devices/huami/Huami2021Support.java | 17 ++++--- .../devices/huami/Huami2021Weather.java | 45 ++++++++++++++----- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java index 68c6409c5..beb2bf47d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java @@ -2237,9 +2237,16 @@ public abstract class Huami2021Support extends HuamiSupport { final Huami2021Weather.Response response = Huami2021Weather.handleHttpRequest(path, query); if (response != null) { - replyHttpSuccess(requestId, response.toJson()); - return; + replyHttpSuccess(requestId, 200, response.toJson()); + } else { + final Huami2021Weather.Response notFoundResponse = new Huami2021Weather.ErrorResponse( + -2001, + "Not found" + ); + replyHttpSuccess(requestId, 404, notFoundResponse.toJson()); } + + return; } LOG.error("Unhandled URL {}", url); @@ -2273,8 +2280,8 @@ public abstract class Huami2021Support extends HuamiSupport { writeToChunked2021("http reply no internet", Huami2021Service.CHUNKED2021_ENDPOINT_HTTP, cmd, true); } - private void replyHttpSuccess(final byte requestId, final String content) { - LOG.debug("Replying with success to http request {} with {}", requestId, content); + private void replyHttpSuccess(final byte requestId, final int status, final String content) { + LOG.debug("Replying with http {} request {} with {}", status, requestId, content); final byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8); final ByteBuffer buf = ByteBuffer.allocate(8 + contentBytes.length); @@ -2283,7 +2290,7 @@ public abstract class Huami2021Support extends HuamiSupport { buf.put((byte) 0x02); buf.put(requestId); buf.put(HTTP_RESPONSE_SUCCESS); - buf.put((byte) 0xc8); // ? + buf.put((byte) status); buf.putInt(contentBytes.length); buf.put(contentBytes); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java index 3cffae38c..15f595a85 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java @@ -99,6 +99,24 @@ public class Huami2021Weather { } } + public static class ErrorResponse extends Response { + private final int code; + private final String message; + + public ErrorResponse(final int code, final String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } + } + public static abstract class Response { public String toJson() { return GSON.toJson(this); @@ -114,13 +132,13 @@ public class Huami2021Weather { // locationKey=00.000,-0.000,xiaomi_accu:000000 public static class ForecastResponse extends Response { public Date pubTime; - public List humidity = new ArrayList<>(); + public List humidity = new ArrayList<>(); public List temperature = new ArrayList<>(); public List weather = new ArrayList<>(); public List windDirection = new ArrayList<>(); public List sunRiseSet = new ArrayList<>(); public List windSpeed = new ArrayList<>(); - public Object moonRiseSet = new Object(); + public Object moonRiseSet = new Object(); // MoonRiseSet public List airQualities = new ArrayList<>(); public ForecastResponse(final WeatherSpec weatherSpec, final int days) { @@ -176,6 +194,11 @@ public class Huami2021Weather { } } + private static class MoonRiseSet { + public List moonPhaseValue = new ArrayList<>(); + public List moonRise = new ArrayList<>(); + } + private static class Range { public String from; public String to; @@ -224,6 +247,7 @@ public class Huami2021Weather { // locationKey=00.000,-0.000,xiaomi_accu:000000 public static class CurrentResponse extends Response { public CurrentWeatherModel currentWeatherModel; + public Object aqiModel = new Object(); public CurrentResponse(final WeatherSpec weatherSpec) { this.currentWeatherModel = new CurrentWeatherModel(weatherSpec); @@ -305,14 +329,14 @@ public class Huami2021Weather { // isGlobal=true // locationKey=00.000,-0.000,xiaomi_accu:000000 public static class HourlyResponse extends Response { - public Object pubTime; - public Object weather; - public Object temperature; - public Object humidity; - public Object fxTime; - public Object windDirection; - public Object windSpeed; - public Object windScale; + public Date pubTime; + public List weather; + public List temperature; + public List humidity; + public List fxTime; // pubTime format + public List windDirection; + public List windSpeed; + public List windScale; // each element in the form of 1-2 } // /weather/alerts @@ -323,6 +347,7 @@ public class Huami2021Weather { // isGlobal=true // locationKey=00.000,-0.000,xiaomi_accu:000000 public static class AlertsResponse extends Response { + public List alerts = new ArrayList<>(); } //@RequiresApi(api = Build.VERSION_CODES.O)