Search in sources :

Example 11 with GBDeviceEventSendBytes

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

the class LiveviewProtocol method decodeResponse.

@Override
public GBDeviceEvent[] decodeResponse(byte[] responseData) {
    int length = responseData.length;
    if (length < 4) {
        //empty message
        return null;
    } else {
        ByteBuffer buffer = ByteBuffer.wrap(responseData, 0, length);
        byte msgId = buffer.get();
        buffer.get();
        int payloadLen = buffer.getInt();
        GBDeviceEventSendBytes reply = new GBDeviceEventSendBytes();
        if (payloadLen + 6 == length) {
            switch(msgId) {
                case LiveviewConstants.MSG_DEVICESTATUS:
                    reply.encodedBytes = constructMessage(LiveviewConstants.MSG_DEVICESTATUS_ACK, new byte[] { LiveviewConstants.RESULT_OK });
                    break;
                case LiveviewConstants.MSG_DISPLAYPANEL_ACK:
                    //hack to make the notifications vibrate!
                    reply.encodedBytes = encodeVibrateRequest((short) 100, (short) 200);
                    break;
                default:
            }
            GBDeviceEventSendBytes ack = new GBDeviceEventSendBytes();
            ack.encodedBytes = constructMessage(LiveviewConstants.MSG_ACK, new byte[] { msgId });
            return new GBDeviceEvent[] { ack, reply };
        }
    }
    return super.decodeResponse(responseData);
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes) ByteBuffer(java.nio.ByteBuffer)

Example 12 with GBDeviceEventSendBytes

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

the class AppMessageHandlerMorpheuz method handleMessage.

@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
    int ctrl_message = 0;
    GBDeviceEventSleepMonitorResult sleepMonitorResult = null;
    for (Pair<Integer, Object> pair : pairs) {
        if (Objects.equals(pair.first, keyTransmit)) {
            sleepMonitorResult = new GBDeviceEventSleepMonitorResult();
            sleepMonitorResult.smartalarm_from = smartalarm_from;
            sleepMonitorResult.smartalarm_to = smartalarm_to;
            sleepMonitorResult.alarm_gone_off = alarm_gone_off;
            sleepMonitorResult.recording_base_timestamp = recording_base_timestamp;
            ctrl_message |= CTRL_TRANSMIT_DONE;
        } else if (pair.first.equals(keyGoneoff)) {
            alarm_gone_off = (int) pair.second;
            LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60);
            ctrl_message |= CTRL_DO_NEXT | CTRL_GONEOFF_DONE;
        } else if (pair.first.equals(keyPoint)) {
            if (recording_base_timestamp == -1) {
                // we have no base timestamp but received points, stop this
                ctrl_message = CTRL_VERSION_DONE | CTRL_GONEOFF_DONE | CTRL_TRANSMIT_DONE | CTRL_SET_LAST_SENT;
            } else {
                int index = ((int) pair.second >> 16);
                int intensity = ((int) pair.second & 0xffff);
                LOG.info("got point:" + index + " " + intensity);
                if (index >= 0) {
                    try (DBHandler db = GBApplication.acquireDB()) {
                        Long userId = DBHelper.getUser(db.getDaoSession()).getId();
                        Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();
                        PebbleMorpheuzSampleProvider sampleProvider = new PebbleMorpheuzSampleProvider(getDevice(), db.getDaoSession());
                        PebbleMorpheuzSample sample = new PebbleMorpheuzSample(recording_base_timestamp + index * 600, deviceId, userId, intensity);
                        sample.setProvider(sampleProvider);
                        sampleProvider.addGBActivitySample(sample);
                    } catch (Exception e) {
                        LOG.error("Error acquiring database", e);
                    }
                }
                ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
            }
        } else if (pair.first.equals(keyFrom)) {
            smartalarm_from = (int) pair.second;
            LOG.info("got from: " + smartalarm_from / 60 + ":" + smartalarm_from % 60);
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyTo)) {
            smartalarm_to = (int) pair.second;
            LOG.info("got to: " + smartalarm_to / 60 + ":" + smartalarm_to % 60);
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyVersion)) {
            int version = (int) pair.second;
            LOG.info("got version: " + ((float) version / 10.0f));
            ctrl_message |= CTRL_VERSION_DONE;
        } else if (pair.first.equals(keyBase)) {
            // fix timestamp
            TimeZone tz = SimpleTimeZone.getDefault();
            recording_base_timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000;
            LOG.info("got base: " + recording_base_timestamp);
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyAutoReset)) {
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keySnoozes)) {
            ctrl_message |= CTRL_SNOOZES_DONE | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyFault)) {
            LOG.info("fault code: " + (int) pair.second);
            ctrl_message |= CTRL_DO_NEXT;
        } else {
            LOG.info("unhandled key: " + pair.first);
        }
    }
    // always ack
    GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
    sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
    // sometimes send control message
    GBDeviceEventSendBytes sendBytesCtrl = null;
    if (ctrl_message > 0) {
        sendBytesCtrl = new GBDeviceEventSendBytes();
        sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(keyCtrl, ctrl_message);
    }
    // ctrl and sleep monitor might be null, thats okay
    return new GBDeviceEvent[] { sendBytesAck, sendBytesCtrl, sleepMonitorResult };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) PebbleMorpheuzSample(nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSample) IOException(java.io.IOException) JSONException(org.json.JSONException) PebbleMorpheuzSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleMorpheuzSampleProvider) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) TimeZone(java.util.TimeZone) SimpleTimeZone(java.util.SimpleTimeZone) JSONObject(org.json.JSONObject) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes) GBDeviceEventSleepMonitorResult(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult)

