Better profile handling

This commit is contained in:
Milos Kozak 2016-12-24 23:53:17 +01:00
parent edcfebfd41
commit 052ac47595
17 changed files with 141 additions and 59 deletions

View file

@ -25,7 +25,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanFragment;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerFragment;
import info.nightscout.androidaps.plugins.NSProfile.NSProfileFragment;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAFragment;
import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
@ -78,7 +78,7 @@ public class MainApp extends Application {
pluginsList.add(VirtualPumpFragment.getPlugin());
if (Config.LOOPENABLED) pluginsList.add(LoopFragment.getPlugin());
if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.getPlugin());
pluginsList.add(NSProfileViewerFragment.getPlugin());
pluginsList.add(NSProfileFragment.getPlugin());
pluginsList.add(SimpleProfileFragment.getPlugin());
pluginsList.add(CircadianPercentageProfileFragment.getPlugin());
pluginsList.add(TreatmentsFragment.getPlugin());

View file

@ -22,9 +22,6 @@ import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
@ -39,10 +36,9 @@ import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.History.DanaRNSHistorySync;
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerPlugin;
import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS;
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin;
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin;
@ -76,7 +72,7 @@ public class DataService extends IntentService {
nsClientEnabled = true;
}
boolean isNSProfile = ConfigBuilderPlugin.getActiveProfile().getClass().equals(NSProfileViewerPlugin.class);
boolean isNSProfile = ConfigBuilderPlugin.getActiveProfile().getClass().equals(NSProfilePlugin.class);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean nsUploadOnly = SP.getBoolean("ns_upload_only", false);

View file

