Search in sources :

Example 16 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip-plus by jamorham.

the class JamBaseBluetoothSequencer method addToWriteQueue.

private void addToWriteQueue(final QueueMe queueMe, final boolean unique, final boolean atHead) {
    Cloner cloner = null;
    if (queueMe.byteslist == null) {
        queueMe.byteslist = new LinkedList<>();
        if (queueMe.generator != null) {
            // create pseudo entry if this is using a generator
            queueMe.byteslist.add(null);
        }
    }
    final boolean multiple = queueMe.byteslist.size() > 1;
    for (final byte[] bytes : queueMe.byteslist) {
        if (unique) {
            // skip if duplicate bytes
            if (doesWriteQueueContainBytes(bytes))
                continue;
        }
        ReplyProcessor replyProcessor = queueMe.processor;
        if (replyProcessor != null) {
            if (multiple) {
                if (cloner == null)
                    cloner = new Cloner();
                replyProcessor = cloner.shallowClone(queueMe.processor);
            }
            if (replyProcessor != null) {
                replyProcessor.setOutbound(bytes);
            } else {
                UserError.Log.wtf(TAG, "Could not create clone of reply processor needed!!");
            }
        }
        UUID queueWriteCharacterstic = queueMe.queueWriteCharacterstic;
        if (queueWriteCharacterstic == null)
            queueWriteCharacterstic = I.queue_write_characterstic;
        if (atHead) {
            I.write_queue.addFirst(new QueueItem(queueWriteCharacterstic, bytes, queueMe.timeout_seconds, queueMe.delay_ms, queueMe.description, queueMe.expect_reply, queueMe.expireAt).setRunnable(queueMe.runnable).setProcessor(replyProcessor).setGenerator(queueMe.generator));
        } else {
            I.write_queue.add(new QueueItem(queueWriteCharacterstic, bytes, queueMe.timeout_seconds, queueMe.delay_ms, queueMe.description, queueMe.expect_reply, queueMe.expireAt).setRunnable(queueMe.runnable).setProcessor(replyProcessor).setGenerator(queueMe.generator));
        }
    }
    if (queueMe.start_now)
        startQueueSend();
}
Also used : UUID(java.util.UUID) Cloner(com.rits.cloning.Cloner) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)

Example 17 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip-plus by jamorham.

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)

Example 18 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip-plus by jamorham.

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 19 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip-plus by jamorham.

the class BlueJayService method getBackFill.

public void getBackFill(int records) {
    if (BlueJay.haveAuthKey(I.address)) {
        if (!flashRunning()) {
            val outbound = new BackFillTx(records);
            val item = new QueueMe().setBytes(outbound.getBytes()).setDescription("Get BackFill " + records).expireInSeconds(30);
            item.setProcessor(new AuthReplyProcessor(new ReplyProcessor(I.connection) {

                @Override
                public void process(byte[] bytes) {
                    UserError.Log.d(TAG, "BackFill request response: " + JoH.bytesToHex(bytes));
                }
            }).setTag(item)).queue();
        } else {
            UserError.Log.d(TAG, "No backfill as flash running");
        }
    } else {
        UserError.Log.d(TAG, "Cannot back fill as we don't have auth key");
    }
}
Also used : lombok.val(lombok.val) BackFillTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BackFillTx) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor)

Example 20 with ReplyProcessor

use of com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor in project xDrip-plus by jamorham.

the class BlueJayService method sendNotification.

public void sendNotification(final String message_type, final String msg) {
    UserError.Log.d(TAG, "SendNotification: " + message_type + " " + msg);
    if (!versionSufficient(getInfo().buildNumber, FEATURE_TJ_DISP_A))
        return;
    if (BlueJayAsset.queueBusy()) {
        UserError.Log.d(TAG, "Refusing to send notification as asset queue is busy");
        return;
    }
    int notificationType = 1;
    if (message_type != null) {
        switch(message_type.toUpperCase()) {
            case THINJAM_NOTIFY_TYPE_CANCEL:
                if (msSince(lastLongPress1) < SECOND_IN_MS) {
                    UserError.Log.d(TAG, "Not doing circular cancel");
                    return;
                }
                notificationType = 0;
                break;
            case THINJAM_NOTIFY_TYPE_TEXTBOX1:
                notificationType = 6;
                break;
            case THINJAM_NOTIFY_TYPE_DIALOG:
                notificationType = 9;
                break;
            case THINJAM_NOTIFY_TYPE_LOW_ALERT:
                notificationType = 2;
                break;
            case THINJAM_NOTIFY_TYPE_HIGH_ALERT:
                notificationType = 1;
                break;
        }
    }
    val list = getPacketStreamForNotification(notificationType, msg);
    int counter = 0;
    for (val part : list) {
        counter++;
        val item = new QueueMe().setBytes(part.getBytes()).expireInSeconds(60).setDescription("Notify type:" + message_type + " part:" + counter);
        item.setProcessor(new AuthReplyProcessor(new ReplyProcessor(I.connection) {

            @Override
            public void process(byte[] bytes) {
                UserError.Log.d(TAG, "Notify  reply processor: " + HexDump.dumpHexString(bytes));
            }
        }).setTag(item));
        item.queue();
    }
    doQueue();
}
Also used : lombok.val(lombok.val) SuppressLint(android.annotation.SuppressLint) SlidingWindowConstraint(com.eveningoutpost.dexdrip.utils.time.SlidingWindowConstraint) 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