diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Cstatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Cstatus.java new file mode 100644 index 0000000000..81d119cd60 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Cstatus.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.PumpInsight; + +/** + * Created by jamorham on 25/01/2018. + * + * Async command status + * + */ +enum Cstatus { + UNKNOWN, + PENDING, + SUCCESS, + FAILURE, + TIMEOUT +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java new file mode 100644 index 0000000000..081ad09140 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java @@ -0,0 +1,89 @@ +package info.nightscout.androidaps.plugins.PumpInsight; + +import com.squareup.otto.Subscribe; + +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback; + +import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.msSince; +import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.tsl; + +/** + * Created by jamorham on 25/01/2018. + * + * Asynchronous adapter + * + */ + +public class InsightPumpAsyncAdapter { + + private final ConcurrentHashMap commandResults = new ConcurrentHashMap<>(); + + InsightPumpAsyncAdapter() { + MainApp.bus().register(this); + } + + // just log during debugging + private static void log(String msg) { + android.util.Log.e("INSIGHTPUMPASYNC", msg); + } + + @Subscribe + public void onStatusEvent(final EventInsightPumpCallback ev) { + log("Received callback event: " + ev.toString()); + commandResults.put(ev.request_uuid, ev); + } + + // poll command result + private Cstatus checkCommandResult(UUID uuid) { + if (uuid == null) return Cstatus.FAILURE; + if (commandResults.containsKey(uuid)) { + if (commandResults.get(uuid).success) { + return Cstatus.SUCCESS; + } else { + return Cstatus.FAILURE; + } + } else { + return Cstatus.PENDING; + } + } + + // blocking call to wait for result callback + Cstatus busyWaitForCommandResult(final UUID uuid, long wait_time) { + log("busy wait for command " + uuid); + if (uuid == null) return Cstatus.FAILURE; + final long start_time = tsl(); + Cstatus status = checkCommandResult(uuid); + while ((status == Cstatus.PENDING) && msSince(start_time) < wait_time) { + //log("command result waiting"); + try { + Thread.sleep(200); + } catch (InterruptedException e) { + log("Got interrupted exception! " + e); + } + status = checkCommandResult(uuid); + } + if (status == Cstatus.PENDING) { + return Cstatus.TIMEOUT; + } else { + return status; + } + } + + // commend field preparation for results + String getCommandComment(final UUID uuid) { + if (commandResults.containsKey(uuid)) { + if (commandResults.get(uuid).success) { + return "OK"; + } else { + return commandResults.get(uuid).message; + } + } else { + return "Unknown reference"; + } + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java new file mode 100644 index 0000000000..c47473aa8d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.plugins.PumpInsight.events; + +import java.util.UUID; + +import info.nightscout.androidaps.events.Event; + +/** + * Created by jamorham on 23/01/2018. + */ +public class EventInsightPumpCallback extends Event { + + public UUID request_uuid; + public boolean success = false; + public String message = null; + + public EventInsightPumpCallback() { + request_uuid = UUID.randomUUID(); + } + + @Override + public String toString() { + return "Event: " + request_uuid + " success: " + success + " msg: " + message; + } + +}