fix #1145 non-consistent singleton-pattern in DanaRPump

This commit is contained in:
Milos Kozak 2018-08-03 23:35:24 +02:00
parent 294253797c
commit 9a2df7b04c
6 changed files with 112 additions and 119 deletions

View file

@ -48,7 +48,6 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
protected AbstractDanaRExecutionService sExecutionService; protected AbstractDanaRExecutionService sExecutionService;
protected DanaRPump pump = DanaRPump.getInstance();
protected boolean useExtendedBoluses = false; protected boolean useExtendedBoluses = false;
public PumpDescription pumpDescription = new PumpDescription(); public PumpDescription pumpDescription = new PumpDescription();
@ -76,7 +75,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
@Override @Override
public boolean isSuspended() { public boolean isSuspended() {
return pump.pumpSuspended; return DanaRPump.getInstance().pumpSuspended;
} }
@Override @Override
@ -125,6 +124,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
public boolean isThisProfileSet(Profile profile) { public boolean isThisProfileSet(Profile profile) {
if (!isInitialized()) if (!isInitialized())
return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS
DanaRPump pump = DanaRPump.getInstance();
if (pump.pumpProfiles == null) if (pump.pumpProfiles == null)
return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS
int basalValues = pump.basal48Enable ? 48 : 24; int basalValues = pump.basal48Enable ? 48 : 24;
@ -144,12 +144,12 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
@Override @Override
public Date lastDataTime() { public Date lastDataTime() {
return new Date(pump.lastConnection); return new Date(DanaRPump.getInstance().lastConnection);
} }
@Override @Override
public double getBaseBasalRate() { public double getBaseBasalRate() {
return pump.currentBasal; return DanaRPump.getInstance().currentBasal;
} }
@Override @Override
@ -163,6 +163,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
@Override @Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) {
DanaRPump pump = DanaRPump.getInstance();
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value(); percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value();
if (percent < 0) { if (percent < 0) {
@ -212,6 +213,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
@Override @Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
DanaRPump pump = DanaRPump.getInstance();
insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value();
// needs to be rounded // needs to be rounded
int durationInHalfHours = Math.max(durationInMinutes / 30, 1); int durationInHalfHours = Math.max(durationInMinutes / 30, 1);
@ -262,7 +264,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
result.enacted = true; result.enacted = true;
result.isTempCancel = true; result.isTempCancel = true;
} }
if (!pump.isExtendedInProgress) { if (!DanaRPump.getInstance().isExtendedInProgress) {
result.success = true; result.success = true;
result.comment = MainApp.gs(R.string.virtualpump_resultok); result.comment = MainApp.gs(R.string.virtualpump_resultok);
if (L.isEnabled(L.PUMP)) if (L.isEnabled(L.PUMP))
@ -280,8 +282,8 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
public void connect(String from) { public void connect(String from) {
if (sExecutionService != null) { if (sExecutionService != null) {
sExecutionService.connect(); sExecutionService.connect();
pumpDescription.basalStep = pump.basalStep; pumpDescription.basalStep = DanaRPump.getInstance().basalStep;
pumpDescription.bolusStep = pump.bolusStep; pumpDescription.bolusStep = DanaRPump.getInstance().bolusStep;
} }
} }
@ -309,13 +311,14 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
public void getPumpStatus() { public void getPumpStatus() {
if (sExecutionService != null) { if (sExecutionService != null) {
sExecutionService.getPumpStatus(); sExecutionService.getPumpStatus();
pumpDescription.basalStep = pump.basalStep; pumpDescription.basalStep = DanaRPump.getInstance().basalStep;
pumpDescription.bolusStep = pump.bolusStep; pumpDescription.bolusStep = DanaRPump.getInstance().bolusStep;
} }
} }
@Override @Override
public JSONObject getJSONStatus(Profile profile, String profilename) { public JSONObject getJSONStatus(Profile profile, String profilename) {
DanaRPump pump = DanaRPump.getInstance();
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (pump.lastConnection + 5 * 60 * 1000L < System.currentTimeMillis()) { if (pump.lastConnection + 5 * 60 * 1000L < System.currentTimeMillis()) {
return null; return null;
@ -365,7 +368,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
@Override @Override
public String deviceID() { public String deviceID() {
return pump.serialNumber; return DanaRPump.getInstance().serialNumber;
} }
@Override @Override
@ -388,8 +391,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
@Override @Override
public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) { public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {
if (pump != null) absoluteRate.setIfSmaller(DanaRPump.getInstance().maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), DanaRPump.getInstance().maxBasal, MainApp.gs(R.string.pumplimit)), this);
absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit)), this);
return absoluteRate; return absoluteRate;
} }
@ -403,27 +405,26 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
@Override @Override
public Constraint<Double> applyBolusConstraints(Constraint<Double> insulin) { public Constraint<Double> applyBolusConstraints(Constraint<Double> insulin) {
if (pump != null) insulin.setIfSmaller(DanaRPump.getInstance().maxBolus, String.format(MainApp.gs(R.string.limitingbolus), DanaRPump.getInstance().maxBolus, MainApp.gs(R.string.pumplimit)), this);
insulin.setIfSmaller(pump.maxBolus, String.format(MainApp.gs(R.string.limitingbolus), pump.maxBolus, MainApp.gs(R.string.pumplimit)), this);
return insulin; return insulin;
} }
@Nullable @Nullable
@Override @Override
public ProfileStore getProfile() { public ProfileStore getProfile() {
if (pump.lastSettingsRead == 0) if (DanaRPump.getInstance().lastSettingsRead == 0)
return null; // no info now return null; // no info now
return pump.createConvertedProfile(); return DanaRPump.getInstance().createConvertedProfile();
} }
@Override @Override
public String getUnits() { public String getUnits() {
return pump.getUnits(); return DanaRPump.getInstance().getUnits();
} }
@Override @Override
public String getProfileName() { public String getProfileName() {
return pump.createConvertedProfileName(); return DanaRPump.getInstance().createConvertedProfileName();
} }
@Override @Override
@ -433,6 +434,7 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
// Reply for sms communicator // Reply for sms communicator
public String shortStatus(boolean veryShort) { public String shortStatus(boolean veryShort) {
DanaRPump pump = DanaRPump.getInstance();
String ret = ""; String ret = "";
if (pump.lastConnection != 0) { if (pump.lastConnection != 0) {
Long agoMsec = System.currentTimeMillis() - pump.lastConnection; Long agoMsec = System.currentTimeMillis() - pump.lastConnection;

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.PumpDanaR;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
@ -18,16 +17,16 @@ import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgBolusStartWithSpeed;
import info.nightscout.androidaps.plugins.Treatments.Treatment;
import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgBolusStartWithSpeed;
import info.nightscout.androidaps.plugins.PumpDanaR.services.DanaRExecutionService; import info.nightscout.androidaps.plugins.PumpDanaR.services.DanaRExecutionService;
import info.nightscout.androidaps.plugins.Treatments.Treatment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.Round; import info.nightscout.utils.Round;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
@ -47,7 +46,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
public DanaRPlugin() { public DanaRPlugin() {
super(); super();
useExtendedBoluses = SP.getBoolean("danar_useextended", false); useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false);
pumpDescription.isBolusCapable = true; pumpDescription.isBolusCapable = true;
pumpDescription.bolusStep = 0.05d; pumpDescription.bolusStep = 0.05d;
@ -87,20 +86,16 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.allow_hardware_pump_text) builder.setMessage(R.string.allow_hardware_pump_text)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.yes, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.invoke();
pluginSwitcher.invoke(); SP.putBoolean("allow_hardware_pump", true);
SP.putBoolean("allow_hardware_pump", true); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("First time HW pump allowed!");
log.debug("First time HW pump allowed!");
}
}) })
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.cancel, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.cancel();
pluginSwitcher.cancel(); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("User does not allow switching to HW pump!");
log.debug("User does not allow switching to HW pump!");
}
}); });
builder.create().show(); builder.create().show();
} }
@ -149,7 +144,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
public void onStatusEvent(final EventPreferenceChange s) { public void onStatusEvent(final EventPreferenceChange s) {
if (isEnabled(PluginType.PUMP)) { if (isEnabled(PluginType.PUMP)) {
boolean previousValue = useExtendedBoluses; boolean previousValue = useExtendedBoluses;
useExtendedBoluses = SP.getBoolean("danar_useextended", false); useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false);
if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) { if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) {
sExecutionService.extendedBolusStop(); sExecutionService.extendedBolusStop();
@ -176,6 +171,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
DanaRPump pump = DanaRPump.getInstance();
return pump.lastConnection > 0 && pump.isExtendedBolusEnabled && pump.maxBasal > 0; return pump.lastConnection > 0 && pump.isExtendedBolusEnabled && pump.maxBasal > 0;
} }
@ -221,6 +217,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
//if (pump.lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis()) { //if (pump.lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis()) {
// connect("setTempBasalAbsolute old data"); // connect("setTempBasalAbsolute old data");
//} //}
DanaRPump pump = DanaRPump.getInstance();
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
@ -381,7 +378,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
return result; return result;
} }
public PumpEnactResult cancelRealTempBasal() { private PumpEnactResult cancelRealTempBasal() {
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
TemporaryBasal runningTB = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis()); TemporaryBasal runningTB = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis());
if (runningTB != null) { if (runningTB != null) {
@ -389,7 +386,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
result.enacted = true; result.enacted = true;
result.isTempCancel = true; result.isTempCancel = true;
} }
if (!pump.isTempBasalInProgress) { if (!DanaRPump.getInstance().isTempBasalInProgress) {
result.success = true; result.success = true;
result.isTempCancel = true; result.isTempCancel = true;
result.comment = MainApp.gs(R.string.virtualpump_resultok); result.comment = MainApp.gs(R.string.virtualpump_resultok);

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.PumpDanaRKorean;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
@ -26,6 +25,7 @@ import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgBolusStart; import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgBolusStart;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.services.DanaRKoreanExecutionService; import info.nightscout.androidaps.plugins.PumpDanaRKorean.services.DanaRKoreanExecutionService;
import info.nightscout.androidaps.plugins.Treatments.Treatment; import info.nightscout.androidaps.plugins.Treatments.Treatment;
@ -49,7 +49,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
public DanaRKoreanPlugin() { public DanaRKoreanPlugin() {
pluginDescription.description(R.string.description_pump_dana_r_korean); pluginDescription.description(R.string.description_pump_dana_r_korean);
useExtendedBoluses = SP.getBoolean("danar_useextended", false); useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false);
pumpDescription.isBolusCapable = true; pumpDescription.isBolusCapable = true;
pumpDescription.bolusStep = 0.1d; pumpDescription.bolusStep = 0.1d;
@ -89,20 +89,16 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.allow_hardware_pump_text) builder.setMessage(R.string.allow_hardware_pump_text)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.yes, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.invoke();
pluginSwitcher.invoke(); SP.putBoolean("allow_hardware_pump", true);
SP.putBoolean("allow_hardware_pump", true); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("First time HW pump allowed!");
log.debug("First time HW pump allowed!");
}
}) })
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.cancel, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.cancel();
pluginSwitcher.cancel(); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("User does not allow switching to HW pump!");
log.debug("User does not allow switching to HW pump!");
}
}); });
builder.create().show(); builder.create().show();
} }
@ -152,7 +148,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
public void onStatusEvent(final EventPreferenceChange s) { public void onStatusEvent(final EventPreferenceChange s) {
if (isEnabled(PluginType.PUMP)) { if (isEnabled(PluginType.PUMP)) {
boolean previousValue = useExtendedBoluses; boolean previousValue = useExtendedBoluses;
useExtendedBoluses = SP.getBoolean("danar_useextended", false); useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false);
if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) { if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) {
sExecutionService.extendedBolusStop(); sExecutionService.extendedBolusStop();
@ -179,6 +175,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
DanaRPump pump = DanaRPump.getInstance();
return pump.lastConnection > 0 && pump.maxBasal > 0 && !pump.isConfigUD && !pump.isEasyModeEnabled && pump.isExtendedBolusEnabled; return pump.lastConnection > 0 && pump.maxBasal > 0 && !pump.isConfigUD && !pump.isEasyModeEnabled && pump.isExtendedBolusEnabled;
} }
@ -224,6 +221,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
//if (pump.lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis()) { //if (pump.lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis()) {
// connect("setTempBasalAbsolute old data"); // connect("setTempBasalAbsolute old data");
//} //}
DanaRPump pump = DanaRPump.getInstance();
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
@ -384,7 +382,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
return result; return result;
} }
public PumpEnactResult cancelRealTempBasal() { private PumpEnactResult cancelRealTempBasal() {
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
TemporaryBasal runningTB = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis()); TemporaryBasal runningTB = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis());
if (runningTB != null) { if (runningTB != null) {
@ -392,7 +390,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
result.enacted = true; result.enacted = true;
result.isTempCancel = true; result.isTempCancel = true;
} }
if (!pump.isTempBasalInProgress) { if (!DanaRPump.getInstance().isTempBasalInProgress) {
result.success = true; result.success = true;
result.isTempCancel = true; result.isTempCancel = true;
result.comment = MainApp.gs(R.string.virtualpump_resultok); result.comment = MainApp.gs(R.string.virtualpump_resultok);

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.PumpDanaRS;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
@ -74,15 +73,14 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
return plugin; return plugin;
} }
public static DanaRSService danaRSService; private static DanaRSService danaRSService;
public static String mDeviceAddress = ""; private static String mDeviceAddress = "";
public static String mDeviceName = ""; public static String mDeviceName = "";
private static DanaRPump pump = DanaRPump.getInstance();
public static PumpDescription pumpDescription = new PumpDescription(); public static PumpDescription pumpDescription = new PumpDescription();
DanaRSPlugin() { private DanaRSPlugin() {
super(new PluginDescription() super(new PluginDescription()
.mainType(PluginType.PUMP) .mainType(PluginType.PUMP)
.fragmentClass(DanaRFragment.class.getName()) .fragmentClass(DanaRFragment.class.getName())
@ -161,20 +159,16 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.allow_hardware_pump_text) builder.setMessage(R.string.allow_hardware_pump_text)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.yes, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.invoke();
pluginSwitcher.invoke(); SP.putBoolean("allow_hardware_pump", true);
SP.putBoolean("allow_hardware_pump", true); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("First time HW pump allowed!");
log.debug("First time HW pump allowed!");
}
}) })
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.cancel, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.cancel();
pluginSwitcher.cancel(); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("User does not allow switching to HW pump!");
log.debug("User does not allow switching to HW pump!");
}
}); });
builder.create().show(); builder.create().show();
} }
@ -245,8 +239,8 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
public void getPumpStatus() { public void getPumpStatus() {
if (danaRSService != null) { if (danaRSService != null) {
danaRSService.getPumpStatus(); danaRSService.getPumpStatus();
pumpDescription.basalStep = pump.basalStep; pumpDescription.basalStep = DanaRPump.getInstance().basalStep;
pumpDescription.bolusStep = pump.bolusStep; pumpDescription.bolusStep = DanaRPump.getInstance().bolusStep;
} }
} }
@ -271,8 +265,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) { public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {
if (pump != null) absoluteRate.setIfSmaller(DanaRPump.getInstance().maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), DanaRPump.getInstance().maxBasal, MainApp.gs(R.string.pumplimit)), this);
absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit)), this);
return absoluteRate; return absoluteRate;
} }
@ -287,8 +280,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public Constraint<Double> applyBolusConstraints(Constraint<Double> insulin) { public Constraint<Double> applyBolusConstraints(Constraint<Double> insulin) {
if (pump != null) insulin.setIfSmaller(DanaRPump.getInstance().maxBolus, String.format(MainApp.gs(R.string.limitingbolus), DanaRPump.getInstance().maxBolus, MainApp.gs(R.string.pumplimit)), this);
insulin.setIfSmaller(pump.maxBolus, String.format(MainApp.gs(R.string.limitingbolus), pump.maxBolus, MainApp.gs(R.string.pumplimit)), this);
return insulin; return insulin;
} }
@ -297,31 +289,31 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Nullable @Nullable
@Override @Override
public ProfileStore getProfile() { public ProfileStore getProfile() {
if (pump.lastSettingsRead == 0) if (DanaRPump.getInstance().lastSettingsRead == 0)
return null; // no info now return null; // no info now
return pump.createConvertedProfile(); return DanaRPump.getInstance().createConvertedProfile();
} }
@Override @Override
public String getUnits() { public String getUnits() {
return pump.getUnits(); return DanaRPump.getInstance().getUnits();
} }
@Override @Override
public String getProfileName() { public String getProfileName() {
return pump.createConvertedProfileName(); return DanaRPump.getInstance().createConvertedProfileName();
} }
// Pump interface // Pump interface
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
return pump.lastConnection > 0 && pump.maxBasal > 0; return DanaRPump.getInstance().lastConnection > 0 && DanaRPump.getInstance().maxBasal > 0;
} }
@Override @Override
public boolean isSuspended() { public boolean isSuspended() {
return pump.pumpSuspended; return DanaRPump.getInstance().pumpSuspended;
} }
@Override @Override
@ -369,6 +361,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
public boolean isThisProfileSet(Profile profile) { public boolean isThisProfileSet(Profile profile) {
if (!isInitialized()) if (!isInitialized())
return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS
DanaRPump pump = DanaRPump.getInstance();
if (pump.pumpProfiles == null) if (pump.pumpProfiles == null)
return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS
int basalValues = pump.basal48Enable ? 48 : 24; int basalValues = pump.basal48Enable ? 48 : 24;
@ -388,12 +381,12 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public Date lastDataTime() { public Date lastDataTime() {
return new Date(pump.lastConnection); return new Date(DanaRPump.getInstance().lastConnection);
} }
@Override @Override
public double getBaseBasalRate() { public double getBaseBasalRate() {
return pump.currentBasal; return DanaRPump.getInstance().currentBasal;
} }
@Override @Override
@ -549,6 +542,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public synchronized PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { public synchronized PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) {
DanaRPump pump = DanaRPump.getInstance();
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value(); percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value();
if (percent < 0) { if (percent < 0) {
@ -601,7 +595,8 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
return result; return result;
} }
public synchronized PumpEnactResult setHighTempBasalPercent(Integer percent) { private synchronized PumpEnactResult setHighTempBasalPercent(Integer percent) {
DanaRPump pump = DanaRPump.getInstance();
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
boolean connectionOK = danaRSService.highTempBasal(percent); boolean connectionOK = danaRSService.highTempBasal(percent);
if (connectionOK && pump.isTempBasalInProgress && pump.tempBasalPercent == percent) { if (connectionOK && pump.isTempBasalInProgress && pump.tempBasalPercent == percent) {
@ -625,6 +620,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public synchronized PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { public synchronized PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
DanaRPump pump = DanaRPump.getInstance();
insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value();
// needs to be rounded // needs to be rounded
int durationInHalfHours = Math.max(durationInMinutes / 30, 1); int durationInHalfHours = Math.max(durationInMinutes / 30, 1);
@ -673,7 +669,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
result.enacted = true; result.enacted = true;
result.isTempCancel = true; result.isTempCancel = true;
} }
if (!pump.isTempBasalInProgress) { if (!DanaRPump.getInstance().isTempBasalInProgress) {
result.success = true; result.success = true;
result.isTempCancel = true; result.isTempCancel = true;
result.comment = MainApp.gs(R.string.virtualpump_resultok); result.comment = MainApp.gs(R.string.virtualpump_resultok);
@ -698,7 +694,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
result.enacted = true; result.enacted = true;
result.isTempCancel = true; result.isTempCancel = true;
} }
if (!pump.isExtendedInProgress) { if (!DanaRPump.getInstance().isExtendedInProgress) {
result.success = true; result.success = true;
result.comment = MainApp.gs(R.string.virtualpump_resultok); result.comment = MainApp.gs(R.string.virtualpump_resultok);
if (L.isEnabled(L.PUMP)) if (L.isEnabled(L.PUMP))
@ -714,6 +710,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public JSONObject getJSONStatus(Profile profile, String profileName) { public JSONObject getJSONStatus(Profile profile, String profileName) {
DanaRPump pump = DanaRPump.getInstance();
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (pump.lastConnection + 5 * 60 * 1000L < System.currentTimeMillis()) { if (pump.lastConnection + 5 * 60 * 1000L < System.currentTimeMillis()) {
return null; return null;
@ -763,7 +760,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public String deviceID() { public String deviceID() {
return pump.serialNumber; return DanaRPump.getInstance().serialNumber;
} }
@Override @Override
@ -773,6 +770,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
@Override @Override
public String shortStatus(boolean veryShort) { public String shortStatus(boolean veryShort) {
DanaRPump pump = DanaRPump.getInstance();
String ret = ""; String ret = "";
if (pump.lastConnection != 0) { if (pump.lastConnection != 0) {
Long agoMsec = System.currentTimeMillis() - pump.lastConnection; Long agoMsec = System.currentTimeMillis() - pump.lastConnection;

View file

@ -90,7 +90,6 @@ public class DanaRSService extends Service {
private IBinder mBinder = new LocalBinder(); private IBinder mBinder = new LocalBinder();
private DanaRPump danaRPump = DanaRPump.getInstance();
private Treatment bolusingTreatment = null; private Treatment bolusingTreatment = null;
private long lastHistoryFetched = 0; private long lastHistoryFetched = 0;
@ -130,6 +129,7 @@ public class DanaRSService extends Service {
} }
public void getPumpStatus() { public void getPumpStatus() {
DanaRPump danaRPump = DanaRPump.getInstance();
try { try {
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus)));
@ -253,7 +253,7 @@ public class DanaRSService extends Service {
lastHistoryFetched = 0; lastHistoryFetched = 0;
if (L.isEnabled(L.PUMPCOMM)) if (L.isEnabled(L.PUMPCOMM))
log.debug("Events loaded"); log.debug("Events loaded");
danaRPump.lastConnection = System.currentTimeMillis(); DanaRPump.getInstance().lastConnection = System.currentTimeMillis();
return new PumpEnactResult().success(true); return new PumpEnactResult().success(true);
} }
@ -362,7 +362,7 @@ public class DanaRSService extends Service {
public boolean tempBasal(Integer percent, int durationInHours) { public boolean tempBasal(Integer percent, int durationInHours) {
if (!isConnected()) return false; if (!isConnected()) return false;
if (danaRPump.isTempBasalInProgress) { if (DanaRPump.getInstance().isTempBasalInProgress) {
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal)));
bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal()); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal());
SystemClock.sleep(500); SystemClock.sleep(500);
@ -377,7 +377,7 @@ public class DanaRSService extends Service {
} }
public boolean highTempBasal(Integer percent) { public boolean highTempBasal(Integer percent) {
if (danaRPump.isTempBasalInProgress) { if (DanaRPump.getInstance().isTempBasalInProgress) {
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal)));
bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal()); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal());
SystemClock.sleep(500); SystemClock.sleep(500);
@ -396,7 +396,7 @@ public class DanaRSService extends Service {
return false; return false;
} }
if (danaRPump.isTempBasalInProgress) { if (DanaRPump.getInstance().isTempBasalInProgress) {
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal)));
bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal()); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal());
SystemClock.sleep(500); SystemClock.sleep(500);
@ -448,7 +448,7 @@ public class DanaRSService extends Service {
bleComm.sendMessage(msgSet); bleComm.sendMessage(msgSet);
DanaRS_Packet_Basal_Set_Profile_Number msgActivate = new DanaRS_Packet_Basal_Set_Profile_Number(0); DanaRS_Packet_Basal_Set_Profile_Number msgActivate = new DanaRS_Packet_Basal_Set_Profile_Number(0);
bleComm.sendMessage(msgActivate); bleComm.sendMessage(msgActivate);
danaRPump.lastSettingsRead = 0; // force read full settings DanaRPump.getInstance().lastSettingsRead = 0; // force read full settings
getPumpStatus(); getPumpStatus();
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING));
return true; return true;

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.PumpDanaRv2;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
@ -24,6 +23,7 @@ import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgBolusStartWithSpeed; import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgBolusStartWithSpeed;
import info.nightscout.androidaps.plugins.PumpDanaRv2.services.DanaRv2ExecutionService; import info.nightscout.androidaps.plugins.PumpDanaRv2.services.DanaRv2ExecutionService;
import info.nightscout.androidaps.plugins.Treatments.Treatment; import info.nightscout.androidaps.plugins.Treatments.Treatment;
@ -141,31 +141,27 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
return pump.lastConnection > 0 && pump.maxBasal > 0; return DanaRPump.getInstance().lastConnection > 0 && DanaRPump.getInstance().maxBasal > 0;
} }
@Override @Override
public void switchAllowed(ConfigBuilderFragment.PluginViewHolder.PluginSwitcher pluginSwitcher, FragmentActivity context) { public void switchAllowed(ConfigBuilderFragment.PluginViewHolder.PluginSwitcher pluginSwitcher, FragmentActivity context) {
boolean allowHardwarePump = SP.getBoolean("allow_hardware_pump", false); boolean allowHardwarePump = SP.getBoolean("allow_hardware_pump", false);
if (allowHardwarePump || context == null){ if (allowHardwarePump || context == null) {
pluginSwitcher.invoke(); pluginSwitcher.invoke();
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.allow_hardware_pump_text) builder.setMessage(R.string.allow_hardware_pump_text)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.yes, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.invoke();
pluginSwitcher.invoke(); SP.putBoolean("allow_hardware_pump", true);
SP.putBoolean("allow_hardware_pump", true); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("First time HW pump allowed!");
log.debug("First time HW pump allowed!");
}
}) })
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.cancel, (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { pluginSwitcher.cancel();
pluginSwitcher.cancel(); if (L.isEnabled(L.PUMP))
if (L.isEnabled(L.PUMP)) log.debug("User does not allow switching to HW pump!");
log.debug("User does not allow switching to HW pump!");
}
}); });
builder.create().show(); builder.create().show();
} }
@ -191,7 +187,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
speed = 60; speed = 60;
break; break;
} }
detailedBolusInfo.date = DateUtil.now() + (long)(speed * detailedBolusInfo.insulin * 1000); detailedBolusInfo.date = DateUtil.now() + (long) (speed * detailedBolusInfo.insulin * 1000);
// clean carbs to prevent counting them as twice because they will picked up as another record // clean carbs to prevent counting them as twice because they will picked up as another record
// I don't think it's necessary to copy DetailedBolusInfo right now for carbs records // I don't think it's necessary to copy DetailedBolusInfo right now for carbs records
double carbs = detailedBolusInfo.carbs; double carbs = detailedBolusInfo.carbs;
@ -323,6 +319,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
@Override @Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) {
DanaRPump pump = DanaRPump.getInstance();
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value(); percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value();
if (percent < 0) { if (percent < 0) {
@ -375,7 +372,8 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
return result; return result;
} }
public PumpEnactResult setHighTempBasalPercent(Integer percent) { private PumpEnactResult setHighTempBasalPercent(Integer percent) {
DanaRPump pump = DanaRPump.getInstance();
PumpEnactResult result = new PumpEnactResult(); PumpEnactResult result = new PumpEnactResult();
boolean connectionOK = sExecutionService.highTempBasal(percent); boolean connectionOK = sExecutionService.highTempBasal(percent);
if (connectionOK && pump.isTempBasalInProgress && pump.tempBasalPercent == percent) { if (connectionOK && pump.isTempBasalInProgress && pump.tempBasalPercent == percent) {
@ -406,7 +404,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
result.enacted = true; result.enacted = true;
result.isTempCancel = true; result.isTempCancel = true;
} }
if (!pump.isTempBasalInProgress) { if (!DanaRPump.getInstance().isTempBasalInProgress) {
result.success = true; result.success = true;
result.isTempCancel = true; result.isTempCancel = true;
result.comment = MainApp.gs(R.string.virtualpump_resultok); result.comment = MainApp.gs(R.string.virtualpump_resultok);