@ -23,6 +23,7 @@ public interface PumpInterface {
// Upload to pump new basal profile
void setNewBasalProfile(NSProfile profile);
boolean isThisProfileSet(NSProfile profile);
double getBaseBasalRate(); // base basal rate, not temp basal
double getTempBasalAbsoluteRate();

View file

@ -1,16 +1,10 @@
package info.nightscout.androidaps.plugins.ConfigBuilder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -32,10 +26,7 @@ import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.interfaces.TempBasalsInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerPlugin;
import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin;
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpPlugin;
@ -291,7 +282,7 @@ public class ConfigBuilderFragment extends Fragment implements FragmentBase {
if (type == PluginBase.PUMP)
MainApp.getSpecificPlugin(VirtualPumpPlugin.class).setFragmentEnabled(type, true);
else if (type == PluginBase.PROFILE)
MainApp.getSpecificPlugin(NSProfileViewerPlugin.class).setFragmentEnabled(type, true);
MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentEnabled(type, true);
else
pluginsInCategory.get(0).setFragmentEnabled(type, true);
}

View file

@ -46,6 +46,9 @@ import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResult;
import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.client.data.DbLogger;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
@ -332,7 +335,29 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
@Override
public void setNewBasalProfile(NSProfile profile) {
activePump.setNewBasalProfile(profile);
// Compare with pump limits
NSProfile.BasalValue[] basalValues = profile.getBasalValues();
for (int index = 0; index < basalValues.length; index++) {
if (basalValues[index].value < getPumpDescription().basalMinimumRate) {
Notification notification = new Notification(Notification.BASAL_VALUE_BELOW_MINIMUM, MainApp.sResources.getString(R.string.basalvaluebelowminimum), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
return;
}
}
MainApp.bus().post(new EventDismissNotification(Notification.BASAL_VALUE_BELOW_MINIMUM));
if (isThisProfileSet(profile)) {
log.debug("Correct profile already set");
} else {
activePump.setNewBasalProfile(profile);
}
}
@Override
public boolean isThisProfileSet(NSProfile profile) {
return activePump.isThisProfileSet(profile);
}
@Override

View file

@ -36,7 +36,7 @@ import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerPlugin;
import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
@ -44,7 +44,6 @@ import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.Round;
import info.nightscout.utils.ToastUtils;
/**
* Created by mike on 05.08.2016.
@ -185,8 +184,8 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf
if (type == PluginBase.PUMP && !fragmentEnabled && this.fragmentProfileEnabled) {
setFragmentEnabled(PluginBase.PROFILE, false);
setFragmentVisible(PluginBase.PROFILE, false);
MainApp.getSpecificPlugin(NSProfileViewerPlugin.class).setFragmentEnabled(PluginBase.PROFILE, true);
MainApp.getSpecificPlugin(NSProfileViewerPlugin.class).setFragmentVisible(PluginBase.PROFILE, true);
MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentEnabled(PluginBase.PROFILE, true);
MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentVisible(PluginBase.PROFILE, true);
}
}
@ -241,6 +240,22 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf
}
}
@Override
public boolean isThisProfileSet(NSProfile profile) {
if (!isInitialized())
return false;
DanaRPump pump = getDanaRPump();
int basalValues = pump.basal48Enable ? 48 : 24;
int basalIncrement = pump.basal48Enable ? 30 * 60 : 60 * 60;
for (int h = 0; h < basalValues; h++) {
Double pumpValue = pump.pumpProfiles[pump.activeProfile][h];
Double profileValue = profile.getBasal(h * basalIncrement);
if (!pumpValue.equals(profileValue))
return false;
}
return true;
}
@Override
public double getBaseBasalRate() {
return getDanaRPump().currentBasal;

View file

@ -36,7 +36,7 @@ import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaRKorean.Services.ExecutionService;
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerPlugin;
import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
@ -44,7 +44,6 @@ import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.Round;
import info.nightscout.utils.ToastUtils;
/**
* Created by mike on 05.08.2016.
@ -185,8 +184,8 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints
if (type == PluginBase.PUMP && !fragmentEnabled && this.fragmentProfileEnabled) {
setFragmentEnabled(PluginBase.PROFILE, false);
setFragmentVisible(PluginBase.PROFILE, false);
MainApp.getSpecificPlugin(NSProfileViewerPlugin.class).setFragmentEnabled(PluginBase.PROFILE, true);
MainApp.getSpecificPlugin(NSProfileViewerPlugin.class).setFragmentVisible(PluginBase.PROFILE, true);
MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentEnabled(PluginBase.PROFILE, true);
MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentVisible(PluginBase.PROFILE, true);
}
}
@ -241,6 +240,24 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints
}
}
@Override
public boolean isThisProfileSet(NSProfile profile) {
if (!isInitialized())
return false;
DanaRKoreanPump pump = getDanaRPump();
int basalValues = pump.basal48Enable ? 48 : 24;
int basalIncrement = pump.basal48Enable ? 30 * 60 : 60 * 60;
for (int h = 0; h < basalValues; h++) {
Double pumpValue = pump.pumpProfiles[pump.activeProfile][h];
Double profileValue = profile.getBasal(h * basalIncrement);
if (Math.abs(pumpValue - profileValue) > getPumpDescription().basalStep) {
log.debug("Diff found. Hour: " + h + " Pump: " + pumpValue + " Profile: " + profileValue);
return false;
}
}
return true;
}
@Override
public double getBaseBasalRate() {
return getDanaRPump().currentBasal;
@ -674,7 +691,8 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints
extended.put("BaseBasalRate", getBaseBasalRate());
try {
extended.put("ActiveProfile", MainApp.getConfigBuilder().getActiveProfile().getProfile().getActiveProfile());
} catch (Exception e) {}
} catch (Exception e) {
}
pump.put("battery", battery);
pump.put("status", status);

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.NSProfileViewer;
package info.nightscout.androidaps.plugins.NSProfile;
import android.app.Activity;
import android.os.Bundle;
@ -13,14 +13,14 @@ import com.squareup.otto.Subscribe;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.NSProfileViewer.events.EventNSProfileViewerUpdateGUI;
import info.nightscout.androidaps.plugins.NSProfile.events.EventNSProfileUpdateGUI;
import info.nightscout.utils.DecimalFormatter;
public class NSProfileViewerFragment extends Fragment implements FragmentBase {
private static NSProfileViewerPlugin nsProfileViewerPlugin = new NSProfileViewerPlugin();
public class NSProfileFragment extends Fragment implements FragmentBase {
private static NSProfilePlugin nsProfilePlugin = new NSProfilePlugin();
public static NSProfileViewerPlugin getPlugin() {
return nsProfileViewerPlugin;
public static NSProfilePlugin getPlugin() {
return nsProfilePlugin;
}
private static TextView noProfile;
@ -63,7 +63,7 @@ public class NSProfileViewerFragment extends Fragment implements FragmentBase {
}
@Subscribe
public void onStatusEvent(final EventNSProfileViewerUpdateGUI ev) {
public void onStatusEvent(final EventNSProfileUpdateGUI ev) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@ -75,19 +75,19 @@ public class NSProfileViewerFragment extends Fragment implements FragmentBase {
}
private void updateGUI() {
if (nsProfileViewerPlugin.profile == null) {
if (nsProfilePlugin.profile == null) {
noProfile.setVisibility(View.VISIBLE);
return;
} else {
noProfile.setVisibility(View.GONE);
}
units.setText(nsProfileViewerPlugin.profile.getUnits());
dia.setText(DecimalFormatter.to2Decimal(nsProfileViewerPlugin.profile.getDia()) + " h");
activeProfile.setText(nsProfileViewerPlugin.profile.getActiveProfile());
ic.setText(nsProfileViewerPlugin.profile.getIcList());
isf.setText(nsProfileViewerPlugin.profile.getIsfList());
basal.setText(nsProfileViewerPlugin.profile.getBasalList());
target.setText(nsProfileViewerPlugin.profile.getTargetList());
units.setText(nsProfilePlugin.profile.getUnits());
dia.setText(DecimalFormatter.to2Decimal(nsProfilePlugin.profile.getDia()) + " h");
activeProfile.setText(nsProfilePlugin.profile.getActiveProfile());
ic.setText(nsProfilePlugin.profile.getIcList());
isf.setText(nsProfilePlugin.profile.getIsfList());
basal.setText(nsProfilePlugin.profile.getBasalList());
target.setText(nsProfilePlugin.profile.getTargetList());
}
}

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.NSProfileViewer;
package info.nightscout.androidaps.plugins.NSProfile;
import android.content.Intent;
import android.content.SharedPreferences;
@ -19,18 +19,18 @@ import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.plugins.NSProfileViewer.events.EventNSProfileViewerUpdateGUI;
import info.nightscout.androidaps.plugins.NSProfile.events.EventNSProfileUpdateGUI;
import info.nightscout.client.data.NSProfile;
/**
* Created by mike on 05.08.2016.
*/
public class NSProfileViewerPlugin implements PluginBase, ProfileInterface {
private static Logger log = LoggerFactory.getLogger(NSProfileViewerPlugin.class);
public class NSProfilePlugin implements PluginBase, ProfileInterface {
private static Logger log = LoggerFactory.getLogger(NSProfilePlugin.class);
@Override
public String getFragmentClass() {
return NSProfileViewerFragment.class.getName();
return NSProfileFragment.class.getName();
}
static boolean fragmentEnabled = true;
@ -38,7 +38,7 @@ public class NSProfileViewerPlugin implements PluginBase, ProfileInterface {
static NSProfile profile = null;
public NSProfileViewerPlugin() {
public NSProfilePlugin() {
MainApp.bus().register(this);
loadNSProfile();
@ -83,7 +83,7 @@ public class NSProfileViewerPlugin implements PluginBase, ProfileInterface {
public void onStatusEvent(final EventNewBasalProfile ev) {
profile = new NSProfile(ev.newNSProfile.getData(), ev.newNSProfile.getActiveProfile());
storeNSProfile();
MainApp.bus().post(new EventNSProfileViewerUpdateGUI());
MainApp.bus().post(new EventNSProfileUpdateGUI());
}
private void storeNSProfile() {

View file

@ -0,0 +1,7 @@
package info.nightscout.androidaps.plugins.NSProfile.events;
/**
* Created by mike on 05.08.2016.
*/
public class EventNSProfileUpdateGUI {
}

View file

@ -1,7 +0,0 @@
package info.nightscout.androidaps.plugins.NSProfileViewer.events;
/**
* Created by mike on 05.08.2016.
*/
public class EventNSProfileViewerUpdateGUI {
}

View file

@ -19,6 +19,7 @@ public class Notification {
public static final int UD_MODE_ENABLED = 4;
public static final int PROFILE_NOT_SET_NOT_INITIALIZED = 5;
public static final int FAILED_UDPATE_PROFILE = 6;
public static final int BASAL_VALUE_BELOW_MINIMUM = 7;
public int id;
public Date date;

View file

@ -128,6 +128,11 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface {
// Do nothing here. we are using MainApp.getConfigBuilder().getActiveProfile().getProfile();
}
@Override
public boolean isThisProfileSet(NSProfile profile) {
return false;
}
@Override
public double getBaseBasalRate() {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();

View file

@ -300,6 +300,34 @@ public class NSProfile {
return getBasalList(getDefaultProfile());
}
public class BasalValue {
public BasalValue(Integer timeAsSeconds, Double value) {
this.timeAsSeconds = timeAsSeconds;
this.value = value;
}
public Integer timeAsSeconds;
public Double value;
}
public BasalValue[] getBasalValues() {
try {
JSONArray array = getDefaultProfile().getJSONArray("basal");
BasalValue[] ret = new BasalValue[array.length()];
for (Integer index = 0; index < array.length(); index++) {
JSONObject o = array.getJSONObject(index);
Integer tas = o.getInt("timeAsSeconds");
Double value = o.getDouble("value");
ret[index] = new BasalValue(tas, value);
}
return ret;
} catch (JSONException e) {
e.printStackTrace();
}
return new BasalValue[0];
}
public String getBasalList(JSONObject profile) {
if (profile != null) {
try {

View file

@ -2,7 +2,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".plugins.NSProfileViewer.NSProfileViewerFragment">
tools:context=".plugins.NSProfile.NSProfileFragment">
<ScrollView
android:layout_width="match_parent"

View file

@ -372,4 +372,5 @@
<string name="profile_set_ok">Bazální profil aktualizován</string>
<string name="pumpNotInitializedProfileNotSet">Pumpa není inicializována, profil nenastaven!</string>
<string name="wrongpumpdriverselected">Vybrán špatný ovladač pumpy</string>
<string name="basalvaluebelowminimum">Hodnota bazálu pod minimem. Nenastaveno!</string>
</resources>

View file

@ -383,4 +383,5 @@
<string name="danar_disableeasymode">Disable EasyUI mode in pump</string>
<string name="danar_enableextendedbolus">Enable extended boluses on pump</string>
<string name="danar_switchtouhmode">Change mode from U/d to U/h on pump</string>
<string name="basalvaluebelowminimum">Basal value below minimum. Profile not set!</string>
</resources>