Connect to IRuffyService in RuffyScripter, rather than ComboPlugin.

This commit is contained in:
Johannes Mockenhaupt 2017-10-17 19:05:55 +02:00
parent 142f9cf6a3
commit 5ab7e4dbd6
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 48 additions and 103 deletions

View file

@ -1,22 +1,15 @@
package info.nightscout.androidaps.plugins.PumpCombo;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import com.squareup.otto.Subscribe;
import org.json.JSONObject;
import org.monkey.d.ruffy.ruffy.driver.IRuffyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -30,17 +23,16 @@ import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress;
import info.nightscout.androidaps.plugins.PumpCombo.events.EventComboPumpUpdateGUI;
import info.nightscout.androidaps.plugins.PumpCombo.spi.PumpState;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.RuffyScripter;
import info.nightscout.androidaps.plugins.PumpCombo.spi.CommandResult;
import info.nightscout.androidaps.plugins.PumpCombo.spi.BolusProgressReporter;
import info.nightscout.androidaps.plugins.PumpCombo.spi.CommandResult;
import info.nightscout.androidaps.plugins.PumpCombo.spi.PumpState;
import info.nightscout.androidaps.plugins.PumpCombo.spi.RuffyCommands;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.SP;
@ -57,7 +49,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
private PumpDescription pumpDescription = new PumpDescription();
private RuffyCommands ruffyScripter;
private ServiceConnection mRuffyServiceConnection;
// TODO access to pump (and its members) is chaotic and needs an update
private ComboPump pump = new ComboPump();
@ -81,7 +72,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
private ComboPlugin() {
definePumpCapabilities();
MainApp.bus().register(this);
bindRuffyService();
startAlerter();
ruffyScripter = new RuffyScripter();
}
@ -172,69 +162,6 @@ public class ComboPlugin implements PluginBase, PumpInterface {
}, "combo-alerter").start();
}
private boolean bindRuffyService() {
Context context = MainApp.instance().getApplicationContext();
boolean boundSucceeded = false;
try {
Intent intent = new Intent()
.setComponent(new ComponentName(
// this must be the base package of the app (check package attribute in
// manifest element in the manifest file of the providing app)
"org.monkey.d.ruffy.ruffy",
// full path to the driver;
// in the logs this service is mentioned as (note the slash)
// "org.monkey.d.ruffy.ruffy/.driver.Ruffy";
// org.monkey.d.ruffy.ruffy is the base package identifier
// and /.driver.Ruffy the service within the package
"org.monkey.d.ruffy.ruffy.driver.Ruffy"
));
context.startService(intent);
mRuffyServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
keepUnbound = false;
// TODO fine until we know whether the impl will be an Android service or not
// and binds things and what not.
((RuffyScripter) ruffyScripter).start(IRuffyService.Stub.asInterface(service));
log.debug("ruffy serivce connected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO stop?
log.debug("ruffy service disconnected");
// try to reconnect ruffy service unless unbind was explicitly requested
// via unbindRuffyService
if (!keepUnbound) {
SystemClock.sleep(250);
bindRuffyService();
}
}
};
boundSucceeded = context.bindService(intent, mRuffyServiceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
log.error("Binding to ruffy service failed", e);
}
if (!boundSucceeded) {
pump.state.errorMsg = "No connection to ruffy. Pump control unavailable.";
}
return true;
}
private boolean keepUnbound = false;
private void unbindRuffyService() {
keepUnbound = true;
// TODO fine until we know whether the impl will be an Android service or not
((RuffyScripter) ruffyScripter).unbind();
MainApp.instance().getApplicationContext().unbindService(mRuffyServiceConnection);
}
@Override
public String getFragmentClass() {
return ComboFragment.class.getName();
@ -712,10 +639,4 @@ public class ComboPlugin implements PluginBase, PumpInterface {
public boolean isFakingTempsByExtendedBoluses() {
return false;
}
@SuppressWarnings("UnusedParameters")
@Subscribe
public void onStatusEvent(final EventAppExit ignored) {
unbindRuffyService();
}
}

View file

@ -1,5 +1,10 @@
package info.nightscout.androidaps.plugins.PumpCombo.scripter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.support.annotation.Nullable;
@ -18,19 +23,20 @@ import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.BolusCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.CancelTbrCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.Command;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.CommandException;
import info.nightscout.androidaps.plugins.PumpCombo.spi.BasalProfile;
import info.nightscout.androidaps.plugins.PumpCombo.spi.CommandResult;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.BolusCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.GetPumpStateCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.ReadBasalProfileCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.ReadHistoryCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.ReadReserverLevelCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.SetBasalProfileCommand;
import info.nightscout.androidaps.plugins.PumpCombo.scripter.internal.commands.SetTbrCommand;
import info.nightscout.androidaps.plugins.PumpCombo.spi.BasalProfile;
import info.nightscout.androidaps.plugins.PumpCombo.spi.BolusProgressReporter;
import info.nightscout.androidaps.plugins.PumpCombo.spi.CommandResult;
import info.nightscout.androidaps.plugins.PumpCombo.spi.PumpHistory;
import info.nightscout.androidaps.plugins.PumpCombo.spi.PumpState;
import info.nightscout.androidaps.plugins.PumpCombo.spi.RuffyCommands;
@ -61,29 +67,47 @@ public class RuffyScripter implements RuffyCommands {
private final Object screenlock = new Object();
public void start(IRuffyService newService) {
private ServiceConnection mRuffyServiceConnection;
public RuffyScripter() {
Context context = MainApp.instance().getApplicationContext();
boolean boundSucceeded = false;
try {
/*
if (ruffyService != null) {
try {
ruffyService.removeHandler(mHandler);
} catch (Exception e) {
// ignore
Intent intent = new Intent()
.setComponent(new ComponentName(
// this must be the base package of the app (check package attribute in
// manifest element in the manifest file of the providing app)
"org.monkey.d.ruffy.ruffy",
// full path to the driver;
// in the logs this service is mentioned as (note the slash)
// "org.monkey.d.ruffy.ruffy/.driver.Ruffy";
// org.monkey.d.ruffy.ruffy is the base package identifier
// and /.driver.Ruffy the service within the package
"org.monkey.d.ruffy.ruffy.driver.Ruffy"
));
context.startService(intent);
mRuffyServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ruffyService = IRuffyService.Stub.asInterface(service);
log.debug("ruffy serivce connected");
}
}
*/
if (newService != null) {
this.ruffyService = newService;
// TODO this'll be done better in v2 via ConnectionManager
if (idleDisconnectMonitorThread.getState() == Thread.State.NEW) {
idleDisconnectMonitorThread.start();
@Override
public void onServiceDisconnected(ComponentName name) {
log.debug("ruffy service disconnected");
}
started = true;
newService.setHandler(mHandler);
}
};
boundSucceeded = context.bindService(intent, mRuffyServiceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
log.error("Unhandled exception starting RuffyScripter", e);
throw new RuntimeException(e);
log.error("Binding to ruffy service failed", e);
}
if (!boundSucceeded) {
log.error("No connection to ruffy. Pump control unavailable.");
}
}