Search in sources :

Example 1 with BulkUpTx

use of com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpTx in project xDrip by NightscoutFoundation.

the class BlueJayService method bulkSend.

// write the data packets
private void bulkSend(final int opcode, final byte[] buffer, final int offset, boolean quiet) {
    UserError.Log.d(TAG, "bulksend called: opcode " + opcode + " total " + buffer.length + " offset: " + offset + " quiet:" + quiet);
    if (buffer != null && offset < buffer.length) {
        final BulkUpTx packet = (opcode >= OPCODE_BULK_R_XFER_0 ? new RBulkUpTx(opcode, buffer, offset) : new BulkUpTx(opcode, buffer, offset));
        if (quiet) {
            packet.setQuiet();
        }
        if (offset == 0) {
            // reset counter as this is new bulk up
            revisedOffset = 0;
            lastActionedRevisedOffset = -1;
        // TODO we should check it if we have failed to act on revised offset
        }
        I.connection.writeCharacteristic(quiet ? THINJAM_BULK : THINJAM_WRITE, packet.getBytes()).observeOn(Schedulers.newThread()).subscribe(response -> {
            if (D)
                UserError.Log.d(TAG, "Bulk Up Send response: " + bytesToHex(response));
            if (packet.responseOk(response)) {
                // WARNING recursion
                final int nextOffset;
                if (revisedOffset != 0 && revisedOffset < offset && revisedOffset != lastActionedRevisedOffset) {
                    // TODO we only catch this if we send a packet
                    nextOffset = revisedOffset;
                    lastActionedRevisedOffset = nextOffset;
                    UserError.Log.d(TAG, "Retrying bulk send from: " + nextOffset);
                } else {
                    nextOffset = offset + packet.getBytesIncluded();
                }
                revisedOffset = 0;
                if (nextOffset < buffer.length) {
                    if (!quiet) {
                        JoH.threadSleep(100);
                    } else {
                        JoH.threadSleep(1);
                    }
                    bulkSend(opcode, buffer, nextOffset, packet.isQuiet());
                } else {
                    UserError.Log.d(TAG, "Bulk send completed!");
                    if (!(packet instanceof RBulkUpTx)) {
                        // removes first item from the queue which should be the one we just processed!
                        commandQueue.poll();
                        // 
                        Inevitable.task("tj-next-queue", 10, this::processQueue);
                    } else {
                        // wait 1 second and then retry this upload if we get success reply notification then we remove it elsewhere
                        Inevitable.task("tj-next-queue", 4000, this::processQueue);
                    }
                }
            } else {
                UserError.Log.d(TAG, "Bulk Send failed: " + packet.responseText(response));
                if (!quiet) {
                    // retry shortly
                    Inevitable.task("tj-next-queue", 20000, this::processQueue);
                    if (JoH.ratelimit("tj-allow-bulk-retry", 2)) {
                        UserError.Log.d(TAG, "Retrying packet");
                        // retry packet send
                        bulkSend(opcode, buffer, offset, quiet);
                    }
                } else {
                    UserError.Log.d(TAG, "Quiet is set so not attempting any response to failure here");
                }
            }
        }, throwable -> {
            UserError.Log.e(TAG, "Failed to write bulk Send: " + throwable);
            if (throwable instanceof BleGattCharacteristicException) {
                final int status = ((BleGattCharacteristicException) throwable).getStatus();
                UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
            } else {
                UserError.Log.d(TAG, "Throwable in Bulk SEnd write: " + throwable);
            }
        });
    } else {
        UserError.Log.d(TAG, "Invalid buffer in bulkSend");
        Inevitable.task("tj-next-queue", 4000, this::processQueue);
    }
}
Also used : BleGattCharacteristicException(com.polidea.rxandroidble2.exceptions.BleGattCharacteristicException) RBulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.RBulkUpTx) SuppressLint(android.annotation.SuppressLint) SlidingWindowConstraint(com.eveningoutpost.dexdrip.utils.time.SlidingWindowConstraint) BulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpTx) RBulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.RBulkUpTx)

Example 2 with BulkUpTx

use of com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpTx in project xDrip-plus by jamorham.

the class BlueJayService method bulkSend.

