use of com.eveningoutpost.dexdrip.insulin.inpen.messages.RecordTx in project xDrip by NightscoutFoundation.
the class InPenService method getRecords.
private void getRecords(final int firstIndex, final int lastIndex) {
final int numberOfRecords = lastIndex - firstIndex;
if (numberOfRecords > 30) {
I.connection.writeCharacteristic(KEEPALIVE, new KeepAliveTx().getBytes()).subscribe(value -> {
UserError.Log.d(TAG, "Wrote keep alive for " + numberOfRecords);
}, throwable -> {
UserError.Log.d(TAG, "Got exception in keep alive" + throwable);
});
}
final RecordTx packet = new RecordTx(firstIndex, lastIndex);
UserError.Log.d(TAG, "getRecords called, loading: " + firstIndex + " to " + lastIndex);
I.connection.setupIndication(RECORD_INDICATE).doOnNext(notificationObservable -> {
I.connection.writeCharacteristic(RECORD_START, packet.startBytes()).subscribe(valueS -> {
UserError.Log.d(TAG, "Wrote record start: " + bytesToHex(valueS));
I.connection.writeCharacteristic(RECORD_END, packet.endBytes()).subscribe(valueE -> {
UserError.Log.d(TAG, "Wrote record end: " + bytesToHex(valueE));
I.connection.writeCharacteristic(RECORD_REQUEST, packet.triggerBytes()).subscribe(characteristicValue -> {
if (D)
UserError.Log.d(TAG, "Wrote record request request: " + bytesToHex(characteristicValue));
}, throwable -> {
UserError.Log.e(TAG, "Failed to write record request: " + throwable);
if (throwable instanceof BleGattCharacteristicException) {
final int status = ((BleGattCharacteristicException) throwable).getStatus();
UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
}
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record End write: " + throwable);
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Start write: " + throwable);
// throws BleGattCharacteristicException status = 128 for "no resources" eg nothing matches
});
}).flatMap(notificationObservable -> notificationObservable).timeout(120, TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(bytes -> {
records.add(bytes);
UserError.Log.d(TAG, "INDICATE INDICATE: " + HexDump.dumpHexString(bytes));
}, throwable -> {
if (!(throwable instanceof OperationSuccess)) {
if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected when waiting to receive indication: " + throwable);
} else {
UserError.Log.e(TAG, "Error receiving indication: " + throwable);
}
Inevitable.task("check-records-queue", 100, this::processRecordsQueue);
}
});
}
use of com.eveningoutpost.dexdrip.insulin.inpen.messages.RecordTx in project xDrip-plus by jamorham.
the class InPenService method getRecords.
private void getRecords(final int firstIndex, final int lastIndex) {
final int numberOfRecords = lastIndex - firstIndex;
if (numberOfRecords > 30) {
I.connection.writeCharacteristic(KEEPALIVE, new KeepAliveTx().getBytes()).subscribe(value -> {
UserError.Log.d(TAG, "Wrote keep alive for " + numberOfRecords);
}, throwable -> {
UserError.Log.d(TAG, "Got exception in keep alive" + throwable);
});
}
final RecordTx packet = new RecordTx(firstIndex, lastIndex);
UserError.Log.d(TAG, "getRecords called, loading: " + firstIndex + " to " + lastIndex);
I.connection.setupIndication(RECORD_INDICATE).doOnNext(notificationObservable -> {
I.connection.writeCharacteristic(RECORD_START, packet.startBytes()).subscribe(valueS -> {
UserError.Log.d(TAG, "Wrote record start: " + bytesToHex(valueS));
I.connection.writeCharacteristic(RECORD_END, packet.endBytes()).subscribe(valueE -> {
UserError.Log.d(TAG, "Wrote record end: " + bytesToHex(valueE));
I.connection.writeCharacteristic(RECORD_REQUEST, packet.triggerBytes()).subscribe(characteristicValue -> {
if (D)
UserError.Log.d(TAG, "Wrote record request request: " + bytesToHex(characteristicValue));
}, throwable -> {
UserError.Log.e(TAG, "Failed to write record request: " + throwable);
if (throwable instanceof BleGattCharacteristicException) {
final int status = ((BleGattCharacteristicException) throwable).getStatus();
UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
}
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record End write: " + throwable);
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Start write: " + throwable);
// throws BleGattCharacteristicException status = 128 for "no resources" eg nothing matches
});
}).flatMap(notificationObservable -> notificationObservable).timeout(120, TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(bytes -> {
records.add(bytes);
UserError.Log.d(TAG, "INDICATE INDICATE: " + HexDump.dumpHexString(bytes));
}, throwable -> {
if (!(throwable instanceof OperationSuccess)) {
if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected when waiting to receive indication: " + throwable);
} else {
UserError.Log.e(TAG, "Error receiving indication: " + throwable);
}
Inevitable.task("check-records-queue", 100, this::processRecordsQueue);
}
});
}
Aggregations