Nightscout + UI performance improvements

This commit is contained in:
Jamorham 2018-01-31 14:23:22 +00:00
parent d07d9d309e
commit 7f73c59530
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9
3 changed files with 69 additions and 33 deletions

View file

@ -25,27 +25,33 @@ import info.nightscout.androidaps.plugins.PumpInsight.utils.ui.StatusItemViewAda
public class InsightPumpFragment extends SubscriberFragment { public class InsightPumpFragment extends SubscriberFragment {
private static Logger log = LoggerFactory.getLogger(InsightPumpFragment.class); private static final Logger log = LoggerFactory.getLogger(InsightPumpFragment.class);
private static Handler sLoopHandler = new Handler(); private static final Handler sLoopHandler = new Handler();
private static Runnable sRefreshLoop = null; private static volatile boolean refresh = false;
private static volatile boolean pending = false;
StatusItemViewAdapter viewAdapter; StatusItemViewAdapter viewAdapter;
LinearLayout holder; LinearLayout holder;
private final Runnable sRefreshLoop = new Runnable() {
@Override
public void run() {
pending = false;
updateGUI();
if (refresh) {
scheduleRefresh();
}
}
};
private synchronized void scheduleRefresh() {
if (!pending) {
pending = true;
sLoopHandler.postDelayed(sRefreshLoop, 30 * 1000L);
}
}
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (sRefreshLoop == null) {
sRefreshLoop = new Runnable() {
@Override
public void run() {
updateGUI();
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
};
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
} }
@Override @Override
@ -64,6 +70,22 @@ public class InsightPumpFragment extends SubscriberFragment {
return null; return null;
} }
@Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(visible);
if (visible) {
refresh = true;
pending = false;
updateGUI();
scheduleRefresh();
} else {
refresh = false;
//sLoopHandler.removeCallbacksAndMessages(null);
}
}
@Subscribe @Subscribe
public void onStatusEvent(final EventInsightPumpUpdateGui ev) { public void onStatusEvent(final EventInsightPumpUpdateGui ev) {
updateGUI(); updateGUI();
@ -77,7 +99,7 @@ public class InsightPumpFragment extends SubscriberFragment {
@Override @Override
public void run() { public void run() {
final InsightPumpPlugin insightPumpPlugin = InsightPumpPlugin.getPlugin(); final InsightPumpPlugin insightPumpPlugin = InsightPumpPlugin.getPlugin();
final List<StatusItem> l = insightPumpPlugin.getStatusItems(); final List<StatusItem> l = insightPumpPlugin.getStatusItems(refresh);
holder.removeAllViews(); holder.removeAllViews();

View file

@ -73,6 +73,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
static Integer batteryPercent = 0; static Integer batteryPercent = 0;
static Integer reservoirInUnits = 0; static Integer reservoirInUnits = 0;
static boolean initialized = false; static boolean initialized = false;
private static volatile boolean update_pending = false;
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 final Handler handler = new Handler(); private final Handler handler = new Handler();
@ -167,6 +168,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
} }
private static void updateGui() { private static void updateGui() {
update_pending = false;
MainApp.bus().post(new EventInsightPumpUpdateGui()); MainApp.bus().post(new EventInsightPumpUpdateGui());
} }
@ -644,13 +646,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
private synchronized UUID deliverBolus(float bolusValue) { private synchronized UUID deliverBolus(float bolusValue) {
log("DeliverBolus: " + bolusValue); log("DeliverBolus: " + bolusValue);
// Bare sanity checking should be done elsewhere
if (bolusValue == 0) return null; if (bolusValue == 0) return null;
if (bolusValue < 0) return null; if (bolusValue < 0) return null;
if (bolusValue > 20) return null; // TODO check limits here or they already occur via a previous constraint interface?
// TODO check limits here?
final StandardBolusMessage message = new StandardBolusMessage(); final StandardBolusMessage message = new StandardBolusMessage();
message.setAmount(bolusValue); message.setAmount(bolusValue);
@ -661,15 +660,19 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
@Override @Override
public JSONObject getJSONStatus() { public JSONObject getJSONStatus() {
// TODO review if (Helpers.msSince(connector.getLastContactTime()) > (60 * 60 * 1000)) {
log("getJSONStatus not returning as data likely stale");
return null;
}
JSONObject pump = new JSONObject(); final JSONObject pump = new JSONObject();
JSONObject battery = new JSONObject(); final JSONObject battery = new JSONObject();
JSONObject status = new JSONObject(); final JSONObject status = new JSONObject();
JSONObject extended = new JSONObject(); final JSONObject extended = new JSONObject();
try { try {
battery.put("percent", batteryPercent); battery.put("percent", batteryPercent);
status.put("status", "normal"); status.put("status", isSuspended() ? "suspended" : "normal");
status.put("timestamp", DateUtil.toISOString(connector.getLastContactTime()));
extended.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION); extended.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION);
try { try {
extended.put("ActiveProfile", MainApp.getConfigBuilder().getProfileName()); extended.put("ActiveProfile", MainApp.getConfigBuilder().getProfileName());
@ -733,7 +736,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
return statusResult.getPumpStatusMessage().getPumpStatus() == PumpStatus.STARTED; return statusResult.getPumpStatusMessage().getPumpStatus() == PumpStatus.STARTED;
} }
List<StatusItem> getStatusItems() { List<StatusItem> getStatusItems(boolean refresh) {
final List<StatusItem> l = new ArrayList<>(); final List<StatusItem> l = new ArrayList<>();
// Todo last contact time // Todo last contact time
@ -802,17 +805,21 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface {
} }
Connector.get().requestHistorySync(); Connector.get().requestHistorySync();
if (refresh) scheduleGUIUpdate();
if (connector.uiFresh()) { return l;
}
private synchronized void scheduleGUIUpdate() {
if (!update_pending && connector.uiFresh()) {
update_pending = true;
Helpers.runOnUiThreadDelayed(new Runnable() { Helpers.runOnUiThreadDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
updateGui(); updateGui();
} }
}, 1000); }, 500);
} }
return l;
} }
private void statusActiveBolus(ActiveBolus activeBolus, long offset_mins, List<StatusItem> l) { private void statusActiveBolus(ActiveBolus activeBolus, long offset_mins, List<StatusItem> l) {

View file

@ -39,6 +39,7 @@ public class Connector {
private volatile Status lastStatus = null; private volatile Status lastStatus = null;
private String compatabilityMessage = null; private String compatabilityMessage = null;
private volatile long lastStatusTime = -1; private volatile long lastStatusTime = -1;
private volatile long lastContactTime = -1;
private boolean companionAppInstalled = false; private boolean companionAppInstalled = false;
private int serviceReconnects = 0; private int serviceReconnects = 0;
private StatusCallback statusCallback = new StatusCallback() { private StatusCallback statusCallback = new StatusCallback() {
@ -48,6 +49,9 @@ public class Connector {
log("Status change: " + status); log("Status change: " + status);
lastStatus = status; lastStatus = status;
lastStatusTime = Helpers.tsl(); lastStatusTime = Helpers.tsl();
if (status == Status.CONNECTED) {
lastContactTime = lastStatusTime;
}
MainApp.bus().post(new EventInsightPumpUpdateGui()); MainApp.bus().post(new EventInsightPumpUpdateGui());
} }
@ -197,6 +201,10 @@ public class Connector {
return isConnected() && getLastStatus() == Status.CONNECTING; return isConnected() && getLastStatus() == Status.CONNECTING;
} }
public long getLastContactTime() {
return lastContactTime;
}
public String getLastStatusMessage() { public String getLastStatusMessage() {
if (!companionAppInstalled) { if (!companionAppInstalled) {
@ -225,10 +233,9 @@ public class Connector {
switch (lastStatus) { switch (lastStatus) {
case CONNECTED: case CONNECTED:
if (lastStatusTime < 1) { if (Helpers.msSince(lastStatusTime) > (60 * 10 * 1000)) {
tryToGetPumpStatusAgain(); tryToGetPumpStatusAgain();
} }
// TODO other refresh?
break; break;
case INCOMPATIBLE: case INCOMPATIBLE:
return lastStatus.toString() + " needs " + getLocalVersion(); return lastStatus.toString() + " needs " + getLocalVersion();