From ecd787bdb31ff1aa739e41849fef3af8f10c74ee Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 27 Dec 2016 16:06:49 +0100 Subject: [PATCH] detect broken communication during bolusing --- .../plugins/DanaR/Services/ExecutionService.java | 9 +++++++-- .../plugins/DanaR/comm/MsgBolusProgress.java | 11 +++++++---- .../androidaps/plugins/DanaR/comm/MsgBolusStop.java | 6 ++---- .../DanaRKorean/Services/ExecutionService.java | 9 +++++++-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java index fc22449883..66acc7292e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java @@ -371,8 +371,8 @@ public class ExecutionService extends Service { public boolean bolus(Double amount, int carbs, Treatment t) { bolusingTreatment = t; MsgBolusStart start = new MsgBolusStart(amount); - MsgBolusProgress progress = new MsgBolusProgress(MainApp.bus(), amount, t); - MsgBolusStop stop = new MsgBolusStop(MainApp.bus(), amount, t); + MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables + MsgBolusStop stop = new MsgBolusStop(amount, t); connect("bolus"); if (!isConnected()) return false; @@ -391,6 +391,11 @@ public class ExecutionService extends Service { } while (!stop.stopped && !start.failed) { waitMsec(100); + if (progress.lastReceive != 0 && (new Date().getTime() - progress.lastReceive) > 5 * 1000L) { // if i didn't receive status for more than 5 sec expecting broken comm + stop.stopped = true; + stop.forced = true; + log.debug("Communication stopped"); + } } waitMsec(300); bolusingTreatment = null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java index a5b7e68818..aa8a77f7bf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java @@ -5,6 +5,8 @@ import com.squareup.otto.Bus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Date; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -13,27 +15,28 @@ import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProg public class MsgBolusProgress extends MessageBase { private static Logger log = LoggerFactory.getLogger(MsgBolusProgress.class); - private static Bus bus = null; private static Treatment t; private static double amount; + public static long lastReceive = 0; public int progress = -1; public MsgBolusProgress() { SetCommand(0x0202); } - public MsgBolusProgress(Bus bus, double amount, Treatment t) { + public MsgBolusProgress(double amount, Treatment t) { this(); this.amount = amount; this.t = t; - this.bus = bus; + lastReceive = 0; } @Override public void handleMessage(byte[] bytes) { progress = intFromBuff(bytes, 0, 2); + lastReceive = new Date().getTime(); Double done = (amount * 100 - progress) / 100d; t.insulin = done; EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); @@ -45,6 +48,6 @@ public class MsgBolusProgress extends MessageBase { log.debug("Bolus remaining: " + progress + " delivered: " + done); } - bus.post(bolusingEvent); + MainApp.bus().post(bolusingEvent); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java index f6c21adc78..68de53d50b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java @@ -14,7 +14,6 @@ public class MsgBolusStop extends MessageBase { private static Logger log = LoggerFactory.getLogger(MsgBolusStop.class); private static Treatment t; private static Double amount; - private static Bus bus = null; public static boolean stopped = false; public static boolean forced = false; @@ -24,9 +23,8 @@ public class MsgBolusStop extends MessageBase { stopped = false; } - public MsgBolusStop(Bus bus, Double amount, Treatment t) { + public MsgBolusStop(Double amount, Treatment t) { this(); - this.bus = bus; this.t = t; this.amount = amount; forced = false; @@ -43,6 +41,6 @@ public class MsgBolusStop extends MessageBase { } else { bolusingEvent.status = MainApp.sResources.getString(R.string.overview_bolusprogress_stoped); } - bus.post(bolusingEvent); + MainApp.bus().post(bolusingEvent); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java index 4177074a81..29c001cc4c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java @@ -362,8 +362,8 @@ public class ExecutionService extends Service { public boolean bolus(Double amount, int carbs, Treatment t) { bolusingTreatment = t; MsgBolusStart start = new MsgBolusStart(amount); - MsgBolusProgress progress = new MsgBolusProgress(MainApp.bus(), amount, t); - MsgBolusStop stop = new MsgBolusStop(MainApp.bus(), amount, t); + MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables + MsgBolusStop stop = new MsgBolusStop(amount, t); connect("bolus"); if (!isConnected()) return false; @@ -382,6 +382,11 @@ public class ExecutionService extends Service { } while (!stop.stopped && !start.failed) { waitMsec(100); + if (progress.lastReceive != 0 && (new Date().getTime() - progress.lastReceive) > 5 * 1000L) { // if i didn't receive status for more than 5 sec expecting broken comm + stop.stopped = true; + stop.forced = true; + log.debug("Communication stopped"); + } } waitMsec(300); bolusingTreatment = null;