Insight status list and connector

This commit is contained in:
Jamorham 2018-01-26 16:58:19 +00:00
parent f2cfc3cdbd
commit 076685992c
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9
2 changed files with 124 additions and 8 deletions

View file

@ -8,7 +8,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import info.nightscout.androidaps.BuildConfig;
@ -33,6 +35,7 @@ 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.utils.Helpers;
import info.nightscout.androidaps.plugins.PumpInsight.utils.StatusItem;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.NSUpload;
import info.nightscout.utils.SP;
@ -41,6 +44,7 @@ import sugar.free.sightparser.applayer.remote_control.CancelTBRMessage;
import sugar.free.sightparser.applayer.remote_control.ExtendedBolusMessage;
import sugar.free.sightparser.applayer.remote_control.StandardBolusMessage;
import sugar.free.sightparser.applayer.status.BolusType;
import sugar.free.sightparser.applayer.status.PumpStatus;
import sugar.free.sightparser.handling.SingleMessageTaskRunner;
import sugar.free.sightparser.handling.TaskRunner;
import sugar.free.sightparser.handling.taskrunners.SetTBRTaskRunner;
@ -69,12 +73,12 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
private static volatile InsightPumpPlugin plugin;
private final Handler handler = new Handler();
private final InsightPumpAsyncAdapter async = new InsightPumpAsyncAdapter();
StatusTaskRunner.StatusResult statusResult;
private Date lastDataTime = new Date(0);
private TaskRunner taskRunner;
private boolean fragmentEnabled = true;
private boolean fragmentVisible = true;
private PumpDescription pumpDescription = new PumpDescription();
private StatusTaskRunner.StatusResult statusResult;
private double basalRate = 0;
private final TaskRunner.ResultCallback statusResultHandler = new TaskRunner.ResultCallback() {
@ -264,10 +268,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
log("InsightPumpPlugin::connect()");
try {
if (!connector.isPumpConnected()) {
if (Helpers.ratelimit("insight-connect-timer",40)) {
log("Actually requesting a connect");
connector.getServiceConnector().connect();
}
if (Helpers.ratelimit("insight-connect-timer", 40)) {
log("Actually requesting a connect");
connector.getServiceConnector().connect();
}
} else {
log("Already connected");
}
@ -515,7 +519,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
}
if (Config.logPumpComm)
log.debug("Setting extended bolus: " + insulin + " mins:" + durationInMinutes+" "+pumpEnactResult.comment);
log.debug("Setting extended bolus: " + insulin + " mins:" + durationInMinutes + " " + pumpEnactResult.comment);
updateGui();
return pumpEnactResult;
@ -640,8 +644,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
}*/
@Override
public JSONObject getJSONStatus() {
@ -708,6 +710,67 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
}
}
private String gs(int id) {
return MainApp.instance().getString(id);
}
public List<StatusItem> getStatusItems() {
final List<StatusItem> l = new ArrayList<>();
// Todo last contact time
l.add(new StatusItem("Status", connector.getLastStatusMessage()));
l.add(new StatusItem("Changed",connector.getNiceLastStatusTime()));
boolean pumpRunning;
// also check time since received
if (statusResult != null) {
pumpRunning = statusResult.getPumpStatusMessage().getPumpStatus() == PumpStatus.STARTED;
if (pumpRunning) {
l.add(new StatusItem(gs(R.string.pump_basebasalrate_label), getBaseBasalRateString() + "U"));
} else {
l.add(new StatusItem("Warning", "PUMP STOPPED", StatusItem.Highlight.CRITICAL));
}
}
if (statusResult != null) {
l.add(new StatusItem(gs(R.string.pump_battery_label), batteryPercent + "%", batteryPercent < 100 ?
(batteryPercent < 90 ?
(batteryPercent < 70 ?
(StatusItem.Highlight.BAD) : StatusItem.Highlight.NOTICE) : StatusItem.Highlight.NORMAL) : StatusItem.Highlight.GOOD));
l.add(new StatusItem(gs(R.string.pump_reservoir_label), reservoirInUnits + "U"));
}
if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
try {
l.add(new StatusItem(gs(R.string.pump_tempbasal_label), MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis()).toStringFull()));
} catch (NullPointerException e) {
//
}
}
if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) {
try {
l.add(new StatusItem(gs(R.string.virtualpump_extendedbolus_label), MainApp.getConfigBuilder().getExtendedBolusFromHistory(System.currentTimeMillis()).toString()));
} catch (NullPointerException e) {
//
}
}
if (connector.uiFresh()) {
Helpers.runOnUiThreadDelayed(new Runnable() {
@Override
public void run() {
updateGui();
}
},1000);
}
return l;
}
// Utility
private synchronized UUID aSyncSingleCommand(final AppLayerMessage msg, final String name) {

View file

@ -1,5 +1,11 @@
package info.nightscout.androidaps.plugins.PumpInsight.connector;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
@ -23,6 +29,8 @@ public class Connector {
private static final String TAG = "InsightConnector";
private static final String COMPANION_APP_PACKAGE = "sugar.free.sightremote";
private static final String HISTORY_IDENTIFIER = "sugar.free.sightremote.history";
private static final String HISTORY_RECEIVER = "sugar.free.sightremote.HISTORY";
private static volatile Connector instance;
private volatile SightServiceConnector serviceConnector;
private volatile Status lastStatus = null;
@ -69,7 +77,24 @@ public class Connector {
}
};
private BroadcastReceiver historyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
log("Receiving history broadcast!");
// TODO check action
final Bundle bundle = intent.getBundleExtra(HISTORY_IDENTIFIER);
if (bundle != null) {
//HistoryArray history = (HistoryArray) bundle.getSerializable("history");
} else {
log("History bundle was null!");
}
}
};
private Connector() {
registerHistoryReceiver();
}
public static Connector get() {
@ -185,6 +210,23 @@ public class Connector {
}
}
public String getNiceLastStatusTime() {
if (lastStatusTime < 1) {
return "STARTUP";
} else {
return Helpers.niceTimeScalar(Helpers.msSince(lastStatusTime)) + " ago";
}
}
public boolean uiFresh() {
// todo check other changes
if (Helpers.msSince(lastStatusTime) < 70000) {
return true;
}
return false;
}
@SuppressWarnings("AccessStaticViaInstance")
private void tryToGetStatusAgain() {
if (Helpers.ratelimit("insight-retry-status-request", 5)) {
try {
@ -195,6 +237,17 @@ public class Connector {
}
}
private synchronized void registerHistoryReceiver() {
try {
MainApp.instance().unregisterReceiver(historyReceiver);
} catch (Exception e) {
//
}
MainApp.instance().registerReceiver(historyReceiver, new IntentFilter(HISTORY_RECEIVER));
}
public boolean lastStatusRecent() {
return true; // TODO evaluate whether current
}