Example 13 with GBDeviceEventSendBytes

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

the class AppMessageHandlerObsidian method onAppStart.

@Override
public GBDeviceEvent[] onAppStart() {
    WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
    if (weatherSpec == null) {
        return new GBDeviceEvent[] { null };
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    sendBytes.encodedBytes = encodeObisdianWeather(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 14 with GBDeviceEventSendBytes

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

the class AppMessageHandlerSquare method onAppStart.

@Override
public GBDeviceEvent[] onAppStart() {
    WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
    if (weatherSpec == null) {
        return new GBDeviceEvent[] { null };
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    sendBytes.encodedBytes = encodeSquareWeatherMessage(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 15 with GBDeviceEventSendBytes

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

the class PebbleProtocol method decodeDictToJSONAppMessage.

private GBDeviceEvent[] decodeDictToJSONAppMessage(UUID uuid, ByteBuffer buf) throws JSONException {
    buf.order(ByteOrder.LITTLE_ENDIAN);
    byte dictSize = buf.get();
    if (dictSize == 0) {
        LOG.info("dict size is 0, ignoring");
        return null;
    }
    JSONArray jsonArray = new JSONArray();
    while (dictSize-- > 0) {
        JSONObject jsonObject = new JSONObject();
        Integer key = buf.getInt();
        byte type = buf.get();
        short length = buf.getShort();
        jsonObject.put("key", key);
        if (type == TYPE_CSTRING) {
            length--;
        }
        jsonObject.put("length", length);
        switch(type) {
            case TYPE_UINT:
                jsonObject.put("type", "uint");
                if (length == 1) {
                    jsonObject.put("value", buf.get() & 0xff);
                } else if (length == 2) {
                    jsonObject.put("value", buf.getShort() & 0xffff);
                } else {
                    jsonObject.put("value", buf.getInt() & 0xffffffffL);
                }
                break;
            case TYPE_INT:
                jsonObject.put("type", "int");
                if (length == 1) {
                    jsonObject.put("value", buf.get());
                } else if (length == 2) {
                    jsonObject.put("value", buf.getShort());
                } else {
                    jsonObject.put("value", buf.getInt());
                }
                break;
            case TYPE_BYTEARRAY:
            case TYPE_CSTRING:
                byte[] bytes = new byte[length];
                buf.get(bytes);
                if (type == TYPE_BYTEARRAY) {
                    jsonObject.put("type", "bytes");
                    jsonObject.put("value", new String(Base64.encode(bytes, Base64.NO_WRAP)));
                } else {
                    jsonObject.put("type", "string");
                    jsonObject.put("value", new String(bytes));
                    // skip null-termination;
                    buf.get();
                }
                break;
            default:
                LOG.info("unknown type in appmessage, ignoring");
                return null;
        }
        jsonArray.put(jsonObject);
    }
    GBDeviceEventSendBytes sendBytesAck = null;
    if (mAlwaysACKPebbleKit) {
        // this is a hack we send an ack to the Pebble immediately because somebody said it helps some PebbleKit apps :P
        sendBytesAck = new GBDeviceEventSendBytes();
        sendBytesAck.encodedBytes = encodeApplicationMessageAck(uuid, last_id);
    }
    GBDeviceEventAppMessage appMessage = new GBDeviceEventAppMessage();
    appMessage.appUUID = uuid;
    appMessage.id = last_id & 0xff;
    appMessage.message = jsonArray.toString();
    return new GBDeviceEvent[] { appMessage, sendBytesAck };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GBDeviceEventAppMessage(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)

Aggregations

GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)17 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)15 WeatherSpec (nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec)7 ByteBuffer (java.nio.ByteBuffer)3 UUID (java.util.UUID)3 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Pair (android.util.Pair)1 IOException (java.io.IOException)1 Date (java.util.Date)1 SimpleTimeZone (java.util.SimpleTimeZone)1 TimeZone (java.util.TimeZone)1 GBDeviceEventAppInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo)1 GBDeviceEventAppManagement (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement)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 GBDeviceEventSleepMonitorResult (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult)1