Insight async callback handling
This commit is contained in:
parent
8f1feefce9
commit
8cf159a6a7
3 changed files with 129 additions and 0 deletions
|
@ -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
|
||||||
|
}
|
|
@ -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<UUID, EventInsightPumpCallback> 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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue