use of com.eveningoutpost.dexdrip.cgm.medtrum.messages.InboundStream in project xDrip-plus by jamorham.
the class MedtrumCollectionService method enable_features_and_listen.
private synchronized void enable_features_and_listen() {
UserError.Log.d(TAG, "enable features - enter");
stopListening();
if (connection != null) {
notificationSubscription = new Subscription(connection.setupNotification(Const.CGM_CHARACTERISTIC_NOTIFY).timeout(LISTEN_STASIS_SECONDS, // WARN
TimeUnit.SECONDS).observeOn(Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Notifications enabled");
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-receive-n", 60000);
try {
UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes));
lastInteractionTime = JoH.tsl();
setFailOverTimer();
lastAnnex = new AnnexARx(bytes);
UserError.Log.d(TAG, "Notification: " + lastAnnex.toS());
createRecordFromAnnexData(lastAnnex);
backFillIfNeeded(lastAnnex);
} finally {
JoH.releaseWakeLock(wl);
}
}, throwable -> {
UserError.Log.d(TAG, "notification throwable: " + throwable);
}));
final InboundStream inboundStream = new InboundStream();
indicationSubscription = new Subscription(connection.setupIndication(CGM_CHARACTERISTIC_INDICATE).timeout(LISTEN_STASIS_SECONDS, // WARN
TimeUnit.SECONDS).observeOn(Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Indications enabled");
sendTx(new AuthTx(serial));
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-receive-i", 60000);
try {
UserError.Log.d(TAG, "Received indication bytes: " + JoH.bytesToHex(bytes));
if (inboundStream.hasSomeData() && msSince(lastInteractionTime) > Constants.SECOND_IN_MS * 10) {
UserError.Log.d(TAG, "Resetting stream due to earlier timeout");
}
lastInteractionTime = JoH.tsl();
inboundStream.push(bytes);
if (!checkAndProcessInboundStream(inboundStream)) {
Inevitable.task("mt-reset-stream-no-data", 3000, () -> {
if (inboundStream.hasSomeData()) {
UserError.Log.d(TAG, "Resetting stream as incomplete after 3s");
inboundStream.reset();
}
});
}
} finally {
JoH.releaseWakeLock(wl);
}
}, throwable -> {
UserError.Log.d(TAG, "indication throwable: " + throwable);
}));
} else {
UserError.Log.e(TAG, "Connection null when trying to set notifications");
}
}
use of com.eveningoutpost.dexdrip.cgm.medtrum.messages.InboundStream in project xDrip by NightscoutFoundation.
the class MedtrumCollectionService method enable_features_and_listen.
private synchronized void enable_features_and_listen() {
UserError.Log.d(TAG, "enable features - enter");
stopListening();
if (connection != null) {
notificationSubscription = new Subscription(connection.setupNotification(Const.CGM_CHARACTERISTIC_NOTIFY).timeout(LISTEN_STASIS_SECONDS, // WARN
TimeUnit.SECONDS).observeOn(Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Notifications enabled");
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-receive-n", 60000);
try {
UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes));
lastInteractionTime = JoH.tsl();
setFailOverTimer();
lastAnnex = new AnnexARx(bytes);
UserError.Log.d(TAG, "Notification: " + lastAnnex.toS());
createRecordFromAnnexData(lastAnnex);
backFillIfNeeded(lastAnnex);
} finally {
JoH.releaseWakeLock(wl);
}
}, throwable -> {
UserError.Log.d(TAG, "notification throwable: " + throwable);
}));
final InboundStream inboundStream = new InboundStream();
indicationSubscription = new Subscription(connection.setupIndication(CGM_CHARACTERISTIC_INDICATE).timeout(LISTEN_STASIS_SECONDS, // WARN
TimeUnit.SECONDS).observeOn(Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Indications enabled");
sendTx(new AuthTx(serial));
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-receive-i", 60000);
try {
UserError.Log.d(TAG, "Received indication bytes: " + JoH.bytesToHex(bytes));
if (inboundStream.hasSomeData() && msSince(lastInteractionTime) > Constants.SECOND_IN_MS * 10) {
UserError.Log.d(TAG, "Resetting stream due to earlier timeout");
}
lastInteractionTime = JoH.tsl();
inboundStream.push(bytes);
if (!checkAndProcessInboundStream(inboundStream)) {
Inevitable.task("mt-reset-stream-no-data", 3000, () -> {
if (inboundStream.hasSomeData()) {
UserError.Log.d(TAG, "Resetting stream as incomplete after 3s");
inboundStream.reset();
}
});
}
} finally {
JoH.releaseWakeLock(wl);
}
}, throwable -> {
UserError.Log.d(TAG, "indication throwable: " + throwable);
}));
} else {
UserError.Log.e(TAG, "Connection null when trying to set notifications");
}
}
Aggregations