Search in sources :

Example 1 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip by NightscoutFoundation.

the class BlueJayService method setTime.

public void setTime() {
    new QueueMe().setGenerator(new BytesGenerator() {

        @Override
        public byte[] produce() {
            timeOutbound = new SetTimeTx();
            return timeOutbound.getBytes();
        }
    }).setDescription("Set time").expireInSeconds(30).setProcessor(new ReplyProcessor(I.connection) {

        @Override
        public void process(byte[] bytes) {
            final SetTimeTx reply = new SetTimeTx(bytes);
            UserError.Log.d(TAG, "Time Process callback: " + JoH.bytesToHex(bytes));
            getInfo().parseSetTime(reply, timeOutbound);
            UserError.Log.d(TAG, "Time difference with watch: " + ((timeOutbound.getTimestamp() - reply.getTimestamp()) / 1000d));
        }
    }).queue();
}
Also used : BytesGenerator(com.eveningoutpost.dexdrip.utils.BytesGenerator) SetTimeTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.SetTimeTx) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)

Example 2 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip by NightscoutFoundation.

the class BlueJayService method authenticate.

private void authenticate(final QueueMe requeueItem) {
    val authReply = AuthReqTx.getNextAuthPacket(null);
    new QueueMe().setBytes(authReply.getBytes()).setDescription("Auth hello packet").setProcessor(new ReplyProcessor(I.connection) {

        @Override
        public void process(byte[] bytes) {
            UserError.Log.d(TAG, "Processing likely auth challenge");
            val authReply2 = AuthReqTx.getNextAuthPacket(bytes);
            if (authReply2 != null) {
                new QueueMe().setBytes(authReply2.getBytes()).setDescription("Auth challenge reply").setProcessor(new ReplyProcessor(I.connection) {

                    @Override
                    public void process(byte[] bytes) {
                        if (AuthReqTx.isAccessGranted(bytes)) {
                            UserError.Log.d(TAG, "Authentication complete!");
                            if (requeueItem != null) {
                                // retry the item that we got stopped for authentication
                                requeueItem.insert();
                            }
                        } else {
                            UserError.Log.e(TAG, "Authentication failed! " + JoH.bytesToHex(bytes));
                        }
                    }
                }).insert();
            }
        }
    }).expireInSeconds(60).insert();
}
Also used : lombok.val(lombok.val) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)

Example 3 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip by NightscoutFoundation.

the class BlueJayService method sendGlucose.

public void sendGlucose() {
    val last = BgReading.last();
    if (last != null && msSince(last.timestamp) < Constants.HOUR_IN_MS) {
        val info = BlueJayInfo.getInfo(BlueJay.getMac());
        if (Math.abs(info.lastReadingTime - last.timestamp) > Constants.MINUTE_IN_MS * 3) {
            val glucoseTx = new GlucoseTx(last);
            if (glucoseTx.isValid()) {
                val item = new QueueMe().setBytes(glucoseTx.getBytes()).expireInSeconds(60).setDescription("Glucose to watch");
                item.setProcessor(new AuthReplyProcessor(new ReplyProcessor(I.connection) {

                    @Override
                    public void process(byte[] bytes) {
                        UserError.Log.d(TAG, "Glucose Incoming reply processor: " + HexDump.dumpHexString(bytes));
                        info.displayUpdated();
                    }
                }).setTag(item));
                item.queue();
                doQueue();
            } else {
                UserError.Log.d(TAG, "GlucoseTX wasn't valid so not sending.");
            }
        } else {
            UserError.Log.d(TAG, "Watch already has recent reading");
        // watch reading too close to the reading we were going to send
        }
    }
}
Also used : lombok.val(lombok.val) GlucoseTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.GlucoseTx) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)

Example 4 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip by NightscoutFoundation.

the class BlueJayService method identify.

public void identify() {
    addToLog("Pairing / Identifying Watch");
    val item = new QueueMe().setBytes(new byte[] { OPCODE_IDENTIFY }).setDescription("Get identity value").setDelayMs(300).expireInSeconds(60);
    item.setProcessor(new AuthReplyProcessor(new ReplyProcessor(I.connection) {

        @Override
        public void process(byte[] bytes) {
            if (bytes.length == 18) {
                if (bytes[0] == (byte) 0x41 && bytes[1] == (byte) 0x92) {
                    final String identityHex = JoH.bytesToHex(Arrays.copyOfRange(bytes, 2, 18));
                    UserError.Log.d(TAG, "Storing identity for " + I.address + " of: " + identityHex);
                    BlueJay.storeIdentityKey(I.address, identityHex);
                }
            } else {
                addToLog("Got wrong reply from pairing - try again");
                UserError.Log.d(TAG, "Wrong size for identity reply: " + bytes.length + " " + JoH.bytesToHex(bytes));
            }
        }
    }).setTag(item)).queue();
    doQueue();
}
Also used : lombok.val(lombok.val) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)

Example 5 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip by NightscoutFoundation.

the class BlueJayAsset method processAssetRequest.

private static void processAssetRequest(final BlueJayService service, final int assetid) {
    UserError.Log.d(TAG, "Processing: " + assetid);
    final byte[] bytes = getAssetBytes(assetid);
    if (bytes == null) {
        UserError.Log.d(TAG, "Cannot get asset bytes");
        Inevitable.task("try next asset", 1000, () -> BlueJayAsset.processAssetQueue(service));
        return;
    }
    int assetlength = bytes.length;
    final BaseTx packet = new DefineWindowTx((byte) 0x80, (byte) 0, (byte) (assetid >> 8), (byte) (assetlength >> 8), (byte) (assetid & 0xff), (byte) (assetlength & 0xff), (byte) 0, (byte) 0);
    val queueMe = service.queueGenericCommand(packet.getBytes(), "asset window: (assetid: " + assetid + " len: " + assetlength + ")", null, service.getARInstance(new ReplyProcessor(service.getI().connection) {

        @Override
        public void process(byte[] response) {
            if (D)
                UserError.Log.d(TAG, "Wrote asset request request: " + bytesToHex(response));
            if (packet.responseOk(response)) {
                UserError.Log.d(TAG, "Request success - sending bulk");
                service.queueBufferForAssetStorage(assetid, bytes);
                service.doThinJamQueue();
            } else {
                UserError.Log.d(TAG, "Define Window failed: " + packet.responseText(response));
            }
        }
    }));
    UserError.Log.d(TAG, "Queued define window: " + queueMe.toString());
}
Also used : lombok.val(lombok.val) DefineWindowTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.DefineWindowTx) BaseTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BaseTx) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)

Aggregations

ReplyProcessor (com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)24 lombok.val (lombok.val)18 BaseTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.BaseTx)4 DefineWindowTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.DefineWindowTx)4 SuppressLint (android.annotation.SuppressLint)2 BytesGenerator (com.eveningoutpost.dexdrip.utils.BytesGenerator)2 SlidingWindowConstraint (com.eveningoutpost.dexdrip.utils.time.SlidingWindowConstraint)2 BackFillTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.BackFillTx)2 GlucoseTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.GlucoseTx)2 SetTimeTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.SetTimeTx)2 StandbyTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.StandbyTx)2 Cloner (com.rits.cloning.Cloner)2 UUID (java.util.UUID)2