Initial work on the ComboPlugin.

This commit is contained in:
Johannes Mockenhaupt 2017-07-13 18:15:37 +02:00
parent ab9908438e
commit 481c63fa57
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -1,18 +1,34 @@
package info.nightscout.androidaps.plugins.PumpCombo; package info.nightscout.androidaps.plugins.PumpCombo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import com.squareup.otto.Subscribe;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.monkey.d.ruffy.ruffy.driver.IRuffyService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Date; import java.util.Date;
import de.jotomo.ruffyscripter.RuffyScripter;
import de.jotomo.ruffyscripter.commands.BolusCommand;
import de.jotomo.ruffyscripter.commands.CancelTbrCommand;
import de.jotomo.ruffyscripter.commands.Command;
import de.jotomo.ruffyscripter.commands.CommandResult;
import de.jotomo.ruffyscripter.commands.SetTbrCommand;
import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
@ -29,11 +45,65 @@ public class ComboPlugin implements PluginBase, PumpInterface {
PumpDescription pumpDescription = new PumpDescription(); PumpDescription pumpDescription = new PumpDescription();
private RuffyScripter ruffyScripter;
private Date lastCmdTime = new Date(0);
private ServiceConnection mRuffyServiceConnection;
private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult();
static {
OPERATION_NOT_SUPPORTED.success = false;
OPERATION_NOT_SUPPORTED.enacted = false;
OPERATION_NOT_SUPPORTED.comment = "Requested operation not supported by pump";
}
private double fakeBasalRate = 0.5d;
public ComboPlugin() { public ComboPlugin() {
definePumpCapabilities();
bindRuffyService();
MainApp.bus().register(this);
}
private void bindRuffyService() {
Context context = MainApp.instance().getApplicationContext();
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.driver.Ruffy"
));
context.startService(intent);
mRuffyServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service));
log.debug("ruffy serivce connected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
log.debug("ruffy service disconnected");
}
};
boolean success = context.bindService(intent, mRuffyServiceConnection, Context.BIND_AUTO_CREATE);
if(!success) {
log.error("Binding to ruffy service failed");
}
}
private void definePumpCapabilities() {
pumpDescription.isBolusCapable = true; pumpDescription.isBolusCapable = true;
pumpDescription.bolusStep = 0.1d; pumpDescription.bolusStep = 0.1d;
pumpDescription.isExtendedBolusCapable = true; pumpDescription.isExtendedBolusCapable = false; // TODO
pumpDescription.extendedBolusStep = 0.05d; pumpDescription.extendedBolusStep = 0.05d;
pumpDescription.extendedBolusDurationStep = 30; pumpDescription.extendedBolusDurationStep = 30;
pumpDescription.extendedBolusMaxDuration = 8 * 60; pumpDescription.extendedBolusMaxDuration = 8 * 60;
@ -44,11 +114,11 @@ public class ComboPlugin implements PluginBase, PumpInterface {
pumpDescription.maxTempPercent = 500; pumpDescription.maxTempPercent = 500;
pumpDescription.tempPercentStep = 10; pumpDescription.tempPercentStep = 10;
pumpDescription.tempDurationStep = 30; pumpDescription.tempDurationStep = 15;
pumpDescription.tempMaxDuration = 24 * 60; pumpDescription.tempMaxDuration = 24 * 60;
pumpDescription.isSetBasalProfileCapable = true; pumpDescription.isSetBasalProfileCapable = false; // TODO
pumpDescription.basalStep = 0.01d; pumpDescription.basalStep = 0.01d;
pumpDescription.basalMinimumRate = 0.01d; pumpDescription.basalMinimumRate = 0.01d;
@ -118,7 +188,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
return true; // TODO
return ruffyScripter != null;
} }
@Override @Override
@ -126,75 +197,122 @@ public class ComboPlugin implements PluginBase, PumpInterface {
return false; return false;
} }
// TODO
@Override @Override
public boolean isBusy() { public boolean isBusy() {
return false; return ruffyScripter.isPumpBusy();
} }
// TODO
@Override @Override
public int setNewBasalProfile(Profile profile) { public int setNewBasalProfile(Profile profile) {
return FAILED; return FAILED;
} }
// TODO
@Override @Override
public boolean isThisProfileSet(Profile profile) { public boolean isThisProfileSet(Profile profile) {
return false; return false;
} }
// TODO
@Override @Override
public Date lastDataTime() { public Date lastDataTime() {
return new Date(); return lastCmdTime;
} }
// TODO
@Override @Override
public void refreshDataFromPump(String reason) { public void refreshDataFromPump(String reason) {
// this is called regulary from keepalive // this is called regulary from keepalive
// TODO how often is this called? use this to run checks regularly, e.g.
// recheck active TBR, basal rate to ensure nothing broke?
} }
// TODO
@Override @Override
public double getBaseBasalRate() { public double getBaseBasalRate() {
return 0d; // TODO this is simple to read, w/o causing vibirations, it's BASAL_RATE in the main menu
return fakeBasalRate;
} }
@Override @Override
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
return null; Command command = new BolusCommand(detailedBolusInfo.insulin);
CommandResult commandResult = ruffyScripter.runCommand(command);
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = commandResult.success;
pumpEnactResult.enacted = commandResult.enacted;
pumpEnactResult.comment = commandResult.message;
pumpEnactResult.bolusDelivered = detailedBolusInfo.insulin;
return pumpEnactResult;
} }
@Override @Override
public void stopBolusDelivering() { public void stopBolusDelivering() {
// there's no way to stop the combo once delivery has started
// but before that, we could interrupt the command thread ... pause
// till pump times out or raises an error
} }
// TODO
@Override @Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult(); return OPERATION_NOT_SUPPORTED;
return result;
} }
@Override @Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) { public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult(); // TODO make each cmd return all the data the main screen displays and cache here ?
return result; Command command = new SetTbrCommand(percent, durationInMinutes);
CommandResult commandResult = ruffyScripter.runCommand(command);
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = commandResult.success;
pumpEnactResult.enacted = commandResult.enacted;
pumpEnactResult.comment = commandResult.message;
pumpEnactResult.isPercent = true;
pumpEnactResult.percent = percent;
fakeBasalRate = fakeBasalRate * percent / 100;
return pumpEnactResult;
} }
// TODO
@Override @Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult(); return OPERATION_NOT_SUPPORTED;
return result;
} }
// TODO
@Override @Override
public PumpEnactResult cancelTempBasal() { public PumpEnactResult cancelTempBasal() {
PumpEnactResult result = new PumpEnactResult(); Command command = new CancelTbrCommand();
return result; CommandResult commandResult = ruffyScripter.runCommand(command);
PumpEnactResult pumpEnactResult = new PumpEnactResult();
pumpEnactResult.success = commandResult.success;
pumpEnactResult.enacted = commandResult.enacted;
pumpEnactResult.comment = commandResult.message;
pumpEnactResult.isTempCancel = true;
fakeBasalRate = 0.5d;
return pumpEnactResult;
} }
// TODO
@Override @Override
public PumpEnactResult cancelExtendedBolus() { public PumpEnactResult cancelExtendedBolus() {
PumpEnactResult result = new PumpEnactResult(); return OPERATION_NOT_SUPPORTED;
return result;
} }
// TODO
// cache as much as possible - every time we interact with the pump it vibrates at the end
@Override @Override
public JSONObject getJSONStatus() { public JSONObject getJSONStatus() {
JSONObject pump = new JSONObject(); JSONObject pump = new JSONObject();
@ -219,6 +337,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
return pump; return pump;
} }
// TODO
@Override @Override
public String deviceID() { public String deviceID() {
// Serial number here // Serial number here
@ -240,6 +359,11 @@ public class ComboPlugin implements PluginBase, PumpInterface {
return false; return false;
} }
@SuppressWarnings("UnusedParameters")
@Subscribe
public void onStatusEvent(final EventAppExit e) {
MainApp.instance().getApplicationContext().unbindService(mRuffyServiceConnection);
}
} }