Current work in progress
This commit is contained in:
parent
8cf159a6a7
commit
54e2f7a712
5 changed files with 210 additions and 126 deletions
Binary file not shown.
|
@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import info.nightscout.androidaps.BuildConfig;
|
import info.nightscout.androidaps.BuildConfig;
|
||||||
import info.nightscout.androidaps.Config;
|
import info.nightscout.androidaps.Config;
|
||||||
|
@ -29,10 +30,12 @@ import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProg
|
||||||
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
|
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.connector.CancelBolusTaskRunner;
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.CancelBolusTaskRunner;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
|
||||||
|
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
|
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
|
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
|
||||||
import info.nightscout.utils.DateUtil;
|
import info.nightscout.utils.DateUtil;
|
||||||
import info.nightscout.utils.NSUpload;
|
import info.nightscout.utils.NSUpload;
|
||||||
|
import sugar.free.sightparser.applayer.AppLayerMessage;
|
||||||
import sugar.free.sightparser.applayer.remote_control.CancelTBRMessage;
|
import sugar.free.sightparser.applayer.remote_control.CancelTBRMessage;
|
||||||
import sugar.free.sightparser.applayer.remote_control.ExtendedBolusMessage;
|
import sugar.free.sightparser.applayer.remote_control.ExtendedBolusMessage;
|
||||||
import sugar.free.sightparser.applayer.remote_control.StandardBolusMessage;
|
import sugar.free.sightparser.applayer.remote_control.StandardBolusMessage;
|
||||||
|
@ -42,6 +45,7 @@ import sugar.free.sightparser.handling.TaskRunner;
|
||||||
import sugar.free.sightparser.handling.taskrunners.SetTBRTaskRunner;
|
import sugar.free.sightparser.handling.taskrunners.SetTBRTaskRunner;
|
||||||
import sugar.free.sightparser.handling.taskrunners.StatusTaskRunner;
|
import sugar.free.sightparser.handling.taskrunners.StatusTaskRunner;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by jamorham on 23/01/2018.
|
* Created by jamorham on 23/01/2018.
|
||||||
*
|
*
|
||||||
|
@ -62,19 +66,22 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
private static Logger log = LoggerFactory.getLogger(InsightPumpPlugin.class);
|
private static Logger log = LoggerFactory.getLogger(InsightPumpPlugin.class);
|
||||||
|
|
||||||
private static volatile InsightPumpPlugin plugin;
|
private static volatile InsightPumpPlugin plugin;
|
||||||
|
|
||||||
private Date lastDataTime = new Date(0);
|
private Date lastDataTime = new Date(0);
|
||||||
private TaskRunner taskRunner;
|
private TaskRunner taskRunner;
|
||||||
private boolean fragmentEnabled = true;
|
private boolean fragmentEnabled = true;
|
||||||
private boolean fragmentVisible = true;
|
private boolean fragmentVisible = true;
|
||||||
private PumpDescription pumpDescription = new PumpDescription();
|
private PumpDescription pumpDescription = new PumpDescription();
|
||||||
private Handler handler = new Handler();
|
private final Handler handler = new Handler();
|
||||||
|
private final InsightPumpAsyncAdapter async = new InsightPumpAsyncAdapter();
|
||||||
private StatusTaskRunner.StatusResult statusResult;
|
private StatusTaskRunner.StatusResult statusResult;
|
||||||
private double basalRate = 0;
|
private double basalRate = 0;
|
||||||
private final TaskRunner.ResultCallback statusResultHandler = new TaskRunner.ResultCallback() {
|
private final TaskRunner.ResultCallback statusResultHandler = new TaskRunner.ResultCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(Exception e) {
|
public void onError(Exception e) {
|
||||||
log("Got error taskrunner: " + e);
|
log("Got error taskrunner: " +e);
|
||||||
|
android.util.Log.e("INSIGHTPUMP","taskrunner stacktrace: ",e);
|
||||||
|
|
||||||
if (e instanceof sugar.free.sightparser.error.DisconnectedError) {
|
if (e instanceof sugar.free.sightparser.error.DisconnectedError) {
|
||||||
if (Helpers.ratelimit("insight-reconnect", 2)) {
|
if (Helpers.ratelimit("insight-reconnect", 2)) {
|
||||||
|
@ -90,8 +97,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
statusResult = (StatusTaskRunner.StatusResult) result;
|
statusResult = (StatusTaskRunner.StatusResult) result;
|
||||||
processStatusResult();
|
processStatusResult();
|
||||||
updateGui();
|
updateGui();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private Connector connector;
|
private Connector connector;
|
||||||
|
@ -125,6 +130,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
|
|
||||||
this.connector = Connector.get();
|
this.connector = Connector.get();
|
||||||
this.connector.init();
|
this.connector.init();
|
||||||
|
|
||||||
log("back from init");
|
log("back from init");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,10 +149,19 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// just log during debugging
|
||||||
private static void log(String msg) {
|
private static void log(String msg) {
|
||||||
android.util.Log.e("INSIGHTPUMP", msg);
|
android.util.Log.e("INSIGHTPUMP", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void updateGui() {
|
||||||
|
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void pushCallbackEvent(EventInsightPumpCallback e) {
|
||||||
|
MainApp.bus().post(e);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getFragmentClass() {
|
public String getFragmentClass() {
|
||||||
return InsightPumpFragment.class.getName();
|
return InsightPumpFragment.class.getName();
|
||||||
|
@ -253,7 +268,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
@Override
|
@Override
|
||||||
public void disconnect(String reason) {
|
public void disconnect(String reason) {
|
||||||
try {
|
try {
|
||||||
connector.getServiceConnector().disconnect();
|
connector.getServiceConnector().disconnect();
|
||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
log("Could not disconnect - null pointer: " + e);
|
log("Could not disconnect - null pointer: " + e);
|
||||||
}
|
}
|
||||||
|
@ -270,7 +285,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
lastDataTime = new Date();
|
lastDataTime = new Date();
|
||||||
if (Connector.get().isPumpConnected()) {
|
if (Connector.get().isPumpConnected()) {
|
||||||
log("is connected.. requesting status");
|
log("is connected.. requesting status");
|
||||||
//handler.removeCallbacks(statusTaskRunnable);
|
|
||||||
handler.postDelayed(new Runnable() {
|
handler.postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -278,12 +292,13 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
taskRunner.fetch(statusResultHandler);
|
taskRunner.fetch(statusResultHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, 500);
|
, 1000);
|
||||||
} else {
|
} else {
|
||||||
log("not connected.. not requesting status");
|
log("not connected.. not requesting status");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO implement
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult setNewBasalProfile(Profile profile) {
|
public PumpEnactResult setNewBasalProfile(Profile profile) {
|
||||||
lastDataTime = new Date();
|
lastDataTime = new Date();
|
||||||
|
@ -315,7 +330,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
return df.format(basalRate);
|
return df.format(basalRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
|
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
|
||||||
PumpEnactResult result = new PumpEnactResult();
|
PumpEnactResult result = new PumpEnactResult();
|
||||||
|
@ -325,29 +339,29 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
result.enacted = result.bolusDelivered > 0 || result.carbsDelivered > 0;
|
result.enacted = result.bolusDelivered > 0 || result.carbsDelivered > 0;
|
||||||
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
|
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
|
||||||
|
|
||||||
Double delivering = 0d;
|
final UUID cmd = deliverBolus((float) detailedBolusInfo.insulin); // actually request delivery
|
||||||
|
final Cstatus cs = async.busyWaitForCommandResult(cmd, 10000);
|
||||||
|
|
||||||
/* while (delivering < detailedBolusInfo.insulin) {
|
result.percent = 100;
|
||||||
SystemClock.sleep(200);
|
result.success = cs == Cstatus.SUCCESS;;
|
||||||
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
|
||||||
bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), delivering);
|
if (result.success) {
|
||||||
bolusingEvent.percent = Math.min((int) (delivering / detailedBolusInfo.insulin * 100), 100);
|
log("Success!");
|
||||||
|
|
||||||
|
if (Config.logPumpComm)
|
||||||
|
log.debug("Delivering treatment insulin: " + detailedBolusInfo.insulin + "U carbs: " + detailedBolusInfo.carbs + "g " + result);
|
||||||
|
|
||||||
|
final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
||||||
|
bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivered), detailedBolusInfo.insulin);
|
||||||
|
bolusingEvent.percent = 100;
|
||||||
MainApp.bus().post(bolusingEvent);
|
MainApp.bus().post(bolusingEvent);
|
||||||
delivering += 0.1d;
|
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
||||||
|
} else {
|
||||||
|
log.debug("Failure to deliver treatment");
|
||||||
}
|
}
|
||||||
SystemClock.sleep(200); */
|
|
||||||
deliverBolus((float) detailedBolusInfo.insulin); // actually request delivery
|
|
||||||
// TODO handle status result
|
|
||||||
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
|
||||||
bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivered), detailedBolusInfo.insulin);
|
|
||||||
bolusingEvent.percent = 100;
|
|
||||||
MainApp.bus().post(bolusingEvent);
|
|
||||||
// SystemClock.sleep(1000);
|
|
||||||
if (Config.logPumpComm)
|
|
||||||
log.debug("Delivering treatment insulin: " + detailedBolusInfo.insulin + "U carbs: " + detailedBolusInfo.carbs + "g " + result);
|
|
||||||
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
||||||
lastDataTime = new Date();
|
lastDataTime = new Date();
|
||||||
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,6 +370,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO implement
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) {
|
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) {
|
||||||
TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder();
|
TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder();
|
||||||
|
@ -380,59 +395,58 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*@Override
|
|
||||||
|
@Override
|
||||||
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) {
|
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) {
|
||||||
TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder();
|
log("Set TBR %");
|
||||||
PumpEnactResult result = new PumpEnactResult();
|
final UUID cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), percent, durationInMinutes), "Set TBR " + percent+"%" + " " + durationInMinutes + "m");
|
||||||
if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
|
|
||||||
result = cancelTempBasal(false);
|
if (cmd == null) {
|
||||||
if (!result.success)
|
return new PumpEnactResult().success(false).enacted(false);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
TemporaryBasal tempBasal = new TemporaryBasal();
|
|
||||||
|
Cstatus cs = async.busyWaitForCommandResult(cmd, 10000);
|
||||||
|
log("Got command status: " + cs);
|
||||||
|
|
||||||
|
// create log entry
|
||||||
|
final TemporaryBasal tempBasal = new TemporaryBasal();
|
||||||
tempBasal.date = System.currentTimeMillis();
|
tempBasal.date = System.currentTimeMillis();
|
||||||
tempBasal.isAbsolute = false;
|
tempBasal.isAbsolute = false;
|
||||||
tempBasal.percentRate = percent;
|
tempBasal.percentRate = percent;
|
||||||
tempBasal.durationInMinutes = durationInMinutes;
|
tempBasal.durationInMinutes = durationInMinutes;
|
||||||
tempBasal.source = Source.USER;
|
tempBasal.source = Source.USER; // TODO check this is correct
|
||||||
result.success = true;
|
MainApp.getConfigBuilder().addToHistoryTempBasal(tempBasal);
|
||||||
result.enacted = true;
|
|
||||||
result.percent = percent;
|
|
||||||
result.isPercent = true;
|
|
||||||
result.isTempCancel = false;
|
|
||||||
result.duration = durationInMinutes;
|
|
||||||
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
|
|
||||||
treatmentsInterface.addToHistoryTempBasal(tempBasal);
|
|
||||||
if (Config.logPumpComm)
|
|
||||||
log.debug("Settings temp basal percent: " + result);
|
|
||||||
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
|
||||||
lastDataTime = new Date();
|
|
||||||
return result;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
PumpEnactResult pumpEnactResult = new PumpEnactResult().success(true).enacted(true).isPercent(true).duration(durationInMinutes);
|
||||||
|
pumpEnactResult.percent = percent;
|
||||||
|
pumpEnactResult.success = cs == Cstatus.SUCCESS;
|
||||||
|
pumpEnactResult.isTempCancel = percent == 100; // 100% temp basal is a cancellation
|
||||||
|
pumpEnactResult.comment=async.getCommandComment(cmd);
|
||||||
|
|
||||||
@Override
|
return pumpEnactResult;
|
||||||
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) {
|
|
||||||
try {
|
|
||||||
SetTBRTaskRunner setTBRTaskRunner = new SetTBRTaskRunner(connector.getServiceConnector(), percent, durationInMinutes);
|
|
||||||
setTBRTaskRunner.fetchBlockingCall();
|
|
||||||
PumpEnactResult pumpEnactResult = new PumpEnactResult().success(true).enacted(true).isPercent(true).duration(durationInMinutes);
|
|
||||||
pumpEnactResult.percent = percent;
|
|
||||||
return pumpEnactResult;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return new PumpEnactResult().success(false).enacted(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO update
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
|
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
|
||||||
|
log("Set Extended Bolus");
|
||||||
ExtendedBolusMessage extendedBolusMessage = new ExtendedBolusMessage();
|
ExtendedBolusMessage extendedBolusMessage = new ExtendedBolusMessage();
|
||||||
extendedBolusMessage.setAmount((float) ((double) insulin));
|
extendedBolusMessage.setAmount((float) ((double) insulin));
|
||||||
extendedBolusMessage.setDuration((short) ((int) durationInMinutes));
|
extendedBolusMessage.setDuration((short) ((int) durationInMinutes));
|
||||||
SingleMessageTaskRunner singleMessageTaskRunner = new SingleMessageTaskRunner(connector.getServiceConnector(), extendedBolusMessage);
|
SingleMessageTaskRunner singleMessageTaskRunner = new SingleMessageTaskRunner(connector.getServiceConnector(), extendedBolusMessage);
|
||||||
try {
|
try {
|
||||||
singleMessageTaskRunner.fetchBlockingCall();
|
singleMessageTaskRunner.fetch(new TaskRunner.ResultCallback() {
|
||||||
|
@Override
|
||||||
|
public void onResult(Object o) {
|
||||||
|
log("OK TBR %");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Exception e) {
|
||||||
|
log("Error TBR %");
|
||||||
|
}
|
||||||
|
});
|
||||||
return new PumpEnactResult().enacted(true).success(true).bolusDelivered(insulin).duration(durationInMinutes);
|
return new PumpEnactResult().enacted(true).success(true).bolusDelivered(insulin).duration(durationInMinutes);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -442,28 +456,18 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult cancelTempBasal(boolean enforceNew) {
|
public PumpEnactResult cancelTempBasal(boolean enforceNew) {
|
||||||
CancelTBRMessage cancelTBRMessage = new CancelTBRMessage();
|
log("Cancel TBR");
|
||||||
SingleMessageTaskRunner singleMessageTaskRunner = new SingleMessageTaskRunner(connector.getServiceConnector(), cancelTBRMessage);
|
final UUID cmd = aSyncSingleCommand(new CancelTBRMessage(),"Cancel Temp Basal");;
|
||||||
try {
|
|
||||||
singleMessageTaskRunner.fetchBlockingCall();
|
if (cmd == null) {
|
||||||
return new PumpEnactResult().success(true).enacted(true);
|
return pumpEnactFailure();
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return new PumpEnactResult().success(false).enacted(false);
|
|
||||||
|
final Cstatus cs = async.busyWaitForCommandResult(cmd,10000);
|
||||||
|
return new PumpEnactResult().success(cs == Cstatus.SUCCESS).enacted(true).isTempCancel(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public PumpEnactResult cancelExtendedBolus() {
|
|
||||||
CancelBolusTaskRunner cancelBolusTaskRunner = new CancelBolusTaskRunner(connector.getServiceConnector(), BolusType.EXTENDED);
|
|
||||||
try {
|
|
||||||
cancelBolusTaskRunner.fetchBlockingCall();
|
|
||||||
return new PumpEnactResult().success(true).enacted(true);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return new PumpEnactResult().success(false).enacted(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@Override
|
@Override
|
||||||
|
@ -533,6 +537,86 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
return result;
|
return result;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
private synchronized UUID aSyncSingleCommand(final AppLayerMessage msg, final String name) {
|
||||||
|
// if (!isConnected()) return false;
|
||||||
|
//if (isBusy()) return false;
|
||||||
|
log("asyncSinglecommand called: "+name);
|
||||||
|
final EventInsightPumpCallback event = new EventInsightPumpCallback();
|
||||||
|
new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
log("asyncSingleCommand thread");
|
||||||
|
final SingleMessageTaskRunner singleMessageTaskRunner = new SingleMessageTaskRunner(connector.getServiceConnector(), msg);
|
||||||
|
try {
|
||||||
|
singleMessageTaskRunner.fetch(new TaskRunner.ResultCallback() {
|
||||||
|
@Override
|
||||||
|
public void onResult(Object o) {
|
||||||
|
log(name+" success");
|
||||||
|
event.success = true;
|
||||||
|
pushCallbackEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Exception e) {
|
||||||
|
log(name+" error");
|
||||||
|
event.message = e.getMessage();
|
||||||
|
pushCallbackEvent(event);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log("EXCEPTION" + e.toString());
|
||||||
|
}
|
||||||
|
}}.start();
|
||||||
|
return event.request_uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized UUID aSyncTaskRunner(final TaskRunner task, final String name) {
|
||||||
|
// if (!isConnected()) return false;
|
||||||
|
//if (isBusy()) return false;
|
||||||
|
log("asyncTaskRunner called: "+name);
|
||||||
|
final EventInsightPumpCallback event = new EventInsightPumpCallback();
|
||||||
|
new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
log("asyncTaskRunner thread");
|
||||||
|
try {
|
||||||
|
task.fetch(new TaskRunner.ResultCallback() {
|
||||||
|
@Override
|
||||||
|
public void onResult(Object o) {
|
||||||
|
log(name+" success");
|
||||||
|
event.success = true;
|
||||||
|
pushCallbackEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Exception e) {
|
||||||
|
log(name+" error");
|
||||||
|
event.message = e.getMessage();
|
||||||
|
pushCallbackEvent(event);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log("EXCEPTION" + e.toString());
|
||||||
|
}
|
||||||
|
}}.start();
|
||||||
|
return event.request_uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO update
|
||||||
|
@Override
|
||||||
|
public PumpEnactResult cancelExtendedBolus() {
|
||||||
|
CancelBolusTaskRunner cancelBolusTaskRunner = new CancelBolusTaskRunner(connector.getServiceConnector(), BolusType.EXTENDED);
|
||||||
|
try {
|
||||||
|
cancelBolusTaskRunner.fetchBlockingCall();
|
||||||
|
return new PumpEnactResult().success(true).enacted(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return new PumpEnactResult().success(false).enacted(false);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject getJSONStatus() {
|
public JSONObject getJSONStatus() {
|
||||||
|
|
||||||
|
@ -590,7 +674,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
return "Insight Pump";
|
return "Insight Pump";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void processStatusResult() {
|
private void processStatusResult() {
|
||||||
if (statusResult != null) {
|
if (statusResult != null) {
|
||||||
batteryPercent = statusResult.getBatteryAmountMessage().getBatteryAmount();
|
batteryPercent = statusResult.getBatteryAmountMessage().getBatteryAmount();
|
||||||
|
@ -600,38 +683,26 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deliverBolus(float bolusValue) {
|
private synchronized UUID deliverBolus(float bolusValue) {
|
||||||
log("!!!!!!!!!! DeliverBolus: " + bolusValue);
|
log("!!!!!!!!!! DeliverBolus: " + bolusValue);
|
||||||
|
|
||||||
|
|
||||||
// Bare sanity checking
|
// Bare sanity checking
|
||||||
if (bolusValue == 0) return;
|
if (bolusValue == 0) return null;
|
||||||
if (bolusValue > 10) return;
|
if (bolusValue > 10) return null;
|
||||||
if (bolusValue < 0) return;
|
if (bolusValue < 0) return null;
|
||||||
|
|
||||||
// TODO check limits here?
|
// TODO check limits here?
|
||||||
|
|
||||||
StandardBolusMessage message = new StandardBolusMessage();
|
final StandardBolusMessage message = new StandardBolusMessage();
|
||||||
message.setAmount(bolusValue);
|
message.setAmount(bolusValue);
|
||||||
final SingleMessageTaskRunner taskRunner = new SingleMessageTaskRunner(Connector.get().getServiceConnector(), message);
|
|
||||||
|
|
||||||
|
return aSyncSingleCommand(message,"Deliver Bolus "+bolusValue);
|
||||||
taskRunner.fetch(new TaskRunner.ResultCallback() {
|
|
||||||
@Override
|
|
||||||
public void onResult(Object result) {
|
|
||||||
log("Bolus result: " + result.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Exception e) {
|
|
||||||
log("Bolus error");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void updateGui() {
|
private PumpEnactResult pumpEnactFailure() {
|
||||||
MainApp.bus().post(new EventInsightPumpUpdateGui());
|
return new PumpEnactResult().success(false).enacted(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
package info.nightscout.androidaps.plugins.PumpInsight.connector;
|
package info.nightscout.androidaps.plugins.PumpInsight.connector;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.pm.PackageInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
|
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
|
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
|
||||||
|
@ -28,7 +23,6 @@ public class Connector {
|
||||||
|
|
||||||
private static final String TAG = "InsightConnector";
|
private static final String TAG = "InsightConnector";
|
||||||
private static final String COMPANION_APP_PACKAGE = "sugar.free.sightremote";
|
private static final String COMPANION_APP_PACKAGE = "sugar.free.sightremote";
|
||||||
private static final String STATUS_RECEIVER = "sugar.free.sightparser.handling.StatusCallback";
|
|
||||||
private static volatile Connector instance;
|
private static volatile Connector instance;
|
||||||
private volatile SightServiceConnector serviceConnector;
|
private volatile SightServiceConnector serviceConnector;
|
||||||
private volatile Status lastStatus = null;
|
private volatile Status lastStatus = null;
|
||||||
|
@ -80,22 +74,8 @@ public class Connector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static boolean isCompanionAppInstalled() {
|
private static boolean isCompanionAppInstalled() {
|
||||||
return checkPackageExists(MainApp.instance(), COMPANION_APP_PACKAGE);
|
return Helpers.checkPackageExists(MainApp.instance(), TAG, COMPANION_APP_PACKAGE);
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean checkPackageExists(Context context, String packageName) {
|
|
||||||
try {
|
|
||||||
final PackageManager pm = context.getPackageManager();
|
|
||||||
final PackageInfo pi = pm.getPackageInfo(packageName, 0);
|
|
||||||
return pi.packageName.equals(packageName);
|
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
|
||||||
return false;
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.wtf(TAG, "Exception trying to determine packages! " + e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void connectToPump() {
|
public static void connectToPump() {
|
||||||
|
@ -121,6 +101,11 @@ public class Connector {
|
||||||
} else {
|
} else {
|
||||||
log("Not trying init due to missing companion app");
|
log("Not trying init due to missing companion app");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (!serviceConnector.isConnectedToService()) {
|
||||||
|
log("Trying to reconnect to service");
|
||||||
|
serviceConnector.connectToService();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +133,6 @@ public class Connector {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPumpConnected() {
|
public boolean isPumpConnected() {
|
||||||
//return isConnected() && serviceConnector.isUseable();
|
|
||||||
return isConnected() && getLastStatus() == Status.CONNECTED;
|
return isConnected() && getLastStatus() == Status.CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,6 +143,10 @@ public class Connector {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isConnected()) {
|
if (!isConnected()) {
|
||||||
|
log("Not connected to companion");
|
||||||
|
if (Helpers.ratelimit("insight-app-not-connected", 5)) {
|
||||||
|
init();
|
||||||
|
}
|
||||||
return "Not connected to companion app!";
|
return "Not connected to companion app!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
package info.nightscout.androidaps.plugins.PumpInsight.utils;
|
package info.nightscout.androidaps.plugins.PumpInsight.utils;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by jamorham on 24/01/2018.
|
* Created by jamorham on 24/01/2018.
|
||||||
*
|
*
|
||||||
|
@ -34,4 +39,24 @@ public class Helpers {
|
||||||
return System.currentTimeMillis();
|
return System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static long msSince(long when) {
|
||||||
|
return (tsl() - when);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long msTill(long when) {
|
||||||
|
return (when - tsl());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkPackageExists(Context context, String TAG, String packageName) {
|
||||||
|
try {
|
||||||
|
final PackageManager pm = context.getPackageManager();
|
||||||
|
final PackageInfo pi = pm.getPackageInfo(packageName, 0);
|
||||||
|
return pi.packageName.equals(packageName);
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.wtf(TAG, "Exception trying to determine packages! " + e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -862,6 +862,6 @@
|
||||||
<string name="combo_reservoir_level_insufficient_for_bolus">Not enough insulin for bolus left in reservoir</string>
|
<string name="combo_reservoir_level_insufficient_for_bolus">Not enough insulin for bolus left in reservoir</string>
|
||||||
<string name="extendedbolusdeliveryerror">Extended bolus delivery error</string>
|
<string name="extendedbolusdeliveryerror">Extended bolus delivery error</string>
|
||||||
<string name="insightpump_shortname">Insight</string>
|
<string name="insightpump_shortname">Insight</string>
|
||||||
<string name="insightpump">Insight</string>
|
<string name="insightpump">Insight Pump</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue