Little bit of refactoring.

This commit is contained in:
Andy Rozman 2018-05-12 14:29:02 +01:00
parent f0e51c72a6
commit 7a388a5715
7 changed files with 152 additions and 152 deletions

View file

@ -1,28 +1,32 @@
package info.nightscout.androidaps.plugins.PumpCommon;
import android.support.annotation.Nullable;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.PumpCombo.ComboFragment;
import info.nightscout.androidaps.plugins.PumpCommon.data.PumpStatus;
import info.nightscout.androidaps.plugins.PumpCommon.driver.PumpDriverInterface;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.DateUtil;
/**
* Created by andy on 23.04.18.
@ -30,26 +34,21 @@ import info.nightscout.androidaps.plugins.PumpCommon.driver.PumpDriverInterface;
public abstract class PumpPluginAbstract extends PluginBase implements PumpInterface, ConstraintsInterface {
// , ProfileInterface
//protected boolean fragmentVisible = false;
//protected boolean fragmentEnabled = false;
private static final Logger LOG = LoggerFactory.getLogger(PumpPluginAbstract.class);
protected boolean pumpServiceRunning = false;
//private static final String TAG = "PumpPluginAbstract";
//protected PumpStatus pumpStatus;
protected static PumpPluginAbstract plugin = null;
protected PumpDriverInterface pumpDriver;
protected PumpStatus pumpStatus;
protected String internalName;
protected PumpPluginAbstract(PumpDriverInterface pumpDriverInterface, //
String internalName, //
String fragmentClassName, //
int pluginName, //
int pluginShortName)
{
int pluginShortName) {
super(new PluginDescription()
.mainType(PluginType.PUMP)
.fragmentClass(fragmentClassName)
@ -59,130 +58,98 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter
this.pumpDriver = pumpDriverInterface;
this.pumpStatus = this.pumpDriver.getPumpStatusData();
this.internalName = internalName;
}
// @Override
// public boolean isVisibleInTabs(int type) {
// return type == PUMP && fragmentVisible;
// }
//
//
// @Override
// public boolean canBeHidden(int type) {
// return true;
// }
//
//
//
// @Override
// public boolean showInList(int type) {
// return type == PUMP;
// }
// @Override
// public void setFragmentVisible(int type, boolean fragmentVisible) {
// if (type == PUMP)
// this.fragmentVisible = fragmentVisible;
// }
protected abstract String getInternalName();
protected String getInternalName() {
return this.internalName;
}
protected abstract void startPumpService();
protected abstract void stopPumpService();
public PumpStatus getPumpStatusData()
{
public PumpStatus getPumpStatusData() {
return pumpDriver.getPumpStatusData();
}
public boolean isInitialized()
{
public boolean isInitialized() {
return pumpDriver.isInitialized();
}
public boolean isSuspended(){
public boolean isSuspended() {
return pumpDriver.isSuspended();
}
public boolean isBusy(){
public boolean isBusy() {
return pumpDriver.isBusy();
}
public boolean isConnected(){
public boolean isConnected() {
return pumpDriver.isConnected();
}
public boolean isConnecting(){
public boolean isConnecting() {
return pumpDriver.isConnecting();
}
public void connect(String reason){
public void connect(String reason) {
pumpDriver.connect(reason);
}
public void disconnect(String reason){
public void disconnect(String reason) {
pumpDriver.disconnect(reason);
}
public void stopConnecting(){
public void stopConnecting() {
pumpDriver.stopConnecting();
}
public void getPumpStatus(){
public void getPumpStatus() {
pumpDriver.getPumpStatus();
}
// Upload to pump new basal profile
public PumpEnactResult setNewBasalProfile(Profile profile){
public PumpEnactResult setNewBasalProfile(Profile profile) {
return pumpDriver.setNewBasalProfile(profile);
}
public boolean isThisProfileSet(Profile profile){
public boolean isThisProfileSet(Profile profile) {
return pumpDriver.isThisProfileSet(profile);
}
public Date lastDataTime(){
public Date lastDataTime() {
return pumpDriver.lastDataTime();
}
public double getBaseBasalRate(){
public double getBaseBasalRate() {
return pumpDriver.getBaseBasalRate();
} // base basal rate, not temp basal
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo){
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
return pumpDriver.deliverTreatment(detailedBolusInfo);
}
public void stopBolusDelivering(){
public void stopBolusDelivering() {
pumpDriver.stopBolusDelivering();
}
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) {
return pumpDriver.setTempBasalAbsolute(absoluteRate, durationInMinutes, profile, enforceNew);
@ -194,55 +161,59 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter
}
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes){
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
return pumpDriver.setExtendedBolus(insulin, durationInMinutes);
}
//some pumps might set a very short temp close to 100% as cancelling a temp can be noisy
//when the cancel request is requested by the user (forced), the pump should always do a real cancel
public PumpEnactResult cancelTempBasal(boolean enforceNew){
public PumpEnactResult cancelTempBasal(boolean enforceNew) {
return pumpDriver.cancelTempBasal(enforceNew);
}
public PumpEnactResult cancelExtendedBolus(){
public PumpEnactResult cancelExtendedBolus() {
return pumpDriver.cancelExtendedBolus();
}
// Status to be passed to NS
public JSONObject getJSONStatus(Profile profile, String profileName){
return pumpDriver.getJSONStatus( profile, profileName);
}
// public JSONObject getJSONStatus(Profile profile, String profileName) {
// return pumpDriver.getJSONStatus(profile, profileName);
// }
public String deviceID(){
public String deviceID() {
return pumpDriver.deviceID();
}
// Pump capabilities
public PumpDescription getPumpDescription(){
public PumpDescription getPumpDescription() {
return pumpDriver.getPumpDescription();
}
// Short info for SMS, Wear etc
public String shortStatus(boolean veryShort){
public String shortStatus(boolean veryShort) {
return pumpDriver.shortStatus(veryShort);
}
public boolean isFakingTempsByExtendedBoluses(){
public boolean isFakingTempsByExtendedBoluses() {
return pumpDriver.isInitialized();
}
@Override
public PumpEnactResult loadTDDs() {
return this.pumpDriver.loadTDDs();
}
// Constraints interface
// @Override
@ -300,28 +271,73 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter
// return maxIob;
// }
@Override
public JSONObject getJSONStatus(Profile profile, String profileName) {
//if (!SP.getBoolean("virtualpump_uploadstatus", false)) {
// return null;
//}
long now = System.currentTimeMillis();
if ((pumpStatus.lastConnection + 5 * 60 * 1000L) < System.currentTimeMillis()) {
return null;
}
JSONObject pump = new JSONObject();
JSONObject battery = new JSONObject();
JSONObject status = new JSONObject();
JSONObject extended = new JSONObject();
try {
battery.put("percent", pumpStatus.batteryRemaining);
status.put("status", pumpStatus.pumpStatusType != null ? pumpStatus.pumpStatusType.getStatus() : "normal");
extended.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION);
try {
extended.put("ActiveProfile", MainApp.getConfigBuilder().getProfileName());
} catch (Exception e) {
}
TemporaryBasal tb = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis());
if (tb != null) {
extended.put("TempBasalAbsoluteRate", tb.tempBasalConvertedToAbsolute(System.currentTimeMillis(), profile));
extended.put("TempBasalStart", DateUtil.dateAndTimeString(tb.date));
extended.put("TempBasalRemaining", tb.getPlannedRemainingMinutes());
}
ExtendedBolus eb = TreatmentsPlugin.getPlugin().getExtendedBolusFromHistory(System.currentTimeMillis());
if (eb != null) {
extended.put("ExtendedBolusAbsoluteRate", eb.absoluteRate());
extended.put("ExtendedBolusStart", DateUtil.dateAndTimeString(eb.date));
extended.put("ExtendedBolusRemaining", eb.getPlannedRemainingMinutes());
}
status.put("timestamp", DateUtil.toISOString(new Date()));
pump.put("battery", battery);
pump.put("status", status);
pump.put("extended", extended);
pump.put("reservoir", pumpStatus.reservoirRemainingUnits);
pump.put("clock", DateUtil.toISOString(new Date()));
} catch (JSONException e) {
LOG.error("Unhandled exception", e);
}
return pump;
}
// Profile interface
@Nullable
public ProfileStore getProfile()
{
public ProfileStore getProfile() {
return this.pumpStatus.profileStore;
}
public String getUnits()
{
public String getUnits() {
return this.pumpStatus.units;
}
public String getProfileName()
{
public String getProfileName() {
return this.pumpStatus.activeProfileName;
}
@Override
public PumpEnactResult loadTDDs() {
return this.pumpDriver.loadTDDs();
}
}

View file

@ -4,6 +4,7 @@ import java.util.Date;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.plugins.PumpCommon.defs.PumpStatusType;
/**
* Created by andy on 4/28/18.
@ -17,14 +18,16 @@ public abstract class PumpStatus {
public String activeProfileName = "0";
public double reservoirRemainingUnits = 0d;
public String reservoirFullUnits = "???";
public double batteryRemaining = 0d;
public int batteryRemaining = 0; // percent, so 0-100
public String iob = "0";
protected PumpDescription pumpDescription;
public boolean validBasalRateProfileSelectedOnPump = true;
public ProfileStore profileStore;
public String units; // Constants.MGDL or Constants.MMOL
public PumpStatusType pumpStatusType = PumpStatusType.Running;
// TODO maybe not needed anymore in 2.0
public Double constraintBasalRateAbsolute;
public Integer constraintBasalRatePercent;
public Double constraintBolus;
@ -32,10 +35,7 @@ public abstract class PumpStatus {
public Double constraintMaxIob;
public PumpStatus(PumpDescription pumpDescription)
{
public PumpStatus(PumpDescription pumpDescription) {
this.pumpDescription = pumpDescription;
this.initSettings();

View file

@ -0,0 +1,21 @@
package info.nightscout.androidaps.plugins.PumpCommon.defs;
/**
* Created by andy on 5/12/18.
*/
public enum PumpStatusType {
Running("normal"), //
Suspended("suspended") //
;
private String statusString;
PumpStatusType(String statusString) {
this.statusString = statusString;
}
public String getStatus() {
return statusString;
}
}

View file

@ -33,7 +33,6 @@ import info.nightscout.androidaps.plugins.PumpCommon.data.PumpStatus;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaR.Dialogs.ProfileViewDialog;
import info.nightscout.androidaps.plugins.PumpDanaR.activities.DanaRHistoryActivity;
import info.nightscout.androidaps.plugins.PumpDanaR.events.EventDanaRNewStatus;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.events.EventQueueChanged;
@ -81,14 +80,14 @@ public class MedtronicFragment extends SubscriberFragment {
TextView errorsView;
@BindView(R.id.medtronic_queue)
TextView queueView;
@BindView(R.id.overview_pumpstatuslayout)
LinearLayout pumpStatusLayout;
@BindView(R.id.overview_pumpstatus) TextView pumpStatusView;
@BindView(R.id.overview_pumpstatus)
TextView pumpStatusView;
public MedtronicFragment() {
}
@ -204,7 +203,7 @@ public class MedtronicFragment extends SubscriberFragment {
@Override
public void run() {
MedtronicPumpPlugin plugin = (MedtronicPumpPlugin)MedtronicPumpPlugin.getPlugin();
MedtronicPumpPlugin plugin = (MedtronicPumpPlugin) MedtronicPumpPlugin.getPlugin();
PumpStatus pump = plugin.getPumpStatusData();
if (pump.lastConnection != 0) {
@ -213,7 +212,7 @@ public class MedtronicFragment extends SubscriberFragment {
lastConnectionView.setText(DateUtil.timeString(pump.lastConnection) + " (" + String.format(MainApp.sResources.getString(R.string.minago), agoMin) + ")");
SetWarnColor.setColor(lastConnectionView, agoMin, 16d, 31d);
}
if (pump.lastBolusTime!=null && pump.lastBolusTime.getTime() != 0) {
if (pump.lastBolusTime != null && pump.lastBolusTime.getTime() != 0) {
Long agoMsec = System.currentTimeMillis() - pump.lastBolusTime.getTime();
double agoHours = agoMsec / 60d / 60d / 1000d;
if (agoHours < 6) // max 6h back
@ -241,10 +240,6 @@ public class MedtronicFragment extends SubscriberFragment {
}
}
reservoirView.setText(DecimalFormatter.to0Decimal(pump.reservoirRemainingUnits) + " / " + pump.reservoirFullUnits + " U");
SetWarnColor.setColorInverse(reservoirView, pump.reservoirRemainingUnits, 50d, 20d);
batteryView.setText("{fa-battery-" + (pump.batteryRemaining / 25) + "}");

View file

@ -15,7 +15,7 @@ import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.PumpCommon.PumpPluginAbstract;
import info.nightscout.androidaps.plugins.PumpMedtronic.medtronic.MedtronicPumpDriver;
import info.nightscout.androidaps.plugins.PumpMedtronic.driver.MedtronicPumpDriver;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.DateUtil;
@ -48,12 +48,6 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter
}
@Override
protected String getInternalName() {
return "MedtronicPump";
}
@Override
protected void startPumpService() {

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.PumpMedtronic.medtronic;
package info.nightscout.androidaps.plugins.PumpMedtronic.driver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -10,8 +10,6 @@ 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.interfaces.PumpDescription;
import info.nightscout.androidaps.plugins.PumpCommon.defs.PumpType;
import info.nightscout.androidaps.plugins.PumpCommon.utils.PumpUtil;
import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpDriver;
import info.nightscout.androidaps.plugins.PumpVirtual.events.EventVirtualPumpUpdateGui;
@ -26,18 +24,17 @@ public class MedtronicPumpDriver extends VirtualPumpDriver /*implements PumpInte
private static final Logger LOG = LoggerFactory.getLogger(MedtronicPumpDriver.class);
MedtronicPumpStatus pumpStatusLocal;
public MedtronicPumpDriver()
{
public MedtronicPumpDriver() {
pumpStatusLocal = new MedtronicPumpStatus(pumpDescription);
pumpStatusLocal.verifyConfiguration();
this.pumpStatusData = pumpStatusLocal;
if (pumpStatusLocal.pumpType!=null)
if (pumpStatusLocal.pumpType != null)
PumpUtil.setPumpDescription(pumpDescription, pumpStatusLocal.pumpType);
if (pumpStatusLocal.maxBasal!=null)
pumpDescription.maxTempAbsolute = (pumpStatusLocal.maxBasal!=null) ? pumpStatusLocal.maxBasal : 35.0d;
if (pumpStatusLocal.maxBasal != null)
pumpDescription.maxTempAbsolute = (pumpStatusLocal.maxBasal != null) ? pumpStatusLocal.maxBasal : 35.0d;
// needs to be changed in configuration, after all functionalities are done
pumpDescription.isBolusCapable = true;
@ -71,8 +68,7 @@ public class MedtronicPumpDriver extends VirtualPumpDriver /*implements PumpInte
@Override
public PumpEnactResult cancelTempBasal(boolean enforceNew)
{
public PumpEnactResult cancelTempBasal(boolean enforceNew) {
// FIXME
// send Cancel Temp Basal
@ -104,10 +100,4 @@ public class MedtronicPumpDriver extends VirtualPumpDriver /*implements PumpInte
}
}

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.PumpMedtronic.medtronic;
package info.nightscout.androidaps.plugins.PumpMedtronic.driver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -11,7 +11,6 @@ import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.plugins.PumpCommon.data.PumpStatus;
import info.nightscout.androidaps.plugins.PumpCommon.defs.PumpType;
import info.nightscout.androidaps.plugins.PumpMedtronic.medtronic.defs.MedtronicPumpType;
import info.nightscout.utils.SP;
/**
@ -24,14 +23,6 @@ public class MedtronicPumpStatus extends PumpStatus {
//private static MedtronicPumpStatus medtronicPumpStatus = new MedtronicPumpStatus();
private static Logger LOG = LoggerFactory.getLogger(MedtronicPumpStatus.class);
//public Date lastDataTime;
//public long lastConnection = 0L;
//public Date lastBolusTime;
//public String activeProfileName = "A";
//public double reservoirRemainingUnits = 50d;
//public double batteryRemaining = 75d;
//public String iob = "0";
public String errorDescription = null;
public String serialNumber;
public PumpType pumpType = null;
@ -45,11 +36,10 @@ public class MedtronicPumpStatus extends PumpStatus {
String regexMac = "([\\da-fA-F]{1,2}(?:\\:|$)){6}";
String regexSN = "[0-9]{6}";
private Map<String,PumpType> medtronicPumpMap = null;
private Map<String, PumpType> medtronicPumpMap = null;
public MedtronicPumpStatus(PumpDescription pumpDescription)
{
public MedtronicPumpStatus(PumpDescription pumpDescription) {
super(pumpDescription);
}
@ -59,9 +49,9 @@ public class MedtronicPumpStatus extends PumpStatus {
this.activeProfileName = "STD";
this.reservoirRemainingUnits = 75d;
this.batteryRemaining = 75d;
this.batteryRemaining = 75;
if (this.medtronicPumpMap==null)
if (this.medtronicPumpMap == null)
createMedtronicPumpMap();
}
@ -86,11 +76,10 @@ public class MedtronicPumpStatus extends PumpStatus {
}
public void verifyConfiguration()
{
public void verifyConfiguration() {
try {
if (this.medtronicPumpMap==null)
if (this.medtronicPumpMap == null)
createMedtronicPumpMap();
@ -174,8 +163,7 @@ public class MedtronicPumpStatus extends PumpStatus {
maxBolus = Integer.parseInt(value);
if (maxBolus> 25)
{
if (maxBolus > 25) {
SP.putString("pref_medtronic_max_bolus", "25");
}
@ -183,25 +171,21 @@ public class MedtronicPumpStatus extends PumpStatus {
maxBasal = Integer.parseInt(value);
if (maxBasal> 35)
{
if (maxBasal > 35) {
SP.putString("pref_medtronic_max_basal", "35");
}
}
catch(Exception ex)
{
} catch (Exception ex) {
this.errorDescription = ex.getMessage();
LOG.error("Error on Verification: " + ex.getMessage(), ex);
}
}
public String getErrorInfo()
{
public String getErrorInfo() {
verifyConfiguration();
return (this.errorDescription==null) ? "-" : this.errorDescription;
return (this.errorDescription == null) ? "-" : this.errorDescription;
}