Search in sources :

Example 6 with GBDeviceEvent

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent in project Gadgetbridge by Freeyourgadget.

the class PebbleProtocol method decodeAction.

private GBDeviceEvent[] decodeAction(ByteBuffer buf) {
    buf.order(ByteOrder.LITTLE_ENDIAN);
    byte command = buf.get();
    if (command == NOTIFICATIONACTION_INVOKE) {
        int id;
        UUID uuid = new UUID(0, 0);
        if (mFwMajor >= 3) {
            uuid = getUUID(buf);
            id = (int) (uuid.getLeastSignificantBits() & 0xffffffffL);
        } else {
            id = buf.getInt();
        }
        byte action = buf.get();
        if (action >= 0x00 && action <= 0x05) {
            GBDeviceEventNotificationControl devEvtNotificationControl = new GBDeviceEventNotificationControl();
            devEvtNotificationControl.handle = id;
            String caption = "undefined";
            int icon_id = 1;
            boolean needsAck2x = true;
            switch(action) {
                case 0x01:
                    devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.OPEN;
                    caption = "Opened";
                    icon_id = PebbleIconID.DURING_PHONE_CALL;
                    break;
                case 0x02:
                    devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.DISMISS;
                    caption = "Dismissed";
                    icon_id = PebbleIconID.RESULT_DISMISSED;
                    needsAck2x = false;
                    break;
                case 0x03:
                    devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.DISMISS_ALL;
                    caption = "All dismissed";
                    icon_id = PebbleIconID.RESULT_DISMISSED;
                    needsAck2x = false;
                    break;
                case 0x04:
                    devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.MUTE;
                    caption = "Muted";
                    icon_id = PebbleIconID.RESULT_MUTE;
                    break;
                case 0x05:
                case 0x00:
                    boolean failed = true;
                    byte attribute_count = buf.get();
                    if (attribute_count > 0) {
                        byte attribute = buf.get();
                        if (attribute == 0x01) {
                            // reply string is in attribute 0x01
                            short length = buf.getShort();
                            if (length > 64)
                                length = 64;
                            byte[] reply = new byte[length];
                            buf.get(reply);
                            devEvtNotificationControl.phoneNumber = null;
                            if (buf.remaining() > 1 && buf.get() == 0x0c) {
                                short phoneNumberLength = buf.getShort();
                                byte[] phoneNumberBytes = new byte[phoneNumberLength];
                                buf.get(phoneNumberBytes);
                                devEvtNotificationControl.phoneNumber = new String(phoneNumberBytes);
                            }
                            devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.REPLY;
                            devEvtNotificationControl.reply = new String(reply);
                            caption = "SENT";
                            icon_id = PebbleIconID.RESULT_SENT;
                            failed = false;
                        }
                    }
                    if (failed) {
                        caption = "FAILED";
                        icon_id = PebbleIconID.RESULT_FAILED;
                        // error
                        devEvtNotificationControl = null;
                    }
                    break;
            }
            GBDeviceEventSendBytes sendBytesAck = null;
            if (mFwMajor >= 3 || needsAck2x) {
                sendBytesAck = new GBDeviceEventSendBytes();
                if (mFwMajor >= 3) {
                    sendBytesAck.encodedBytes = encodeActionResponse(uuid, icon_id, caption);
                } else {
                    sendBytesAck.encodedBytes = encodeActionResponse2x(id, action, 6, caption);
                }
            }
            return new GBDeviceEvent[] { sendBytesAck, devEvtNotificationControl };
        }
        LOG.info("unexpected action: " + action);
    }
    return null;
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) GBDeviceEventNotificationControl(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes) UUID(java.util.UUID)

Example 7 with GBDeviceEvent

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent in project Gadgetbridge by Freeyourgadget.

the class AppMessageHandlerMarioTime method onAppStart.

