From 092e62bfad2f5b31aab3acc76d3c2f48ac1d2af0 Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Wed, 28 Feb 2018 12:09:33 +0100 Subject: [PATCH] Pebble: add support for weather in Simply Light watchface Celsius forced as with other watchfaces #482 --- .../pebble/AppMessageHandlerSimplyLight.java | 139 ++++++++++++++++++ .../devices/pebble/PebbleProtocol.java | 2 + 2 files changed, 141 insertions(+) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSimplyLight.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSimplyLight.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSimplyLight.java new file mode 100644 index 000000000..0836ee5d5 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSimplyLight.java @@ -0,0 +1,139 @@ +/* Copyright (C) 2016-2018 Andreas Shimokawa, Sergio Lopez + + This file is part of Gadgetbridge. + + Gadgetbridge is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Gadgetbridge is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ +package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; + +import android.util.Pair; +import android.widget.Toast; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.UUID; + +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; +import nodomain.freeyourgadget.gadgetbridge.model.Weather; +import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; +import nodomain.freeyourgadget.gadgetbridge.util.GB; + +class AppMessageHandlerSimplyLight extends AppMessageHandler { + private static final int CLEAR = 0; + private static final int CLOUDY = 1; + private static final int FOG = 2; + private static final int LIGHT_RAIN = 3; + private static final int RAIN = 4; + private static final int THUNDERSTORM = 5; + private static final int SNOW = 6; + private static final int HAIL = 7; + private static final int WIND = 8; + private static final int EXTREME_WIND = 9; + private static final int TORNADO = 10; + private static final int HURRICANE = 11; + private static final int EXTREME_COLD = 12; + private static final int EXTREME_HEAT = 13; + private static final int SNOW_THUNDERSTORM = 14; + + private Integer KEY_TEMPERATURE; + private Integer KEY_CONDITION; + private Integer KEY_ERR; + + AppMessageHandlerSimplyLight(UUID uuid, PebbleProtocol pebbleProtocol) { + super(uuid, pebbleProtocol); + + try { + JSONObject appKeys = getAppKeys(); + KEY_TEMPERATURE = appKeys.getInt("temperature"); + KEY_CONDITION = appKeys.getInt("condition"); + KEY_ERR = appKeys.getInt("err"); + } catch (JSONException e) { + GB.toast("There was an error accessing the Simply Light watchface configuration.", Toast.LENGTH_LONG, GB.ERROR); + } catch (IOException ignore) { + } + } + + +private int getConditionForConditionCode(int conditionCode) { + if (conditionCode == 800 || conditionCode == 951) { + return CLEAR; + } else if (conditionCode > 800 && conditionCode < 900) { + return CLOUDY; + } else if (conditionCode >= 700 && conditionCode < 800) { + return FOG; + } else if (conditionCode >= 300 && conditionCode < 400) { + return LIGHT_RAIN; + } else if (conditionCode >= 500 && conditionCode < 600) { + return RAIN; + } else if (conditionCode >= 200 && conditionCode < 300) { + return THUNDERSTORM; + } else if (conditionCode >= 600 && conditionCode < 700) { + return SNOW; + } else if (conditionCode == 906) { + return HAIL; + } else if (conditionCode >= 907 && conditionCode < 957) { + return WIND; + } else if (conditionCode == 905 || (conditionCode >= 957 && conditionCode < 900)) { + return EXTREME_WIND; + } else if (conditionCode == 900) { + return TORNADO; + } else if (conditionCode == 901 || conditionCode == 902 || conditionCode == 962) { + return HURRICANE; + } else if (conditionCode == 903) { + return EXTREME_COLD; + } else if (conditionCode == 904) { + return EXTREME_HEAT; + } + + return 0; + } + + private byte[] encodeSimplyLightWeatherMessage(WeatherSpec weatherSpec) { + if (weatherSpec == null) { + return null; + } + + ArrayList> pairs = new ArrayList<>(2); + pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) (weatherSpec.currentTemp - 273))); + pairs.add(new Pair<>(KEY_CONDITION, (Object) (getConditionForConditionCode(weatherSpec.currentConditionCode)))); + pairs.add(new Pair<>(KEY_ERR, (Object) 0)); + byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null); + + ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length); + + buf.put(weatherMessage); + + return buf.array(); + } + + @Override + public GBDeviceEvent[] onAppStart() { + WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec(); + if (weatherSpec == null) { + return new GBDeviceEvent[]{null}; + } + GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes(); + sendBytes.encodedBytes = encodeSimplyLightWeatherMessage(weatherSpec); + return new GBDeviceEvent[]{sendBytes}; + } + + @Override + public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) { + return encodeSimplyLightWeatherMessage(weatherSpec); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index a86efbaea..e0bc369e0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -394,6 +394,7 @@ public class PebbleProtocol extends GBDeviceProtocol { private static final UUID UUID_ZALEWSZCZAK_FANCY = UUID.fromString("014e17bf-5878-4781-8be1-8ef998cee1ba"); private static final UUID UUID_ZALEWSZCZAK_TALLY = UUID.fromString("abb51965-52e2-440a-b93c-843eeacb697d"); private static final UUID UUID_OBSIDIAN = UUID.fromString("ef42caba-0c65-4879-ab23-edd2bde68824"); + private static final UUID UUID_SIMPLY_LIGHT = UUID.fromString("04a6e68a-42d6-4738-87b2-1c80a994dee4"); private static final UUID UUID_ZERO = new UUID(0, 0); @@ -419,6 +420,7 @@ public class PebbleProtocol extends GBDeviceProtocol { mAppMessageHandlers.put(UUID_ZALEWSZCZAK_TALLY, new AppMessageHandlerZalewszczak(UUID_ZALEWSZCZAK_TALLY, PebbleProtocol.this)); mAppMessageHandlers.put(UUID_OBSIDIAN, new AppMessageHandlerObsidian(UUID_OBSIDIAN, PebbleProtocol.this)); mAppMessageHandlers.put(UUID_GBPEBBLE, new AppMessageHandlerGBPebble(UUID_GBPEBBLE, PebbleProtocol.this)); + mAppMessageHandlers.put(UUID_SIMPLY_LIGHT, new AppMessageHandlerSimplyLight(UUID_SIMPLY_LIGHT, PebbleProtocol.this)); } }