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.deviceevents.GBDeviceEvent;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||||
|
|
||||||
public class AppMessageHandler {
|
public class AppMessageHandler {
|
||||||
protected final PebbleProtocol mPebbleProtocol;
|
final PebbleProtocol mPebbleProtocol;
|
||||||
protected final UUID mUUID;
|
final UUID mUUID;
|
||||||
|
|
||||||
AppMessageHandler(UUID uuid, PebbleProtocol pebbleProtocol) {
|
AppMessageHandler(UUID uuid, PebbleProtocol pebbleProtocol) {
|
||||||
mUUID = uuid;
|
mUUID = uuid;
|
||||||
|
@ -34,6 +35,10 @@ public class AppMessageHandler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
protected GBDevice getDevice() {
|
protected GBDevice getDevice() {
|
||||||
return mPebbleProtocol.getDevice();
|
return mPebbleProtocol.getDevice();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,7 @@ import java.util.UUID;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||||
import ru.gelin.android.weather.notification.ParcelableWeather2;
|
|
||||||
|
|
||||||
public class AppMessageHandlerTimeStylePebble extends AppMessageHandler {
|
public class AppMessageHandlerTimeStylePebble extends AppMessageHandler {
|
||||||
private static final int MESSAGE_KEY_WeatherCondition = 10000;
|
private static final int MESSAGE_KEY_WeatherCondition = 10000;
|
||||||
|
@ -95,35 +94,35 @@ public class AppMessageHandlerTimeStylePebble extends AppMessageHandler {
|
||||||
return iconToLoad;
|
return iconToLoad;
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] encodeTimeStylePebbleWeather() {
|
private byte[] encodeTimeStylePebbleWeather(WeatherSpec weatherSpec) {
|
||||||
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
|
|
||||||
ParcelableWeather2 weather = Weather.getInstance().getWeather2();
|
|
||||||
|
|
||||||
boolean isNight = false; //TODO: use the night icons when night
|
if (weatherSpec == null) {
|
||||||
if (weather != null) {
|
return 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)));
|
|
||||||
}
|
}
|
||||||
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
|
@Override
|
||||||
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
|
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
|
@Override
|
||||||
public GBDeviceEvent[] pushMessage() {
|
public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) {
|
||||||
GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
|
return encodeTimeStylePebbleWeather(weatherSpec);
|
||||||
sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
|
|
||||||
|
|
||||||
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
|
|
||||||
sendBytes.encodedBytes = encodeTimeStylePebbleWeather();
|
|
||||||
return new GBDeviceEvent[]{sendBytesAck, sendBytes};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -386,6 +386,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||||
|
|
||||||
private final Map<UUID, AppMessageHandler> mAppMessageHandlers = new HashMap<>();
|
private final Map<UUID, AppMessageHandler> mAppMessageHandlers = new HashMap<>();
|
||||||
|
|
||||||
|
private UUID currentRunningApp = UUID_ZERO;
|
||||||
|
|
||||||
public PebbleProtocol(GBDevice device) {
|
public PebbleProtocol(GBDevice device) {
|
||||||
super(device);
|
super(device);
|
||||||
mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this));
|
mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this));
|
||||||
|
@ -1134,7 +1136,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||||
if (mFwMajor < 4) {
|
if (mFwMajor < 4) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return encodeWeatherForecast(weatherSpec.timestamp,
|
byte[] watchfaceProtocol = null;
|
||||||
|
byte[] forecastProtocol = encodeWeatherForecast(weatherSpec.timestamp,
|
||||||
weatherSpec.location,
|
weatherSpec.location,
|
||||||
weatherSpec.currentTemp - 273,
|
weatherSpec.currentTemp - 273,
|
||||||
weatherSpec.todayMaxTemp - 273,
|
weatherSpec.todayMaxTemp - 273,
|
||||||
|
@ -1145,6 +1148,19 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||||
weatherSpec.tomorrowMinTemp - 273,
|
weatherSpec.tomorrowMinTemp - 273,
|
||||||
Weather.mapToPebbleCondition(weatherSpec.tomorrowConditionCode)
|
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) {
|
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) {
|
switch (command) {
|
||||||
case APPRUNSTATE_START:
|
case APPRUNSTATE_START:
|
||||||
LOG.info(ENDPOINT_NAME + ": started " + uuid);
|
LOG.info(ENDPOINT_NAME + ": started " + uuid);
|
||||||
|
currentRunningApp = uuid;
|
||||||
AppMessageHandler handler = mAppMessageHandlers.get(uuid);
|
AppMessageHandler handler = mAppMessageHandlers.get(uuid);
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
return handler.pushMessage();
|
return handler.pushMessage();
|
||||||
|
@ -2442,6 +2458,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||||
devEvts = handler.handleMessage(dict);
|
devEvts = handler.handleMessage(dict);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
currentRunningApp = uuid;
|
||||||
devEvts = handler.pushMessage();
|
devEvts = handler.pushMessage();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -2453,6 +2470,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||||
devEvts = decodeDictToJSONAppMessage(uuid, buf);
|
devEvts = decodeDictToJSONAppMessage(uuid, buf);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
currentRunningApp = uuid;
|
||||||
GBDeviceEventAppManagement gbDeviceEventAppManagement = new GBDeviceEventAppManagement();
|
GBDeviceEventAppManagement gbDeviceEventAppManagement = new GBDeviceEventAppManagement();
|
||||||
gbDeviceEventAppManagement.uuid = uuid;
|
gbDeviceEventAppManagement.uuid = uuid;
|
||||||
gbDeviceEventAppManagement.type = GBDeviceEventAppManagement.EventType.START;
|
gbDeviceEventAppManagement.type = GBDeviceEventAppManagement.EventType.START;
|
||||||
|
|
|
@ -22,7 +22,7 @@ public class DeviceCommunicationServiceTestCase extends TestBase {
|
||||||
* Factory that always returns the mockSupport instance
|
* Factory that always returns the mockSupport instance
|
||||||
*/
|
*/
|
||||||
private class TestDeviceSupportFactory extends DeviceSupportFactory {
|
private class TestDeviceSupportFactory extends DeviceSupportFactory {
|
||||||
public TestDeviceSupportFactory(Context context) {
|
TestDeviceSupportFactory(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ public class DeviceCommunicationServiceTestCase extends TestBase {
|
||||||
mDeviceService = new TestDeviceService(getContext());
|
mDeviceService = new TestDeviceService(getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected GBDevice getDevice() {
|
private GBDevice getDevice() {
|
||||||
return realSupport.getDevice();
|
return realSupport.getDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,11 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
||||||
* Extends GBDeviceServer so that communication with the service works
|
* Extends GBDeviceServer so that communication with the service works
|
||||||
* with Robolectric.
|
* with Robolectric.
|
||||||
*/
|
*/
|
||||||
public class TestDeviceService extends GBDeviceService {
|
class TestDeviceService extends GBDeviceService {
|
||||||
private final ServiceController<DeviceCommunicationService> serviceController;
|
private final ServiceController<DeviceCommunicationService> serviceController;
|
||||||
private final DeviceCommunicationService service;
|
private final DeviceCommunicationService service;
|
||||||
|
|
||||||
public TestDeviceService(Context context) throws Exception {
|
TestDeviceService(Context context) throws Exception {
|
||||||
super(context);
|
super(context);
|
||||||
|
|
||||||
serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent());
|
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.NotificationSpec;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||||
|
|
||||||
public class TestDeviceSupport extends AbstractDeviceSupport {
|
class TestDeviceSupport extends AbstractDeviceSupport {
|
||||||
|
|
||||||
public TestDeviceSupport() {
|
TestDeviceSupport() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue