Safeguard against communication issues with ruffy.

In most cases we can't do much, but we can handle exceptions
and show a notification and message what's wrong, so the user
can try to fix the issue.

Fixes jotomo/AndroidAPS#35.
This commit is contained in:
Johannes Mockenhaupt 2017-07-24 12:40:54 +02:00
parent 81f3aea42a
commit 674fb398b8
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -114,6 +114,14 @@ public class ComboPlugin implements PluginBase, PumpInterface {
pumpDescription.isRefillingCapable = true; pumpDescription.isRefillingCapable = true;
} }
/**
* The alerter frequently checks the result of the last executed command via the lastCmdResult
* field and shows a notification with sound and vibration if an error occurred.
* More details on the error can then be looked up in the Combo tab.
*
* The alarm is re-raised every 5 minutes for as long as the error persist. As soon
* as a command succeeds no more new alerts are raised.
*/
private void startAlerter() { private void startAlerter() {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
@ -161,7 +169,9 @@ public class ComboPlugin implements PluginBase, PumpInterface {
private void bindRuffyService() { private void bindRuffyService() {
Context context = MainApp.instance().getApplicationContext(); Context context = MainApp.instance().getApplicationContext();
boolean boundSucceeded = false;
try {
Intent intent = new Intent() Intent intent = new Intent()
.setComponent(new ComponentName( .setComponent(new ComponentName(
// this must be the base package of the app (check package attribute in // this must be the base package of the app (check package attribute in
@ -187,14 +197,13 @@ public class ComboPlugin implements PluginBase, PumpInterface {
log.debug("ruffy service disconnected"); log.debug("ruffy service disconnected");
} }
}; };
boundSucceeded = context.bindService(intent, mRuffyServiceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
log.error("Binding to ruffy service failed", e);
}
boolean success = context.bindService(intent, mRuffyServiceConnection, Context.BIND_AUTO_CREATE); if (!boundSucceeded) {
if (!success) { statusSummary = "No connection to ruffy. Pump control not available.";
log.error("Binding to ruffy service failed");
// AAPS will still crash since it continues trying to access the pump, even when isInitalized
// returns false and isBusy returns true; this will however not crash the alerter
// which will raise an exception. Not really ideal.
statusSummary = "Failed to bind to ruffy service";
} }
} }
@ -277,7 +286,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override @Override
public boolean isBusy() { public boolean isBusy() {
return ruffyScripter.isPumpBusy(); return ruffyScripter == null || ruffyScripter.isPumpBusy();
} }
// TODO // TODO
@ -304,7 +313,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
// if Android is sluggish this might get called before ruffy is bound // if Android is sluggish this might get called before ruffy is bound
if (ruffyScripter == null) { if (ruffyScripter == null) {
log.debug("Rejecting call to RefreshDataFromPump: ruffy service not bound yet"); log.warn("Rejecting call to RefreshDataFromPump: ruffy service not bound (yet)");
return; return;
} }
@ -386,6 +395,12 @@ public class ComboPlugin implements PluginBase, PumpInterface {
} }
private CommandResult runCommand(Command command) { private CommandResult runCommand(Command command) {
if (ruffyScripter == null) {
String msg = "No connection to ruffy. Pump control not available.";
statusSummary = msg;
return new CommandResult().message(msg);
}
statusSummary = "Executing " + command; statusSummary = "Executing " + command;
MainApp.bus().post(new EventComboPumpUpdateGUI()); MainApp.bus().post(new EventComboPumpUpdateGUI());
@ -393,10 +408,17 @@ public class ComboPlugin implements PluginBase, PumpInterface {
if (!commandResult.success && commandResult.exception != null) { if (!commandResult.success && commandResult.exception != null) {
log.error("CommandResult has exception, rebinding ruffy service", commandResult.exception); log.error("CommandResult has exception, rebinding ruffy service", commandResult.exception);
// attempt to rebind the ruffy service, will start ruffy again if it crashed
try {
unbindRuffyService(); unbindRuffyService();
SystemClock.sleep(5000); SystemClock.sleep(5000);
bindRuffyService(); bindRuffyService();
SystemClock.sleep(5000); SystemClock.sleep(5000);
} catch (Exception e) {
String msg = "No connection to ruffy. Pump control not available.";
statusSummary = msg;
return new CommandResult().message(msg);
}
if (ruffyScripter == null) { if (ruffyScripter == null) {
log.error("Rebinding failed"); log.error("Rebinding failed");