@Override
public GBDeviceEvent[] onAppStart() {
    WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
    if (weatherSpec == null) {
        return new GBDeviceEvent[] { null };
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    sendBytes.encodedBytes = encodeMarioWeatherMessage(weatherSpec);
    return new GBDeviceEvent[] { sendBytes };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)

Example 8 with GBDeviceEvent

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent in project Gadgetbridge by Freeyourgadget.

the class AppMessageHandlerMisfit method handleMessage.

@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
    GBDevice device = getDevice();
    for (Pair<Integer, Object> pair : pairs) {
        switch(pair.first) {
            case KEY_INCOMING_DATA_BEGIN:
                LOG.info("incoming data start");
                break;
            case KEY_INCOMING_DATA_END:
                LOG.info("incoming data end");
                break;
            case KEY_INCOMING_DATA:
                byte[] data = (byte[]) pair.second;
                ByteBuffer buf = ByteBuffer.wrap(data);
                buf.order(ByteOrder.LITTLE_ENDIAN);
                int timestamp = buf.getInt();
                int key = buf.getInt();
                int samples = (data.length - 8) / 2;
                if (samples <= 0) {
                    break;
                }
                if (mPebbleProtocol.mFwMajor < 3) {
                    timestamp -= SimpleTimeZone.getDefault().getOffset(timestamp * 1000L) / 1000;
                }
                Date startDate = new Date((long) timestamp * 1000L);
                Date endDate = new Date((long) (timestamp + samples * 60) * 1000L);
                LOG.info("got data from " + startDate + " to " + endDate);
                int totalSteps = 0;
                PebbleMisfitSample[] misfitSamples = new PebbleMisfitSample[samples];
                try (DBHandler db = GBApplication.acquireDB()) {
                    PebbleMisfitSampleProvider sampleProvider = new PebbleMisfitSampleProvider(device, db.getDaoSession());
                    Long userId = DBHelper.getUser(db.getDaoSession()).getId();
                    Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();
                    for (int i = 0; i < samples; i++) {
                        short sample = buf.getShort();
                        misfitSamples[i] = new PebbleMisfitSample(timestamp + i * 60, deviceId, userId, sample & 0xffff);
                        misfitSamples[i].setProvider(sampleProvider);
                        int steps = misfitSamples[i].getSteps();
                        totalSteps += steps;
                        LOG.info("got steps for sample " + i + " : " + steps + "(" + Integer.toHexString(sample & 0xffff) + ")");
                    }
                    LOG.info("total steps for above period: " + totalSteps);
                    sampleProvider.addGBActivitySamples(misfitSamples);
                } catch (Exception e) {
                    LOG.error("Error acquiring database", e);
                    return null;
                }
                break;
            default:
                LOG.info("unhandled key: " + pair.first);
                break;
        }
    }
    // always ack
    GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
    sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
    return new GBDeviceEvent[] { sendBytesAck };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) PebbleMisfitSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleMisfitSampleProvider) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes) PebbleMisfitSample(nodomain.freeyourgadget.gadgetbridge.entities.PebbleMisfitSample)

Example 9 with GBDeviceEvent

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent in project Gadgetbridge by Freeyourgadget.

the class AppMessageHandlerTimeStylePebble method onAppStart.

@Override
public GBDeviceEvent[] onAppStart() {
    WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
    if (weatherSpec == null) {
        return new GBDeviceEvent[] { null };
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    sendBytes.encodedBytes = encodeTimeStylePebbleWeather(weatherSpec);
    return new GBDeviceEvent[] { sendBytes };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)

Example 10 with GBDeviceEvent

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent in project Gadgetbridge by Freeyourgadget.

the class AppMessageHandlerTrekVolle method onAppStart.

@Override
public GBDeviceEvent[] onAppStart() {
    WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
    if (weatherSpec == null) {
        return new GBDeviceEvent[] { null };
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    sendBytes.encodedBytes = encodeTrekVolleWeather(weatherSpec);
    return new GBDeviceEvent[] { sendBytes };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)

Aggregations

GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)18 GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)15 WeatherSpec (nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec)7 ByteBuffer (java.nio.ByteBuffer)4 UUID (java.util.UUID)4 IOException (java.io.IOException)3 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)2 GBDeviceEventAppManagement (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Pair (android.util.Pair)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Date (java.util.Date)1 SimpleTimeZone (java.util.SimpleTimeZone)1 TimeZone (java.util.TimeZone)1 GBDeviceEventAppInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo)1 GBDeviceEventAppMessage (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage)1 GBDeviceEventCallControl (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl)1 GBDeviceEventMusicControl (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl)1 GBDeviceEventNotificationControl (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl)1