// write the data packets
private void bulkSend(final int opcode, final byte[] buffer, final int offset, boolean quiet) {
    UserError.Log.d(TAG, "bulksend called: opcode " + opcode + " total " + buffer.length + " offset: " + offset + " quiet:" + quiet);
    if (buffer != null && offset < buffer.length) {
        final BulkUpTx packet = (opcode >= OPCODE_BULK_R_XFER_0 ? new RBulkUpTx(opcode, buffer, offset) : new BulkUpTx(opcode, buffer, offset));
        if (quiet) {
            packet.setQuiet();
        }
        if (offset == 0) {
            // reset counter as this is new bulk up
            revisedOffset = 0;
            lastActionedRevisedOffset = -1;
        // TODO we should check it if we have failed to act on revised offset
        }
        I.connection.writeCharacteristic(quiet ? THINJAM_BULK : THINJAM_WRITE, packet.getBytes()).observeOn(Schedulers.newThread()).subscribe(response -> {
            if (D)
                UserError.Log.d(TAG, "Bulk Up Send response: " + bytesToHex(response));
            if (packet.responseOk(response)) {
                // WARNING recursion
                final int nextOffset;
                if (revisedOffset != 0 && revisedOffset < offset && revisedOffset != lastActionedRevisedOffset) {
                    // TODO we only catch this if we send a packet
                    nextOffset = revisedOffset;
                    lastActionedRevisedOffset = nextOffset;
                    UserError.Log.d(TAG, "Retrying bulk send from: " + nextOffset);
                } else {
                    nextOffset = offset + packet.getBytesIncluded();
                }
                revisedOffset = 0;
                if (nextOffset < buffer.length) {
                    if (!quiet) {
                        JoH.threadSleep(100);
                    } else {
                        JoH.threadSleep(1);
                    }
                    bulkSend(opcode, buffer, nextOffset, packet.isQuiet());
                } else {
                    UserError.Log.d(TAG, "Bulk send completed!");
                    if (!(packet instanceof RBulkUpTx)) {
                        // removes first item from the queue which should be the one we just processed!
                        commandQueue.poll();
                        // 
                        Inevitable.task("tj-next-queue", 10, this::processQueue);
                    } else {
                        // wait 1 second and then retry this upload if we get success reply notification then we remove it elsewhere
                        Inevitable.task("tj-next-queue", 4000, this::processQueue);
                    }
                }
            } else {
                UserError.Log.d(TAG, "Bulk Send failed: " + packet.responseText(response));
                if (!quiet) {
                    // retry shortly
                    Inevitable.task("tj-next-queue", 20000, this::processQueue);
                    if (JoH.ratelimit("tj-allow-bulk-retry", 2)) {
                        UserError.Log.d(TAG, "Retrying packet");
                        // retry packet send
                        bulkSend(opcode, buffer, offset, quiet);
                    }
                } else {
                    UserError.Log.d(TAG, "Quiet is set so not attempting any response to failure here");
                }
            }
        }, throwable -> {
            UserError.Log.e(TAG, "Failed to write bulk Send: " + throwable);
            if (throwable instanceof BleGattCharacteristicException) {
                final int status = ((BleGattCharacteristicException) throwable).getStatus();
                UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
            } else {
                UserError.Log.d(TAG, "Throwable in Bulk SEnd write: " + throwable);
            }
        });
    } else {
        UserError.Log.d(TAG, "Invalid buffer in bulkSend");
        Inevitable.task("tj-next-queue", 4000, this::processQueue);
    }
}
Also used : BleGattCharacteristicException(com.polidea.rxandroidble2.exceptions.BleGattCharacteristicException) RBulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.RBulkUpTx) SuppressLint(android.annotation.SuppressLint) SlidingWindowConstraint(com.eveningoutpost.dexdrip.utils.time.SlidingWindowConstraint) BulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpTx) RBulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.RBulkUpTx)

Aggregations

SuppressLint (android.annotation.SuppressLint)2 SlidingWindowConstraint (com.eveningoutpost.dexdrip.utils.time.SlidingWindowConstraint)2 BulkUpTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpTx)2 RBulkUpTx (com.eveningoutpost.dexdrip.watch.thinjam.messages.RBulkUpTx)2 BleGattCharacteristicException (com.polidea.rxandroidble2.exceptions.BleGattCharacteristicException)2