mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-02 08:22:59 +01:00
Pebble: add encodeUpadteWeather() to AppMessageHandler for easier watchface support
Now in Timestyle weather is in sync with what we get from weather notification
This commit is contained in:
parent
19c5cbfbb9
commit
82c0f35c58
@ -8,10 +8,11 @@ import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
|
||||
public class AppMessageHandler {
|
||||
protected final PebbleProtocol mPebbleProtocol;
|
||||
protected final UUID mUUID;
|
||||
final PebbleProtocol mPebbleProtocol;
|
||||
final UUID mUUID;
|
||||
|
||||
AppMessageHandler(UUID uuid, PebbleProtocol pebbleProtocol) {
|
||||
mUUID = uuid;
|
||||
@ -34,6 +35,10 @@ public class AppMessageHandler {
|
||||
return null;
|
||||
}
|
||||
|
||||
public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected GBDevice getDevice() {
|
||||
return mPebbleProtocol.getDevice();
|
||||
}
|
||||
|
@ -10,8 +10,7 @@ import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
|
||||
import ru.gelin.android.weather.notification.ParcelableWeather2;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
|
||||
public class AppMessageHandlerTimeStylePebble extends AppMessageHandler {
|
||||
private static final int MESSAGE_KEY_WeatherCondition = 10000;
|
||||
@ -95,35 +94,35 @@ public class AppMessageHandlerTimeStylePebble extends AppMessageHandler {
|
||||
return iconToLoad;
|
||||
}
|
||||
|
||||
private byte[] encodeTimeStylePebbleWeather() {
|
||||
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
|
||||
ParcelableWeather2 weather = Weather.getInstance().getWeather2();
|
||||
private byte[] encodeTimeStylePebbleWeather(WeatherSpec weatherSpec) {
|
||||
|
||||
boolean isNight = false; //TODO: use the night icons when night
|
||||
if (weather != null) {
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherUseNightIcon, (Object) (isNight ? 1 : 0)));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherTemperature, (Object) (weather.currentTemp - 273)));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherCondition, (Object) (getIconForConditionCode(weather.currentConditionCode, isNight))));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastCondition, (Object) (getIconForConditionCode(weather.forecastConditionCode, isNight))));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastHighTemp, (Object) (weather.todayHighTemp - 273)));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastLowTemp, (Object) (weather.todayLowTemp - 273)));
|
||||
if (weatherSpec == null) {
|
||||
return null;
|
||||
}
|
||||
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
|
||||
|
||||
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
|
||||
boolean isNight = false; //TODO: use the night icons when night
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherUseNightIcon, (Object) (isNight ? 1 : 0)));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherTemperature, (Object) (weatherSpec.currentTemp - 273)));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherCondition, (Object) (getIconForConditionCode(weatherSpec.currentConditionCode, isNight))));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastCondition, (Object) (getIconForConditionCode(weatherSpec.tomorrowConditionCode, isNight))));
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastHighTemp, (Object) (weatherSpec.todayMaxTemp - 273)));
|
||||
|
||||
pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastLowTemp, (Object) (weatherSpec.todayMinTemp - 273)));
|
||||
|
||||
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
|
||||
return pushMessage();
|
||||
GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
|
||||
sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
|
||||
return new GBDeviceEvent[]{sendBytesAck};
|
||||
// TODO: trigger update of weather?
|
||||
}
|
||||
|
||||
@Override
|
||||
public GBDeviceEvent[] pushMessage() {
|
||||
GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
|
||||
sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
|
||||
|
||||
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
|
||||
sendBytes.encodedBytes = encodeTimeStylePebbleWeather();
|
||||
return new GBDeviceEvent[]{sendBytesAck, sendBytes};
|
||||
public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) {
|
||||
return encodeTimeStylePebbleWeather(weatherSpec);
|
||||
}
|
||||
}
|
@ -386,6 +386,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||
|
||||
private final Map<UUID, AppMessageHandler> mAppMessageHandlers = new HashMap<>();
|
||||
|
||||
private UUID currentRunningApp = UUID_ZERO;
|
||||
|
||||
public PebbleProtocol(GBDevice device) {
|
||||
super(device);
|
||||
mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this));
|
||||
@ -1134,7 +1136,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||
if (mFwMajor < 4) {
|
||||
return null;
|
||||
}
|
||||
return encodeWeatherForecast(weatherSpec.timestamp,
|
||||
byte[] watchfaceProtocol = null;
|
||||
byte[] forecastProtocol = encodeWeatherForecast(weatherSpec.timestamp,
|
||||
weatherSpec.location,
|
||||
weatherSpec.currentTemp - 273,
|
||||
weatherSpec.todayMaxTemp - 273,
|
||||
@ -1145,6 +1148,19 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||
weatherSpec.tomorrowMinTemp - 273,
|
||||
Weather.mapToPebbleCondition(weatherSpec.tomorrowConditionCode)
|
||||
);
|
||||
AppMessageHandler handler = mAppMessageHandlers.get(currentRunningApp);
|
||||
if (handler != null) {
|
||||
watchfaceProtocol = handler.encodeUpdateWeather(weatherSpec);
|
||||
}
|
||||
|
||||
if (watchfaceProtocol != null) {
|
||||
ByteBuffer buf = ByteBuffer.allocate(forecastProtocol.length + watchfaceProtocol.length);
|
||||
buf.put(forecastProtocol);
|
||||
buf.put(watchfaceProtocol);
|
||||
return buf.array();
|
||||
}
|
||||
|
||||
return forecastProtocol;
|
||||
}
|
||||
|
||||
private byte[] encodeWeatherForecast(int timestamp, String location, int tempNow, int tempHighToday, int tempLowToday, int conditionCodeToday, String conditionToday, int tempHighTomorrow, int tempLowTomorrow, int conditionCodeTomorrow) {
|
||||
@ -2107,7 +2123,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||
switch (command) {
|
||||
case APPRUNSTATE_START:
|
||||
LOG.info(ENDPOINT_NAME + ": started " + uuid);
|
||||
|
||||
currentRunningApp = uuid;
|
||||
AppMessageHandler handler = mAppMessageHandlers.get(uuid);
|
||||
if (handler != null) {
|
||||
return handler.pushMessage();
|
||||
@ -2442,6 +2458,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||
devEvts = handler.handleMessage(dict);
|
||||
}
|
||||
else {
|
||||
currentRunningApp = uuid;
|
||||
devEvts = handler.pushMessage();
|
||||
}
|
||||
} else {
|
||||
@ -2453,6 +2470,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||
devEvts = decodeDictToJSONAppMessage(uuid, buf);
|
||||
}
|
||||
else {
|
||||
currentRunningApp = uuid;
|
||||
GBDeviceEventAppManagement gbDeviceEventAppManagement = new GBDeviceEventAppManagement();
|
||||
gbDeviceEventAppManagement.uuid = uuid;
|
||||
gbDeviceEventAppManagement.type = GBDeviceEventAppManagement.EventType.START;
|
||||
|
@ -22,7 +22,7 @@ public class DeviceCommunicationServiceTestCase extends TestBase {
|
||||
* Factory that always returns the mockSupport instance
|
||||
*/
|
||||
private class TestDeviceSupportFactory extends DeviceSupportFactory {
|
||||
public TestDeviceSupportFactory(Context context) {
|
||||
TestDeviceSupportFactory(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ public class DeviceCommunicationServiceTestCase extends TestBase {
|
||||
mDeviceService = new TestDeviceService(getContext());
|
||||
}
|
||||
|
||||
protected GBDevice getDevice() {
|
||||
private GBDevice getDevice() {
|
||||
return realSupport.getDevice();
|
||||
}
|
||||
|
||||
|
@ -13,11 +13,11 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
||||
* Extends GBDeviceServer so that communication with the service works
|
||||
* with Robolectric.
|
||||
*/
|
||||
public class TestDeviceService extends GBDeviceService {
|
||||
class TestDeviceService extends GBDeviceService {
|
||||
private final ServiceController<DeviceCommunicationService> serviceController;
|
||||
private final DeviceCommunicationService service;
|
||||
|
||||
public TestDeviceService(Context context) throws Exception {
|
||||
TestDeviceService(Context context) throws Exception {
|
||||
super(context);
|
||||
|
||||
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
|
||||
|
@ -17,9 +17,9 @@ import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
|
||||
public class TestDeviceSupport extends AbstractDeviceSupport {
|
||||
class TestDeviceSupport extends AbstractDeviceSupport {
|
||||
|
||||
public TestDeviceSupport() {
|
||||
TestDeviceSupport() {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user