plugins refactoring

This commit is contained in:
Milos Kozak 2016-08-05 23:54:03 +02:00
parent 7cc0fb593d
commit b711648bee
87 changed files with 4989 additions and 5058 deletions

View file

@ -15,3 +15,12 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-dontwarn android.support.**
-keep class !android.support.v7.internal.view.menu.**,android.support.** {*;}
-ignorewarnings
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-keep public class * extends android.support.v4.** {*;}
-keep public class * extends android.app.Fragment

View file

@ -7,7 +7,6 @@ public class Config {
// MAIN FUCTIONALITY
public static final boolean APS = BuildConfig.APS;
// PLUGINS
public static final boolean LOWSUSPEDENABLED = APS;
public static final boolean OPENAPSMAENABLED = APS;
public static final boolean LOOPENABLED = APS;

View file

@ -80,9 +80,9 @@ public class MainActivity extends AppCompatActivity {
}
private void setUpTabs(boolean switchToLast) {
pageAdapter = new TabPageAdapter(getSupportFragmentManager());
for (PluginBase f : MainApp.getPluginsList()) {
pageAdapter.registerNewFragment((Fragment) f);
pageAdapter = new TabPageAdapter(getSupportFragmentManager(), this);
for (PluginBase p : MainApp.getPluginsList()) {
pageAdapter.registerNewFragment(p);
}
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(pageAdapter);

View file

@ -9,11 +9,18 @@ import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.plugins.LowSuspend.LowSuspendFragment;
import info.nightscout.androidaps.plugins.MM640g.MM640gFragment;
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerFragment;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
@ -28,14 +35,6 @@ import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment;
import io.fabric.sdk.android.Fabric;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Iterator;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
public class MainApp extends Application {
@ -46,7 +45,7 @@ public class MainApp extends Application {
public static Resources sResources;
private static DatabaseHelper sDatabaseHelper = null;
private static ConfigBuilderFragment sConfigBuilder = null;
private static ConfigBuilderPlugin sConfigBuilder = null;
private static ArrayList<PluginBase> pluginsList = null;
@ -60,26 +59,26 @@ public class MainApp extends Application {
sResources = getResources();
if (pluginsList == null) {
pluginsList = new ArrayList<PluginBase>();
pluginsList = new ArrayList<>();
// Register all tabs in app here
pluginsList.add(OverviewFragment.newInstance());
if (Config.DANAR) pluginsList.add(DanaRFragment.newInstance());
if (Config.MM640G) pluginsList.add(MM640gFragment.newInstance());
pluginsList.add(VirtualPumpFragment.newInstance());
if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.newInstance());
if (Config.LOOPENABLED) pluginsList.add(LoopFragment.newInstance());
if (Config.LOWSUSPEDENABLED) pluginsList.add(LowSuspendFragment.newInstance());
if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.newInstance());
pluginsList.add(NSProfileViewerFragment.newInstance());
pluginsList.add(SimpleProfileFragment.newInstance());
pluginsList.add(TreatmentsFragment.newInstance());
pluginsList.add(TempBasalsFragment.newInstance());
pluginsList.add(SafetyFragment.newInstance());
if (Config.APS) pluginsList.add(ObjectivesFragment.newInstance());
pluginsList.add(SourceXdripFragment.newInstance());
pluginsList.add(SourceNSClientFragment.newInstance());
if (Config.SMSCOMMUNICATORENABLED) pluginsList.add(SmsCommunicatorFragment.newInstance());
pluginsList.add(sConfigBuilder = ConfigBuilderFragment.newInstance());
pluginsList.add(OverviewFragment.getPlugin());
if (Config.DANAR) pluginsList.add(DanaRFragment.getPlugin());
if (Config.MM640G) pluginsList.add(MM640gFragment.getPlugin());
pluginsList.add(VirtualPumpFragment.getPlugin());
if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.getPlugin());
if (Config.LOOPENABLED) pluginsList.add(LoopFragment.getPlugin());
if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.getPlugin());
pluginsList.add(NSProfileViewerFragment.getPlugin());
pluginsList.add(SimpleProfileFragment.getPlugin());
pluginsList.add(TreatmentsFragment.getPlugin());
pluginsList.add(TempBasalsFragment.getPlugin());
pluginsList.add(SafetyFragment.getPlugin());
if (Config.APS) pluginsList.add(ObjectivesFragment.getPlugin());
pluginsList.add(SourceXdripFragment.getPlugin());
pluginsList.add(SourceNSClientFragment.getPlugin());
if (Config.SMSCOMMUNICATORENABLED)
pluginsList.add(SmsCommunicatorFragment.getPlugin());
pluginsList.add(sConfigBuilder = ConfigBuilderFragment.getPlugin());
MainApp.getConfigBuilder().initialize();
}
@ -88,6 +87,7 @@ public class MainApp extends Application {
public static Bus bus() {
return sBus;
}
public static MainApp instance() {
return sInstance;
}
@ -106,11 +106,7 @@ public class MainApp extends Application {
}
}
public static void setConfigBuilder(ConfigBuilderFragment cb) {
sConfigBuilder = cb;
}
public static ConfigBuilderFragment getConfigBuilder() {
public static ConfigBuilderPlugin getConfigBuilder() {
return sConfigBuilder;
}
@ -119,12 +115,10 @@ public class MainApp extends Application {
}
public static ArrayList<PluginBase> getSpecificPluginsList(int type) {
ArrayList<PluginBase> newList = new ArrayList<PluginBase>();
ArrayList<PluginBase> newList = new ArrayList<>();
if (pluginsList != null) {
Iterator<PluginBase> it = pluginsList.iterator();
while (it.hasNext()) {
PluginBase p = it.next();
for (PluginBase p : pluginsList) {
if (p.getType() == type)
newList.add(p);
}
@ -135,13 +129,11 @@ public class MainApp extends Application {
}
public static ArrayList<PluginBase> getSpecificPluginsListByInterface(Class interfaceClass) {
ArrayList<PluginBase> newList = new ArrayList<PluginBase>();
ArrayList<PluginBase> newList = new ArrayList<>();
if (pluginsList != null) {
Iterator<PluginBase> it = pluginsList.iterator();
while (it.hasNext()) {
PluginBase p = it.next();
if (p.getClass() != ConfigBuilderFragment.class && interfaceClass.isAssignableFrom(p.getClass()))
for (PluginBase p : pluginsList) {
if (p.getClass() != ConfigBuilderPlugin.class && interfaceClass.isAssignableFrom(p.getClass()))
newList.add(p);
}
} else {
@ -153,9 +145,7 @@ public class MainApp extends Application {
@Nullable
public static PluginBase getSpecificPlugin(Class pluginClass) {
if (pluginsList != null) {
Iterator<PluginBase> it = pluginsList.iterator();
while (it.hasNext()) {
PluginBase p = it.next();
for (PluginBase p : pluginsList) {
if (p.getClass() == pluginClass)
return p;
}

View file

@ -16,6 +16,7 @@ import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.DanaR.BluetoothDevicePreference;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.utils.LocaleHelper;
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -84,13 +85,11 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
addPreferencesFromResource(R.xml.pref_closedmode);
if (Config.OPENAPSMAENABLED)
addPreferencesFromResource(R.xml.pref_openapsma);
if (Config.LOWSUSPEDENABLED)
addPreferencesFromResource(R.xml.pref_lowsuspend);
addPreferencesFromResource(R.xml.pref_nightscout);
if (Config.DANAR) {
addPreferencesFromResource(R.xml.pref_danar);
DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class);
if (danaRFragment.isEnabled(PluginBase.PROFILE)) {
DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
if (danaRPlugin.isEnabled(PluginBase.PROFILE)) {
addPreferencesFromResource(R.xml.pref_danarprofile);
}
}

View file

@ -38,10 +38,12 @@ import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.History.DanaRNSHistorySync;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
import info.nightscout.androidaps.plugins.SmsCommunicator.Events.EventNewSMS;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS;
import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment;
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment;
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment;
@ -178,14 +180,14 @@ public class DataService extends IntentService {
if (Config.logIncommingData)
log.debug("Received status: " + bundles);
if (bundles.containsKey("nsclientversioncode")) {
ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder();
if (configBuilderFragment != null) {
configBuilderFragment.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203
configBuilderFragment.nightscoutVersionName = bundles.getString("nightscoutversionname");
configBuilderFragment.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117
configBuilderFragment.nsClientVersionName = bundles.getString("nsclientversionname");
log.debug("Got versions: NSClient: " + configBuilderFragment.nsClientVersionName + " Nightscout: " + configBuilderFragment.nightscoutVersionName);
if (configBuilderFragment.nsClientVersionCode < 118)
ConfigBuilderPlugin configBuilder = MainApp.getConfigBuilder();
if (configBuilder != null) {
configBuilder.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203
configBuilder.nightscoutVersionName = bundles.getString("nightscoutversionname");
configBuilder.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117
configBuilder.nsClientVersionName = bundles.getString("nsclientversionname");
log.debug("Got versions: NSClient: " + configBuilder.nsClientVersionName + " Nightscout: " + configBuilder.nightscoutVersionName);
if (configBuilder.nsClientVersionCode < 118)
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.unsupportedclientver));
}
} else {
@ -222,10 +224,10 @@ public class DataService extends IntentService {
JSONObject devicestatusJson = jsonArray.getJSONObject(0);
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainApp.getSpecificPlugin(ObjectivesFragment.class);
if (objectivesFragment != null) {
objectivesFragment.pumpStatusIsAvailableInNS = true;
objectivesFragment.saveProgress();
ObjectivesPlugin objectivesPlugin = (ObjectivesPlugin) MainApp.getSpecificPlugin(ObjectivesPlugin.class);
if (objectivesPlugin != null) {
objectivesPlugin.pumpStatusIsAvailableInNS = true;
objectivesPlugin.saveProgress();
}
}
}
@ -245,7 +247,7 @@ public class DataService extends IntentService {
log.error("Config builder not ready on receive profile");
return;
}
PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
PumpInterface pump = MainApp.getConfigBuilder();
if (pump != null) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (SP.getBoolean("syncprofiletopump", false))
@ -368,10 +370,10 @@ public class DataService extends IntentService {
MainApp.bus().post(new EventNewBG());
}
// Objectives 0
ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainApp.getSpecificPlugin(ObjectivesFragment.class);
if (objectivesFragment != null) {
objectivesFragment.bgIsAvailableInNS = true;
objectivesFragment.saveProgress();
ObjectivesPlugin objectivesPlugin = (ObjectivesPlugin) MainApp.getSpecificPlugin(ObjectivesPlugin.class);
if (objectivesPlugin != null) {
objectivesPlugin.bgIsAvailableInNS = true;
objectivesPlugin.saveProgress();
}
}

View file

@ -14,6 +14,7 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.GraphSeriesExtension.DataPointWithLabelInterface;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
@ -137,7 +138,7 @@ public class Treatment implements DataPointWithLabelInterface {
} catch (JSONException e) {
e.printStackTrace();
}
ConfigBuilderFragment.uploadCareportalEntryToNS(data);
ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
}
}

View file

@ -0,0 +1,7 @@
package info.nightscout.androidaps.interfaces;
/**
* Created by mike on 05.08.2016.
*/
public interface FragmentBase {
}

View file

@ -17,7 +17,8 @@ public interface PluginBase {
int BGSOURCE = 9;
int LAST = 10; // keep always highest number
public int getType();
int getType();
String getFragmentClass();
String getName();
boolean isEnabled(int type);

View file

@ -5,6 +5,7 @@ import java.util.List;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
/**
* Created by mike on 14.06.2016.
@ -13,6 +14,6 @@ public interface TreatmentsInterface {
void updateTotalIOB();
IobTotal getLastCalculation();
TreatmentsFragment.MealData getMealData();
TreatmentsPlugin.MealData getMealData();
List<Treatment> getTreatments();
}

View file

@ -10,10 +10,17 @@ import android.view.ViewGroup;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog;
public class CareportalFragment extends Fragment implements PluginBase, View.OnClickListener {
public class CareportalFragment extends Fragment implements FragmentBase, View.OnClickListener {
static CareportalPlugin careportalPlugin = new CareportalPlugin();
static public CareportalPlugin getPlugin() {
return careportalPlugin;
}
public class OptionsToShow {
public int eventType;
@ -73,58 +80,6 @@ public class CareportalFragment extends Fragment implements PluginBase, View.OnC
final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, false, false, false, true, false);
final OptionsToShow openapsoffline = new OptionsToShow(R.id.careportal_openapsoffline, R.string.careportal_openapsoffline, false, false, false, false, true, false, false, false, false);
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
public CareportalFragment() {
// Required empty public constructor
}
public static CareportalFragment newInstance() {
CareportalFragment fragment = new CareportalFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public String getName() {
return MainApp.sResources.getString(R.string.careportal);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View file

@ -0,0 +1,52 @@
package info.nightscout.androidaps.plugins.Careportal;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
public class CareportalPlugin implements PluginBase {
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public String getFragmentClass() {
return CareportalFragment.class.getName();
}
@Override
public String getName() {
return MainApp.sResources.getString(R.string.careportal);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
}

View file

@ -47,6 +47,7 @@ import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog;
import info.nightscout.client.data.DbLogger;
import info.nightscout.client.data.NSProfile;
@ -513,7 +514,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
builder.setMessage(confirmText);
builder.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ConfigBuilderFragment.uploadCareportalEntryToNS(data);
ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
}
});
builder.setNegativeButton(getContext().getString(R.string.cancel), null);

View file

@ -20,48 +20,29 @@ import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
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.DanaR.events.EventDanaRBolusProgress;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.plugins.Loop.DeviceStatus;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResult;
import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog;
import info.nightscout.client.data.DbLogger;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpInterface, ConstraintsInterface {
private static Logger log = LoggerFactory.getLogger(ConfigBuilderFragment.class);
public class ConfigBuilderFragment extends Fragment implements FragmentBase {
static ConfigBuilderPlugin configBuilderPlugin = new ConfigBuilderPlugin();
static public ConfigBuilderPlugin getPlugin() {
return configBuilderPlugin;
}
ListView bgsourceListView;
ListView pumpListView;
@ -88,49 +69,9 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
PluginCustomAdapter generalDataAdapter = null;
BgSourceInterface activeBgSource;
PumpInterface activePump;
ProfileInterface activeProfile;
TreatmentsInterface activeTreatments;
TempBasalsInterface activeTempBasals;
LoopFragment activeLoop;
public String nightscoutVersionName = "";
public Integer nightscoutVersionCode = 0;
public String nsClientVersionName = "";
public Integer nsClientVersionCode = 0;
ArrayList<PluginBase> pluginList;
Date lastDeviceStatusUpload = new Date(0);
// TODO: sorting
// TODO: Toast and sound when command failed
public ConfigBuilderFragment() {
super();
registerBus();
}
public void initialize() {
pluginList = MainApp.getPluginsList();
loadSettings();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -149,10 +90,10 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
nsclientVerView = (TextView) view.findViewById(R.id.configbuilder_nsclientversion);
nightscoutVerView = (TextView) view.findViewById(R.id.configbuilder_nightscoutversion);
nsclientVerView.setText(nsClientVersionName);
nightscoutVerView.setText(nightscoutVersionName);
if (nsClientVersionCode < 117) nsclientVerView.setTextColor(Color.RED);
if (nightscoutVersionCode < 900) nightscoutVerView.setTextColor(Color.RED);
nsclientVerView.setText(ConfigBuilderPlugin.nsClientVersionName);
nightscoutVerView.setText(ConfigBuilderPlugin.nightscoutVersionName);
if (ConfigBuilderPlugin.nsClientVersionCode < 117) nsclientVerView.setTextColor(Color.RED);
if (ConfigBuilderPlugin.nightscoutVersionCode < 900) nightscoutVerView.setTextColor(Color.RED);
setViews();
return view;
}
@ -192,340 +133,6 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
}
/*
* PluginBase interface
*/
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.configbuilder);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
return true;
}
@Override
public boolean canBeHidden(int type) {
return false;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
// Always enabled
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
// Always visible
}
public static ConfigBuilderFragment newInstance() {
return new ConfigBuilderFragment();
}
/*
* Pump interface
*
* Config builder return itself as a pump and check constraints before it passes command to pump driver
*/
@Override
public boolean isTempBasalInProgress() {
return activePump.isTempBasalInProgress();
}
@Override
public boolean isExtendedBoluslInProgress() {
return activePump.isExtendedBoluslInProgress();
}
@Override
public void setNewBasalProfile(NSProfile profile) {
activePump.setNewBasalProfile(profile);
}
@Override
public double getBaseBasalRate() {
return activePump.getBaseBasalRate();
}
@Override
public double getTempBasalAbsoluteRate() {
return activePump.getTempBasalAbsoluteRate();
}
@Override
public double getTempBasalRemainingMinutes() {
return activePump.getTempBasalRemainingMinutes();
}
@Override
public TempBasal getTempBasal(Date time) {
return activePump.getTempBasal(time);
}
@Override
public TempBasal getTempBasal() {
return activePump.getTempBasal();
}
@Override
public TempBasal getExtendedBolus() {
return activePump.getExtendedBolus();
}
/*
{
"_id": {
"$oid": "5789fea07ef0c37deb388240"
},
"boluscalc": {
"profile": "Posunuta snidane",
"eventTime": "2016-07-16T09:30:14.139Z",
"targetBGLow": "5.6",
"targetBGHigh": "5.6",
"isf": "17",
"ic": "26",
"iob": "0.89",
"cob": "0",
"insulincob": "0",
"bg": "3.6",
"insulinbg": "-0.12",
"bgdiff": "-2",
"carbs": "42",
"gi": "2",
"insulincarbs": "1.62",
"othercorrection": "0",
"insulin": "0.6000000000000001",
"roundingcorrection": "-0.009999999999999898",
"carbsneeded": "0"
},
"enteredBy": "",
"eventType": "Bolus Wizard",
"glucose": 3.6,
"glucoseType": "Sensor",
"units": "mmol",
"carbs": 42,
"insulin": 0.6,
"created_at": "2016-07-16T09:30:12.783Z"
}
*/
public PumpEnactResult deliverTreatmentFromBolusWizard(Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) {
insulin = applyBolusConstraints(insulin);
carbs = applyCarbsConstraints(carbs);
PumpEnactResult result = activePump.deliverTreatment(insulin, carbs, context);
if (Config.logCongigBuilderActions)
log.debug("deliverTreatmentFromBolusWizard insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered);
if (result.success) {
Treatment t = new Treatment();
t.insulin = result.bolusDelivered;
t.carbs = (double) result.carbsDelivered;
t.created_at = new Date();
try {
MainApp.getDbHelper().getDaoTreatments().create(t);
} catch (SQLException e) {
e.printStackTrace();
}
t.setTimeIndex(t.getTimeIndex());
uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc);
MainApp.bus().post(new EventTreatmentChange());
}
return result;
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
insulin = applyBolusConstraints(insulin);
carbs = applyCarbsConstraints(carbs);
BolusProgressDialog bolusProgressDialog = null;
if (context != null) {
bolusProgressDialog = new BolusProgressDialog(insulin);
bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress");
}
PumpEnactResult result = activePump.deliverTreatment(insulin, carbs, context);
if (bolusProgressDialog != null) {
bolusProgressDialog.dismiss();
}
if (Config.logCongigBuilderActions)
log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered);
if (result.success) {
Treatment t = new Treatment();
t.insulin = result.bolusDelivered;
t.carbs = (double) result.carbsDelivered;
t.created_at = new Date();
try {
MainApp.getDbHelper().getDaoTreatments().create(t);
} catch (SQLException e) {
e.printStackTrace();
}
t.setTimeIndex(t.getTimeIndex());
t.sendToNSClient();
MainApp.bus().post(new EventTreatmentChange());
}
return result;
}
@Override
public void stopBolusDelivering() {
activePump.stopBolusDelivering();
}
/**
* apply constraints, set temp based on absolute valus and expecting absolute result
*
* @param absoluteRate
* @param durationInMinutes
* @return
*/
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
Double rateAfterConstraints = applyBasalConstraints(absoluteRate);
PumpEnactResult result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setTempBasalAbsolute rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
if (result.isPercent) {
uploadTempBasalStartPercent(result.percent, result.duration);
} else {
uploadTempBasalStartAbsolute(result.absolute, result.duration);
}
MainApp.bus().post(new EventTempBasalChange());
}
return result;
}
/**
* apply constraints, set temp based on percent and expecting result in percent
*
* @param percent 0 ... 100 ...
* @param durationInMinutes
* @return result
*/
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
Integer percentAfterConstraints = applyBasalConstraints(percent);
PumpEnactResult result = activePump.setTempBasalPercent(percentAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setTempBasalPercent percent: " + percentAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
uploadTempBasalStartPercent(result.percent, result.duration);
MainApp.bus().post(new EventTempBasalChange());
}
return result;
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
Double rateAfterConstraints = applyBolusConstraints(insulin);
PumpEnactResult result = activePump.setExtendedBolus(rateAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setExtendedBolus rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
uploadExtendedBolus(result.bolusDelivered, result.duration);
MainApp.bus().post(new EventTreatmentChange());
}
return result;
}
@Override
public PumpEnactResult cancelTempBasal() {
PumpEnactResult result = activePump.cancelTempBasal();
if (Config.logCongigBuilderActions)
log.debug("cancelTempBasal success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
uploadTempBasalEnd();
MainApp.bus().post(new EventTempBasalChange());
}
return result;
}
@Override
public PumpEnactResult cancelExtendedBolus() {
PumpEnactResult result = activePump.cancelExtendedBolus();
if (Config.logCongigBuilderActions)
log.debug("cancelExtendedBolus success: " + result.success + " enacted: " + result.enacted);
return result;
}
/**
* expect absolute request and allow both absolute and percent response based on pump capabilities
*
* @param request
* @return
*/
public PumpEnactResult applyAPSRequest(APSResult request) {
request.rate = applyBasalConstraints(request.rate);
PumpEnactResult result;
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: " + request.toString());
if ((request.rate == 0 && request.duration == 0) || Math.abs(request.rate - getBaseBasalRate()) < 0.1) {
if (isTempBasalInProgress()) {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: cancelTempBasal()");
result = cancelTempBasal();
} else {
result = new PumpEnactResult();
result.absolute = request.rate;
result.duration = 0;
result.enacted = false;
result.comment = "Basal set correctly";
result.success = true;
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Basal set correctly");
}
} else if (isTempBasalInProgress() && Math.abs(request.rate - getTempBasalAbsoluteRate()) < 0.1) {
result = new PumpEnactResult();
result.absolute = getTempBasalAbsoluteRate();
result.duration = activePump.getTempBasal().getPlannedRemainingMinutes();
result.enacted = false;
result.comment = "Temp basal set correctly";
result.success = true;
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Temp basal set correctly");
} else {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: setTempBasalAbsolute()");
result = setTempBasalAbsolute(request.rate, request.duration);
}
return result;
}
@Nullable
@Override
public JSONObject getJSONStatus() {
if (activePump != null)
return activePump.getJSONStatus();
else return null;
}
@Override
public String deviceID() {
if (activePump != null)
return activePump.deviceID();
else return "Unknown";
}
/*
* ConfigBuilderFragment code
*/
@ -570,7 +177,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
plugin.setFragmentEnabled(type, cb.isChecked());
plugin.setFragmentVisible(type, cb.isChecked());
onEnabledCategoryChanged(plugin, type);
storeSettings();
configBuilderPlugin.storeSettings();
MainApp.bus().post(new EventRefreshGui());
}
});
@ -580,7 +187,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
CheckBox cb = (CheckBox) v;
PluginBase plugin = (PluginBase) cb.getTag();
plugin.setFragmentVisible(type, cb.isChecked());
storeSettings();
configBuilderPlugin.storeSettings();
MainApp.bus().post(new EventRefreshGui());
}
});
@ -627,42 +234,19 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
}
public BgSourceInterface getActiveBgSource() {
return activeBgSource;
}
public PumpInterface getActivePump() {
return this;
}
@Nullable
public ProfileInterface getActiveProfile() {
return activeProfile;
}
public TreatmentsInterface getActiveTreatments() {
return activeTreatments;
}
public TempBasalsInterface getActiveTempBasals() {
return activeTempBasals;
}
public LoopFragment getActiveLoop() {
return activeLoop;
}
void onEnabledCategoryChanged(PluginBase changedPlugin, int type) {
int category = changedPlugin.getType();
ArrayList<PluginBase> pluginsInCategory = null;
switch (category) {
// Multiple selection allowed
case PluginBase.APS:
case PluginBase.GENERAL:
case PluginBase.CONSTRAINTS:
case PluginBase.LOOP:
break;
// Single selection allowed
case PluginBase.APS:
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class);
break;
case PluginBase.PROFILE:
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class);
break;
@ -693,132 +277,6 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
}
}
private void verifySelectionInCategories() {
ArrayList<PluginBase> pluginsInCategory;
// PluginBase.PROFILE
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class);
activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PROFILE);
if (Config.logConfigBuilder)
log.debug("Selected profile interface: " + ((PluginBase) activeProfile).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeProfile).getName())) {
p.setFragmentVisible(PluginBase.PROFILE, false);
}
}
// PluginBase.BGSOURCE
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class);
activeBgSource = (BgSourceInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.BGSOURCE);
if (Config.logConfigBuilder)
log.debug("Selected bgSource interface: " + ((PluginBase) activeBgSource).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeBgSource).getName())) {
p.setFragmentVisible(PluginBase.BGSOURCE, false);
}
}
// PluginBase.PUMP
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.PUMP);
activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PUMP);
if (Config.logConfigBuilder)
log.debug("Selected pump interface: " + ((PluginBase) activePump).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activePump).getName())) {
p.setFragmentVisible(PluginBase.PUMP, false);
}
}
// PluginBase.LOOP
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.LOOP);
activeLoop = (LoopFragment) getTheOneEnabledInArray(pluginsInCategory, PluginBase.LOOP);
if (activeLoop != null) {
if (Config.logConfigBuilder)
log.debug("Selected loop interface: " + activeLoop.getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(activeLoop.getName())) {
p.setFragmentVisible(PluginBase.LOOP, false);
}
}
}
// PluginBase.TEMPBASAL
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TEMPBASAL);
activeTempBasals = (TempBasalsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TEMPBASAL);
if (Config.logConfigBuilder)
log.debug("Selected tempbasal interface: " + ((PluginBase) activeTempBasals).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeTempBasals).getName())) {
p.setFragmentVisible(PluginBase.TEMPBASAL, false);
}
}
// PluginBase.TREATMENT
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TREATMENT);
activeTreatments = (TreatmentsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TREATMENT);
if (Config.logConfigBuilder)
log.debug("Selected treatment interface: " + ((PluginBase) activeTreatments).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeTreatments).getName())) {
p.setFragmentVisible(PluginBase.TREATMENT, false);
}
}
}
@Nullable
private PluginBase getTheOneEnabledInArray(ArrayList<PluginBase> pluginsInCategory, int type) {
PluginBase found = null;
for (PluginBase p : pluginsInCategory) {
if (p.isEnabled(type) && found == null) {
found = p;
} else if (p.isEnabled(type)) {
// set others disabled
p.setFragmentEnabled(type, false);
}
}
// If none enabled, enable first one
if (found == null && pluginsInCategory.size() > 0)
found = pluginsInCategory.get(0);
return found;
}
private void storeSettings() {
if (pluginList != null) {
if (Config.logPrefsChange)
log.debug("Storing settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
for (int type = 1; type < PluginBase.LAST; type++) {
for (PluginBase p : pluginList) {
String settingEnabled = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Enabled";
String settingVisible = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Visible";
editor.putBoolean(settingEnabled, p.isEnabled(type));
editor.putBoolean(settingVisible, p.isVisibleInTabs(type));
}
}
editor.apply();
verifySelectionInCategories();
}
}
private void loadSettings() {
if (Config.logPrefsChange)
log.debug("Loading stored settings");
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
for (int type = 1; type < PluginBase.LAST; type++) {
for (PluginBase p : pluginList) {
String settingEnabled = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Enabled";
String settingVisible = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Visible";
if (SP.contains(settingEnabled))
p.setFragmentEnabled(type, SP.getBoolean(settingEnabled, true));
if (SP.contains(settingVisible))
p.setFragmentVisible(type, SP.getBoolean(settingVisible, true));
}
}
verifySelectionInCategories();
}
/****
* Method for Setting the Height of the ListView dynamically.
* *** Hack to fix the issue of not showing all the items of the ListView
@ -845,323 +303,4 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
listView.setLayoutParams(params);
}
/**
* Constraints interface
**/
@Override
public boolean isLoopEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isLoopEnabled();
}
return result;
}
@Override
public boolean isClosedModeEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isClosedModeEnabled();
}
return result;
}
@Override
public boolean isAutosensModeEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isAutosensModeEnabled();
}
return result;
}
@Override
public boolean isAMAModeEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isAMAModeEnabled();
}
return result;
}
@Override
public Double applyBasalConstraints(Double absoluteRate) {
Double rateAfterConstrain = absoluteRate;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
rateAfterConstrain = Math.min(constrain.applyBasalConstraints(absoluteRate), rateAfterConstrain);
}
return rateAfterConstrain;
}
@Override
public Integer applyBasalConstraints(Integer percentRate) {
Integer rateAfterConstrain = percentRate;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
rateAfterConstrain = Math.min(constrain.applyBasalConstraints(percentRate), rateAfterConstrain);
}
return rateAfterConstrain;
}
@Override
public Double applyBolusConstraints(Double insulin) {
Double insulinAfterConstrain = insulin;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
insulinAfterConstrain = Math.min(constrain.applyBolusConstraints(insulin), insulinAfterConstrain);
}
return insulinAfterConstrain;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
Integer carbsAfterConstrain = carbs;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
carbsAfterConstrain = Math.min(constrain.applyCarbsConstraints(carbs), carbsAfterConstrain);
}
return carbsAfterConstrain;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
Double maxIobAfterConstrain = maxIob;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
maxIobAfterConstrain = Math.min(constrain.applyMaxIOBConstraints(maxIob), maxIobAfterConstrain);
}
return maxIobAfterConstrain;
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
// Give some time to Loop
try {
Thread.sleep(120 * 1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if status not uploaded, upload pump status only
if (new Date().getTime() - lastDeviceStatusUpload.getTime() > 120 * 1000L) {
uploadDeviceStatus();
}
}
public static void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Temp Basal");
data.put("duration", durationInMinutes);
data.put("absolute", absolute);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void uploadTempBasalStartPercent(Integer percent, double durationInMinutes) {
try {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
boolean useAbsolute = SP.getBoolean("ns_sync_use_absolute", false);
if (useAbsolute) {
double absolute = MainApp.getConfigBuilder().getActivePump().getBaseBasalRate() * percent / 100d;
uploadTempBasalStartAbsolute(absolute, durationInMinutes);
} else {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Temp Basal");
data.put("duration", durationInMinutes);
data.put("percent", percent - 100);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void uploadTempBasalEnd() {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Temp Basal");
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void uploadExtendedBolus(Double insulin, double durationInMinutes) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Combo Bolus");
data.put("duration", durationInMinutes);
data.put("splitNow", 0);
data.put("splitExt", 100);
data.put("enteredinsulin", insulin);
data.put("relative", insulin);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void uploadDeviceStatus() {
DeviceStatus deviceStatus = new DeviceStatus();
try {
LoopFragment.LastRun lastRun = LoopFragment.lastRun;
if (lastRun != null && lastRun.lastAPSRun.getTime() > new Date().getTime() - 60 * 1000L) {
// do not send if result is older than 1 min
APSResult apsResult = lastRun.request;
apsResult.json().put("timestamp", DateUtil.toISOString(lastRun.lastAPSRun));
deviceStatus.suggested = apsResult.json();
if (lastRun.request instanceof DetermineBasalResult) {
DetermineBasalResult result = (DetermineBasalResult) lastRun.request;
deviceStatus.iob = result.iob.json();
deviceStatus.iob.put("time", DateUtil.toISOString(lastRun.lastAPSRun));
}
if (lastRun.setByPump != null && lastRun.setByPump.enacted) { // enacted
deviceStatus.enacted = lastRun.request.json();
deviceStatus.enacted.put("rate", lastRun.setByPump.json().get("rate"));
deviceStatus.enacted.put("duration", lastRun.setByPump.json().get("duration"));
deviceStatus.enacted.put("recieved", true);
JSONObject requested = new JSONObject();
requested.put("duration", lastRun.request.duration);
requested.put("rate", lastRun.request.rate);
requested.put("temp", "absolute");
deviceStatus.enacted.put("requested", requested);
}
}
if (getActivePump() != null) {
deviceStatus.device = "openaps://" + getActivePump().deviceID();
deviceStatus.pump = getActivePump().getJSONStatus();
deviceStatus.created_at = DateUtil.toISOString(new Date());
deviceStatus.sendToNSClient();
lastDeviceStatusUpload = new Date();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void uploadBolusWizardRecord(Treatment t, double glucose, String glucoseType, int carbTime, JSONObject boluscalc) {
JSONObject data = new JSONObject();
try {
data.put("eventType", "Bolus Wizard");
if (t.insulin != 0d) data.put("insulin", t.insulin);
if (t.carbs != 0d) data.put("carbs", t.carbs.intValue());
data.put("created_at", DateUtil.toISOString(t.created_at));
data.put("timeIndex", t.timeIndex);
if (glucose != 0d) data.put("glucose", glucose);
data.put("glucoseType", glucoseType);
data.put("boluscalc", boluscalc);
if (carbTime != 0) data.put("preBolus", carbTime);
} catch (JSONException e) {
e.printStackTrace();
}
uploadCareportalEntryToNS(data);
}
public static void uploadCareportalEntryToNS(JSONObject data) {
try {
if (data.has("preBolus") && data.has("carbs")) {
JSONObject prebolus = new JSONObject();
prebolus.put("carbs", data.get("carbs"));
data.remove("carbs");
prebolus.put("eventType", data.get("eventType"));
if (data.has("enteredBy")) prebolus.put("enteredBy", data.get("enteredBy"));
if (data.has("notes")) prebolus.put("notes", data.get("notes"));
long mills = DateUtil.fromISODateString(data.getString("created_at")).getTime();
Date preBolusDate = new Date(mills + data.getInt("preBolus") * 60000L);
prebolus.put("created_at", DateUtil.toISOString(preBolusDate));
uploadCareportalEntryToNS(prebolus);
}
Context context = MainApp.instance().getApplicationContext();
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), NewExtendedBolusDialog.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,858 @@
package info.nightscout.androidaps.plugins.ConfigBuilder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.squareup.otto.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
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.APSResult;
import info.nightscout.androidaps.plugins.Loop.DeviceStatus;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
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.Overview.Dialogs.NewExtendedBolusDialog;
import info.nightscout.client.data.DbLogger;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
/**
* Created by mike on 05.08.2016.
*/
public class ConfigBuilderPlugin implements PluginBase, PumpInterface, ConstraintsInterface {
private static Logger log = LoggerFactory.getLogger(ConfigBuilderPlugin.class);
static BgSourceInterface activeBgSource;
static PumpInterface activePump;
static ProfileInterface activeProfile;
static TreatmentsInterface activeTreatments;
static TempBasalsInterface activeTempBasals;
static APSInterface activeAPS;
static LoopPlugin activeLoop;
static public String nightscoutVersionName = "";
static public Integer nightscoutVersionCode = 0;
static public String nsClientVersionName = "";
static public Integer nsClientVersionCode = 0;
static ArrayList<PluginBase> pluginList;
static Date lastDeviceStatusUpload = new Date(0);
public ConfigBuilderPlugin() {
MainApp.bus().register(this);
}
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public String getFragmentClass() {
return ConfigBuilderFragment.class.getName();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.configbuilder);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
return true;
}
@Override
public boolean canBeHidden(int type) {
return false;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
// Always enabled
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
// Always visible
}
public void initialize() {
pluginList = MainApp.getPluginsList();
loadSettings();
}
public void storeSettings() {
if (pluginList != null) {
if (Config.logPrefsChange)
log.debug("Storing settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
for (int type = 1; type < PluginBase.LAST; type++) {
for (PluginBase p : pluginList) {
String settingEnabled = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Enabled";
String settingVisible = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Visible";
editor.putBoolean(settingEnabled, p.isEnabled(type));
editor.putBoolean(settingVisible, p.isVisibleInTabs(type));
}
}
editor.apply();
verifySelectionInCategories();
}
}
private void loadSettings() {
if (Config.logPrefsChange)
log.debug("Loading stored settings");
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
for (int type = 1; type < PluginBase.LAST; type++) {
for (PluginBase p : pluginList) {
String settingEnabled = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Enabled";
String settingVisible = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Visible";
if (SP.contains(settingEnabled))
p.setFragmentEnabled(type, SP.getBoolean(settingEnabled, true));
if (SP.contains(settingVisible))
p.setFragmentVisible(type, SP.getBoolean(settingVisible, true));
}
}
verifySelectionInCategories();
}
public static BgSourceInterface getActiveBgSource() {
return activeBgSource;
}
@Nullable
public static ProfileInterface getActiveProfile() {
return activeProfile;
}
public static TreatmentsInterface getActiveTreatments() {
return activeTreatments;
}
public static TempBasalsInterface getActiveTempBasals() {
return activeTempBasals;
}
public static APSInterface getActiveAPS() {
return activeAPS;
}
public static LoopPlugin getActiveLoop() {
return activeLoop;
}
private void verifySelectionInCategories() {
ArrayList<PluginBase> pluginsInCategory;
// PluginBase.APS
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class);
activeAPS = (APSInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.APS);
if (Config.logConfigBuilder)
log.debug("Selected APS interface: " + ((PluginBase) activeAPS).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeAPS).getName())) {
p.setFragmentVisible(PluginBase.APS, false);
}
}
// PluginBase.PROFILE
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class);
activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PROFILE);
if (Config.logConfigBuilder)
log.debug("Selected profile interface: " + ((PluginBase) activeProfile).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeProfile).getName())) {
p.setFragmentVisible(PluginBase.PROFILE, false);
}
}
// PluginBase.BGSOURCE
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class);
activeBgSource = (BgSourceInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.BGSOURCE);
if (Config.logConfigBuilder)
log.debug("Selected bgSource interface: " + ((PluginBase) activeBgSource).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeBgSource).getName())) {
p.setFragmentVisible(PluginBase.BGSOURCE, false);
}
}
// PluginBase.PUMP
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.PUMP);
activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PUMP);
if (Config.logConfigBuilder)
log.debug("Selected pump interface: " + ((PluginBase) activePump).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activePump).getName())) {
p.setFragmentVisible(PluginBase.PUMP, false);
}
}
// PluginBase.LOOP
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.LOOP);
activeLoop = (LoopPlugin) getTheOneEnabledInArray(pluginsInCategory, PluginBase.LOOP);
if (activeLoop != null) {
if (Config.logConfigBuilder)
log.debug("Selected loop interface: " + activeLoop.getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(activeLoop.getName())) {
p.setFragmentVisible(PluginBase.LOOP, false);
}
}
}
// PluginBase.TEMPBASAL
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TEMPBASAL);
activeTempBasals = (TempBasalsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TEMPBASAL);
if (Config.logConfigBuilder)
log.debug("Selected tempbasal interface: " + ((PluginBase) activeTempBasals).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeTempBasals).getName())) {
p.setFragmentVisible(PluginBase.TEMPBASAL, false);
}
}
// PluginBase.TREATMENT
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TREATMENT);
activeTreatments = (TreatmentsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TREATMENT);
if (Config.logConfigBuilder)
log.debug("Selected treatment interface: " + ((PluginBase) activeTreatments).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeTreatments).getName())) {
p.setFragmentVisible(PluginBase.TREATMENT, false);
}
}
}
@Nullable
private PluginBase getTheOneEnabledInArray(ArrayList<PluginBase> pluginsInCategory, int type) {
PluginBase found = null;
for (PluginBase p : pluginsInCategory) {
if (p.isEnabled(type) && found == null) {
found = p;
} else if (p.isEnabled(type)) {
// set others disabled
p.setFragmentEnabled(type, false);
}
}
// If none enabled, enable first one
if (found == null && pluginsInCategory.size() > 0)
found = pluginsInCategory.get(0);
return found;
}
/*
* Pump interface
*
* Config builder return itself as a pump and check constraints before it passes command to pump driver
*/
@Override
public boolean isTempBasalInProgress() {
return activePump.isTempBasalInProgress();
}
@Override
public boolean isExtendedBoluslInProgress() {
return activePump.isExtendedBoluslInProgress();
}
@Override
public void setNewBasalProfile(NSProfile profile) {
activePump.setNewBasalProfile(profile);
}
@Override
public double getBaseBasalRate() {
return activePump.getBaseBasalRate();
}
@Override
public double getTempBasalAbsoluteRate() {
return activePump.getTempBasalAbsoluteRate();
}
@Override
public double getTempBasalRemainingMinutes() {
return activePump.getTempBasalRemainingMinutes();
}
@Override
public TempBasal getTempBasal(Date time) {
return activePump.getTempBasal(time);
}
@Override
public TempBasal getTempBasal() {
return activePump.getTempBasal();
}
@Override
public TempBasal getExtendedBolus() {
return activePump.getExtendedBolus();
}
public PumpEnactResult deliverTreatmentFromBolusWizard(Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) {
insulin = applyBolusConstraints(insulin);
carbs = applyCarbsConstraints(carbs);
PumpEnactResult result = activePump.deliverTreatment(insulin, carbs, context);
if (Config.logCongigBuilderActions)
log.debug("deliverTreatmentFromBolusWizard insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered);
if (result.success) {
Treatment t = new Treatment();
t.insulin = result.bolusDelivered;
t.carbs = (double) result.carbsDelivered;
t.created_at = new Date();
try {
MainApp.getDbHelper().getDaoTreatments().create(t);
} catch (SQLException e) {
e.printStackTrace();
}
t.setTimeIndex(t.getTimeIndex());
uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc);
MainApp.bus().post(new EventTreatmentChange());
}
return result;
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
insulin = applyBolusConstraints(insulin);
carbs = applyCarbsConstraints(carbs);
BolusProgressDialog bolusProgressDialog = null;
if (context != null) {
bolusProgressDialog = new BolusProgressDialog(insulin);
bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress");
}
PumpEnactResult result = activePump.deliverTreatment(insulin, carbs, context);
if (bolusProgressDialog != null) {
bolusProgressDialog.dismiss();
}
if (Config.logCongigBuilderActions)
log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered);
if (result.success) {
Treatment t = new Treatment();
t.insulin = result.bolusDelivered;
t.carbs = (double) result.carbsDelivered;
t.created_at = new Date();
try {
MainApp.getDbHelper().getDaoTreatments().create(t);
} catch (SQLException e) {
e.printStackTrace();
}
t.setTimeIndex(t.getTimeIndex());
t.sendToNSClient();
MainApp.bus().post(new EventTreatmentChange());
}
return result;
}
@Override
public void stopBolusDelivering() {
activePump.stopBolusDelivering();
}
/**
* apply constraints, set temp based on absolute valus and expecting absolute result
*
* @param absoluteRate
* @param durationInMinutes
* @return
*/
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
Double rateAfterConstraints = applyBasalConstraints(absoluteRate);
PumpEnactResult result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setTempBasalAbsolute rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
if (result.isPercent) {
uploadTempBasalStartPercent(result.percent, result.duration);
} else {
uploadTempBasalStartAbsolute(result.absolute, result.duration);
}
MainApp.bus().post(new EventTempBasalChange());
}
return result;
}
/**
* apply constraints, set temp based on percent and expecting result in percent
*
* @param percent 0 ... 100 ...
* @param durationInMinutes
* @return result
*/
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
Integer percentAfterConstraints = applyBasalConstraints(percent);
PumpEnactResult result = activePump.setTempBasalPercent(percentAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setTempBasalPercent percent: " + percentAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
uploadTempBasalStartPercent(result.percent, result.duration);
MainApp.bus().post(new EventTempBasalChange());
}
return result;
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
Double rateAfterConstraints = applyBolusConstraints(insulin);
PumpEnactResult result = activePump.setExtendedBolus(rateAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setExtendedBolus rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
uploadExtendedBolus(result.bolusDelivered, result.duration);
MainApp.bus().post(new EventTreatmentChange());
}
return result;
}
@Override
public PumpEnactResult cancelTempBasal() {
PumpEnactResult result = activePump.cancelTempBasal();
if (Config.logCongigBuilderActions)
log.debug("cancelTempBasal success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
uploadTempBasalEnd();
MainApp.bus().post(new EventTempBasalChange());
}
return result;
}
@Override
public PumpEnactResult cancelExtendedBolus() {
PumpEnactResult result = activePump.cancelExtendedBolus();
if (Config.logCongigBuilderActions)
log.debug("cancelExtendedBolus success: " + result.success + " enacted: " + result.enacted);
return result;
}
/**
* expect absolute request and allow both absolute and percent response based on pump capabilities
*
* @param request
* @return
*/
public PumpEnactResult applyAPSRequest(APSResult request) {
request.rate = applyBasalConstraints(request.rate);
PumpEnactResult result;
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: " + request.toString());
if ((request.rate == 0 && request.duration == 0) || Math.abs(request.rate - getBaseBasalRate()) < 0.1) {
if (isTempBasalInProgress()) {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: cancelTempBasal()");
result = cancelTempBasal();
} else {
result = new PumpEnactResult();
result.absolute = request.rate;
result.duration = 0;
result.enacted = false;
result.comment = "Basal set correctly";
result.success = true;
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Basal set correctly");
}
} else if (isTempBasalInProgress() && Math.abs(request.rate - getTempBasalAbsoluteRate()) < 0.1) {
result = new PumpEnactResult();
result.absolute = getTempBasalAbsoluteRate();
result.duration = activePump.getTempBasal().getPlannedRemainingMinutes();
result.enacted = false;
result.comment = "Temp basal set correctly";
result.success = true;
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Temp basal set correctly");
} else {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: setTempBasalAbsolute()");
result = setTempBasalAbsolute(request.rate, request.duration);
}
return result;
}
@Nullable
@Override
public JSONObject getJSONStatus() {
if (activePump != null)
return activePump.getJSONStatus();
else return null;
}
@Override
public String deviceID() {
if (activePump != null)
return activePump.deviceID();
else return "Unknown";
}
/**
* Constraints interface
**/
@Override
public boolean isLoopEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isLoopEnabled();
}
return result;
}
@Override
public boolean isClosedModeEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isClosedModeEnabled();
}
return result;
}
@Override
public boolean isAutosensModeEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isAutosensModeEnabled();
}
return result;
}
@Override
public boolean isAMAModeEnabled() {
boolean result = true;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
result = result && constrain.isAMAModeEnabled();
}
return result;
}
@Override
public Double applyBasalConstraints(Double absoluteRate) {
Double rateAfterConstrain = absoluteRate;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
rateAfterConstrain = Math.min(constrain.applyBasalConstraints(absoluteRate), rateAfterConstrain);
}
return rateAfterConstrain;
}
@Override
public Integer applyBasalConstraints(Integer percentRate) {
Integer rateAfterConstrain = percentRate;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
rateAfterConstrain = Math.min(constrain.applyBasalConstraints(percentRate), rateAfterConstrain);
}
return rateAfterConstrain;
}
@Override
public Double applyBolusConstraints(Double insulin) {
Double insulinAfterConstrain = insulin;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
insulinAfterConstrain = Math.min(constrain.applyBolusConstraints(insulin), insulinAfterConstrain);
}
return insulinAfterConstrain;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
Integer carbsAfterConstrain = carbs;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
carbsAfterConstrain = Math.min(constrain.applyCarbsConstraints(carbs), carbsAfterConstrain);
}
return carbsAfterConstrain;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
Double maxIobAfterConstrain = maxIob;
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue;
maxIobAfterConstrain = Math.min(constrain.applyMaxIOBConstraints(maxIob), maxIobAfterConstrain);
}
return maxIobAfterConstrain;
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
// Give some time to Loop
try {
Thread.sleep(120 * 1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if status not uploaded, upload pump status only
if (new Date().getTime() - lastDeviceStatusUpload.getTime() > 120 * 1000L) {
uploadDeviceStatus();
}
}
public void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Temp Basal");
data.put("duration", durationInMinutes);
data.put("absolute", absolute);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void uploadTempBasalStartPercent(Integer percent, double durationInMinutes) {
try {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
boolean useAbsolute = SP.getBoolean("ns_sync_use_absolute", false);
if (useAbsolute) {
double absolute = getBaseBasalRate() * percent / 100d;
uploadTempBasalStartAbsolute(absolute, durationInMinutes);
} else {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Temp Basal");
data.put("duration", durationInMinutes);
data.put("percent", percent - 100);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void uploadTempBasalEnd() {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Temp Basal");
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void uploadExtendedBolus(Double insulin, double durationInMinutes) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", "Combo Bolus");
data.put("duration", durationInMinutes);
data.put("splitNow", 0);
data.put("splitExt", 100);
data.put("enteredinsulin", insulin);
data.put("relative", insulin);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), ConfigBuilderFragment.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void uploadDeviceStatus() {
DeviceStatus deviceStatus = new DeviceStatus();
try {
LoopPlugin.LastRun lastRun = LoopPlugin.lastRun;
if (lastRun != null && lastRun.lastAPSRun.getTime() > new Date().getTime() - 60 * 1000L) {
// do not send if result is older than 1 min
APSResult apsResult = lastRun.request;
apsResult.json().put("timestamp", DateUtil.toISOString(lastRun.lastAPSRun));
deviceStatus.suggested = apsResult.json();
if (lastRun.request instanceof DetermineBasalResult) {
DetermineBasalResult result = (DetermineBasalResult) lastRun.request;
deviceStatus.iob = result.iob.json();
deviceStatus.iob.put("time", DateUtil.toISOString(lastRun.lastAPSRun));
}
if (lastRun.setByPump != null && lastRun.setByPump.enacted) { // enacted
deviceStatus.enacted = lastRun.request.json();
deviceStatus.enacted.put("rate", lastRun.setByPump.json().get("rate"));
deviceStatus.enacted.put("duration", lastRun.setByPump.json().get("duration"));
deviceStatus.enacted.put("recieved", true);
JSONObject requested = new JSONObject();
requested.put("duration", lastRun.request.duration);
requested.put("rate", lastRun.request.rate);
requested.put("temp", "absolute");
deviceStatus.enacted.put("requested", requested);
}
}
if (activePump != null) {
deviceStatus.device = "openaps://" + deviceID();
deviceStatus.pump = getJSONStatus();
deviceStatus.created_at = DateUtil.toISOString(new Date());
deviceStatus.sendToNSClient();
lastDeviceStatusUpload = new Date();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void uploadBolusWizardRecord(Treatment t, double glucose, String glucoseType, int carbTime, JSONObject boluscalc) {
JSONObject data = new JSONObject();
try {
data.put("eventType", "Bolus Wizard");
if (t.insulin != 0d) data.put("insulin", t.insulin);
if (t.carbs != 0d) data.put("carbs", t.carbs.intValue());
data.put("created_at", DateUtil.toISOString(t.created_at));
data.put("timeIndex", t.timeIndex);
if (glucose != 0d) data.put("glucose", glucose);
data.put("glucoseType", glucoseType);
data.put("boluscalc", boluscalc);
if (carbTime != 0) data.put("preBolus", carbTime);
} catch (JSONException e) {
e.printStackTrace();
}
uploadCareportalEntryToNS(data);
}
public static void uploadCareportalEntryToNS(JSONObject data) {
try {
if (data.has("preBolus") && data.has("carbs")) {
JSONObject prebolus = new JSONObject();
prebolus.put("carbs", data.get("carbs"));
data.remove("carbs");
prebolus.put("eventType", data.get("eventType"));
if (data.has("enteredBy")) prebolus.put("enteredBy", data.get("enteredBy"));
if (data.has("notes")) prebolus.put("notes", data.get("notes"));
long mills = DateUtil.fromISODateString(data.getString("created_at")).getTime();
Date preBolusDate = new Date(mills + data.getInt("preBolus") * 60000L);
prebolus.put("created_at", DateUtil.toISOString(preBolusDate));
uploadCareportalEntryToNS(prebolus);
}
Context context = MainApp.instance().getApplicationContext();
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
bundle.putString("data", data.toString());
Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString(), NewExtendedBolusDialog.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -3,17 +3,10 @@ package info.nightscout.androidaps.plugins.DanaR;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
@ -24,56 +17,35 @@ import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DateFormat;
import java.util.Date;
import java.util.Objects;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog;
import info.nightscout.androidaps.plugins.DanaR.History.DanaRHistoryActivity;
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus;
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.SetWarnColor;
import info.nightscout.utils.ToastUtils;
public class DanaRFragment extends Fragment implements PluginBase, PumpInterface, ConstraintsInterface, ProfileInterface {
public class DanaRFragment extends Fragment implements FragmentBase {
private static Logger log = LoggerFactory.getLogger(DanaRFragment.class);
private Handler mHandler;
private static HandlerThread mHandlerThread;
private static DanaRPlugin danaRPlugin = new DanaRPlugin();
private static ExecutionService mExecutionService;
public static DanaRPlugin getPlugin() {
return danaRPlugin;
}
private static DanaRPump sDanaRPump = new DanaRPump();
private static boolean useExtendedBoluses = false;
boolean fragmentPumpEnabled = true;
boolean fragmentProfileEnabled = true;
boolean fragmentPumpVisible = true;
private static Handler sHandler;
private static HandlerThread sHandlerThread;
private Handler loopHandler = new Handler();
private Runnable refreshLoop = null;
@ -91,37 +63,18 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface
Button viewProfileButton;
Button historyButton;
public static DanaRPump getDanaRPump() {
return sDanaRPump;
}
public DanaRFragment() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
mHandlerThread = new HandlerThread(DanaRFragment.class.getSimpleName());
mHandlerThread.start();
this.mHandler = new Handler(mHandlerThread.getLooper());
registerBus();
Context context = MainApp.instance().getApplicationContext();
Intent intent = new Intent(context, ExecutionService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
if (sHandlerThread == null) {
sHandlerThread = new HandlerThread(DanaRFragment.class.getSimpleName());
sHandlerThread.start();
sHandler = new Handler(sHandlerThread.getLooper());
}
}
public static DanaRFragment newInstance() {
return new DanaRFragment();
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -173,10 +126,10 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface
btConnectionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHandler.post(new Runnable() {
sHandler.post(new Runnable() {
@Override
public void run() {
mExecutionService.connect("Connect request from GUI");
DanaRPlugin.sExecutionService.connect("Connect request from GUI");
}
}
);
@ -187,46 +140,17 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface
return view;
}
@SuppressWarnings("UnusedParameters")
@Subscribe
public void onStatusEvent(final EventAppExit e) {
MainApp.instance().getApplicationContext().unbindService(mConnection);
}
/*
@Override
public void onStart() {
super.onStart();
Context context = getContext();
Intent intent = new Intent(context, ExecutionService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
*/
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
log.debug("Service is disconnected");
mExecutionService = null;
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
}
public void onServiceConnected(ComponentName name, IBinder service) {
log.debug("Service is connected");
ExecutionService.LocalBinder mLocalBinder = (ExecutionService.LocalBinder) service;
mExecutionService = mLocalBinder.getServiceInstance();
}
};
/*
@Override
public void onStop() {
super.onStop();
if (mBounded) {
getContext().unbindService(mConnection);
mBounded = false;
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
}
*/
@Subscribe
public void onStatusEvent(final EventDanaRConnectionStatus c) {
@ -260,521 +184,9 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface
@Subscribe
public void onStatusEvent(final EventPreferenceChange s) {
boolean previousValue = useExtendedBoluses;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
if (useExtendedBoluses != previousValue && isExtendedBoluslInProgress()) {
mExecutionService.extendedBolusStop();
}
updateGUI();
}
// Plugin base interface
@Override
public int getType() {
return PluginBase.PUMP;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.danarpump);
}
@Override
public boolean isEnabled(int type) {
if (type == PluginBase.PROFILE) return fragmentProfileEnabled;
else if (type == PluginBase.PUMP) return fragmentPumpEnabled;
else if (type == PluginBase.CONSTRAINTS) return fragmentPumpEnabled;
return false;
}
@Override
public boolean isVisibleInTabs(int type) {
if (type == PluginBase.PROFILE || type == PluginBase.CONSTRAINTS) return false;
else if (type == PluginBase.PUMP) return fragmentPumpVisible;
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if (type == PluginBase.PROFILE) this.fragmentProfileEnabled = fragmentEnabled;
else if (type == PluginBase.PUMP) this.fragmentPumpEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == PluginBase.PUMP)
this.fragmentPumpVisible = fragmentVisible;
}
// Pump interface
@Override
public boolean isTempBasalInProgress() {
if (getRealTempBasal() != null) return true;
if (getExtendedBolus() != null && useExtendedBoluses) return true;
return false;
}
public boolean isRealTempBasalInProgress() {
return getRealTempBasal() != null; //TODO: crosscheck here
}
@Override
public boolean isExtendedBoluslInProgress() {
return getExtendedBolus() != null; //TODO: crosscheck here
}
@Override
public void setNewBasalProfile(NSProfile profile) {
if (mExecutionService == null) {
log.error("setNewBasalProfile mExecutionService is null");
return;
}
if (!mExecutionService.updateBasalsInPump(profile))
ToastUtils.showToastInUiThread(getContext(), MainApp.sResources.getString(R.string.failedupdatebasalprofile));
}
@Override
public double getBaseBasalRate() {
return getDanaRPump().currentBasal;
}
@Override
public double getTempBasalAbsoluteRate() {
if (isRealTempBasalInProgress()) {
if (getRealTempBasal().isAbsolute) {
return getRealTempBasal().absolute;
} else {
Double baseRate = getBaseBasalRate();
Double tempRate = baseRate * (getRealTempBasal().percent / 100d);
return tempRate;
}
}
if (isExtendedBoluslInProgress() && useExtendedBoluses) {
return getBaseBasalRate() + getExtendedBolus().absolute;
}
return 0;
}
@Override
public double getTempBasalRemainingMinutes() {
if (isRealTempBasalInProgress())
return getRealTempBasal().getPlannedRemainingMinutes();
if (isExtendedBoluslInProgress() && useExtendedBoluses)
return getExtendedBolus().getPlannedRemainingMinutes();
return 0;
}
@Override
public TempBasal getTempBasal() {
if (isRealTempBasalInProgress())
return getRealTempBasal();
if (isExtendedBoluslInProgress() && useExtendedBoluses)
return getExtendedBolus();
return null;
}
public TempBasal getTempBasal(Date time) {
TempBasal temp = MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(time);
if (temp != null) return temp;
if (useExtendedBoluses)
return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(time);
return null;
}
public TempBasal getRealTempBasal() {
return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date());
}
@Override
public TempBasal getExtendedBolus() {
return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(new Date());
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder();
insulin = configBuilderFragment.applyBolusConstraints(insulin);
if (insulin > 0 || carbs > 0) {
Treatment t = new Treatment();
boolean connectionOK = false;
if (carbs > 0) connectionOK = mExecutionService.carbsEntry(carbs);
if (insulin > 0) connectionOK = mExecutionService.bolus(insulin, t);
PumpEnactResult result = new PumpEnactResult();
result.success = connectionOK;
result.bolusDelivered = t.insulin;
result.carbsDelivered = carbs;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpActions)
log.debug("deliverTreatment: OK. Asked: " + insulin + " Delivered: " + result.bolusDelivered);
return result;
} else {
PumpEnactResult result = new PumpEnactResult();
result.success = false;
result.bolusDelivered = 0d;
result.carbsDelivered = 0;
result.comment = MainApp.instance().getString(R.string.danar_invalidinput);
log.error("deliverTreatment: Invalid input");
return result;
}
}
@Override
public void stopBolusDelivering() {
if (mExecutionService == null) {
log.error("stopBolusDelivering mExecutionService is null");
return;
}
mExecutionService.bolusStop();
}
// This is called from APS
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult();
ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder();
absoluteRate = configBuilderFragment.applyBasalConstraints(absoluteRate);
final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d;
final boolean doLowTemp = absoluteRate < getBaseBasalRate();
final boolean doHighTemp = absoluteRate > getBaseBasalRate() && !useExtendedBoluses;
final boolean doExtendedTemp = absoluteRate > getBaseBasalRate() && useExtendedBoluses;
if (doTempOff) {
// If extended in progress
if (isExtendedBoluslInProgress() && useExtendedBoluses) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping extended bolus (doTempOff)");
return cancelExtendedBolus();
}
// If temp in progress
if (isRealTempBasalInProgress()) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping temp basal (doTempOff)");
return cancelRealTempBasal();
}
result.success = true;
result.enacted = false;
result.percent = 100;
result.isPercent = true;
result.isTempCancel = true;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: doTempOff OK");
return result;
}
if (doLowTemp || doHighTemp) {
Integer percentRate = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue();
if (percentRate < 100) percentRate = Round.ceilTo((double) percentRate, 10d).intValue();
else percentRate = Round.floorTo((double) percentRate, 10d).intValue();
if (percentRate > 200) {
percentRate = 200;
}
// If extended in progress
if (isExtendedBoluslInProgress() && useExtendedBoluses) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping extended bolus (doLowTemp || doHighTemp)");
result = cancelExtendedBolus();
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to stop previous extended bolus (doLowTemp || doHighTemp)");
return result;
}
}
// Check if some temp is already in progress
if (isRealTempBasalInProgress()) {
// Correct basal already set ?
if (getRealTempBasal().percent == percentRate) {
result.success = true;
result.percent = percentRate;
result.absolute = getTempBasalAbsoluteRate();
result.enacted = false;
result.duration = ((Double) getTempBasalRemainingMinutes()).intValue();
result.isPercent = true;
result.isTempCancel = false;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Correct temp basal already set (doLowTemp || doHighTemp)");
return result;
} else {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping temp basal (doLowTemp || doHighTemp)");
result = cancelRealTempBasal();
// Check for proper result
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to stop previous temp basal (doLowTemp || doHighTemp)");
return result;
}
}
}
// Convert duration from minutes to hours
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Setting temp basal " + percentRate + "% for " + durationInMinutes + " mins (doLowTemp || doHighTemp)");
return setTempBasalPercent(percentRate, durationInMinutes);
}
if (doExtendedTemp) {
// Check if some temp is already in progress
if (isRealTempBasalInProgress()) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping temp basal (doExtendedTemp)");
result = cancelRealTempBasal();
// Check for proper result
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to stop previous temp basal (doExtendedTemp)");
return result;
}
}
// Calculate # of halfHours from minutes
Integer durationInHalfHours = Math.max(durationInMinutes / 30, 1);
// We keep current basal running so need to sub current basal
Double extendedRateToSet = absoluteRate - getBaseBasalRate();
extendedRateToSet = configBuilderFragment.applyBasalConstraints(extendedRateToSet);
// needs to be rounded to 0.1
extendedRateToSet = Round.roundTo(extendedRateToSet, 0.1d);
// What is current rate of extended bolusing in u/h?
if (Config.logPumpActions) {
log.debug("setTempBasalAbsolute: Extended bolus in progress: " + isExtendedBoluslInProgress() + " rate: " + getDanaRPump().extendedBolusAbsoluteRate + "U/h duration remaining: " + getDanaRPump().extendedBolusRemainingMinutes + "min");
log.debug("setTempBasalAbsolute: Rate to set: " + extendedRateToSet + "U/h");
}
// Compare with extended rate in progress
if (Math.abs(getDanaRPump().extendedBolusAbsoluteRate - extendedRateToSet) < 0.02D) { // Allow some rounding diff
// correct extended already set
result.success = true;
result.absolute = getDanaRPump().extendedBolusAbsoluteRate;
result.enacted = false;
result.duration = getDanaRPump().extendedBolusRemainingMinutes;
result.isPercent = false;
result.isTempCancel = false;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Correct extended already set");
return result;
}
// Now set new extended, no need to to stop previous (if running) because it's replaced
Double extendedAmount = extendedRateToSet / 2 * durationInHalfHours;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Setting extended: " + extendedAmount + "U halfhours: " + durationInHalfHours);
result = setExtendedBolus(extendedAmount, durationInMinutes);
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to set extended bolus");
return result;
}
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Extended bolus set ok");
result.absolute = result.absolute + getBaseBasalRate();
return result;
}
// We should never end here
log.error("setTempBasalAbsolute: Internal error");
result.success = false;
result.comment = "Internal error";
return result;
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult();
ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder();
percent = configBuilderFragment.applyBasalConstraints(percent);
if (percent < 0) {
result.isTempCancel = false;
result.enacted = false;
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_invalidinput);
log.error("setTempBasalPercent: Invalid input");
return result;
}
if (percent > 200) percent = 200;
if (getDanaRPump().isTempBasalInProgress && getDanaRPump().tempBasalPercent == percent) {
result.enacted = false;
result.success = true;
result.isTempCancel = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.duration = getDanaRPump().tempBasalRemainingMin;
result.percent = getDanaRPump().tempBasalPercent;
result.isPercent = true;
if (Config.logPumpActions)
log.debug("setTempBasalPercent: Correct value already set");
return result;
}
int durationInHours = Math.max(durationInMinutes / 60, 1);
boolean connectionOK = mExecutionService.tempBasal(percent, durationInHours);
if (connectionOK && getDanaRPump().isTempBasalInProgress && getDanaRPump().tempBasalPercent == percent) {
result.enacted = true;
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.isTempCancel = false;
result.duration = getDanaRPump().tempBasalRemainingMin;
result.percent = getDanaRPump().tempBasalPercent;
result.isPercent = true;
if (Config.logPumpActions)
log.debug("setTempBasalPercent: OK");
return result;
}
result.enacted = false;
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
log.error("setTempBasalPercent: Failed to set temp basal");
return result;
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder();
insulin = configBuilderFragment.applyBolusConstraints(insulin);
// needs to be rounded to 0.1
insulin = Round.roundTo(insulin, 0.1d);
PumpEnactResult result = new PumpEnactResult();
if (getDanaRPump().isExtendedInProgress && Math.abs(getDanaRPump().extendedBolusAmount - insulin) < 0.1d) {
result.enacted = false;
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.duration = getDanaRPump().extendedBolusRemainingMinutes;
result.absolute = getDanaRPump().extendedBolusAbsoluteRate;
result.isPercent = false;
result.isTempCancel = false;
if (Config.logPumpActions)
log.debug("setExtendedBolus: Correct extended bolus already set");
return result;
}
int durationInHalfHours = Math.max(durationInMinutes / 30, 1);
boolean connectionOK = mExecutionService.extendedBolus(insulin, durationInHalfHours);
if (connectionOK && getDanaRPump().isExtendedInProgress && Math.abs(getDanaRPump().extendedBolusAmount - insulin) < 0.1d) {
result.enacted = true;
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.isTempCancel = false;
result.duration = getDanaRPump().extendedBolusRemainingMinutes;
result.absolute = getDanaRPump().extendedBolusAbsoluteRate;
result.bolusDelivered = getDanaRPump().extendedBolusAmount;
result.isPercent = false;
if (Config.logPumpActions)
log.debug("setExtendedBolus: OK");
return result;
}
result.enacted = false;
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
log.error("setExtendedBolus: Failed to extended bolus");
return result;
}
@Override
public PumpEnactResult cancelTempBasal() {
if (isRealTempBasalInProgress())
return cancelRealTempBasal();
if (isExtendedBoluslInProgress())
return cancelExtendedBolus();
PumpEnactResult result = new PumpEnactResult();
result.success = true;
result.enacted = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.isTempCancel = true;
return result;
}
public PumpEnactResult cancelRealTempBasal() {
PumpEnactResult result = new PumpEnactResult();
if (getDanaRPump().isTempBasalInProgress) {
mExecutionService.tempBasalStop();
result.enacted = true;
result.isTempCancel = true;
}
if (!getDanaRPump().isTempBasalInProgress) {
result.success = true;
result.isTempCancel = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpActions)
log.debug("cancelRealTempBasal: OK");
return result;
} else {
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
result.isTempCancel = true;
log.error("cancelRealTempBasal: Failed to cancel temp basal");
return result;
}
}
@Override
public PumpEnactResult cancelExtendedBolus() {
PumpEnactResult result = new PumpEnactResult();
if (getDanaRPump().isExtendedInProgress) {
mExecutionService.extendedBolusStop();
result.enacted = true;
result.isTempCancel = true;
}
if (!getDanaRPump().isExtendedInProgress) {
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpActions)
log.debug("cancelExtendedBolus: OK");
return result;
} else {
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
log.error("cancelExtendedBolus: Failed to cancel extended bolus");
return result;
}
}
public static void doConnect(String from) {
if (mExecutionService != null) mExecutionService.connect(from);
}
public static boolean isConnected() {
return mExecutionService != null && mExecutionService.isConnected();
}
public static boolean isConnecting() {
return mExecutionService != null && mExecutionService.isConnecting();
}
@Override
public JSONObject getJSONStatus() {
JSONObject pump = new JSONObject();
JSONObject battery = new JSONObject();
JSONObject status = new JSONObject();
JSONObject extended = new JSONObject();
try {
battery.put("percent", getDanaRPump().batteryRemaining);
status.put("status", "normal");
status.put("timestamp", DateUtil.toISOString(new Date()));
if (isTempBasalInProgress()) {
extended.put("TempBasalAbsoluteRate", getTempBasalAbsoluteRate());
extended.put("TempBasalStart", DateUtil.toISOString(getTempBasal().timeStart));
extended.put("TempBasalRemaining", getTempBasal().getPlannedRemainingMinutes());
extended.put("IsExtended", getTempBasal().isExtended);
}
extended.put("PumpIOB", getDanaRPump().iob);
extended.put("LastBolus", DateUtil.toISOString(getDanaRPump().lastBolusTime));
extended.put("LastBolusAmount", getDanaRPump().lastBolusAmount);
pump.put("battery", battery);
pump.put("status", status);
pump.put("extended", extended);
pump.put("reservoir", (int) getDanaRPump().reservoirRemainingUnits);
pump.put("clock", DateUtil.toISOString(new Date()));
} catch (JSONException e) {
e.printStackTrace();
}
return pump;
}
@Override
public String deviceID() {
return getDanaRPump().serialNumber;
}
// GUI functions
private void updateGUI() {
final DateFormat formatTime = DateFormat.getTimeInstance(DateFormat.SHORT);
@ -786,149 +198,41 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface
@Override
public void run() {
if (getDanaRPump().lastConnection.getTime() != 0) {
Long agoMsec = new Date().getTime() - getDanaRPump().lastConnection.getTime();
if (DanaRPlugin.getDanaRPump().lastConnection.getTime() != 0) {
Long agoMsec = new Date().getTime() - DanaRPlugin.getDanaRPump().lastConnection.getTime();
int agoMin = (int) (agoMsec / 60d / 1000d);
lastConnectionView.setText(formatTime.format(getDanaRPump().lastConnection) + " (" + agoMin + " " + getString(R.string.minago) + ")");
lastConnectionView.setText(formatTime.format(DanaRPlugin.getDanaRPump().lastConnection) + " (" + agoMin + " " + getString(R.string.minago) + ")");
SetWarnColor.setColor(lastConnectionView, agoMin, 16d, 31d);
}
if (getDanaRPump().lastBolusTime.getTime() != 0) {
Long agoMsec = new Date().getTime() - getDanaRPump().lastBolusTime.getTime();
if (DanaRPlugin.getDanaRPump().lastBolusTime.getTime() != 0) {
Long agoMsec = new Date().getTime() - DanaRPlugin.getDanaRPump().lastBolusTime.getTime();
double agoHours = (int) (agoMsec / 60d / 60d / 1000d);
if (agoHours < 6) // max 6h back
lastBolusView.setText(formatTime.format(getDanaRPump().lastBolusTime) + " (" + DecimalFormatter.to1Decimal(agoHours) + " " + getString(R.string.hoursago) + ") " + DecimalFormatter.to2Decimal(getDanaRPump().lastBolusAmount) + " U");
lastBolusView.setText(formatTime.format(DanaRPlugin.getDanaRPump().lastBolusTime) + " (" + DecimalFormatter.to1Decimal(agoHours) + " " + getString(R.string.hoursago) + ") " + DecimalFormatter.to2Decimal(danaRPlugin.getDanaRPump().lastBolusAmount) + " U");
else lastBolusView.setText("");
}
dailyUnitsView.setText(DecimalFormatter.to0Decimal(getDanaRPump().dailyTotalUnits) + " / " + getDanaRPump().maxDailyTotalUnits + " U");
SetWarnColor.setColor(dailyUnitsView, getDanaRPump().dailyTotalUnits, getDanaRPump().maxDailyTotalUnits * 0.75d, getDanaRPump().maxDailyTotalUnits * 0.9d);
basaBasalRateView.setText("( " + (getDanaRPump().activeProfile + 1) + " ) " + DecimalFormatter.to2Decimal(getBaseBasalRate()) + " U/h");
if (isRealTempBasalInProgress()) {
tempBasalView.setText(getRealTempBasal().toString());
dailyUnitsView.setText(DecimalFormatter.to0Decimal(DanaRPlugin.getDanaRPump().dailyTotalUnits) + " / " + DanaRPlugin.getDanaRPump().maxDailyTotalUnits + " U");
SetWarnColor.setColor(dailyUnitsView, DanaRPlugin.getDanaRPump().dailyTotalUnits, DanaRPlugin.getDanaRPump().maxDailyTotalUnits * 0.75d, DanaRPlugin.getDanaRPump().maxDailyTotalUnits * 0.9d);
basaBasalRateView.setText("( " + (DanaRPlugin.getDanaRPump().activeProfile + 1) + " ) " + DecimalFormatter.to2Decimal(danaRPlugin.getBaseBasalRate()) + " U/h");
if (danaRPlugin.isRealTempBasalInProgress()) {
tempBasalView.setText(danaRPlugin.getRealTempBasal().toString());
} else {
tempBasalView.setText("");
}
if (isExtendedBoluslInProgress()) {
extendedBolusView.setText(getExtendedBolus().toString());
if (danaRPlugin.isExtendedBoluslInProgress()) {
extendedBolusView.setText(danaRPlugin.getExtendedBolus().toString());
} else {
extendedBolusView.setText("");
}
reservoirView.setText(DecimalFormatter.to0Decimal(getDanaRPump().reservoirRemainingUnits) + " / 300 U");
SetWarnColor.setColorInverse(reservoirView, getDanaRPump().reservoirRemainingUnits, 50d, 20d);
batteryView.setText("{fa-battery-" + (getDanaRPump().batteryRemaining / 25) + "}");
SetWarnColor.setColorInverse(batteryView, getDanaRPump().batteryRemaining, 51d, 26d);
iobView.setText(getDanaRPump().iob + " U");
reservoirView.setText(DecimalFormatter.to0Decimal(DanaRPlugin.getDanaRPump().reservoirRemainingUnits) + " / 300 U");
SetWarnColor.setColorInverse(reservoirView, DanaRPlugin.getDanaRPump().reservoirRemainingUnits, 50d, 20d);
batteryView.setText("{fa-battery-" + (DanaRPlugin.getDanaRPump().batteryRemaining / 25) + "}");
SetWarnColor.setColorInverse(batteryView, DanaRPlugin.getDanaRPump().batteryRemaining, 51d, 26d);
iobView.setText(DanaRPlugin.getDanaRPump().iob + " U");
}
});
}
/**
* Constraint interface
*/
@Override
public boolean isLoopEnabled() {
return true;
}
@Override
public boolean isClosedModeEnabled() {
return true;
}
@Override
public boolean isAutosensModeEnabled() {
return true;
}
@Override
public boolean isAMAModeEnabled() {
return true;
}
@SuppressWarnings("PointlessBooleanExpression")
@Override
public Double applyBasalConstraints(Double absoluteRate) {
double origAbsoluteRate = absoluteRate;
if (getDanaRPump() != null) {
if (absoluteRate > getDanaRPump().maxBasal) {
absoluteRate = getDanaRPump().maxBasal;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origAbsoluteRate + "U/h by pump constraint to " + absoluteRate + "U/h");
}
}
return absoluteRate;
}
@SuppressWarnings("PointlessBooleanExpression")
@Override
public Integer applyBasalConstraints(Integer percentRate) {
Integer origPercentRate = percentRate;
if (percentRate < 0) percentRate = 0;
if (percentRate > 200) percentRate = 200;
if (!Objects.equals(percentRate, origPercentRate) && Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit))
log.debug("Limiting percent rate " + origPercentRate + "% to " + percentRate + "%");
return percentRate;
}
@SuppressWarnings("PointlessBooleanExpression")
@Override
public Double applyBolusConstraints(Double insulin) {
double origInsulin = insulin;
if (getDanaRPump() != null) {
if (insulin > getDanaRPump().maxBolus) {
insulin = getDanaRPump().maxBolus;
if (Config.logConstraintsChanges && origInsulin != Constants.bolusOnlyForCheckLimit)
log.debug("Limiting bolus " + origInsulin + "U by pump constraint to " + insulin + "U");
}
}
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
return carbs;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
return maxIob;
}
@Nullable
@Override
public NSProfile getProfile() {
DanaRPump pump = getDanaRPump();
if (pump.lastSettingsRead.getTime() == 0)
return null; // no info now
return pump.createConvertedProfile();
}
// Reply for sms communicator
public String shortStatus() {
final DateFormat formatTime = DateFormat.getTimeInstance(DateFormat.SHORT);
String ret = "";
if (getDanaRPump().lastConnection.getTime() != 0) {
Long agoMsec = new Date().getTime() - getDanaRPump().lastConnection.getTime();
int agoMin = (int) (agoMsec / 60d / 1000d);
ret += "LastConn: " + agoMin + " minago\n";
}
if (getDanaRPump().lastBolusTime.getTime() != 0) {
Long agoMsec = new Date().getTime() - getDanaRPump().lastBolusTime.getTime();
long agoHours = (int) (agoMsec / 60d / 60d / 1000d);
ret += "LastBolus: " + DecimalFormatter.to2Decimal(getDanaRPump().lastBolusAmount) + "U @" + formatTime.format(getDanaRPump().lastBolusTime) + "\n";
}
if (isRealTempBasalInProgress()) {
ret += "Temp: " + getRealTempBasal().toString() + "\n";
}
if (isExtendedBoluslInProgress()) {
ret += "Extended: " + getExtendedBolus().toString() + "\n";
}
ret += "IOB: " + getDanaRPump().iob + "U\n";
ret += "Reserv: " + DecimalFormatter.to0Decimal(getDanaRPump().reservoirRemainingUnits) + "U\n";
ret += "Batt: " + getDanaRPump().batteryRemaining + "\n";
return ret;
}
// TODO: daily total constraint
}

View file

@ -0,0 +1,721 @@
package info.nightscout.androidaps.plugins.DanaR;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import com.squareup.otto.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DateFormat;
import java.util.Date;
import java.util.Objects;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
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.
*/
public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterface, ProfileInterface {
private static Logger log = LoggerFactory.getLogger(DanaRPlugin.class);
@Override
public String getFragmentClass() {
return DanaRFragment.class.getName();
}
static boolean fragmentPumpEnabled = true;
static boolean fragmentProfileEnabled = true;
static boolean fragmentPumpVisible = true;
public static ExecutionService sExecutionService;
private static DanaRPump sDanaRPump = new DanaRPump();
private static boolean useExtendedBoluses = false;
public static DanaRPump getDanaRPump() {
return sDanaRPump;
}
public DanaRPlugin() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
Context context = MainApp.instance().getApplicationContext();
Intent intent = new Intent(context, ExecutionService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
MainApp.bus().register(this);
}
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
log.debug("Service is disconnected");
sExecutionService = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
log.debug("Service is connected");
ExecutionService.LocalBinder mLocalBinder = (ExecutionService.LocalBinder) service;
sExecutionService = mLocalBinder.getServiceInstance();
}
};
@SuppressWarnings("UnusedParameters")
@Subscribe
public void onStatusEvent(final EventAppExit e) {
MainApp.instance().getApplicationContext().unbindService(mConnection);
}
@Subscribe
public void onStatusEvent(final EventPreferenceChange s) {
boolean previousValue = useExtendedBoluses;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
if (useExtendedBoluses != previousValue && isExtendedBoluslInProgress()) {
sExecutionService.extendedBolusStop();
}
}
// Plugin base interface
@Override
public int getType() {
return PluginBase.PUMP;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.danarpump);
}
@Override
public boolean isEnabled(int type) {
if (type == PluginBase.PROFILE) return fragmentProfileEnabled;
else if (type == PluginBase.PUMP) return fragmentPumpEnabled;
else if (type == PluginBase.CONSTRAINTS) return fragmentPumpEnabled;
return false;
}
@Override
public boolean isVisibleInTabs(int type) {
if (type == PluginBase.PROFILE || type == PluginBase.CONSTRAINTS) return false;
else if (type == PluginBase.PUMP) return fragmentPumpVisible;
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if (type == PluginBase.PROFILE) this.fragmentProfileEnabled = fragmentEnabled;
else if (type == PluginBase.PUMP) this.fragmentPumpEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == PluginBase.PUMP)
this.fragmentPumpVisible = fragmentVisible;
}
// Pump interface
@Override
public boolean isTempBasalInProgress() {
if (getRealTempBasal() != null) return true;
if (getExtendedBolus() != null && useExtendedBoluses) return true;
return false;
}
public boolean isRealTempBasalInProgress() {
return getRealTempBasal() != null; //TODO: crosscheck here
}
@Override
public boolean isExtendedBoluslInProgress() {
return getExtendedBolus() != null; //TODO: crosscheck here
}
@Override
public void setNewBasalProfile(NSProfile profile) {
if (sExecutionService == null) {
log.error("setNewBasalProfile sExecutionService is null");
return;
}
if (!sExecutionService.updateBasalsInPump(profile))
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.failedupdatebasalprofile));
}
@Override
public double getBaseBasalRate() {
return getDanaRPump().currentBasal;
}
@Override
public double getTempBasalAbsoluteRate() {
if (isRealTempBasalInProgress()) {
if (getRealTempBasal().isAbsolute) {
return getRealTempBasal().absolute;
} else {
Double baseRate = getBaseBasalRate();
Double tempRate = baseRate * (getRealTempBasal().percent / 100d);
return tempRate;
}
}
if (isExtendedBoluslInProgress() && useExtendedBoluses) {
return getBaseBasalRate() + getExtendedBolus().absolute;
}
return 0;
}
@Override
public double getTempBasalRemainingMinutes() {
if (isRealTempBasalInProgress())
return getRealTempBasal().getPlannedRemainingMinutes();
if (isExtendedBoluslInProgress() && useExtendedBoluses)
return getExtendedBolus().getPlannedRemainingMinutes();
return 0;
}
@Override
public TempBasal getTempBasal() {
if (isRealTempBasalInProgress())
return getRealTempBasal();
if (isExtendedBoluslInProgress() && useExtendedBoluses)
return getExtendedBolus();
return null;
}
public TempBasal getTempBasal(Date time) {
TempBasal temp = MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(time);
if (temp != null) return temp;
if (useExtendedBoluses)
return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(time);
return null;
}
public TempBasal getRealTempBasal() {
return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date());
}
@Override
public TempBasal getExtendedBolus() {
return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(new Date());
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder();
insulin = configBuilderPlugin.applyBolusConstraints(insulin);
if (insulin > 0 || carbs > 0) {
Treatment t = new Treatment();
boolean connectionOK = false;
if (carbs > 0) connectionOK = sExecutionService.carbsEntry(carbs);
if (insulin > 0) connectionOK = sExecutionService.bolus(insulin, t);
PumpEnactResult result = new PumpEnactResult();
result.success = connectionOK;
result.bolusDelivered = t.insulin;
result.carbsDelivered = carbs;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpActions)
log.debug("deliverTreatment: OK. Asked: " + insulin + " Delivered: " + result.bolusDelivered);
return result;
} else {
PumpEnactResult result = new PumpEnactResult();
result.success = false;
result.bolusDelivered = 0d;
result.carbsDelivered = 0;
result.comment = MainApp.instance().getString(R.string.danar_invalidinput);
log.error("deliverTreatment: Invalid input");
return result;
}
}
@Override
public void stopBolusDelivering() {
if (sExecutionService == null) {
log.error("stopBolusDelivering sExecutionService is null");
return;
}
sExecutionService.bolusStop();
}
// This is called from APS
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult();
ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder();
absoluteRate = configBuilderPlugin.applyBasalConstraints(absoluteRate);
final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d;
final boolean doLowTemp = absoluteRate < getBaseBasalRate();
final boolean doHighTemp = absoluteRate > getBaseBasalRate() && !useExtendedBoluses;
final boolean doExtendedTemp = absoluteRate > getBaseBasalRate() && useExtendedBoluses;
if (doTempOff) {
// If extended in progress
if (isExtendedBoluslInProgress() && useExtendedBoluses) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping extended bolus (doTempOff)");
return cancelExtendedBolus();
}
// If temp in progress
if (isRealTempBasalInProgress()) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping temp basal (doTempOff)");
return cancelRealTempBasal();
}
result.success = true;
result.enacted = false;
result.percent = 100;
result.isPercent = true;
result.isTempCancel = true;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: doTempOff OK");
return result;
}
if (doLowTemp || doHighTemp) {
Integer percentRate = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue();
if (percentRate < 100) percentRate = Round.ceilTo((double) percentRate, 10d).intValue();
else percentRate = Round.floorTo((double) percentRate, 10d).intValue();
if (percentRate > 200) {
percentRate = 200;
}
// If extended in progress
if (isExtendedBoluslInProgress() && useExtendedBoluses) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping extended bolus (doLowTemp || doHighTemp)");
result = cancelExtendedBolus();
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to stop previous extended bolus (doLowTemp || doHighTemp)");
return result;
}
}
// Check if some temp is already in progress
if (isRealTempBasalInProgress()) {
// Correct basal already set ?
if (getRealTempBasal().percent == percentRate) {
result.success = true;
result.percent = percentRate;
result.absolute = getTempBasalAbsoluteRate();
result.enacted = false;
result.duration = ((Double) getTempBasalRemainingMinutes()).intValue();
result.isPercent = true;
result.isTempCancel = false;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Correct temp basal already set (doLowTemp || doHighTemp)");
return result;
} else {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping temp basal (doLowTemp || doHighTemp)");
result = cancelRealTempBasal();
// Check for proper result
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to stop previous temp basal (doLowTemp || doHighTemp)");
return result;
}
}
}
// Convert duration from minutes to hours
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Setting temp basal " + percentRate + "% for " + durationInMinutes + " mins (doLowTemp || doHighTemp)");
return setTempBasalPercent(percentRate, durationInMinutes);
}
if (doExtendedTemp) {
// Check if some temp is already in progress
if (isRealTempBasalInProgress()) {
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Stopping temp basal (doExtendedTemp)");
result = cancelRealTempBasal();
// Check for proper result
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to stop previous temp basal (doExtendedTemp)");
return result;
}
}
// Calculate # of halfHours from minutes
Integer durationInHalfHours = Math.max(durationInMinutes / 30, 1);
// We keep current basal running so need to sub current basal
Double extendedRateToSet = absoluteRate - getBaseBasalRate();
extendedRateToSet = configBuilderPlugin.applyBasalConstraints(extendedRateToSet);
// needs to be rounded to 0.1
extendedRateToSet = Round.roundTo(extendedRateToSet, 0.1d);
// What is current rate of extended bolusing in u/h?
if (Config.logPumpActions) {
log.debug("setTempBasalAbsolute: Extended bolus in progress: " + isExtendedBoluslInProgress() + " rate: " + getDanaRPump().extendedBolusAbsoluteRate + "U/h duration remaining: " + getDanaRPump().extendedBolusRemainingMinutes + "min");
log.debug("setTempBasalAbsolute: Rate to set: " + extendedRateToSet + "U/h");
}
// Compare with extended rate in progress
if (Math.abs(getDanaRPump().extendedBolusAbsoluteRate - extendedRateToSet) < 0.02D) { // Allow some rounding diff
// correct extended already set
result.success = true;
result.absolute = getDanaRPump().extendedBolusAbsoluteRate;
result.enacted = false;
result.duration = getDanaRPump().extendedBolusRemainingMinutes;
result.isPercent = false;
result.isTempCancel = false;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Correct extended already set");
return result;
}
// Now set new extended, no need to to stop previous (if running) because it's replaced
Double extendedAmount = extendedRateToSet / 2 * durationInHalfHours;
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Setting extended: " + extendedAmount + "U halfhours: " + durationInHalfHours);
result = setExtendedBolus(extendedAmount, durationInMinutes);
if (!result.success) {
log.error("setTempBasalAbsolute: Failed to set extended bolus");
return result;
}
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Extended bolus set ok");
result.absolute = result.absolute + getBaseBasalRate();
return result;
}
// We should never end here
log.error("setTempBasalAbsolute: Internal error");
result.success = false;
result.comment = "Internal error";
return result;
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult();
ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder();
percent = configBuilderPlugin.applyBasalConstraints(percent);
if (percent < 0) {
result.isTempCancel = false;
result.enacted = false;
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_invalidinput);
log.error("setTempBasalPercent: Invalid input");
return result;
}
if (percent > 200) percent = 200;
if (getDanaRPump().isTempBasalInProgress && getDanaRPump().tempBasalPercent == percent) {
result.enacted = false;
result.success = true;
result.isTempCancel = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.duration = getDanaRPump().tempBasalRemainingMin;
result.percent = getDanaRPump().tempBasalPercent;
result.isPercent = true;
if (Config.logPumpActions)
log.debug("setTempBasalPercent: Correct value already set");
return result;
}
int durationInHours = Math.max(durationInMinutes / 60, 1);
boolean connectionOK = sExecutionService.tempBasal(percent, durationInHours);
if (connectionOK && getDanaRPump().isTempBasalInProgress && getDanaRPump().tempBasalPercent == percent) {
result.enacted = true;
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.isTempCancel = false;
result.duration = getDanaRPump().tempBasalRemainingMin;
result.percent = getDanaRPump().tempBasalPercent;
result.isPercent = true;
if (Config.logPumpActions)
log.debug("setTempBasalPercent: OK");
return result;
}
result.enacted = false;
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
log.error("setTempBasalPercent: Failed to set temp basal");
return result;
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder();
insulin = configBuilderPlugin.applyBolusConstraints(insulin);
// needs to be rounded to 0.1
insulin = Round.roundTo(insulin, 0.1d);
PumpEnactResult result = new PumpEnactResult();
if (getDanaRPump().isExtendedInProgress && Math.abs(getDanaRPump().extendedBolusAmount - insulin) < 0.1d) {
result.enacted = false;
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.duration = getDanaRPump().extendedBolusRemainingMinutes;
result.absolute = getDanaRPump().extendedBolusAbsoluteRate;
result.isPercent = false;
result.isTempCancel = false;
if (Config.logPumpActions)
log.debug("setExtendedBolus: Correct extended bolus already set");
return result;
}
int durationInHalfHours = Math.max(durationInMinutes / 30, 1);
boolean connectionOK = sExecutionService.extendedBolus(insulin, durationInHalfHours);
if (connectionOK && getDanaRPump().isExtendedInProgress && Math.abs(getDanaRPump().extendedBolusAmount - insulin) < 0.1d) {
result.enacted = true;
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.isTempCancel = false;
result.duration = getDanaRPump().extendedBolusRemainingMinutes;
result.absolute = getDanaRPump().extendedBolusAbsoluteRate;
result.bolusDelivered = getDanaRPump().extendedBolusAmount;
result.isPercent = false;
if (Config.logPumpActions)
log.debug("setExtendedBolus: OK");
return result;
}
result.enacted = false;
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
log.error("setExtendedBolus: Failed to extended bolus");
return result;
}
@Override
public PumpEnactResult cancelTempBasal() {
if (isRealTempBasalInProgress())
return cancelRealTempBasal();
if (isExtendedBoluslInProgress())
return cancelExtendedBolus();
PumpEnactResult result = new PumpEnactResult();
result.success = true;
result.enacted = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
result.isTempCancel = true;
return result;
}
public PumpEnactResult cancelRealTempBasal() {
PumpEnactResult result = new PumpEnactResult();
if (getDanaRPump().isTempBasalInProgress) {
sExecutionService.tempBasalStop();
result.enacted = true;
result.isTempCancel = true;
}
if (!getDanaRPump().isTempBasalInProgress) {
result.success = true;
result.isTempCancel = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpActions)
log.debug("cancelRealTempBasal: OK");
return result;
} else {
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
result.isTempCancel = true;
log.error("cancelRealTempBasal: Failed to cancel temp basal");
return result;
}
}
@Override
public PumpEnactResult cancelExtendedBolus() {
PumpEnactResult result = new PumpEnactResult();
if (getDanaRPump().isExtendedInProgress) {
sExecutionService.extendedBolusStop();
result.enacted = true;
result.isTempCancel = true;
}
if (!getDanaRPump().isExtendedInProgress) {
result.success = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpActions)
log.debug("cancelExtendedBolus: OK");
return result;
} else {
result.success = false;
result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly);
log.error("cancelExtendedBolus: Failed to cancel extended bolus");
return result;
}
}
public static void doConnect(String from) {
if (sExecutionService != null) sExecutionService.connect(from);
}
public static boolean isConnected() {
return sExecutionService != null && sExecutionService.isConnected();
}
public static boolean isConnecting() {
return sExecutionService != null && sExecutionService.isConnecting();
}
@Override
public JSONObject getJSONStatus() {
JSONObject pump = new JSONObject();
JSONObject battery = new JSONObject();
JSONObject status = new JSONObject();
JSONObject extended = new JSONObject();
try {
battery.put("percent", getDanaRPump().batteryRemaining);
status.put("status", "normal");
status.put("timestamp", DateUtil.toISOString(new Date()));
if (isTempBasalInProgress()) {
extended.put("TempBasalAbsoluteRate", getTempBasalAbsoluteRate());
extended.put("TempBasalStart", DateUtil.toISOString(getTempBasal().timeStart));
extended.put("TempBasalRemaining", getTempBasal().getPlannedRemainingMinutes());
extended.put("IsExtended", getTempBasal().isExtended);
}
extended.put("PumpIOB", getDanaRPump().iob);
extended.put("LastBolus", DateUtil.toISOString(getDanaRPump().lastBolusTime));
extended.put("LastBolusAmount", getDanaRPump().lastBolusAmount);
pump.put("battery", battery);
pump.put("status", status);
pump.put("extended", extended);
pump.put("reservoir", (int) getDanaRPump().reservoirRemainingUnits);
pump.put("clock", DateUtil.toISOString(new Date()));
} catch (JSONException e) {
e.printStackTrace();
}
return pump;
}
@Override
public String deviceID() {
return getDanaRPump().serialNumber;
}
/**
* Constraint interface
*/
@Override
public boolean isLoopEnabled() {
return true;
}
@Override
public boolean isClosedModeEnabled() {
return true;
}
@Override
public boolean isAutosensModeEnabled() {
return true;
}
@Override
public boolean isAMAModeEnabled() {
return true;
}
@SuppressWarnings("PointlessBooleanExpression")
@Override
public Double applyBasalConstraints(Double absoluteRate) {
double origAbsoluteRate = absoluteRate;
if (getDanaRPump() != null) {
if (absoluteRate > getDanaRPump().maxBasal) {
absoluteRate = getDanaRPump().maxBasal;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origAbsoluteRate + "U/h by pump constraint to " + absoluteRate + "U/h");
}
}
return absoluteRate;
}
@SuppressWarnings("PointlessBooleanExpression")
@Override
public Integer applyBasalConstraints(Integer percentRate) {
Integer origPercentRate = percentRate;
if (percentRate < 0) percentRate = 0;
if (percentRate > 200) percentRate = 200;
if (!Objects.equals(percentRate, origPercentRate) && Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit))
log.debug("Limiting percent rate " + origPercentRate + "% to " + percentRate + "%");
return percentRate;
}
@SuppressWarnings("PointlessBooleanExpression")
@Override
public Double applyBolusConstraints(Double insulin) {
double origInsulin = insulin;
if (getDanaRPump() != null) {
if (insulin > getDanaRPump().maxBolus) {
insulin = getDanaRPump().maxBolus;
if (Config.logConstraintsChanges && origInsulin != Constants.bolusOnlyForCheckLimit)
log.debug("Limiting bolus " + origInsulin + "U by pump constraint to " + insulin + "U");
}
}
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
return carbs;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
return maxIob;
}
@Nullable
@Override
public NSProfile getProfile() {
DanaRPump pump = getDanaRPump();
if (pump.lastSettingsRead.getTime() == 0)
return null; // no info now
return pump.createConvertedProfile();
}
// Reply for sms communicator
public String shortStatus() {
final DateFormat formatTime = DateFormat.getTimeInstance(DateFormat.SHORT);
String ret = "";
if (getDanaRPump().lastConnection.getTime() != 0) {
Long agoMsec = new Date().getTime() - getDanaRPump().lastConnection.getTime();
int agoMin = (int) (agoMsec / 60d / 1000d);
ret += "LastConn: " + agoMin + " minago\n";
}
if (getDanaRPump().lastBolusTime.getTime() != 0) {
ret += "LastBolus: " + DecimalFormatter.to2Decimal(getDanaRPump().lastBolusAmount) + "U @" + formatTime.format(getDanaRPump().lastBolusTime) + "\n";
}
if (isRealTempBasalInProgress()) {
ret += "Temp: " + getRealTempBasal().toString() + "\n";
}
if (isExtendedBoluslInProgress()) {
ret += "Extended: " + getExtendedBolus().toString() + "\n";
}
ret += "IOB: " + getDanaRPump().iob + "U\n";
ret += "Reserv: " + DecimalFormatter.to0Decimal(getDanaRPump().reservoirRemainingUnits) + "U\n";
ret += "Batt: " + getDanaRPump().batteryRemaining + "\n";
return ret;
}
// TODO: daily total constraint
}

View file

@ -19,6 +19,7 @@ import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DecimalFormatter;
@ -49,7 +50,7 @@ public class ProfileViewDialog extends DialogFragment {
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
profile = ((DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class)).getProfile();
profile = ((DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class)).getProfile();
}
@Override
@ -73,8 +74,8 @@ public class ProfileViewDialog extends DialogFragment {
mHandler.post(new Runnable() {
@Override
public void run() {
DanaRFragment.getDanaRPump().lastSettingsRead = new Date(0);
DanaRFragment.doConnect("ProfileViewDialog");
DanaRPlugin.getDanaRPump().lastSettingsRead = new Date(0);
DanaRPlugin.doConnect("ProfileViewDialog");
}
});
dismiss();

View file

@ -40,7 +40,7 @@ import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.DanaRHistoryRecord;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus;
@ -68,7 +68,7 @@ public class DanaRHistoryActivity extends Activity {
LinearLayoutManager llm;
static byte showingType = RecordTypes.RECORD_TYPE_ALARM;
List<DanaRHistoryRecord> historyList = new ArrayList<DanaRHistoryRecord>();
List<DanaRHistoryRecord> historyList = new ArrayList<>();
public static class TypeList {
public byte type;
@ -169,7 +169,7 @@ public class DanaRHistoryActivity extends Activity {
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_GLUCOSE, getString(R.string.danar_history_glucose)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_REFILL, getString(R.string.danar_history_refill)));
typeList.add(new TypeList(RecordTypes.RECORD_TYPE_SUSPEND, getString(R.string.danar_history_syspend)));
ArrayAdapter<TypeList> spinnerAdapter = new ArrayAdapter<TypeList>(this,
ArrayAdapter<TypeList> spinnerAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, typeList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
historyTypeSpinner.setAdapter(spinnerAdapter);
@ -215,7 +215,6 @@ public class DanaRHistoryActivity extends Activity {
mHandler.post(new Runnable() {
@Override
public void run() {
TypeList selected = (TypeList) historyTypeSpinner.getSelectedItem();
runOnUiThread(new Runnable() {
@Override
public void run() {
@ -252,12 +251,10 @@ public class DanaRHistoryActivity extends Activity {
clearCardView();
}
});
ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder();
profile = configBuilderFragment.getActiveProfile().getProfile();
profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
if (profile == null) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.noprofile));
finish();
return;
}
}
@ -272,8 +269,7 @@ public class DanaRHistoryActivity extends Activity {
@Override
public HistoryViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.danar_history_item, viewGroup, false);
HistoryViewHolder tempBasalsViewHolder = new HistoryViewHolder(v);
return tempBasalsViewHolder;
return new HistoryViewHolder(v);
}
@Override
@ -398,7 +394,7 @@ public class DanaRHistoryActivity extends Activity {
historyList = dao.query(preparedQuery);
} catch (SQLException e) {
e.printStackTrace();
historyList = new ArrayList<DanaRHistoryRecord>();
historyList = new ArrayList<>();
}
runOnUiThread(new Runnable() {
@Override
@ -409,7 +405,7 @@ public class DanaRHistoryActivity extends Activity {
}
private void clearCardView() {
historyList = new ArrayList<DanaRHistoryRecord>();
historyList = new ArrayList<>();
runOnUiThread(new Runnable() {
@Override
public void run() {
@ -436,10 +432,10 @@ public class DanaRHistoryActivity extends Activity {
new Runnable() {
@Override
public void run() {
if (c.sStatus == c.CONNECTING) {
if (c.sStatus == EventDanaRConnectionStatus.CONNECTING) {
statusView.setText(String.format(getString(R.string.danar_history_connectingfor), c.sSecondsElapsed));
log.debug("EventDanaRConnectionStatus: " + "Connecting for " + c.sSecondsElapsed + "s");
} else if (c.sStatus == c.CONNECTED) {
} else if (c.sStatus == EventDanaRConnectionStatus.CONNECTED) {
statusView.setText(MainApp.sResources.getString(R.string.connected));
log.debug("EventDanaRConnectionStatus: Connected");
} else {

View file

@ -11,7 +11,7 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.DanaRHistoryRecord;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus;
import info.nightscout.client.data.NSProfile;
@ -44,8 +44,8 @@ public class DanaRNSHistorySync {
public void sync(int what) {
try {
ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder();
NSProfile profile = configBuilderFragment.getActiveProfile().getProfile();
ConfigBuilderPlugin ConfigBuilderPlugin = MainApp.getConfigBuilder();
NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
if (profile == null) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.noprofile));
return;
@ -73,7 +73,7 @@ public class DanaRNSHistorySync {
nsrec.put("insulin", record.getRecordValue());
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_sbolus);
break;
@ -91,7 +91,7 @@ public class DanaRNSHistorySync {
cal.add(Calendar.MINUTE, -1 * record.getRecordDuration());
nsrec.put("created_at", DateUtil.toISOString(cal.getTime()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_ebolus);
} else {
@ -107,7 +107,7 @@ public class DanaRNSHistorySync {
nsrec.put("splitExt", 0);
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_dsbolus);
break;
@ -123,7 +123,7 @@ public class DanaRNSHistorySync {
cal.add(Calendar.MINUTE, -1 * record.getRecordDuration());
nsrec.put("created_at", DateUtil.toISOString(cal.getTime()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_debolus);
break;
@ -140,7 +140,7 @@ public class DanaRNSHistorySync {
nsrec.put("notes", "Error");
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_error);
break;
@ -152,7 +152,7 @@ public class DanaRNSHistorySync {
nsrec.put("notes", "Refill " + record.getRecordValue() + "U");
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_refill);
break;
@ -165,7 +165,7 @@ public class DanaRNSHistorySync {
nsrec.put("duration", 60);
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_basalhour);
break;
@ -181,7 +181,7 @@ public class DanaRNSHistorySync {
nsrec.put("glucoseType", "Finger");
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_glucose);
break;
@ -193,7 +193,7 @@ public class DanaRNSHistorySync {
nsrec.put("carbs", record.getRecordValue());
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_carbohydrate);
break;
@ -205,7 +205,7 @@ public class DanaRNSHistorySync {
nsrec.put("notes", "Alarm: " + record.getRecordAlarm());
nsrec.put("created_at", DateUtil.toISOString(record.getRecordDate()));
nsrec.put("enteredBy", MainApp.sResources.getString(R.string.app_name));
ConfigBuilderFragment.uploadCareportalEntryToNS(nsrec);
ConfigBuilderPlugin.uploadCareportalEntryToNS(nsrec);
uploaded++;
ev.message += MainApp.sResources.getString(R.string.danar_alarm);
break;

View file

@ -32,6 +32,7 @@ import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
import info.nightscout.androidaps.plugins.DanaR.SerialIOThread;
import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase;
@ -120,8 +121,7 @@ public class ExecutionService extends Service {
public ExecutionService() {
registerBus();
MainApp.instance().getApplicationContext().registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class);
danaRPump = danaRFragment.getDanaRPump();
danaRPump = DanaRPlugin.getDanaRPump();
PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ExecutionService");

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
/**
* Created by mike on 28.05.2016.
@ -28,10 +28,10 @@ public class MsgInitConnStatusOption extends MessageBase {
int h = intFromBuff(bytes, 7, 1);
int i = intFromBuff(bytes, 8, 1);
if (bytes.length >= 21) {
DanaRFragment.getDanaRPump().password = intFromBuff(bytes, 9, 2) ^ 0x3463;
DanaRFragment.getDanaRPump().isNewPump = true;
DanaRPlugin.getDanaRPump().password = intFromBuff(bytes, 9, 2) ^ 0x3463;
DanaRPlugin.getDanaRPump().isNewPump = true;
if (Config.logDanaMessageDetail)
log.debug("Pump password: " + DanaRFragment.getDanaRPump().password);
log.debug("Pump password: " + DanaRPlugin.getDanaRPump().password);
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
/**
* Created by mike on 05.07.2016.
@ -17,9 +17,9 @@ public class MsgSettingActiveProfile extends MessageBase {
}
public void handleMessage(byte[] bytes) {
DanaRFragment.getDanaRPump().activeProfile = intFromBuff(bytes, 0, 1);
DanaRPlugin.getDanaRPump().activeProfile = intFromBuff(bytes, 0, 1);
if (Config.logDanaMessageDetail)
log.debug("Active profile number: " + DanaRFragment.getDanaRPump().activeProfile);
log.debug("Active profile number: " + DanaRPlugin.getDanaRPump().activeProfile);
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
/**
@ -18,7 +18,7 @@ public class MsgSettingBasal extends MessageBase {
}
public void handleMessage(byte[] bytes) {
DanaRPump pump = DanaRFragment.getDanaRPump();
DanaRPump pump = DanaRPlugin.getDanaRPump();
if (pump.pumpProfiles == null) pump.pumpProfiles = new double[4][];
pump.pumpProfiles[pump.activeProfile] = new double[24];
for (int index = 0; index < 24; index++) {
@ -29,7 +29,7 @@ public class MsgSettingBasal extends MessageBase {
if (Config.logDanaMessageDetail)
for (int index = 0; index < 24; index++) {
log.debug("Basal " + String.format("%02d", index) + "h: " + DanaRFragment.getDanaRPump().pumpProfiles[DanaRFragment.getDanaRPump().activeProfile][index]);
log.debug("Basal " + String.format("%02d", index) + "h: " + DanaRPlugin.getDanaRPump().pumpProfiles[DanaRPlugin.getDanaRPump().activeProfile][index]);
}
}
}

View file

@ -4,16 +4,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
/**
* Created by mike on 05.07.2016.
*
*
* <p/>
* <p/>
* THIS IS BROKEN IN PUMP... SENDING ONLY 1 PROFILE
*
*/
public class MsgSettingBasalProfileAll extends MessageBase {
private static Logger log = LoggerFactory.getLogger(MsgSettingBasalProfileAll.class);
@ -23,8 +22,8 @@ public class MsgSettingBasalProfileAll extends MessageBase {
}
public void handleMessage(byte[] bytes) {
DanaRPump pump = DanaRFragment.getDanaRPump();
if (DanaRFragment.getDanaRPump().basal48Enable) {
DanaRPump pump = DanaRPlugin.getDanaRPump();
if (DanaRPlugin.getDanaRPump().basal48Enable) {
pump.pumpProfiles = new double[4][];
for (int profile = 0; profile < 4; profile++) {
int position = intFromBuff(bytes, 107 * profile, 1);
@ -51,10 +50,10 @@ public class MsgSettingBasalProfileAll extends MessageBase {
}
if (Config.logDanaMessageDetail) {
if (DanaRFragment.getDanaRPump().basal48Enable) {
if (DanaRPlugin.getDanaRPump().basal48Enable) {
for (int profile = 0; profile < 4; profile++) {
for (int index = 0; index < 24; index++) {
log.debug("Basal profile " + profile + ": " + String.format("%02d", index) + "h: " + DanaRFragment.getDanaRPump().pumpProfiles[profile][index]);
log.debug("Basal profile " + profile + ": " + String.format("%02d", index) + "h: " + DanaRPlugin.getDanaRPump().pumpProfiles[profile][index]);
}
}
} else {
@ -63,7 +62,7 @@ public class MsgSettingBasalProfileAll extends MessageBase {
log.debug("Basal profile " + profile + ": " +
String.format("%02d", (index / 2)) +
":" + String.format("%02d", (index % 2) * 30) + " : " +
DanaRFragment.getDanaRPump().pumpProfiles[profile][index]);
DanaRPlugin.getDanaRPump().pumpProfiles[profile][index]);
}
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
/**
@ -18,12 +18,12 @@ public class MsgSettingGlucose extends MessageBase {
}
public void handleMessage(byte[] bytes) {
DanaRFragment.getDanaRPump().units = intFromBuff(bytes, 0, 1);
DanaRFragment.getDanaRPump().easyBasalMode = intFromBuff(bytes, 1, 1);
DanaRPlugin.getDanaRPump().units = intFromBuff(bytes, 0, 1);
DanaRPlugin.getDanaRPump().easyBasalMode = intFromBuff(bytes, 1, 1);
if (Config.logDanaMessageDetail) {
log.debug("Pump units: " + (DanaRFragment.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Easy basal mode: " + DanaRFragment.getDanaRPump().easyBasalMode);
log.debug("Pump units: " + (DanaRPlugin.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Easy basal mode: " + DanaRPlugin.getDanaRPump().easyBasalMode);
}
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
/**
@ -18,14 +18,14 @@ public class MsgSettingMaxValues extends MessageBase {
}
public void handleMessage(byte[] bytes) {
DanaRFragment.getDanaRPump().maxBolus = intFromBuff(bytes, 0, 2) / 100d;
DanaRFragment.getDanaRPump().maxBasal = intFromBuff(bytes, 2, 2) / 100d;
DanaRFragment.getDanaRPump().maxDailyTotalUnits = intFromBuff(bytes, 4, 2) / 100;
DanaRPlugin.getDanaRPump().maxBolus = intFromBuff(bytes, 0, 2) / 100d;
DanaRPlugin.getDanaRPump().maxBasal = intFromBuff(bytes, 2, 2) / 100d;
DanaRPlugin.getDanaRPump().maxDailyTotalUnits = intFromBuff(bytes, 4, 2) / 100;
if (Config.logDanaMessageDetail) {
log.debug("Max bolus: " + DanaRFragment.getDanaRPump().maxBolus);
log.debug("Max basal: " + DanaRFragment.getDanaRPump().maxBasal);
log.debug("Total daily max units: " + DanaRFragment.getDanaRPump().maxDailyTotalUnits);
log.debug("Max bolus: " + DanaRPlugin.getDanaRPump().maxBolus);
log.debug("Max basal: " + DanaRPlugin.getDanaRPump().maxBasal);
log.debug("Total daily max units: " + DanaRPlugin.getDanaRPump().maxDailyTotalUnits);
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
/**
@ -18,27 +18,27 @@ public class MsgSettingProfileRatios extends MessageBase {
}
public void handleMessage(byte[] bytes) {
if (DanaRFragment.getDanaRPump().units == DanaRPump.UNITS_MGDL) {
DanaRFragment.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRFragment.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2);
DanaRFragment.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRFragment.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2);
DanaRFragment.getDanaRPump().currentAIDR = intFromBuff(bytes, 8, 1);
if (DanaRPlugin.getDanaRPump().units == DanaRPump.UNITS_MGDL) {
DanaRPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2);
DanaRPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2);
DanaRPlugin.getDanaRPump().currentAIDR = intFromBuff(bytes, 8, 1);
} else {
DanaRFragment.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRFragment.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2) / 100d;
DanaRFragment.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRFragment.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2) / 100d;
DanaRFragment.getDanaRPump().currentAIDR = intFromBuff(bytes, 8, 1);
DanaRPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2) / 100d;
DanaRPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2) / 100d;
DanaRPlugin.getDanaRPump().currentAIDR = intFromBuff(bytes, 8, 1);
}
if (Config.logDanaMessageDetail) {
log.debug("Pump units (saved): " + (DanaRFragment.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Current pump CIR: " + DanaRFragment.getDanaRPump().currentCIR);
log.debug("Current pump CF: " + DanaRFragment.getDanaRPump().currentCF);
log.debug("Current pump AI: " + DanaRFragment.getDanaRPump().currentAI);
log.debug("Current pump target: " + DanaRFragment.getDanaRPump().currentTarget);
log.debug("Current pump AIDR: " + DanaRFragment.getDanaRPump().currentAIDR);
log.debug("Pump units (saved): " + (DanaRPlugin.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Current pump CIR: " + DanaRPlugin.getDanaRPump().currentCIR);
log.debug("Current pump CF: " + DanaRPlugin.getDanaRPump().currentCF);
log.debug("Current pump AI: " + DanaRPlugin.getDanaRPump().currentAI);
log.debug("Current pump target: " + DanaRPlugin.getDanaRPump().currentTarget);
log.debug("Current pump AIDR: " + DanaRPlugin.getDanaRPump().currentAIDR);
}
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
/**
@ -18,38 +18,38 @@ public class MsgSettingProfileRatiosAll extends MessageBase {
}
public void handleMessage(byte[] bytes) {
if (DanaRFragment.getDanaRPump().units == DanaRPump.UNITS_MGDL) {
DanaRFragment.getDanaRPump().morningCIR = intFromBuff(bytes, 0, 2);
DanaRFragment.getDanaRPump().morningCF = intFromBuff(bytes, 2, 2);
DanaRFragment.getDanaRPump().afternoonCIR = intFromBuff(bytes, 4, 2);
DanaRFragment.getDanaRPump().afternoonCF = intFromBuff(bytes, 6, 2);
DanaRFragment.getDanaRPump().eveningCIR = intFromBuff(bytes, 8, 2);
DanaRFragment.getDanaRPump().eveningCF = intFromBuff(bytes, 10, 2);
DanaRFragment.getDanaRPump().nightCIR = intFromBuff(bytes, 12, 2);
DanaRFragment.getDanaRPump().nightCF = intFromBuff(bytes, 14, 2);
if (DanaRPlugin.getDanaRPump().units == DanaRPump.UNITS_MGDL) {
DanaRPlugin.getDanaRPump().morningCIR = intFromBuff(bytes, 0, 2);
DanaRPlugin.getDanaRPump().morningCF = intFromBuff(bytes, 2, 2);
DanaRPlugin.getDanaRPump().afternoonCIR = intFromBuff(bytes, 4, 2);
DanaRPlugin.getDanaRPump().afternoonCF = intFromBuff(bytes, 6, 2);
DanaRPlugin.getDanaRPump().eveningCIR = intFromBuff(bytes, 8, 2);
DanaRPlugin.getDanaRPump().eveningCF = intFromBuff(bytes, 10, 2);
DanaRPlugin.getDanaRPump().nightCIR = intFromBuff(bytes, 12, 2);
DanaRPlugin.getDanaRPump().nightCF = intFromBuff(bytes, 14, 2);
} else {
DanaRFragment.getDanaRPump().morningCIR = intFromBuff(bytes, 0, 2);
DanaRFragment.getDanaRPump().morningCF = intFromBuff(bytes, 2, 2) / 100d;
DanaRFragment.getDanaRPump().afternoonCIR = intFromBuff(bytes, 4, 2);
DanaRFragment.getDanaRPump().afternoonCF = intFromBuff(bytes, 6, 2) / 100d;
DanaRFragment.getDanaRPump().eveningCIR = intFromBuff(bytes, 8, 2);
DanaRFragment.getDanaRPump().eveningCF = intFromBuff(bytes, 10, 2) / 100d;
DanaRFragment.getDanaRPump().nightCIR = intFromBuff(bytes, 12, 2);
DanaRFragment.getDanaRPump().nightCF = intFromBuff(bytes, 14, 2) / 100d;
DanaRPlugin.getDanaRPump().morningCIR = intFromBuff(bytes, 0, 2);
DanaRPlugin.getDanaRPump().morningCF = intFromBuff(bytes, 2, 2) / 100d;
DanaRPlugin.getDanaRPump().afternoonCIR = intFromBuff(bytes, 4, 2);
DanaRPlugin.getDanaRPump().afternoonCF = intFromBuff(bytes, 6, 2) / 100d;
DanaRPlugin.getDanaRPump().eveningCIR = intFromBuff(bytes, 8, 2);
DanaRPlugin.getDanaRPump().eveningCF = intFromBuff(bytes, 10, 2) / 100d;
DanaRPlugin.getDanaRPump().nightCIR = intFromBuff(bytes, 12, 2);
DanaRPlugin.getDanaRPump().nightCF = intFromBuff(bytes, 14, 2) / 100d;
}
if (Config.logDanaMessageDetail) {
log.debug("Pump units: " + (DanaRFragment.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Current pump morning CIR: " + DanaRFragment.getDanaRPump().morningCIR);
log.debug("Current pump morning CF: " + DanaRFragment.getDanaRPump().morningCF);
log.debug("Current pump afternoon CIR: " + DanaRFragment.getDanaRPump().afternoonCIR);
log.debug("Current pump afternoon CF: " + DanaRFragment.getDanaRPump().afternoonCF);
log.debug("Current pump evening CIR: " + DanaRFragment.getDanaRPump().eveningCIR);
log.debug("Current pump evening CF: " + DanaRFragment.getDanaRPump().eveningCF);
log.debug("Current pump night CIR: " + DanaRFragment.getDanaRPump().nightCIR);
log.debug("Current pump night CF: " + DanaRFragment.getDanaRPump().nightCF);
log.debug("Pump units: " + (DanaRPlugin.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Current pump morning CIR: " + DanaRPlugin.getDanaRPump().morningCIR);
log.debug("Current pump morning CF: " + DanaRPlugin.getDanaRPump().morningCF);
log.debug("Current pump afternoon CIR: " + DanaRPlugin.getDanaRPump().afternoonCIR);
log.debug("Current pump afternoon CF: " + DanaRPlugin.getDanaRPump().afternoonCF);
log.debug("Current pump evening CIR: " + DanaRPlugin.getDanaRPump().eveningCIR);
log.debug("Current pump evening CF: " + DanaRPlugin.getDanaRPump().eveningCF);
log.debug("Current pump night CIR: " + DanaRPlugin.getDanaRPump().nightCIR);
log.debug("Current pump night CF: " + DanaRPlugin.getDanaRPump().nightCF);
}
DanaRFragment.getDanaRPump().createConvertedProfile();
DanaRPlugin.getDanaRPump().createConvertedProfile();
}
}

View file

@ -6,7 +6,7 @@ import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
public class MsgSettingPumpTime extends MessageBase {
private static Logger log = LoggerFactory.getLogger(MsgSettingPumpTime.class);
@ -29,6 +29,6 @@ public class MsgSettingPumpTime extends MessageBase {
if (Config.logDanaMessageDetail)
log.debug("Pump time: " + time);
DanaRFragment.getDanaRPump().pumpTime = time;
DanaRPlugin.getDanaRPump().pumpTime = time;
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
/**
* Created by mike on 05.07.2016.
@ -17,13 +17,13 @@ public class MsgSettingShippingInfo extends MessageBase {
}
public void handleMessage(byte[] bytes) {
DanaRFragment.getDanaRPump().serialNumber = stringFromBuff(bytes, 0, 10);
DanaRFragment.getDanaRPump().shippingDate = dateFromBuff(bytes, 10);
DanaRFragment.getDanaRPump().shippingCountry = asciiStringFromBuff(bytes, 13, 3);
DanaRPlugin.getDanaRPump().serialNumber = stringFromBuff(bytes, 0, 10);
DanaRPlugin.getDanaRPump().shippingDate = dateFromBuff(bytes, 10);
DanaRPlugin.getDanaRPump().shippingCountry = asciiStringFromBuff(bytes, 13, 3);
if (Config.logDanaMessageDetail) {
log.debug("Serial number: " + DanaRFragment.getDanaRPump().serialNumber);
log.debug("Shipping date: " + DanaRFragment.getDanaRPump().shippingDate);
log.debug("Shipping country: " + DanaRFragment.getDanaRPump().shippingCountry);
log.debug("Serial number: " + DanaRPlugin.getDanaRPump().serialNumber);
log.debug("Shipping date: " + DanaRPlugin.getDanaRPump().shippingDate);
log.debug("Shipping country: " + DanaRPlugin.getDanaRPump().shippingCountry);
}
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
public class MsgStatus extends MessageBase {
private static Logger log = LoggerFactory.getLogger(MsgStatus.class);
@ -14,25 +14,25 @@ public class MsgStatus extends MessageBase {
}
public void handleMessage(byte[] bytes) {
DanaRFragment.getDanaRPump().dailyTotalUnits = intFromBuff(bytes, 0, 3) / 750d;
DanaRFragment.getDanaRPump().isExtendedInProgress = intFromBuff(bytes, 3, 1) == 1;
DanaRFragment.getDanaRPump().extendedBolusMinutes = intFromBuff(bytes, 4, 2);
DanaRFragment.getDanaRPump().extendedBolusAmount = intFromBuff(bytes, 6, 2) / 100d;
DanaRPlugin.getDanaRPump().dailyTotalUnits = intFromBuff(bytes, 0, 3) / 750d;
DanaRPlugin.getDanaRPump().isExtendedInProgress = intFromBuff(bytes, 3, 1) == 1;
DanaRPlugin.getDanaRPump().extendedBolusMinutes = intFromBuff(bytes, 4, 2);
DanaRPlugin.getDanaRPump().extendedBolusAmount = intFromBuff(bytes, 6, 2) / 100d;
Double lastBolusAmount = intFromBuff(bytes, 13, 2) / 100d;
if (lastBolusAmount != 0d) {
DanaRFragment.getDanaRPump().lastBolusTime = dateTimeFromBuff(bytes, 8);
DanaRFragment.getDanaRPump().lastBolusAmount = lastBolusAmount;
DanaRPlugin.getDanaRPump().lastBolusTime = dateTimeFromBuff(bytes, 8);
DanaRPlugin.getDanaRPump().lastBolusAmount = lastBolusAmount;
}
DanaRFragment.getDanaRPump().iob = intFromBuff(bytes, 15, 2) / 100d;
DanaRPlugin.getDanaRPump().iob = intFromBuff(bytes, 15, 2) / 100d;
if (Config.logDanaMessageDetail) {
log.debug("Daily total: " + DanaRFragment.getDanaRPump().dailyTotalUnits);
log.debug("Is extended bolus running: " + DanaRFragment.getDanaRPump().isExtendedInProgress);
log.debug("Extended bolus min: " + DanaRFragment.getDanaRPump().extendedBolusMinutes);
log.debug("Extended bolus amount: " + DanaRFragment.getDanaRPump().extendedBolusAmount);
log.debug("Last bolus time: " + DanaRFragment.getDanaRPump().lastBolusTime);
log.debug("Last bolus amount: " + DanaRFragment.getDanaRPump().lastBolusAmount);
log.debug("IOB: " + DanaRFragment.getDanaRPump().iob);
log.debug("Daily total: " + DanaRPlugin.getDanaRPump().dailyTotalUnits);
log.debug("Is extended bolus running: " + DanaRPlugin.getDanaRPump().isExtendedInProgress);
log.debug("Extended bolus min: " + DanaRPlugin.getDanaRPump().extendedBolusMinutes);
log.debug("Extended bolus amount: " + DanaRPlugin.getDanaRPump().extendedBolusAmount);
log.debug("Last bolus time: " + DanaRPlugin.getDanaRPump().lastBolusTime);
log.debug("Last bolus amount: " + DanaRPlugin.getDanaRPump().lastBolusAmount);
log.debug("IOB: " + DanaRPlugin.getDanaRPump().iob);
}
}
}

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
public class MsgStatusBasic extends MessageBase {
@ -27,17 +27,17 @@ public class MsgStatusBasic extends MessageBase {
boolean isTempBasalInProgress = intFromBuff(bytes, 15, 1) == 1;
int batteryRemaining = intFromBuff(bytes, 20, 1);
DanaRFragment.getDanaRPump().pumpSuspended = pumpSuspended;
DanaRFragment.getDanaRPump().calculatorEnabled = calculatorEnabled;
DanaRFragment.getDanaRPump().dailyTotalUnits = dailyTotalUnits;
DanaRFragment.getDanaRPump().maxDailyTotalUnits = maxDailyTotalUnits;
DanaRFragment.getDanaRPump().reservoirRemainingUnits = reservoirRemainingUnits;
DanaRFragment.getDanaRPump().bolusBlocked = bolusBlocked;
DanaRFragment.getDanaRPump().currentBasal = currentBasal;
DanaRFragment.getDanaRPump().tempBasalPercent = tempBasalPercent;
DanaRFragment.getDanaRPump().isExtendedInProgress = isExtendedInProgress;
DanaRFragment.getDanaRPump().isTempBasalInProgress = isTempBasalInProgress;
DanaRFragment.getDanaRPump().batteryRemaining = batteryRemaining;
DanaRPlugin.getDanaRPump().pumpSuspended = pumpSuspended;
DanaRPlugin.getDanaRPump().calculatorEnabled = calculatorEnabled;
DanaRPlugin.getDanaRPump().dailyTotalUnits = dailyTotalUnits;
DanaRPlugin.getDanaRPump().maxDailyTotalUnits = maxDailyTotalUnits;
DanaRPlugin.getDanaRPump().reservoirRemainingUnits = reservoirRemainingUnits;
DanaRPlugin.getDanaRPump().bolusBlocked = bolusBlocked;
DanaRPlugin.getDanaRPump().currentBasal = currentBasal;
DanaRPlugin.getDanaRPump().tempBasalPercent = tempBasalPercent;
DanaRPlugin.getDanaRPump().isExtendedInProgress = isExtendedInProgress;
DanaRPlugin.getDanaRPump().isTempBasalInProgress = isTempBasalInProgress;
DanaRPlugin.getDanaRPump().batteryRemaining = batteryRemaining;
if (Config.logDanaMessageDetail) {
log.debug("Pump suspended: " + pumpSuspended);

View file

@ -9,11 +9,10 @@ import java.sql.SQLException;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
public class MsgStatusBolusExtended extends MessageBase {
@ -36,13 +35,13 @@ public class MsgStatusBolusExtended extends MessageBase {
Date extendedBolusStart = isExtendedInProgress ? getDateFromSecAgo(extendedBolusSoFarInSecs) : new Date(0);
int extendedBolusRemainingMinutes = extendedBolusMinutes - extendedBolusSoFarInMinutes;
DanaRFragment.getDanaRPump().isExtendedInProgress = isExtendedInProgress;
DanaRFragment.getDanaRPump().extendedBolusMinutes = extendedBolusMinutes;
DanaRFragment.getDanaRPump().extendedBolusAmount = extendedBolusAmount;
DanaRFragment.getDanaRPump().extendedBolusSoFarInMinutes = extendedBolusSoFarInMinutes;
DanaRFragment.getDanaRPump().extendedBolusAbsoluteRate = extendedBolusAbsoluteRate;
DanaRFragment.getDanaRPump().extendedBolusStart = extendedBolusStart;
DanaRFragment.getDanaRPump().extendedBolusRemainingMinutes = extendedBolusRemainingMinutes;
DanaRPlugin.getDanaRPump().isExtendedInProgress = isExtendedInProgress;
DanaRPlugin.getDanaRPump().extendedBolusMinutes = extendedBolusMinutes;
DanaRPlugin.getDanaRPump().extendedBolusAmount = extendedBolusAmount;
DanaRPlugin.getDanaRPump().extendedBolusSoFarInMinutes = extendedBolusSoFarInMinutes;
DanaRPlugin.getDanaRPump().extendedBolusAbsoluteRate = extendedBolusAbsoluteRate;
DanaRPlugin.getDanaRPump().extendedBolusStart = extendedBolusStart;
DanaRPlugin.getDanaRPump().extendedBolusRemainingMinutes = extendedBolusRemainingMinutes;
updateExtendedBolusInDB();
@ -63,14 +62,14 @@ public class MsgStatusBolusExtended extends MessageBase {
}
public static void updateExtendedBolusInDB() {
DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class);
DanaRPump danaRPump = danaRFragment.getDanaRPump();
DanaRPlugin DanaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
DanaRPump danaRPump = DanaRPlugin.getDanaRPump();
Date now = new Date();
try {
if (danaRFragment.isExtendedBoluslInProgress()) {
TempBasal extendedBolus = danaRFragment.getExtendedBolus();
if (DanaRPlugin.isExtendedBoluslInProgress()) {
TempBasal extendedBolus = DanaRPlugin.getExtendedBolus();
if (danaRPump.isExtendedInProgress) {
if (extendedBolus.absolute != danaRPump.extendedBolusAbsoluteRate) {
// Close current extended

View file

@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
/**
@ -18,25 +18,25 @@ public class MsgStatusProfile extends MessageBase {
}
public void handleMessage(byte[] bytes) {
if (DanaRFragment.getDanaRPump().units == DanaRPump.UNITS_MGDL) {
DanaRFragment.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRFragment.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2);
DanaRFragment.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRFragment.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2);
if (DanaRPlugin.getDanaRPump().units == DanaRPump.UNITS_MGDL) {
DanaRPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2);
DanaRPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2);
} else {
DanaRFragment.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRFragment.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2) / 100d;
DanaRFragment.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRFragment.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2) / 100d;
DanaRPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2);
DanaRPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2) / 100d;
DanaRPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d;
DanaRPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2) / 100d;
}
if (Config.logDanaMessageDetail) {
log.debug("Pump units (saved): " + (DanaRFragment.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Current pump CIR: " + DanaRFragment.getDanaRPump().currentCIR);
log.debug("Current pump CF: " + DanaRFragment.getDanaRPump().currentCF);
log.debug("Current pump AI: " + DanaRFragment.getDanaRPump().currentAI);
log.debug("Current pump target: " + DanaRFragment.getDanaRPump().currentTarget);
log.debug("Current pump AIDR: " + DanaRFragment.getDanaRPump().currentAIDR);
log.debug("Pump units (saved): " + (DanaRPlugin.getDanaRPump().units == DanaRPump.UNITS_MGDL ? "MGDL" : "MMOL"));
log.debug("Current pump CIR: " + DanaRPlugin.getDanaRPump().currentCIR);
log.debug("Current pump CF: " + DanaRPlugin.getDanaRPump().currentCF);
log.debug("Current pump AI: " + DanaRPlugin.getDanaRPump().currentAI);
log.debug("Current pump target: " + DanaRPlugin.getDanaRPump().currentTarget);
log.debug("Current pump AIDR: " + DanaRPlugin.getDanaRPump().currentAIDR);
}
}
}

View file

@ -9,11 +9,10 @@ import java.sql.SQLException;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.DanaRPump;
public class MsgStatusTempBasal extends MessageBase {
@ -31,11 +30,11 @@ public class MsgStatusTempBasal extends MessageBase {
int tempBasalRemainingMin = (tempBasalTotalSec - tempBasalRunningSeconds) / 60;
Date tempBasalStart = isTempBasalInProgress ? getDateFromTempBasalSecAgo(tempBasalRunningSeconds) : new Date(0);
DanaRFragment.getDanaRPump().isTempBasalInProgress = isTempBasalInProgress;
DanaRFragment.getDanaRPump().tempBasalPercent = tempBasalPercent;
DanaRFragment.getDanaRPump().tempBasalRemainingMin = tempBasalRemainingMin;
DanaRFragment.getDanaRPump().tempBasalTotalSec = tempBasalTotalSec;
DanaRFragment.getDanaRPump().tempBasalStart = tempBasalStart;
DanaRPlugin.getDanaRPump().isTempBasalInProgress = isTempBasalInProgress;
DanaRPlugin.getDanaRPump().tempBasalPercent = tempBasalPercent;
DanaRPlugin.getDanaRPump().tempBasalRemainingMin = tempBasalRemainingMin;
DanaRPlugin.getDanaRPump().tempBasalTotalSec = tempBasalTotalSec;
DanaRPlugin.getDanaRPump().tempBasalStart = tempBasalStart;
updateTempBasalInDB();
@ -54,14 +53,14 @@ public class MsgStatusTempBasal extends MessageBase {
}
public static void updateTempBasalInDB() {
DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class);
DanaRPump danaRPump = danaRFragment.getDanaRPump();
DanaRPlugin DanaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
DanaRPump danaRPump = DanaRPlugin.getDanaRPump();
Date now = new Date();
try {
if (danaRFragment.isRealTempBasalInProgress()) {
TempBasal tempBasal = danaRFragment.getRealTempBasal();
if (DanaRPlugin.isRealTempBasalInProgress()) {
TempBasal tempBasal = DanaRPlugin.getRealTempBasal();
if (danaRPump.isTempBasalInProgress) {
if (tempBasal.percent != danaRPump.tempBasalPercent) {
// Close current temp basal

View file

@ -2,17 +2,8 @@ package info.nightscout.androidaps.plugins.Loop;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.app.Fragment;
import android.support.v7.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -24,25 +15,21 @@ import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventRefreshOpenLoop;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui;
public class LoopFragment extends Fragment implements View.OnClickListener, PluginBase {
public class LoopFragment extends Fragment implements View.OnClickListener, FragmentBase {
private static Logger log = LoggerFactory.getLogger(LoopFragment.class);
private static LoopPlugin loopPlugin = new LoopPlugin();
public static LoopPlugin getPlugin() {
return loopPlugin;
}
Button runNowButton;
TextView lastRunView;
TextView lastEnactView;
@ -51,72 +38,6 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug
TextView constraintsProcessedView;
TextView setByPumpView;
Handler mHandler;
public static HandlerThread mHandlerThread;
public class LastRun {
public APSResult request = null;
public APSResult constraintsProcessed = null;
public PumpEnactResult setByPump = null;
public String source = null;
public Date lastAPSRun = null;
public Date lastEnact = null;
public Date lastOpenModeAccept = null;
}
static public LastRun lastRun = null;
private boolean fragmentEnabled = false;
private boolean fragmentVisible = true;
@Override
public int getType() {
return PluginBase.LOOP;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.loop);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
public LoopFragment() {
super();
mHandlerThread = new HandlerThread(LoopFragment.class.getSimpleName());
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
registerBus();
}
public static LoopFragment newInstance() {
LoopFragment fragment = new LoopFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -136,12 +57,15 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug
return view;
}
private void registerBus() {
try {
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
@Override
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@ -149,145 +73,30 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug
public void onClick(View view) {
switch (view.getId()) {
case R.id.loop_run:
invoke(true);
loopPlugin.invoke(true);
break;
}
}
@Subscribe
public void onStatusEvent(final EventTreatmentChange ev) {
ConstraintsInterface constraintsInterface = MainApp.getConfigBuilder();
invoke(true);
public void onStatusEvent(final EventLoopUpdateGui ev) {
updateGUI();
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
invoke(true);
}
public void invoke(boolean allowNotification) {
ConstraintsInterface constraintsInterface = MainApp.getConfigBuilder();
if (!constraintsInterface.isLoopEnabled()) {
public void onStatusEvent(final EventLoopSetLastRunGui ev) {
clearGUI();
final Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
lastRunView.setText(activity.getString(R.string.loopdisabled));
lastRunView.setText(ev.text);
}
});
return;
}
final ConfigBuilderFragment configBuilder = MainApp.getConfigBuilder();
APSResult result = null;
if (configBuilder == null || !isEnabled(PluginBase.GENERAL))
return;
APSInterface usedAPS = null;
ArrayList<PluginBase> apsPlugins = MainApp.getSpecificPluginsList(PluginBase.APS);
for (PluginBase p : apsPlugins) {
APSInterface aps = (APSInterface) p;
if (!p.isEnabled(PluginBase.APS)) continue;
aps.invoke();
result = aps.getLastAPSResult();
if (result == null) continue;
if (result.changeRequested) {
// APS plugin is requesting change, stop processing
usedAPS = aps;
break;
}
}
// Check if we have any result
if (result == null) {
clearGUI();
final Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
lastRunView.setText(activity.getString(R.string.noapsselected));
}
});
return;
}
// check rate for constrais
final APSResult resultAfterConstraints = result.clone();
resultAfterConstraints.rate = constraintsInterface.applyBasalConstraints(resultAfterConstraints.rate);
if (lastRun == null) lastRun = new LastRun();
lastRun.request = result;
lastRun.constraintsProcessed = resultAfterConstraints;
lastRun.lastAPSRun = new Date();
lastRun.source = usedAPS != null ? ((PluginBase) usedAPS).getName() : "";
lastRun.setByPump = null;
if (constraintsInterface.isClosedModeEnabled()) {
if (result.changeRequested) {
final PumpEnactResult waiting = new PumpEnactResult();
final PumpEnactResult previousResult = lastRun.setByPump;
waiting.queued = true;
lastRun.setByPump = waiting;
updateGUI();
mHandler.post(new Runnable() {
@Override
public void run() {
final PumpEnactResult applyResult = configBuilder.applyAPSRequest(resultAfterConstraints);
if (applyResult.enacted) {
lastRun.setByPump = applyResult;
lastRun.lastEnact = lastRun.lastAPSRun;
} else {
lastRun.setByPump = previousResult;
}
updateGUI();
}
});
} else {
lastRun.setByPump = null;
lastRun.source = null;
}
} else {
if (result.changeRequested && allowNotification) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainApp.instance().getApplicationContext());
builder.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(MainApp.sResources.getString(R.string.openloop_newsuggestion))
.setContentText(resultAfterConstraints.toString())
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_ALARM)
.setVisibility(Notification.VISIBILITY_PUBLIC);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(MainApp.instance().getApplicationContext(), MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainApp.instance().getApplicationContext());
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManager mNotificationManager =
(NotificationManager) MainApp.instance().getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(Constants.notificationID, builder.build());
MainApp.bus().post(new EventRefreshOpenLoop());
}
}
updateGUI();
MainApp.getConfigBuilder().uploadDeviceStatus();
}
void updateGUI() {
Activity activity = getActivity();
@ -295,13 +104,13 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (lastRun != null) {
requestView.setText(lastRun.request != null ? lastRun.request.toSpanned() : "");
constraintsProcessedView.setText(lastRun.constraintsProcessed != null ? lastRun.constraintsProcessed.toSpanned() : "");
setByPumpView.setText(lastRun.setByPump != null ? lastRun.setByPump.toSpanned() : "");
sourceView.setText(lastRun.source != null ? lastRun.source : "");
lastRunView.setText(lastRun.lastAPSRun != null && lastRun.lastAPSRun.getTime() != 0 ? lastRun.lastAPSRun.toLocaleString() : "");
lastEnactView.setText(lastRun.lastEnact != null && lastRun.lastEnact.getTime() != 0 ? lastRun.lastEnact.toLocaleString() : "");
if (loopPlugin.lastRun != null) {
requestView.setText(loopPlugin.lastRun.request != null ? loopPlugin.lastRun.request.toSpanned() : "");
constraintsProcessedView.setText(loopPlugin.lastRun.constraintsProcessed != null ? loopPlugin.lastRun.constraintsProcessed.toSpanned() : "");
setByPumpView.setText(loopPlugin.lastRun.setByPump != null ? loopPlugin.lastRun.setByPump.toSpanned() : "");
sourceView.setText(loopPlugin.lastRun.source != null ? loopPlugin.lastRun.source : "");
lastRunView.setText(loopPlugin.lastRun.lastAPSRun != null && loopPlugin.lastRun.lastAPSRun.getTime() != 0 ? loopPlugin.lastRun.lastAPSRun.toLocaleString() : "");
lastEnactView.setText(loopPlugin.lastRun.lastEnact != null && loopPlugin.lastRun.lastEnact.getTime() != 0 ? loopPlugin.lastRun.lastEnact.toLocaleString() : "");
}
}
});

View file

@ -0,0 +1,212 @@
package info.nightscout.androidaps.plugins.Loop;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v7.app.NotificationCompat;
import com.squareup.otto.Subscribe;
import java.util.ArrayList;
import java.util.Date;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventRefreshOpenLoop;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui;
/**
* Created by mike on 05.08.2016.
*/
public class LoopPlugin implements PluginBase {
private static Handler sHandler;
private static HandlerThread sHandlerThread;
private boolean fragmentEnabled = false;
private boolean fragmentVisible = true;
public class LastRun {
public APSResult request = null;
public APSResult constraintsProcessed = null;
public PumpEnactResult setByPump = null;
public String source = null;
public Date lastAPSRun = null;
public Date lastEnact = null;
public Date lastOpenModeAccept;
}
static public LastRun lastRun = null;
public LoopPlugin() {
if (sHandlerThread == null) {
sHandlerThread = new HandlerThread(LoopFragment.class.getSimpleName());
sHandlerThread.start();
sHandler = new Handler(sHandlerThread.getLooper());
}
MainApp.bus().register(this);
}
@Override
public String getFragmentClass() {
return LoopFragment.class.getName();
}
@Override
public int getType() {
return PluginBase.LOOP;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.loop);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Subscribe
public void onStatusEvent(final EventTreatmentChange ev) {
invoke(true);
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
invoke(true);
}
public void invoke(boolean allowNotification) {
ConstraintsInterface constraintsInterface = MainApp.getConfigBuilder();
if (!constraintsInterface.isLoopEnabled()) {
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.sResources.getString(R.string.loopdisabled)));
return;
}
final ConfigBuilderPlugin configBuilder = MainApp.getConfigBuilder();
APSResult result = null;
if (configBuilder == null || !isEnabled(PluginBase.GENERAL))
return;
APSInterface usedAPS = configBuilder.getActiveAPS();
if (usedAPS != null && ((PluginBase) usedAPS).isEnabled(PluginBase.APS)) {
usedAPS.invoke();
result = usedAPS.getLastAPSResult();
}
// Check if we have any result
if (result == null) {
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.sResources.getString(R.string.noapsselected)));
return;
}
// check rate for constrais
final APSResult resultAfterConstraints = result.clone();
resultAfterConstraints.rate = constraintsInterface.applyBasalConstraints(resultAfterConstraints.rate);
if (lastRun == null) lastRun = new LastRun();
lastRun.request = result;
lastRun.constraintsProcessed = resultAfterConstraints;
lastRun.lastAPSRun = new Date();
lastRun.source = ((PluginBase) usedAPS).getName();
lastRun.setByPump = null;
if (constraintsInterface.isClosedModeEnabled()) {
if (result.changeRequested) {
final PumpEnactResult waiting = new PumpEnactResult();
final PumpEnactResult previousResult = lastRun.setByPump;
waiting.queued = true;
lastRun.setByPump = waiting;
MainApp.bus().post(new EventLoopUpdateGui());
sHandler.post(new Runnable() {
@Override
public void run() {
final PumpEnactResult applyResult = configBuilder.applyAPSRequest(resultAfterConstraints);
if (applyResult.enacted) {
lastRun.setByPump = applyResult;
lastRun.lastEnact = lastRun.lastAPSRun;
} else {
lastRun.setByPump = previousResult;
}
MainApp.bus().post(new EventLoopUpdateGui());
}
});
} else {
lastRun.setByPump = null;
lastRun.source = null;
}
} else {
if (result.changeRequested && allowNotification) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainApp.instance().getApplicationContext());
builder.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(MainApp.sResources.getString(R.string.openloop_newsuggestion))
.setContentText(resultAfterConstraints.toString())
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_ALARM)
.setVisibility(Notification.VISIBILITY_PUBLIC);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(MainApp.instance().getApplicationContext(), MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainApp.instance().getApplicationContext());
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManager mNotificationManager =
(NotificationManager) MainApp.instance().getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(Constants.notificationID, builder.build());
MainApp.bus().post(new EventRefreshOpenLoop());
}
}
MainApp.bus().post(new EventLoopUpdateGui());
MainApp.getConfigBuilder().uploadDeviceStatus();
}
}

View file

@ -0,0 +1,12 @@
package info.nightscout.androidaps.plugins.Loop.events;
/**
* Created by mike on 05.08.2016.
*/
public class EventLoopSetLastRunGui {
public String text = null;
public EventLoopSetLastRunGui(String text) {
this.text = text;
}
}

View file

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

View file

@ -1,289 +0,0 @@
package info.nightscout.androidaps.plugins.LowSuspend;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.SafeParse;
/**
* LOW SUSPEND ALGORITHM
* <p/>
* Define projection as BG + 6 * avgdelta (estimated BG in 30 min)
* <p/>
* If BG is bellow low threshold and projection too: set basal rate to 0 U/h if low temp is not running
* else if projection is bellow low threshold: set basal rate to 0 U/h if low temp is not running
* else if exists low temp: cancel it
* else no change
*/
// TODO: replace algorithm and name
public class LowSuspendFragment extends Fragment implements View.OnClickListener, PluginBase, APSInterface {
private static Logger log = LoggerFactory.getLogger(LowSuspendFragment.class);
Button run;
TextView lastRunView;
TextView glucoseStatusView;
TextView minBgView;
TextView resultView;
TextView requestView;
// last values
class LastRun {
public Boolean lastLow = null;
public Boolean lastLowProjected = null;
public Double lastMinBg = null;
public DatabaseHelper.GlucoseStatus lastGlucoseStatus = null;
public Date lastAPSRun = null;
public APSResult lastAPSResult = null;
}
static LastRun lastRun = null;
private boolean fragmentEnabled = false;
private boolean fragmentVisible = true;
public LowSuspendFragment() {
super();
registerBus();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.lowsuspend);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.APS;
}
@Override
public APSResult getLastAPSResult() {
if (lastRun != null)
return lastRun.lastAPSResult;
else return null;
}
@Override
public Date getLastAPSRun() {
if (lastRun != null)
return lastRun.lastAPSRun;
else return null;
}
public static LowSuspendFragment newInstance() {
return new LowSuspendFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.lowsuspend_fragment, container, false);
run = (Button) view.findViewById(R.id.lowsuspend_run);
run.setOnClickListener(this);
lastRunView = (TextView) view.findViewById(R.id.lowsuspend_lastrun);
glucoseStatusView = (TextView) view.findViewById(R.id.lowsuspend_glucosestatus);
minBgView = (TextView) view.findViewById(R.id.lowsuspend_minbg);
resultView = (TextView) view.findViewById(R.id.lowsuspend_result);
requestView = (TextView) view.findViewById(R.id.lowsuspend_request);
updateGUI();
return view;
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.lowsuspend_run:
invoke();
break;
}
}
@Override
public void invoke() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
DatabaseHelper.GlucoseStatus glucoseStatus = MainApp.getDbHelper().getGlucoseStatusData();
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
if (!isEnabled(PluginBase.APS)) {
updateResultGUI(MainApp.instance().getString(R.string.openapsma_disabled));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_disabled));
return;
}
if (glucoseStatus == null) {
updateResultGUI(MainApp.instance().getString(R.string.openapsma_noglucosedata));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_noglucosedata));
return;
}
if (profile == null) {
updateResultGUI(MainApp.instance().getString(R.string.openapsma_noprofile));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_noprofile));
return;
}
if (pump == null) {
updateResultGUI(MainApp.instance().getString(R.string.openapsma_nopump));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_nopump));
return;
}
String minBgDefault = "90";
if (!profile.getUnits().equals(Constants.MGDL)) {
minBgDefault = "5";
}
double minBgMgdl = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("lowsuspend_lowthreshold", minBgDefault)), profile.getUnits());
boolean lowProjected = (glucoseStatus.glucose + 6.0 * glucoseStatus.avgdelta) < minBgMgdl;
boolean low = glucoseStatus.glucose < minBgMgdl;
APSResult request = new APSResult();
boolean isTempBasalInProgress = pump.isTempBasalInProgress();
Double tempBasalRate = pump.getTempBasalAbsoluteRate();
Date now = new Date();
if (low && lowProjected) {
if (!isTempBasalInProgress || tempBasalRate != 0d) {
request.changeRequested = true;
request.rate = 0d;
request.duration = 30;
request.reason = MainApp.instance().getString(R.string.lowsuspend_lowmessage);
} else {
request.changeRequested = false;
request.reason = MainApp.instance().getString(R.string.nochangerequested);
}
} else if (lowProjected) {
if (!isTempBasalInProgress || tempBasalRate != 0d) {
request.changeRequested = true;
request.rate = 0d;
request.duration = 30;
request.reason = MainApp.instance().getString(R.string.lowsuspend_lowprojectedmessage);
} else {
request.changeRequested = false;
request.reason = MainApp.instance().getString(R.string.nochangerequested);
}
} else if (isTempBasalInProgress && tempBasalRate == 0d) {
request.changeRequested = true;
request.rate = 0;
request.duration = 0;
request.reason = MainApp.instance().getString(R.string.lowsuspend_cancelmessage);
} else {
request.changeRequested = false;
request.reason = MainApp.instance().getString(R.string.nochangerequested);
}
lastRun = new LastRun();
lastRun.lastMinBg = minBgMgdl;
lastRun.lastLow = low;
lastRun.lastLowProjected = lowProjected;
lastRun.lastGlucoseStatus = glucoseStatus;
lastRun.lastAPSResult = request;
lastRun.lastAPSRun = now;
updateGUI();
}
void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (lastRun != null) {
glucoseStatusView.setText(lastRun.lastGlucoseStatus.toSpanned());
minBgView.setText(DecimalFormatter.to1Decimal(lastRun.lastMinBg) + " mgdl");
resultView.setText(Html.fromHtml("<b>" + getString(R.string.lowsuspend_low) + "</b>: " + lastRun.lastLow + "<br></b>" + getString(R.string.lowsuspend_lowprojected) + "</b>: " + lastRun.lastLowProjected));
requestView.setText(lastRun.lastAPSResult.toSpanned());
lastRunView.setText(lastRun.lastAPSRun.toLocaleString());
}
}
});
}
void updateResultGUI(final String text) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
resultView.setText(text);
glucoseStatusView.setText("");
minBgView.setText("");
requestView.setText("");
lastRunView.setText("");
}
});
}
}

View file

@ -20,35 +20,17 @@ import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.client.data.NSProfile;
public class MM640gFragment extends Fragment implements PluginBase, PumpInterface, BgSourceInterface {
public class MM640gFragment extends Fragment implements FragmentBase {
private static MM640gPlugin mm640gPlugin = new MM640gPlugin();
boolean fragmentPumpEnabled = false;
boolean fragmentProfileEnabled = false;
boolean fragmentBgSourceEnabled = false;
boolean fragmentPumpVisible = true;
public MM640gFragment() {
registerBus();
}
public static MM640gFragment newInstance() {
MM640gFragment fragment = new MM640gFragment();
return fragment;
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
public static MM640gPlugin getPlugin() {
return mm640gPlugin;
}
@Override
@ -58,154 +40,4 @@ public class MM640gFragment extends Fragment implements PluginBase, PumpInterfac
return view;
}
/**
* Plugin base interface
*/
@Override
public int getType() {
return PluginBase.PUMP;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.mm640g);
}
@Override
public boolean isEnabled(int type) {
if (type == PluginBase.PROFILE) return fragmentProfileEnabled;
else if (type == PluginBase.BGSOURCE) return fragmentBgSourceEnabled;
else if (type == PluginBase.PUMP) return fragmentPumpEnabled;
else if (type == PluginBase.CONSTRAINTS) return fragmentPumpEnabled;
return false;
}
@Override
public boolean isVisibleInTabs(int type) {
if (type == PluginBase.PROFILE || type == PluginBase.CONSTRAINTS) return false;
else if (type == PluginBase.PUMP) return fragmentPumpVisible;
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if (type == PluginBase.PROFILE) this.fragmentProfileEnabled = fragmentEnabled;
if (type == PluginBase.BGSOURCE) this.fragmentBgSourceEnabled = fragmentEnabled;
else if (type == PluginBase.PUMP) this.fragmentPumpEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == PluginBase.PUMP)
this.fragmentPumpVisible = fragmentVisible;
}
/**
* Plugin communications
*/
@Subscribe
public void onStatusEvent(final EventPreferenceChange s) {
}
/**
* Pump Interface
*/
@Override
public boolean isTempBasalInProgress() {
return false;
}
@Override
public boolean isExtendedBoluslInProgress() {
return false;
}
@Override
public void setNewBasalProfile(NSProfile profile) {
}
@Override
public double getBaseBasalRate() {
return 0;
}
@Override
public double getTempBasalAbsoluteRate() {
return 0;
}
@Override
public double getTempBasalRemainingMinutes() {
return 0;
}
@Override
public TempBasal getTempBasal(Date time) {
return null;
}
@Override
public TempBasal getTempBasal() {
return null;
}
@Override
public TempBasal getExtendedBolus() {
return null;
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
return new PumpEnactResult();
}
@Override
public void stopBolusDelivering() {
}
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
return new PumpEnactResult();
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
return new PumpEnactResult();
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
return new PumpEnactResult();
}
@Override
public PumpEnactResult cancelTempBasal() {
return new PumpEnactResult();
}
@Override
public PumpEnactResult cancelExtendedBolus() {
return new PumpEnactResult();
}
@Override
public JSONObject getJSONStatus() {
return new JSONObject();
}
@Override
public String deviceID() {
return "MM640G"; // TODO: probably serial goes here
}
}

View file

@ -0,0 +1,187 @@
package info.nightscout.androidaps.plugins.MM640g;
import android.content.Context;
import com.squareup.otto.Subscribe;
import org.json.JSONObject;
import java.util.Date;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.client.data.NSProfile;
/**
* Created by mike on 05.08.2016.
*/
public class MM640gPlugin implements PluginBase, PumpInterface, BgSourceInterface {
boolean fragmentPumpEnabled = false;
boolean fragmentProfileEnabled = false;
boolean fragmentBgSourceEnabled = false;
boolean fragmentPumpVisible = true;
@Override
public String getFragmentClass() {
return MM640gFragment.class.getName();
}
/**
* Plugin base interface
*/
@Override
public int getType() {
return PluginBase.PUMP;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.mm640g);
}
@Override
public boolean isEnabled(int type) {
if (type == PluginBase.PROFILE) return fragmentProfileEnabled;
else if (type == PluginBase.BGSOURCE) return fragmentBgSourceEnabled;
else if (type == PluginBase.PUMP) return fragmentPumpEnabled;
else if (type == PluginBase.CONSTRAINTS) return fragmentPumpEnabled;
return false;
}
@Override
public boolean isVisibleInTabs(int type) {
if (type == PluginBase.PROFILE || type == PluginBase.CONSTRAINTS) return false;
else if (type == PluginBase.PUMP) return fragmentPumpVisible;
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if (type == PluginBase.PROFILE) this.fragmentProfileEnabled = fragmentEnabled;
if (type == PluginBase.BGSOURCE) this.fragmentBgSourceEnabled = fragmentEnabled;
else if (type == PluginBase.PUMP) this.fragmentPumpEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == PluginBase.PUMP)
this.fragmentPumpVisible = fragmentVisible;
}
/**
* Plugin communications
*/
@Subscribe
public void onStatusEvent(final EventPreferenceChange s) {
}
/**
* Pump Interface
*/
@Override
public boolean isTempBasalInProgress() {
return false;
}
@Override
public boolean isExtendedBoluslInProgress() {
return false;
}
@Override
public void setNewBasalProfile(NSProfile profile) {
}
@Override
public double getBaseBasalRate() {
return 0;
}
@Override
public double getTempBasalAbsoluteRate() {
return 0;
}
@Override
public double getTempBasalRemainingMinutes() {
return 0;
}
@Override
public TempBasal getTempBasal(Date time) {
return null;
}
@Override
public TempBasal getTempBasal() {
return null;
}
@Override
public TempBasal getExtendedBolus() {
return null;
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
return new PumpEnactResult();
}
@Override
public void stopBolusDelivering() {
}
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
return new PumpEnactResult();
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
return new PumpEnactResult();
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
return new PumpEnactResult();
}
@Override
public PumpEnactResult cancelTempBasal() {
return new PumpEnactResult();
}
@Override
public PumpEnactResult cancelExtendedBolus() {
return new PumpEnactResult();
}
@Override
public JSONObject getJSONStatus() {
return new JSONObject();
}
@Override
public String deviceID() {
return "MM640G"; // TODO: probably serial goes here
}
}

View file

@ -1,11 +1,7 @@
package info.nightscout.androidaps.plugins.NSProfileViewer;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
@ -14,23 +10,18 @@ import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
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.client.data.NSProfile;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.NSProfileViewer.events.EventNSProfileViewerUpdateGui;
import info.nightscout.utils.DecimalFormatter;
public class NSProfileViewerFragment extends Fragment implements PluginBase, ProfileInterface {
private static Logger log = LoggerFactory.getLogger(NSProfileViewerFragment.class);
public class NSProfileViewerFragment extends Fragment implements FragmentBase {
private static NSProfileViewerPlugin nsProfileViewerPlugin = new NSProfileViewerPlugin();
public static NSProfileViewerPlugin getPlugin() {
return nsProfileViewerPlugin;
}
private static TextView noProfile;
private static TextView units;
@ -41,57 +32,6 @@ public class NSProfileViewerFragment extends Fragment implements PluginBase, Pro
private static TextView basal;
private static TextView target;
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
NSProfile profile = null;
public NSProfileViewerFragment () {
super();
loadNSProfile();
registerBus();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.profileviewer);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.PROFILE;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -106,95 +46,48 @@ public class NSProfileViewerFragment extends Fragment implements PluginBase, Pro
basal = (TextView) layout.findViewById(R.id.profileview_basal);
target = (TextView) layout.findViewById(R.id.profileview_target);
setContent();
updateGUI();
return layout;
}
public static NSProfileViewerFragment newInstance() {
return new NSProfileViewerFragment();
}
private void setContent() {
if (profile == null) {
noProfile.setVisibility(View.VISIBLE);
return;
} else {
noProfile.setVisibility(View.GONE);
}
units.setText(profile.getUnits());
dia.setText(DecimalFormatter.to2Decimal(profile.getDia()) + " h");
activeProfile.setText(profile.getActiveProfile());
ic.setText(profile.getIcList());
isf.setText(profile.getIsfList());
basal.setText(profile.getBasalList());
target.setText(profile.getTargetList());
}
private void registerBus() {
try {
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
@Override
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@Subscribe
public void onStatusEvent(final EventNewBasalProfile ev) {
profile = new NSProfile(ev.newNSProfile.getData(), ev.newNSProfile.getActiveProfile());
storeNSProfile();
public void onStatusEvent(final EventNSProfileViewerUpdateGui ev) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
setContent();
updateGUI();
}
});
else
log.debug("EventNewBG: Activity is null");
}
private void storeNSProfile() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("profile", profile.getData().toString());
editor.putString("activeProfile", profile.getActiveProfile());
editor.apply();
if (Config.logPrefsChange)
log.debug("Storing profile");
}
private void loadNSProfile() {
if (Config.logPrefsChange)
log.debug("Loading stored profile");
SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String activeProfile = store.getString("activeProfile", null);
String profileString = store.getString("profile", null);
if (profileString != null) {
if (Config.logPrefsChange) {
log.debug("Loaded profile: " + profileString);
log.debug("Loaded active profile: " + activeProfile);
try {
profile = new NSProfile(new JSONObject(profileString), activeProfile);
} catch (JSONException e) {
e.printStackTrace();
profile = null;
}
}
private void updateGUI() {
if (nsProfileViewerPlugin.profile == null) {
noProfile.setVisibility(View.VISIBLE);
return;
} else {
if (Config.logPrefsChange) {
log.debug("Stored profile not found");
// force restart of nsclient to fetch profile
Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
}
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());
}
@Nullable
@Override
public NSProfile getProfile() {
return profile;
}
}

View file

@ -0,0 +1,131 @@
package info.nightscout.androidaps.plugins.NSProfileViewer;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import com.squareup.otto.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
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.client.data.NSProfile;
/**
* Created by mike on 05.08.2016.
*/
public class NSProfileViewerPlugin implements PluginBase, ProfileInterface {
private static Logger log = LoggerFactory.getLogger(NSProfileViewerPlugin.class);
@Override
public String getFragmentClass() {
return NSProfileViewerFragment.class.getName();
}
static boolean fragmentEnabled = true;
static boolean fragmentVisible = true;
static NSProfile profile = null;
public NSProfileViewerPlugin() {
MainApp.bus().register(this);
loadNSProfile();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.profileviewer);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.PROFILE;
}
@Subscribe
public void onStatusEvent(final EventNewBasalProfile ev) {
profile = new NSProfile(ev.newNSProfile.getData(), ev.newNSProfile.getActiveProfile());
storeNSProfile();
MainApp.bus().post(new EventNSProfileViewerUpdateGui());
}
private void storeNSProfile() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("profile", profile.getData().toString());
editor.putString("activeProfile", profile.getActiveProfile());
editor.apply();
if (Config.logPrefsChange)
log.debug("Storing profile");
}
private void loadNSProfile() {
if (Config.logPrefsChange)
log.debug("Loading stored profile");
SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String activeProfile = store.getString("activeProfile", null);
String profileString = store.getString("profile", null);
if (profileString != null) {
if (Config.logPrefsChange) {
log.debug("Loaded profile: " + profileString);
log.debug("Loaded active profile: " + activeProfile);
try {
profile = new NSProfile(new JSONObject(profileString), activeProfile);
} catch (JSONException e) {
e.printStackTrace();
profile = null;
}
}
} else {
if (Config.logPrefsChange) {
log.debug("Stored profile not found");
// force restart of nsclient to fetch profile
Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
}
}
}
@Nullable
@Override
public NSProfile getProfile() {
return profile;
}
}

View file

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

View file

@ -2,9 +2,7 @@ package info.nightscout.androidaps.plugins.Objectives;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
@ -17,67 +15,29 @@ import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.interfaces.FragmentBase;
public class ObjectivesFragment extends Fragment implements View.OnClickListener, PluginBase, ConstraintsInterface {
public class ObjectivesFragment extends Fragment implements View.OnClickListener, FragmentBase {
private static Logger log = LoggerFactory.getLogger(ObjectivesFragment.class);
private static ObjectivesPlugin objectivesPlugin = new ObjectivesPlugin();
public static ObjectivesPlugin getPlugin() {
return objectivesPlugin;
}
RecyclerView recyclerView;
LinearLayoutManager llm;
CheckBox enableFake;
boolean fragmentVisible = true;
@Override
public int getType() {
return PluginBase.CONSTRAINTS;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.objectives);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
LoopFragment loopFragment = (LoopFragment) MainApp.getSpecificPlugin(LoopFragment.class);
return fragmentVisible && loopFragment != null && loopFragment.isVisibleInTabs(type);
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public void onClick(View v) {
int id = v.getId();
@ -87,146 +47,11 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
}
}
class Objective {
Integer num;
String objective;
String gate;
Date started;
Integer durationInDays;
Date accomplished;
Objective(Integer num, String objective, String gate, Date started, Integer durationInDays, Date accomplished) {
this.num = num;
this.objective = objective;
this.gate = gate;
this.started = started;
this.durationInDays = durationInDays;
this.accomplished = accomplished;
}
}
// Objective 0
public boolean bgIsAvailableInNS = false;
public boolean pumpStatusIsAvailableInNS = false;
// Objective 1
public Integer manualEnacts = 0;
public final Integer manualEnactsNeeded = 20;
class RequirementResult {
boolean done = false;
String comment = "";
public RequirementResult(boolean done, String comment) {
this.done = done;
this.comment = comment;
}
}
private String yesOrNo(boolean yes) {
if (yes) return "";
else return "---";
}
private RequirementResult requirementsMet(Integer objNum) {
switch (objNum) {
case 0:
return new RequirementResult(bgIsAvailableInNS && pumpStatusIsAvailableInNS,
getString(R.string.objectives_bgavailableinns) + ": " + yesOrNo(bgIsAvailableInNS)
+ " " + getString(R.string.objectives_pumpstatusavailableinns) + ": " + yesOrNo(pumpStatusIsAvailableInNS));
case 1:
return new RequirementResult(manualEnacts >= manualEnactsNeeded,
getString(R.string.objectives_manualenacts) + ": " + manualEnacts + "/" + manualEnactsNeeded);
case 2:
return new RequirementResult(true, "");
default:
return new RequirementResult(false, "");
}
}
private List<Objective> objectives;
private void initializeData() {
objectives = new ArrayList<>();
objectives.add(new Objective(0,
MainApp.sResources.getString(R.string.objectives_0_objective),
MainApp.sResources.getString(R.string.objectives_0_gate),
new Date(0, 0, 0),
1, // 1 day
new Date(0, 0, 0)));
objectives.add(new Objective(1,
MainApp.sResources.getString(R.string.objectives_1_objective),
MainApp.sResources.getString(R.string.objectives_1_gate),
new Date(0, 0, 0),
7, // 7 days
new Date(0, 0, 0)));
objectives.add(new Objective(2,
MainApp.sResources.getString(R.string.objectives_2_objective),
MainApp.sResources.getString(R.string.objectives_2_gate),
new Date(0, 0, 0),
0, // 0 days
new Date(0, 0, 0)));
objectives.add(new Objective(3,
MainApp.sResources.getString(R.string.objectives_3_objective),
MainApp.sResources.getString(R.string.objectives_3_gate),
new Date(0, 0, 0),
5, // 5 days
new Date(0, 0, 0)));
objectives.add(new Objective(4,
MainApp.sResources.getString(R.string.objectives_4_objective),
MainApp.sResources.getString(R.string.objectives_4_gate),
new Date(0, 0, 0),
1,
new Date(0, 0, 0)));
objectives.add(new Objective(5,
MainApp.sResources.getString(R.string.objectives_5_objective),
MainApp.sResources.getString(R.string.objectives_5_gate),
new Date(0, 0, 0),
7,
new Date(0, 0, 0)));
objectives.add(new Objective(6,
MainApp.sResources.getString(R.string.objectives_6_objective),
"",
new Date(0, 0, 0),
1,
new Date(0, 0, 0)));
}
public void saveProgress() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
for (int num = 0; num < objectives.size(); num++) {
Objective o = objectives.get(num);
editor.putLong("Objectives" + num + "started", o.started.getTime());
editor.putLong("Objectives" + num + "accomplished", o.accomplished.getTime());
}
editor.putBoolean("Objectives" + "bgIsAvailableInNS", bgIsAvailableInNS);
editor.putBoolean("Objectives" + "pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS);
editor.putInt("Objectives" + "manualEnacts", manualEnacts);
editor.apply();
if (Config.logPrefsChange)
log.debug("Objectives stored");
}
void loadProgress() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
for (int num = 0; num < objectives.size(); num++) {
Objective o = objectives.get(num);
o.started = new Date(settings.getLong("Objectives" + num + "started", 0));
o.accomplished = new Date(settings.getLong("Objectives" + num + "accomplished", 0));
}
bgIsAvailableInNS = settings.getBoolean("Objectives" + "bgIsAvailableInNS", false);
pumpStatusIsAvailableInNS = settings.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false);
manualEnacts = settings.getInt("Objectives" + "manualEnacts", 0);
if (Config.logPrefsChange)
log.debug("Objectives loaded");
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ObjectiveViewHolder> {
List<Objective> objectives;
List<ObjectivesPlugin.Objective> objectives;
RecyclerViewAdapter(List<Objective> objectives) {
RecyclerViewAdapter(List<ObjectivesPlugin.Objective> objectives) {
this.objectives = objectives;
}
@ -238,8 +63,8 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
@Override
public void onBindViewHolder(ObjectiveViewHolder holder, int position) {
Objective o = objectives.get(position);
RequirementResult requirementsMet = requirementsMet(position);
ObjectivesPlugin.Objective o = objectives.get(position);
ObjectivesPlugin.RequirementResult requirementsMet = objectivesPlugin.requirementsMet(position);
Context context = MainApp.instance().getApplicationContext();
holder.position.setText(String.valueOf(position + 1));
holder.objective.setText(o.objective);
@ -254,19 +79,19 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
holder.startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Objective o = (Objective) v.getTag();
ObjectivesPlugin.Objective o = (ObjectivesPlugin.Objective) v.getTag();
o.started = new Date();
updateGUI();
saveProgress();
objectivesPlugin.saveProgress();
}
});
holder.verifyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Objective o = (Objective) v.getTag();
if (requirementsMet(o.num).done || enableFake.isChecked()) {
ObjectivesPlugin.Objective o = (ObjectivesPlugin.Objective) v.getTag();
if (objectivesPlugin.requirementsMet(o.num).done || enableFake.isChecked()) {
o.accomplished = new Date();
updateGUI();
saveProgress();
objectivesPlugin.saveProgress();
}
}
});
@ -350,22 +175,6 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
}
}
public ObjectivesFragment() {
super();
initializeData();
loadProgress();
registerBus();
}
public static ObjectivesFragment newInstance() {
return new ObjectivesFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -383,111 +192,46 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
});
// Add correct translations to array after app is initialized
objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective);
objectives.get(1).objective = MainApp.sResources.getString(R.string.objectives_1_objective);
objectives.get(2).objective = MainApp.sResources.getString(R.string.objectives_2_objective);
objectives.get(3).objective = MainApp.sResources.getString(R.string.objectives_3_objective);
objectives.get(4).objective = MainApp.sResources.getString(R.string.objectives_4_objective);
objectives.get(5).objective = MainApp.sResources.getString(R.string.objectives_5_objective);
objectives.get(6).objective = MainApp.sResources.getString(R.string.objectives_6_objective);
objectives.get(0).gate = MainApp.sResources.getString(R.string.objectives_0_gate);
objectives.get(1).gate = MainApp.sResources.getString(R.string.objectives_1_gate);
objectives.get(2).gate = MainApp.sResources.getString(R.string.objectives_2_gate);
objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate);
objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate);
objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate);
objectivesPlugin.objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective);
objectivesPlugin.objectives.get(1).objective = MainApp.sResources.getString(R.string.objectives_1_objective);
objectivesPlugin.objectives.get(2).objective = MainApp.sResources.getString(R.string.objectives_2_objective);
objectivesPlugin.objectives.get(3).objective = MainApp.sResources.getString(R.string.objectives_3_objective);
objectivesPlugin.objectives.get(4).objective = MainApp.sResources.getString(R.string.objectives_4_objective);
objectivesPlugin.objectives.get(5).objective = MainApp.sResources.getString(R.string.objectives_5_objective);
objectivesPlugin.objectives.get(6).objective = MainApp.sResources.getString(R.string.objectives_6_objective);
objectivesPlugin.objectives.get(0).gate = MainApp.sResources.getString(R.string.objectives_0_gate);
objectivesPlugin.objectives.get(1).gate = MainApp.sResources.getString(R.string.objectives_1_gate);
objectivesPlugin.objectives.get(2).gate = MainApp.sResources.getString(R.string.objectives_2_gate);
objectivesPlugin.objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate);
objectivesPlugin.objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate);
objectivesPlugin.objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate);
updateGUI();
return view;
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
updateGUI();
}
});
else
log.debug("EventNewBG: Activity is null");
}
void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(objectives);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(objectivesPlugin.objectives);
recyclerView.setAdapter(adapter);
}
});
}
/**
* Constraints interface
**/
@Override
public boolean isLoopEnabled() {
return objectives.get(1).started.getTime() > 0;
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
}
@Override
public boolean isClosedModeEnabled() {
return objectives.get(3).started.getTime() > 0;
}
@Override
public boolean isAutosensModeEnabled() {
return objectives.get(5).started.getTime() > 0;
}
@Override
public boolean isAMAModeEnabled() {
return objectives.get(6).started.getTime() > 0;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
if (objectives.get(4).started.getTime() > 0)
return maxIob;
else {
if (Config.logConstraintsChanges)
log.debug("Limiting maxIOB " + maxIob + " to " + 0 + "U");
return 0d;
}
}
@Override
public Double applyBasalConstraints(Double absoluteRate) {
return absoluteRate;
}
@Override
public Integer applyBasalConstraints(Integer percentRate) {
return percentRate;
}
@Override
public Double applyBolusConstraints(Double insulin) {
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
return carbs;
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
}

View file

@ -0,0 +1,264 @@
package info.nightscout.androidaps.plugins.Objectives;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
/**
* Created by mike on 05.08.2016.
*/
public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
private static Logger log = LoggerFactory.getLogger(ObjectivesPlugin.class);
public List<Objective> objectives;
boolean fragmentVisible = true;
public ObjectivesPlugin() {
initializeData();
loadProgress();
MainApp.bus().register(this);
}
@Override
public String getFragmentClass() {
return ObjectivesFragment.class.getName();
}
@Override
public int getType() {
return PluginBase.CONSTRAINTS;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.objectives);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
LoopPlugin loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class);
return fragmentVisible && loopPlugin != null && loopPlugin.isVisibleInTabs(type);
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
public class Objective {
Integer num;
String objective;
String gate;
Date started;
Integer durationInDays;
Date accomplished;
Objective(Integer num, String objective, String gate, Date started, Integer durationInDays, Date accomplished) {
this.num = num;
this.objective = objective;
this.gate = gate;
this.started = started;
this.durationInDays = durationInDays;
this.accomplished = accomplished;
}
}
// Objective 0
public boolean bgIsAvailableInNS = false;
public boolean pumpStatusIsAvailableInNS = false;
// Objective 1
public Integer manualEnacts = 0;
public final Integer manualEnactsNeeded = 20;
public class RequirementResult {
boolean done = false;
String comment = "";
public RequirementResult(boolean done, String comment) {
this.done = done;
this.comment = comment;
}
}
private String yesOrNo(boolean yes) {
if (yes) return "";
else return "---";
}
public RequirementResult requirementsMet(Integer objNum) {
switch (objNum) {
case 0:
return new RequirementResult(bgIsAvailableInNS && pumpStatusIsAvailableInNS,
MainApp.sResources.getString(R.string.objectives_bgavailableinns) + ": " + yesOrNo(bgIsAvailableInNS)
+ " " + MainApp.sResources.getString(R.string.objectives_pumpstatusavailableinns) + ": " + yesOrNo(pumpStatusIsAvailableInNS));
case 1:
return new RequirementResult(manualEnacts >= manualEnactsNeeded,
MainApp.sResources.getString(R.string.objectives_manualenacts) + ": " + manualEnacts + "/" + manualEnactsNeeded);
case 2:
return new RequirementResult(true, "");
default:
return new RequirementResult(false, "");
}
}
private void initializeData() {
objectives = new ArrayList<>();
objectives.add(new Objective(0,
MainApp.sResources.getString(R.string.objectives_0_objective),
MainApp.sResources.getString(R.string.objectives_0_gate),
new Date(0, 0, 0),
1, // 1 day
new Date(0, 0, 0)));
objectives.add(new Objective(1,
MainApp.sResources.getString(R.string.objectives_1_objective),
MainApp.sResources.getString(R.string.objectives_1_gate),
new Date(0, 0, 0),
7, // 7 days
new Date(0, 0, 0)));
objectives.add(new Objective(2,
MainApp.sResources.getString(R.string.objectives_2_objective),
MainApp.sResources.getString(R.string.objectives_2_gate),
new Date(0, 0, 0),
0, // 0 days
new Date(0, 0, 0)));
objectives.add(new Objective(3,
MainApp.sResources.getString(R.string.objectives_3_objective),
MainApp.sResources.getString(R.string.objectives_3_gate),
new Date(0, 0, 0),
5, // 5 days
new Date(0, 0, 0)));
objectives.add(new Objective(4,
MainApp.sResources.getString(R.string.objectives_4_objective),
MainApp.sResources.getString(R.string.objectives_4_gate),
new Date(0, 0, 0),
1,
new Date(0, 0, 0)));
objectives.add(new Objective(5,
MainApp.sResources.getString(R.string.objectives_5_objective),
MainApp.sResources.getString(R.string.objectives_5_gate),
new Date(0, 0, 0),
7,
new Date(0, 0, 0)));
objectives.add(new Objective(6,
MainApp.sResources.getString(R.string.objectives_6_objective),
"",
new Date(0, 0, 0),
1,
new Date(0, 0, 0)));
}
public void saveProgress() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
for (int num = 0; num < objectives.size(); num++) {
Objective o = objectives.get(num);
editor.putLong("Objectives" + num + "started", o.started.getTime());
editor.putLong("Objectives" + num + "accomplished", o.accomplished.getTime());
}
editor.putBoolean("Objectives" + "bgIsAvailableInNS", bgIsAvailableInNS);
editor.putBoolean("Objectives" + "pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS);
editor.putInt("Objectives" + "manualEnacts", manualEnacts);
editor.apply();
if (Config.logPrefsChange)
log.debug("Objectives stored");
}
void loadProgress() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
for (int num = 0; num < objectives.size(); num++) {
Objective o = objectives.get(num);
o.started = new Date(settings.getLong("Objectives" + num + "started", 0));
o.accomplished = new Date(settings.getLong("Objectives" + num + "accomplished", 0));
}
bgIsAvailableInNS = settings.getBoolean("Objectives" + "bgIsAvailableInNS", false);
pumpStatusIsAvailableInNS = settings.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false);
manualEnacts = settings.getInt("Objectives" + "manualEnacts", 0);
if (Config.logPrefsChange)
log.debug("Objectives loaded");
}
/**
* Constraints interface
**/
@Override
public boolean isLoopEnabled() {
return objectives.get(1).started.getTime() > 0;
}
@Override
public boolean isClosedModeEnabled() {
return objectives.get(3).started.getTime() > 0;
}
@Override
public boolean isAutosensModeEnabled() {
return objectives.get(5).started.getTime() > 0;
}
@Override
public boolean isAMAModeEnabled() {
return objectives.get(6).started.getTime() > 0;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
if (objectives.get(4).started.getTime() > 0)
return maxIob;
else {
if (Config.logConstraintsChanges)
log.debug("Limiting maxIOB " + maxIob + " to " + 0 + "U");
return 0d;
}
}
@Override
public Double applyBasalConstraints(Double absoluteRate) {
return absoluteRate;
}
@Override
public Integer applyBasalConstraints(Integer percentRate) {
return percentRate;
}
@Override
public Double applyBolusConstraints(Double insulin) {
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
return carbs;
}
}

View file

@ -20,6 +20,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.plugins.Loop.ScriptReader;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.client.data.NSProfile;
public class DetermineBasalAdapterJS implements Parcelable {
@ -265,7 +266,7 @@ public class DetermineBasalAdapterJS implements Parcelable {
PumpInterface pump,
IobTotal iobData,
DatabaseHelper.GlucoseStatus glucoseStatus,
TreatmentsFragment.MealData mealData) {
TreatmentsPlugin.MealData mealData) {
String units = profile.getUnits();

View file

@ -1,11 +1,7 @@
package info.nightscout.androidaps.plugins.OpenAPSMA;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
@ -13,36 +9,27 @@ import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.TempBasalsInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Loop.ScriptReader;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSMAUpdateGui;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSMAUpdateResultGui;
import info.nightscout.utils.JSONFormatter;
import info.nightscout.utils.Round;
import info.nightscout.utils.SafeParse;
public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, PluginBase, APSInterface {
public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, FragmentBase {
private static Logger log = LoggerFactory.getLogger(OpenAPSMAFragment.class);
private static OpenAPSMAPlugin openAPSMAPlugin = new OpenAPSMAPlugin();
public static OpenAPSMAPlugin getPlugin() {
return openAPSMAPlugin;
}
Button run;
TextView lastRunView;
TextView glucoseStatusView;
@ -53,107 +40,6 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
TextView resultView;
TextView requestView;
// last values
class LastRun implements Parcelable {
DetermineBasalAdapterJS lastDetermineBasalAdapterJS = null;
Date lastAPSRun = null;
DetermineBasalResult lastAPSResult = null;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(lastDetermineBasalAdapterJS, 0);
dest.writeParcelable(lastAPSResult, 0);
dest.writeLong(lastAPSRun.getTime());
dest.writeParcelable(lastAPSResult, 0);
}
public final Parcelable.Creator<LastRun> CREATOR = new Parcelable.Creator<LastRun>() {
public LastRun createFromParcel(Parcel in) {
return new LastRun(in);
}
public LastRun[] newArray(int size) {
return new LastRun[size];
}
};
private LastRun(Parcel in) {
lastDetermineBasalAdapterJS = in.readParcelable(DetermineBasalAdapterJS.class.getClassLoader());
lastAPSResult = in.readParcelable(DetermineBasalResult.class.getClassLoader());
lastAPSRun = new Date(in.readLong());
lastAPSResult = in.readParcelable(APSResult.class.getClassLoader());
}
public LastRun() {
}
}
LastRun lastRun = null;
boolean fragmentEnabled = false;
boolean fragmentVisible = true;
public OpenAPSMAFragment() {
super();
registerBus();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.openapsma);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public int getType() {
return PluginBase.APS;
}
@Override
public APSResult getLastAPSResult() {
if (lastRun == null) return null;
return lastRun.lastAPSResult;
}
@Override
public Date getLastAPSRun() {
return lastRun.lastAPSRun;
}
public static OpenAPSMAFragment newInstance() {
OpenAPSMAFragment fragment = new OpenAPSMAFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -170,133 +56,40 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
resultView = (TextView) view.findViewById(R.id.openapsma_result);
requestView = (TextView) view.findViewById(R.id.openapsma_request);
// if (savedInstanceState != null) {
// lastRun = savedInstanceState.getParcelable("lastrun");
// }
updateGUI();
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//outState.putParcelable("lastrun", lastRun);
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.openapsma_run:
invoke();
openAPSMAPlugin.invoke();
break;
}
}
@Override
public void invoke() {
DetermineBasalAdapterJS determineBasalAdapterJS = null;
try {
determineBasalAdapterJS = new DetermineBasalAdapterJS(new ScriptReader(MainApp.instance().getBaseContext()));
} catch (IOException e) {
log.error(e.getMessage(), e);
return;
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
}
DatabaseHelper.GlucoseStatus glucoseStatus = MainApp.getDbHelper().getGlucoseStatusData();
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
if (!isEnabled(PluginBase.APS)) {
updateResultGUI(MainApp.instance().getString(R.string.openapsma_disabled));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_disabled));
return;
@Override
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
if (glucoseStatus == null) {
updateResultGUI(MainApp.instance().getString(R.string.openapsma_noglucosedata));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_noglucosedata));
return;
}
if (profile == null) {
updateResultGUI(MainApp.instance().getString(R.string.openapsma_noprofile));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_noprofile));
return;
}
if (pump == null) {
updateResultGUI(getString(R.string.openapsma_nopump));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_nopump));
return;
}
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String units = profile.getUnits();
String maxBgDefault = "180";
String minBgDefault = "100";
if (!units.equals(Constants.MGDL)) {
maxBgDefault = "10";
minBgDefault = "5";
}
Date now = new Date();
double maxIob = SafeParse.stringToDouble(SP.getString("openapsma_max_iob", "1.5"));
double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1"));
double minBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_min_bg", minBgDefault)), units);
double maxBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_max_bg", maxBgDefault)), units);
minBg = Round.roundTo(minBg, 0.1d);
maxBg = Round.roundTo(maxBg, 0.1d);
TreatmentsInterface treatments = MainApp.getConfigBuilder().getActiveTreatments();
TempBasalsInterface tempBasals = MainApp.getConfigBuilder().getActiveTempBasals();
treatments.updateTotalIOB();
tempBasals.updateTotalIOB();
IobTotal bolusIob = treatments.getLastCalculation();
IobTotal basalIob = tempBasals.getLastCalculation();
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
TreatmentsFragment.MealData mealData = treatments.getMealData();
maxIob = MainApp.getConfigBuilder().applyMaxIOBConstraints(maxIob);
determineBasalAdapterJS.setData(profile, maxIob, maxBasal, minBg, maxBg, pump, iobTotal, glucoseStatus, mealData);
DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke();
determineBasalResult.iob = iobTotal;
determineBasalAdapterJS.release();
try {
determineBasalResult.json.put("timestamp", DateUtil.toISOString(now));
} catch (JSONException e) {
e.printStackTrace();
}
lastRun = new LastRun();
lastRun.lastDetermineBasalAdapterJS = determineBasalAdapterJS;
lastRun.lastAPSResult = determineBasalResult;
lastRun.lastAPSRun = now;
@Subscribe
public void onStatusEvent(final EventOpenAPSMAUpdateGui ev) {
updateGUI();
}
//deviceStatus.suggested = determineBasalResult.json;
@Subscribe
public void onStatusEvent(final EventOpenAPSMAUpdateResultGui ev) {
updateResultGUI(ev.text);
}
void updateGUI() {
@ -305,15 +98,15 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (lastRun != null) {
glucoseStatusView.setText(JSONFormatter.format(lastRun.lastDetermineBasalAdapterJS.getGlucoseStatusParam()));
currentTempView.setText(JSONFormatter.format(lastRun.lastDetermineBasalAdapterJS.getCurrentTempParam()));
iobDataView.setText(JSONFormatter.format(lastRun.lastDetermineBasalAdapterJS.getIobDataParam()));
profileView.setText(JSONFormatter.format(lastRun.lastDetermineBasalAdapterJS.getProfileParam()));
mealDataView.setText(JSONFormatter.format(lastRun.lastDetermineBasalAdapterJS.getMealDataParam()));
resultView.setText(JSONFormatter.format(lastRun.lastAPSResult.json));
requestView.setText(lastRun.lastAPSResult.toSpanned());
lastRunView.setText(lastRun.lastAPSRun.toLocaleString());
if (openAPSMAPlugin.lastAPSResult != null) {
glucoseStatusView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getGlucoseStatusParam()));
currentTempView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getCurrentTempParam()));
iobDataView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getIobDataParam()));
profileView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getProfileParam()));
mealDataView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getMealDataParam()));
resultView.setText(JSONFormatter.format(openAPSMAPlugin.lastAPSResult.json));
requestView.setText(openAPSMAPlugin.lastAPSResult.toSpanned());
lastRunView.setText(openAPSMAPlugin.lastAPSRun.toLocaleString());
}
}
});

View file

@ -0,0 +1,193 @@
package info.nightscout.androidaps.plugins.OpenAPSMA;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.interfaces.TempBasalsInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.plugins.Loop.ScriptReader;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSMAUpdateGui;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSMAUpdateResultGui;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.Round;
import info.nightscout.utils.SafeParse;
/**
* Created by mike on 05.08.2016.
*/
public class OpenAPSMAPlugin implements PluginBase, APSInterface {
private static Logger log = LoggerFactory.getLogger(OpenAPSMAPlugin.class);
// last values
DetermineBasalAdapterJS lastDetermineBasalAdapterJS = null;
Date lastAPSRun = null;
DetermineBasalResult lastAPSResult = null;
boolean fragmentEnabled = false;
boolean fragmentVisible = true;
@Override
public String getName() {
return MainApp.instance().getString(R.string.openapsma);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public int getType() {
return PluginBase.APS;
}
@Override
public String getFragmentClass() {
return OpenAPSMAFragment.class.getName();
}
@Override
public APSResult getLastAPSResult() {
return lastAPSResult;
}
@Override
public Date getLastAPSRun() {
return lastAPSRun;
}
@Override
public void invoke() {
DetermineBasalAdapterJS determineBasalAdapterJS = null;
try {
determineBasalAdapterJS = new DetermineBasalAdapterJS(new ScriptReader(MainApp.instance().getBaseContext()));
} catch (IOException e) {
log.error(e.getMessage(), e);
return;
}
DatabaseHelper.GlucoseStatus glucoseStatus = MainApp.getDbHelper().getGlucoseStatusData();
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
PumpInterface pump = MainApp.getConfigBuilder();
if (!isEnabled(PluginBase.APS)) {
MainApp.bus().post(new EventOpenAPSMAUpdateResultGui(MainApp.instance().getString(R.string.openapsma_disabled)));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_disabled));
return;
}
if (glucoseStatus == null) {
MainApp.bus().post(new EventOpenAPSMAUpdateResultGui(MainApp.instance().getString(R.string.openapsma_noglucosedata)));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_noglucosedata));
return;
}
if (profile == null) {
MainApp.bus().post(new EventOpenAPSMAUpdateResultGui(MainApp.instance().getString(R.string.openapsma_noprofile)));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_noprofile));
return;
}
if (pump == null) {
MainApp.bus().post(new EventOpenAPSMAUpdateResultGui(MainApp.instance().getString(R.string.openapsma_nopump)));
if (Config.logAPSResult)
log.debug(MainApp.instance().getString(R.string.openapsma_nopump));
return;
}
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String units = profile.getUnits();
String maxBgDefault = "180";
String minBgDefault = "100";
if (!units.equals(Constants.MGDL)) {
maxBgDefault = "10";
minBgDefault = "5";
}
Date now = new Date();
double maxIob = SafeParse.stringToDouble(SP.getString("openapsma_max_iob", "1.5"));
double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1"));
double minBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_min_bg", minBgDefault)), units);
double maxBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_max_bg", maxBgDefault)), units);
minBg = Round.roundTo(minBg, 0.1d);
maxBg = Round.roundTo(maxBg, 0.1d);
TreatmentsInterface treatments = MainApp.getConfigBuilder().getActiveTreatments();
TempBasalsInterface tempBasals = MainApp.getConfigBuilder().getActiveTempBasals();
treatments.updateTotalIOB();
tempBasals.updateTotalIOB();
IobTotal bolusIob = treatments.getLastCalculation();
IobTotal basalIob = tempBasals.getLastCalculation();
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
TreatmentsPlugin.MealData mealData = treatments.getMealData();
maxIob = MainApp.getConfigBuilder().applyMaxIOBConstraints(maxIob);
determineBasalAdapterJS.setData(profile, maxIob, maxBasal, minBg, maxBg, pump, iobTotal, glucoseStatus, mealData);
DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke();
determineBasalResult.iob = iobTotal;
determineBasalAdapterJS.release();
try {
determineBasalResult.json.put("timestamp", DateUtil.toISOString(now));
} catch (JSONException e) {
e.printStackTrace();
}
lastDetermineBasalAdapterJS = determineBasalAdapterJS;
lastAPSResult = determineBasalResult;
lastAPSRun = now;
MainApp.bus().post(new EventOpenAPSMAUpdateGui());
//deviceStatus.suggested = determineBasalResult.json;
}
}

View file

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

View file

@ -0,0 +1,12 @@
package info.nightscout.androidaps.plugins.OpenAPSMA.events;
/**
* Created by mike on 05.08.2016.
*/
public class EventOpenAPSMAUpdateResultGui {
public String text = null;
public EventOpenAPSMAUpdateResultGui(String text) {
this.text = text;
}
}

View file

@ -19,11 +19,9 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusProgress;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusStart;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus;
import info.nightscout.utils.ToastUtils;
public class BolusProgressDialog extends DialogFragment implements View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(BolusProgressDialog.class);
@ -75,7 +73,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
case R.id.overview_bolusprogress_stop:
log.debug("Stop bolus delivery button pressed");
stopPressedView.setVisibility(View.VISIBLE);
PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
PumpInterface pump = MainApp.getConfigBuilder();
pump.stopBolusDelivering();
break;
}

View file

@ -4,7 +4,6 @@ import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
@ -95,7 +94,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli
builder.setMessage(confirmMessage);
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
final PumpInterface pump = MainApp.getConfigBuilder();
mHandler.post(new Runnable() {
@Override
public void run() {

View file

@ -130,7 +130,7 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi
builder.setMessage(confirmMessage);
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
final PumpInterface pump = MainApp.getConfigBuilder();
mHandler.post(new Runnable() {
@Override
public void run() {

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps.plugins.Overview.Dialogs;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
@ -8,8 +7,12 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
@ -94,7 +97,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) {
final PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
final PumpInterface pump = MainApp.getConfigBuilder();
mHandler.post(new Runnable() {
@Override
public void run() {

View file

@ -39,6 +39,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.interfaces.TempBasalsInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
@ -192,7 +193,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) {
final ConfigBuilderFragment pump = MainApp.getConfigBuilder();
final ConfigBuilderPlugin pump = MainApp.getConfigBuilder();
mHandler.post(new Runnable() {
@Override
public void run() {
@ -227,7 +228,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
}
private void initDialog() {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
if (profile == null) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.noprofile));

View file

@ -38,7 +38,6 @@ import java.util.List;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
@ -52,10 +51,9 @@ import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.events.EventRefreshOpenLoop;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTempBasalDialog;
@ -67,9 +65,15 @@ import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.Round;
public class OverviewFragment extends Fragment implements PluginBase {
public class OverviewFragment extends Fragment {
private static Logger log = LoggerFactory.getLogger(OverviewFragment.class);
private static OverviewPlugin overviewPlugin = new OverviewPlugin();
public static OverviewPlugin getPlugin() {
return overviewPlugin;
}
TextView bgView;
TextView timeAgoView;
TextView deltaView;
@ -88,56 +92,22 @@ public class OverviewFragment extends Fragment implements PluginBase {
Button setExtenedButton;
Button acceptTempButton;
Handler loopHandler = new Handler();
Runnable refreshLoop = null;
private static Handler sLoopHandler = new Handler();
private static Runnable sRefreshLoop = null;
Handler mHandler;
public static HandlerThread mHandlerThread;
private static Handler sHandler;
private static HandlerThread sHandlerThread;
public Double bgTargetLow = 80d;
public Double bgTargetHigh = 180d;
public OverviewFragment() {
super();
mHandlerThread = new HandlerThread(OverviewFragment.class.getSimpleName());
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
registerBus();
if (sHandlerThread == null) {
sHandlerThread = new HandlerThread(OverviewFragment.class.getSimpleName());
sHandlerThread.start();
sHandler = new Handler(sHandlerThread.getLooper());
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.overview);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
return true;
}
@Override
public boolean canBeHidden(int type) {
return false;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
// Always enabled
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
// Always visible
}
@Override
public int getType() {
return PluginBase.GENERAL;
}
public static OverviewFragment newInstance() {
@ -147,15 +117,15 @@ public class OverviewFragment extends Fragment implements PluginBase {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (refreshLoop == null) {
refreshLoop = new Runnable() {
if (sRefreshLoop == null) {
sRefreshLoop = new Runnable() {
@Override
public void run() {
updateGUIIfVisible();
loopHandler.postDelayed(refreshLoop, 60 * 1000L);
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
};
loopHandler.postDelayed(refreshLoop, 60 * 1000L);
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
}
@ -202,9 +172,9 @@ public class OverviewFragment extends Fragment implements PluginBase {
cancelTempButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
final PumpInterface pump = MainApp.getConfigBuilder();
if (pump.isTempBasalInProgress()) {
mHandler.post(new Runnable() {
sHandler.post(new Runnable() {
@Override
public void run() {
pump.cancelTempBasal();
@ -237,14 +207,14 @@ public class OverviewFragment extends Fragment implements PluginBase {
@Override
public void onClick(View view) {
MainApp.getConfigBuilder().getActiveLoop().invoke(false);
final LoopFragment.LastRun finalLastRun = LoopFragment.lastRun;
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
if (finalLastRun != null && finalLastRun.lastAPSRun != null && finalLastRun.constraintsProcessed.changeRequested) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getContext().getString(R.string.confirmation));
builder.setMessage(getContext().getString(R.string.setbasalquestion) + "\n" + finalLastRun.constraintsProcessed);
builder.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mHandler.post(new Runnable() {
sHandler.post(new Runnable() {
@Override
public void run() {
PumpEnactResult applyResult = MainApp.getConfigBuilder().applyAPSRequest(finalLastRun.constraintsProcessed);
@ -253,10 +223,10 @@ public class OverviewFragment extends Fragment implements PluginBase {
finalLastRun.lastEnact = new Date();
finalLastRun.lastOpenModeAccept = new Date();
MainApp.getConfigBuilder().uploadDeviceStatus();
ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainApp.getSpecificPlugin(ObjectivesFragment.class);
if (objectivesFragment != null) {
objectivesFragment.manualEnacts++;
objectivesFragment.saveProgress();
ObjectivesPlugin objectivesPlugin = (ObjectivesPlugin) MainApp.getSpecificPlugin(ObjectivesPlugin.class);
if (objectivesPlugin != null) {
objectivesPlugin.manualEnacts++;
objectivesPlugin.saveProgress();
}
}
updateGUIIfVisible();
@ -275,12 +245,15 @@ public class OverviewFragment extends Fragment implements PluginBase {
return view;
}
private void registerBus() {
try {
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
@Override
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@ -337,7 +310,7 @@ public class OverviewFragment extends Fragment implements PluginBase {
return;
// open loop mode
final LoopFragment.LastRun finalLastRun = LoopFragment.lastRun;
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
if (Config.APS) {
apsModeView.setVisibility(View.VISIBLE);
if (MainApp.getConfigBuilder().isClosedModeEnabled())
@ -361,7 +334,7 @@ public class OverviewFragment extends Fragment implements PluginBase {
// **** Temp button ****
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
PumpInterface pump = MainApp.getConfigBuilder();
if (pump.isTempBasalInProgress()) {
TempBasal activeTemp = pump.getTempBasal();
@ -483,7 +456,7 @@ public class OverviewFragment extends Fragment implements PluginBase {
long now = new Date().getTime();
List<BarDataPoint> basalArray = new ArrayList<BarDataPoint>();
for (long time = fromTime; time < now; time += 5 * 60 * 1000L) {
TempBasal tb = MainApp.getConfigBuilder().getActivePump().getTempBasal(new Date(time));
TempBasal tb = MainApp.getConfigBuilder().getTempBasal(new Date(time));
if (tb != null)
basalArray.add(new BarDataPoint(time, tb.tempBasalConvertedToAbsolute(new Date(time)), true));
else

View file

@ -0,0 +1,53 @@
package info.nightscout.androidaps.plugins.Overview;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
/**
* Created by mike on 05.08.2016.
*/
public class OverviewPlugin implements PluginBase {
@Override
public String getFragmentClass() {
return OverviewFragment.class.getName();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.overview);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
return true;
}
@Override
public boolean canBeHidden(int type) {
return false;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
// Always enabled
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
// Always visible
}
@Override
public int getType() {
return PluginBase.GENERAL;
}
}

View file

@ -1,202 +1,19 @@
package info.nightscout.androidaps.plugins.SafetyFragment;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.Round;
import info.nightscout.utils.SafeParse;
import info.nightscout.androidaps.interfaces.FragmentBase;
public class SafetyFragment extends Fragment implements PluginBase, ConstraintsInterface {
public class SafetyFragment extends Fragment implements FragmentBase{
private static Logger log = LoggerFactory.getLogger(SafetyFragment.class);
private static SafetyPlugin safetyPlugin = new SafetyPlugin();
@Override
public int getType() {
return PluginBase.CONSTRAINTS;
public static SafetyPlugin getPlugin() {
return safetyPlugin;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.safety);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int tyep, boolean fragmentEnabled) {
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
}
public static SafetyFragment newInstance() {
SafetyFragment fragment = new SafetyFragment();
return fragment;
}
@Override
public boolean isLoopEnabled() {
return true;
}
/**
* Constraints interface
**/
@Override
public boolean isClosedModeEnabled() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String mode = SP.getString("aps_mode", "open");
return mode.equals("closed") && BuildConfig.CLOSEDLOOP;
}
@Override
public boolean isAutosensModeEnabled() {
return true;
}
@Override
public boolean isAMAModeEnabled() {
return true;
}
@Override
public Double applyBasalConstraints(Double absoluteRate) {
Double origAbsoluteRate = absoluteRate;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1"));
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) return absoluteRate;
if (absoluteRate < 0) absoluteRate = 0d;
Integer maxBasalMult = 4;
Integer maxBasalFromDaily = 3;
// Check percentRate but absolute rate too, because we know real current basal in pump
Double origRate = absoluteRate;
if (absoluteRate > maxBasal) {
absoluteRate = maxBasal;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h");
}
if (absoluteRate > maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight())) {
absoluteRate = Math.floor(maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight()) * 100) / 100;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h");
}
if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) {
absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h");
}
return absoluteRate;
}
@Override
public Integer applyBasalConstraints(Integer percentRate) {
Integer origPercentRate = percentRate;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1"));
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) return percentRate;
Double currentBasal = profile.getBasal(profile.secondsFromMidnight());
Double absoluteRate = currentBasal * ((double) percentRate / 100);
if (Config.logConstraintsChanges)
log.debug("Percent rate " + percentRate + "% recalculated to " + absoluteRate + "U/h with current basal " + currentBasal + "U/h");
if (absoluteRate < 0) absoluteRate = 0d;
Integer maxBasalMult = 4;
Integer maxBasalFromDaily = 3;
// Check percentRate but absolute rate too, because we know real current basal in pump
Double origRate = absoluteRate;
if (absoluteRate > maxBasal) {
absoluteRate = maxBasal;
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h");
}
if (absoluteRate > maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight())) {
absoluteRate = Math.floor(maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight()) * 100) / 100;
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h");
}
if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) {
absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily;
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h");
}
Integer percentRateAfterConst = new Double(absoluteRate / currentBasal * 100).intValue();
if (percentRateAfterConst < 100) percentRateAfterConst = Round.ceilTo((double) percentRateAfterConst, 10d).intValue();
else percentRateAfterConst = Round.floorTo((double) percentRateAfterConst, 10d).intValue();
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Recalculated percent rate " + percentRate + "% to " + percentRateAfterConst + "%");
return percentRateAfterConst;
}
@Override
public Double applyBolusConstraints(Double insulin) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
try {
Double maxBolus = SafeParse.stringToDouble(SP.getString("treatmentssafety_maxbolus", "3"));
if (insulin < 0) insulin = 0d;
if (insulin > maxBolus) insulin = maxBolus;
} catch (Exception e) {
insulin = 0d;
}
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
try {
Integer maxCarbs = Integer.parseInt(SP.getString("treatmentssafety_maxcarbs", "48"));
if (carbs < 0) carbs = 0;
if (carbs > maxCarbs) carbs = maxCarbs;
} catch (Exception e) {
carbs = 0;
}
return carbs;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
return maxIob;
}
}

View file

@ -0,0 +1,202 @@
package info.nightscout.androidaps.plugins.SafetyFragment;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.Round;
import info.nightscout.utils.SafeParse;
/**
* Created by mike on 05.08.2016.
*/
public class SafetyPlugin implements PluginBase, ConstraintsInterface {
private static Logger log = LoggerFactory.getLogger(SafetyPlugin.class);
@Override
public String getFragmentClass() {
return SafetyFragment.class.getName();
}
@Override
public int getType() {
return PluginBase.CONSTRAINTS;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.safety);
}
@Override
public boolean isEnabled(int type) {
return true;
}
@Override
public boolean isVisibleInTabs(int type) {
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int tyep, boolean fragmentEnabled) {
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
}
@Override
public boolean isLoopEnabled() {
return true;
}
/**
* Constraints interface
**/
@Override
public boolean isClosedModeEnabled() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String mode = SP.getString("aps_mode", "open");
return mode.equals("closed") && BuildConfig.CLOSEDLOOP;
}
@Override
public boolean isAutosensModeEnabled() {
return true;
}
@Override
public boolean isAMAModeEnabled() {
return true;
}
@Override
public Double applyBasalConstraints(Double absoluteRate) {
Double origAbsoluteRate = absoluteRate;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1"));
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) return absoluteRate;
if (absoluteRate < 0) absoluteRate = 0d;
Integer maxBasalMult = 4;
Integer maxBasalFromDaily = 3;
// Check percentRate but absolute rate too, because we know real current basal in pump
Double origRate = absoluteRate;
if (absoluteRate > maxBasal) {
absoluteRate = maxBasal;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h");
}
if (absoluteRate > maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight())) {
absoluteRate = Math.floor(maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight()) * 100) / 100;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h");
}
if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) {
absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h");
}
return absoluteRate;
}
@Override
public Integer applyBasalConstraints(Integer percentRate) {
Integer origPercentRate = percentRate;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1"));
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) return percentRate;
Double currentBasal = profile.getBasal(profile.secondsFromMidnight());
Double absoluteRate = currentBasal * ((double) percentRate / 100);
if (Config.logConstraintsChanges)
log.debug("Percent rate " + percentRate + "% recalculated to " + absoluteRate + "U/h with current basal " + currentBasal + "U/h");
if (absoluteRate < 0) absoluteRate = 0d;
Integer maxBasalMult = 4;
Integer maxBasalFromDaily = 3;
// Check percentRate but absolute rate too, because we know real current basal in pump
Double origRate = absoluteRate;
if (absoluteRate > maxBasal) {
absoluteRate = maxBasal;
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h");
}
if (absoluteRate > maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight())) {
absoluteRate = Math.floor(maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight()) * 100) / 100;
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h");
}
if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) {
absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily;
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h");
}
Integer percentRateAfterConst = new Double(absoluteRate / currentBasal * 100).intValue();
if (percentRateAfterConst < 100)
percentRateAfterConst = Round.ceilTo((double) percentRateAfterConst, 10d).intValue();
else percentRateAfterConst = Round.floorTo((double) percentRateAfterConst, 10d).intValue();
if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
log.debug("Recalculated percent rate " + percentRate + "% to " + percentRateAfterConst + "%");
return percentRateAfterConst;
}
@Override
public Double applyBolusConstraints(Double insulin) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
try {
Double maxBolus = SafeParse.stringToDouble(SP.getString("treatmentssafety_maxbolus", "3"));
if (insulin < 0) insulin = 0d;
if (insulin > maxBolus) insulin = maxBolus;
} catch (Exception e) {
insulin = 0d;
}
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
try {
Integer maxCarbs = Integer.parseInt(SP.getString("treatmentssafety_maxcarbs", "48"));
if (carbs < 0) carbs = 0;
if (carbs > maxCarbs) carbs = maxCarbs;
} catch (Exception e) {
carbs = 0;
}
return carbs;
}
@Override
public Double applyMaxIOBConstraints(Double maxIob) {
return maxIob;
}
}

View file

@ -1,11 +1,8 @@
package info.nightscout.androidaps.plugins.SimpleProfile;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
@ -13,30 +10,22 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.client.data.NSProfile;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.utils.SafeParse;
public class SimpleProfileFragment extends Fragment implements PluginBase, ProfileInterface {
public class SimpleProfileFragment extends Fragment implements FragmentBase {
private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class);
private static final String PREFS_NAME = "SimpleProfile";
private static SimpleProfilePlugin simpleProfilePlugin = new SimpleProfilePlugin();
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
public static SimpleProfilePlugin getPlugin() {
return simpleProfilePlugin;
}
EditText diaView;
RadioButton mgdlView;
@ -48,59 +37,6 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi
EditText targetlowView;
EditText targethighView;
boolean mgdl;
boolean mmol;
Double dia;
Double ic;
Double isf;
Double car;
Double basal;
Double targetLow;
Double targetHigh;
NSProfile convertedProfile = null;
public SimpleProfileFragment() {
super();
registerBus();
loadSettings();
}
@Override
public int getType() {
return PluginBase.PROFILE;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.simpleprofile);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -115,32 +51,32 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi
targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow);
targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh);
mgdlView.setChecked(mgdl);
mmolView.setChecked(mmol);
diaView.setText(dia.toString());
icView.setText(ic.toString());
isfView.setText(isf.toString());
carView.setText(car.toString());
basalView.setText(basal.toString());
targetlowView.setText(targetLow.toString());
targethighView.setText(targetHigh.toString());
mgdlView.setChecked(simpleProfilePlugin.mgdl);
mmolView.setChecked(simpleProfilePlugin.mmol);
diaView.setText(simpleProfilePlugin.dia.toString());
icView.setText(simpleProfilePlugin.ic.toString());
isfView.setText(simpleProfilePlugin.isf.toString());
carView.setText(simpleProfilePlugin.car.toString());
basalView.setText(simpleProfilePlugin.basal.toString());
targetlowView.setText(simpleProfilePlugin.targetLow.toString());
targethighView.setText(simpleProfilePlugin.targetHigh.toString());
mgdlView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mgdl = mgdlView.isChecked();
mmol = !mgdl;
mmolView.setChecked(mmol);
storeSettings();
simpleProfilePlugin.mgdl = mgdlView.isChecked();
simpleProfilePlugin.mmol = !simpleProfilePlugin.mgdl;
mmolView.setChecked(simpleProfilePlugin.mmol);
simpleProfilePlugin.storeSettings();
}
});
mmolView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mmol = mmolView.isChecked();
mgdl = !mmol;
mgdlView.setChecked(mgdl);
storeSettings();
simpleProfilePlugin.mmol = mmolView.isChecked();
simpleProfilePlugin.mgdl = !simpleProfilePlugin.mmol;
mgdlView.setChecked(simpleProfilePlugin.mgdl);
simpleProfilePlugin.storeSettings();
}
});
@ -158,16 +94,17 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
dia = SafeParse.stringToDouble(diaView.getText().toString());
ic = SafeParse.stringToDouble(icView.getText().toString());
isf = SafeParse.stringToDouble(isfView.getText().toString());
car = SafeParse.stringToDouble(carView.getText().toString());
basal = SafeParse.stringToDouble(basalView.getText().toString());
targetLow = SafeParse.stringToDouble(targetlowView.getText().toString());
targetHigh = SafeParse.stringToDouble(targethighView.getText().toString());
storeSettings();
simpleProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString());
simpleProfilePlugin.ic = SafeParse.stringToDouble(icView.getText().toString());
simpleProfilePlugin.isf = SafeParse.stringToDouble(isfView.getText().toString());
simpleProfilePlugin.car = SafeParse.stringToDouble(carView.getText().toString());
simpleProfilePlugin.basal = SafeParse.stringToDouble(basalView.getText().toString());
simpleProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString());
simpleProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString());
simpleProfilePlugin.storeSettings();
}
};
diaView.addTextChangedListener(textWatch);
icView.addTextChangedListener(textWatch);
isfView.addTextChangedListener(textWatch);
@ -178,121 +115,4 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi
return layout;
}
public static SimpleProfileFragment newInstance() {
SimpleProfileFragment fragment = new SimpleProfileFragment();
return fragment;
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
private void storeSettings() {
if (Config.logPrefsChange)
log.debug("Storing settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("SimpleProfile" + "mmol", mmol);
editor.putBoolean("SimpleProfile" + "mgdl", mgdl);
editor.putFloat("SimpleProfile" + "dia", new Float(dia));
editor.putFloat("SimpleProfile" + "ic", new Float(ic));
editor.putFloat("SimpleProfile" + "isf", new Float(isf));
editor.putFloat("SimpleProfile" + "car", new Float(car));
editor.putFloat("SimpleProfile" + "basal", new Float(basal));
editor.putFloat("SimpleProfile" + "targetlow", new Float(targetLow));
editor.putFloat("SimpleProfile" + "targethigh", new Float(targetHigh));
editor.commit();
createConvertedProfile();
}
private void loadSettings() {
if (Config.logPrefsChange)
log.debug("Loading stored settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
if (settings.contains("SimpleProfile" + "mgdl")) mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true); else mgdl = true;
if (settings.contains("SimpleProfile" + "mmol")) mmol = settings.getBoolean("SimpleProfile" + "mmol", false); else mmol = false;
if (settings.contains("SimpleProfile" + "dia")) dia = (double) settings.getFloat("SimpleProfile" + "dia", 3); else dia = 3d;
if (settings.contains("SimpleProfile" + "ic")) ic = (double) settings.getFloat("SimpleProfile" + "ic", 20); else ic = 20d;
if (settings.contains("SimpleProfile" + "isf")) isf = (double) settings.getFloat("SimpleProfile" + "isf", 200); else isf = 200d;
if (settings.contains("SimpleProfile" + "car")) car = (double) settings.getFloat("SimpleProfile" + "car", 20); else car = 20d;
if (settings.contains("SimpleProfile" + "basal")) basal = (double) settings.getFloat("SimpleProfile" + "basal", 1); else basal = 1d;
if (settings.contains("SimpleProfile" + "targetlow")) targetLow = (double) settings.getFloat("SimpleProfile" + "targetlow", 80); else targetLow = 80d;
if (settings.contains("SimpleProfile" + "targethigh"))
targetHigh = (double) settings.getFloat("SimpleProfile" + "targethigh", 120); else targetHigh = 120d;
createConvertedProfile();
}
/*
{
"_id": "576264a12771b7500d7ad184",
"startDate": "2016-06-16T08:35:00.000Z",
"defaultProfile": "Default",
"store": {
"Default": {
"dia": "3",
"carbratio": [{
"time": "00:00",
"value": "30"
}],
"carbs_hr": "20",
"delay": "20",
"sens": [{
"time": "00:00",
"value": "100"
}],
"timezone": "UTC",
"basal": [{
"time": "00:00",
"value": "0.1"
}],
"target_low": [{
"time": "00:00",
"value": "0"
}],
"target_high": [{
"time": "00:00",
"value": "0"
}],
"startDate": "1970-01-01T00:00:00.000Z",
"units": "mmol"
}
},
"created_at": "2016-06-16T08:34:41.256Z"
}
*/
void createConvertedProfile() {
JSONObject json = new JSONObject();
JSONObject store = new JSONObject();
JSONObject profile = new JSONObject();
try {
json.put("defaultProfile", "Profile");
json.put("store", store);
profile.put("dia", dia);
profile.put("carbratio", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", ic)));
profile.put("carbs_hr", car);
profile.put("sens", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", isf)));
profile.put("basal", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", basal)));
profile.put("target_low", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetLow)));
profile.put("target_high", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetHigh)));
profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL);
store.put("Profile", profile);
} catch (JSONException e) {
e.printStackTrace();
}
convertedProfile = new NSProfile(json, null);
}
@Override
public NSProfile getProfile() {
return convertedProfile;
}
}

View file

@ -0,0 +1,205 @@
package info.nightscout.androidaps.plugins.SimpleProfile;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.client.data.NSProfile;
/**
* Created by mike on 05.08.2016.
*/
public class SimpleProfilePlugin implements PluginBase, ProfileInterface {
private static Logger log = LoggerFactory.getLogger(SimpleProfilePlugin.class);
private static boolean fragmentEnabled = true;
private static boolean fragmentVisible = true;
private static NSProfile convertedProfile = null;
boolean mgdl;
boolean mmol;
Double dia;
Double ic;
Double isf;
Double car;
Double basal;
Double targetLow;
Double targetHigh;
public SimpleProfilePlugin() {
loadSettings();
}
@Override
public String getFragmentClass() {
return SimpleProfilePlugin.class.getName();
}
@Override
public int getType() {
return PluginBase.PROFILE;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.simpleprofile);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
public void storeSettings() {
if (Config.logPrefsChange)
log.debug("Storing settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("SimpleProfile" + "mmol", mmol);
editor.putBoolean("SimpleProfile" + "mgdl", mgdl);
editor.putFloat("SimpleProfile" + "dia", new Float(dia));
editor.putFloat("SimpleProfile" + "ic", new Float(ic));
editor.putFloat("SimpleProfile" + "isf", new Float(isf));
editor.putFloat("SimpleProfile" + "car", new Float(car));
editor.putFloat("SimpleProfile" + "basal", new Float(basal));
editor.putFloat("SimpleProfile" + "targetlow", new Float(targetLow));
editor.putFloat("SimpleProfile" + "targethigh", new Float(targetHigh));
editor.commit();
createConvertedProfile();
}
private void loadSettings() {
if (Config.logPrefsChange)
log.debug("Loading stored settings");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
if (settings.contains("SimpleProfile" + "mgdl"))
mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true);
else mgdl = true;
if (settings.contains("SimpleProfile" + "mmol"))
mmol = settings.getBoolean("SimpleProfile" + "mmol", false);
else mmol = false;
if (settings.contains("SimpleProfile" + "dia"))
dia = (double) settings.getFloat("SimpleProfile" + "dia", 3);
else dia = 3d;
if (settings.contains("SimpleProfile" + "ic"))
ic = (double) settings.getFloat("SimpleProfile" + "ic", 20);
else ic = 20d;
if (settings.contains("SimpleProfile" + "isf"))
isf = (double) settings.getFloat("SimpleProfile" + "isf", 200);
else isf = 200d;
if (settings.contains("SimpleProfile" + "car"))
car = (double) settings.getFloat("SimpleProfile" + "car", 20);
else car = 20d;
if (settings.contains("SimpleProfile" + "basal"))
basal = (double) settings.getFloat("SimpleProfile" + "basal", 1);
else basal = 1d;
if (settings.contains("SimpleProfile" + "targetlow"))
targetLow = (double) settings.getFloat("SimpleProfile" + "targetlow", 80);
else targetLow = 80d;
if (settings.contains("SimpleProfile" + "targethigh"))
targetHigh = (double) settings.getFloat("SimpleProfile" + "targethigh", 120);
else targetHigh = 120d;
createConvertedProfile();
}
/*
{
"_id": "576264a12771b7500d7ad184",
"startDate": "2016-06-16T08:35:00.000Z",
"defaultProfile": "Default",
"store": {
"Default": {
"dia": "3",
"carbratio": [{
"time": "00:00",
"value": "30"
}],
"carbs_hr": "20",
"delay": "20",
"sens": [{
"time": "00:00",
"value": "100"
}],
"timezone": "UTC",
"basal": [{
"time": "00:00",
"value": "0.1"
}],
"target_low": [{
"time": "00:00",
"value": "0"
}],
"target_high": [{
"time": "00:00",
"value": "0"
}],
"startDate": "1970-01-01T00:00:00.000Z",
"units": "mmol"
}
},
"created_at": "2016-06-16T08:34:41.256Z"
}
*/
void createConvertedProfile() {
JSONObject json = new JSONObject();
JSONObject store = new JSONObject();
JSONObject profile = new JSONObject();
try {
json.put("defaultProfile", "Profile");
json.put("store", store);
profile.put("dia", dia);
profile.put("carbratio", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", ic)));
profile.put("carbs_hr", car);
profile.put("sens", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", isf)));
profile.put("basal", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", basal)));
profile.put("target_low", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetLow)));
profile.put("target_high", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetHigh)));
profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL);
store.put("Profile", profile);
} catch (JSONException e) {
e.printStackTrace();
}
convertedProfile = new NSProfile(json, null);
}
@Override
public NSProfile getProfile() {
return convertedProfile;
}
}

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.SmsCommunicator.Events;
package info.nightscout.androidaps.plugins.SmsCommunicator.events;
import android.os.Bundle;

View file

@ -2,14 +2,8 @@ package info.nightscout.androidaps.plugins.SmsCommunicator;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
@ -22,80 +16,31 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.SmsCommunicator.Events.EventNewSMS;
import info.nightscout.utils.SafeParse;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui;
/**
* A simple {@link Fragment} subclass.
*/
public class SmsCommunicatorFragment extends Fragment implements PluginBase {
public class SmsCommunicatorFragment extends Fragment {
private static Logger log = LoggerFactory.getLogger(SmsCommunicatorFragment.class);
private static SmsCommunicatorPlugin smsCommunicatorPlugin = new SmsCommunicatorPlugin();
public static SmsCommunicatorPlugin getPlugin() {
return smsCommunicatorPlugin;
}
TextView logView;
boolean fragmentEnabled = false;
boolean fragmentVisible = true;
public SmsCommunicatorFragment() {
super();
registerBus();
}
public static SmsCommunicatorFragment newInstance() {
SmsCommunicatorFragment fragment = new SmsCommunicatorFragment();
return fragment;
}
public class Sms {
String phoneNumber;
String text;
Date date;
boolean received = false;
boolean sent = false;
boolean processed = false;
String confirmCode;
double bolusRequested = 0d;
public Sms(SmsMessage message) {
phoneNumber = message.getOriginatingAddress();
text = message.getMessageBody();
date = new Date(message.getTimestampMillis());
received = true;
}
public Sms(String phoneNumber, String text, Date date) {
this.phoneNumber = phoneNumber;
this.text = text;
this.date = date;
sent = true;
}
public String toString() {
return "SMS from " + phoneNumber + ": " + text;
}
}
Sms bolusWaitingForConfirmation = null;
Date lastRemoteBolusTime = new Date(0);
ArrayList<Sms> messages = new ArrayList<Sms>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -107,162 +52,20 @@ public class SmsCommunicatorFragment extends Fragment implements PluginBase {
return view;
}
private void registerBus() {
try {
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
@Override
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public String getName() {
return MainApp.sResources.getString(R.string.smscommunicator);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Subscribe
public void onStatusEvent(final EventNewSMS ev) {
Object[] pdus = (Object[]) ev.bundle.get("pdus");
// For every SMS message received
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
processSms(new Sms(message));
}
}
private void processSms(Sms receivedSms) {
if (!isEnabled(PluginBase.GENERAL)) {
log.debug("Ignoring SMS. Plugin disabled.");
return;
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String allowedNumbers = sharedPreferences.getString("smscommunicator_allowednumbers", "");
if (allowedNumbers.indexOf(receivedSms.phoneNumber) < 0) {
log.debug("Ignoring SMS from: " + receivedSms.phoneNumber + ". Sender not allowed");
return;
}
String reply = "";
messages.add(receivedSms);
log.debug(receivedSms.toString());
String[] splited = receivedSms.text.split("\\s+");
double amount = 0d;
String passCode = "";
if (splited.length > 0) {
switch (splited[0].toUpperCase()) {
case "RT":
Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.getDbHelper().resetTreatments();
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "RT " + q.size() + " receivers";
receivedSms.processed = true;
break;
case "RNSC":
restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "RNSC " + q.size() + " receivers";
receivedSms.processed = true;
break;
case "DANAR":
DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class);
if (danaRFragment != null) reply = danaRFragment.shortStatus();
receivedSms.processed = true;
break;
case "BOLUS":
if (new Date().getTime() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) {
reply = MainApp.sResources.getString(R.string.remotebolusnotallowed);
} else if (splited.length > 1) {
amount = SafeParse.stringToDouble(splited[1]);
amount = MainApp.getConfigBuilder().applyBolusConstraints(amount);
boolean remoteBolusingAllowed = sharedPreferences.getBoolean("smscommunicator_remotebolusingallowed", false);
if (amount > 0d && remoteBolusingAllowed) {
int startChar1 = 'A'; // on iphone 1st char is uppercase :)
passCode = Character.toString((char) (startChar1 + Math.random() * ('z' - 'a' + 1)));
int startChar2 = Math.random() > 0.5 ? 'a' : 'A';
passCode += Character.toString((char) (startChar2 + Math.random() * ('z' - 'a' + 1)));
int startChar3 = Math.random() > 0.5 ? 'a' : 'A';
passCode += Character.toString((char) (startChar3 + Math.random() * ('z' - 'a' + 1)));
reply = String.format(MainApp.sResources.getString(R.string.replywithcode), amount, passCode);
receivedSms.processed = true;
} else {
reply = MainApp.sResources.getString(R.string.remotebolusnotallowed);
}
}
break;
default: // expect passCode here
if (bolusWaitingForConfirmation != null && !bolusWaitingForConfirmation.processed &&
bolusWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - bolusWaitingForConfirmation.date.getTime() < 5 * 60 * 1000L) {
bolusWaitingForConfirmation.processed = true;
PumpInterface pumpInterface = MainApp.getConfigBuilder().getActivePump();
if (pumpInterface != null) {
danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class);
PumpEnactResult result = pumpInterface.deliverTreatment(bolusWaitingForConfirmation.bolusRequested, 0, null);
if (result.success) {
reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), bolusWaitingForConfirmation.bolusRequested);
if (danaRFragment != null) reply += "\n" + danaRFragment.shortStatus();
lastRemoteBolusTime = new Date();
} else {
reply = MainApp.sResources.getString(R.string.bolusfailed);
if (danaRFragment != null) reply += "\n" + danaRFragment.shortStatus();
}
}
}
break;
}
}
if (!reply.equals("")) {
SmsManager smsManager = SmsManager.getDefault();
Sms newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
if (amount > 0d) {
newSms.bolusRequested = amount;
newSms.confirmCode = passCode;
bolusWaitingForConfirmation = newSms;
} else {
bolusWaitingForConfirmation = null;
newSms.processed = true;
}
smsManager.sendTextMessage(newSms.phoneNumber, null, newSms.text, null, null);
messages.add(newSms);
}
public void onStatusEvent(final EventSmsCommunicatorUpdateGui ev) {
updateGUI();
}
@ -273,20 +76,20 @@ public class SmsCommunicatorFragment extends Fragment implements PluginBase {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
class CustomComparator implements Comparator<Sms> {
public int compare(Sms object1, Sms object2) {
class CustomComparator implements Comparator<SmsCommunicatorPlugin.Sms> {
public int compare(SmsCommunicatorPlugin.Sms object1, SmsCommunicatorPlugin.Sms object2) {
return (int) (object1.date.getTime() - object2.date.getTime());
}
}
Collections.sort(messages, new CustomComparator());
Collections.sort(smsCommunicatorPlugin.messages, new CustomComparator());
int messagesToShow = 40;
int start = Math.max(0, messages.size() - messagesToShow);
int start = Math.max(0, smsCommunicatorPlugin.messages.size() - messagesToShow);
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
String logText = "";
for (int x = start; x < messages.size(); x++) {
Sms sms = messages.get(x);
for (int x = start; x < smsCommunicatorPlugin.messages.size(); x++) {
SmsCommunicatorPlugin.Sms sms = smsCommunicatorPlugin.messages.get(x);
if (sms.received) {
logText += df.format(sms.date) + " &lt;&lt;&lt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
} else if (sms.sent) {

View file

@ -0,0 +1,236 @@
package info.nightscout.androidaps.plugins.SmsCommunicator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui;
import info.nightscout.utils.SafeParse;
/**
* Created by mike on 05.08.2016.
*/
public class SmsCommunicatorPlugin implements PluginBase {
private static Logger log = LoggerFactory.getLogger(SmsCommunicatorPlugin.class);
private static boolean fragmentEnabled = false;
private static boolean fragmentVisible = true;
public class Sms {
String phoneNumber;
String text;
Date date;
boolean received = false;
boolean sent = false;
boolean processed = false;
String confirmCode;
double bolusRequested = 0d;
public Sms(SmsMessage message) {
phoneNumber = message.getOriginatingAddress();
text = message.getMessageBody();
date = new Date(message.getTimestampMillis());
received = true;
}
public Sms(String phoneNumber, String text, Date date) {
this.phoneNumber = phoneNumber;
this.text = text;
this.date = date;
sent = true;
}
public String toString() {
return "SMS from " + phoneNumber + ": " + text;
}
}
Sms bolusWaitingForConfirmation = null;
Date lastRemoteBolusTime = new Date(0);
ArrayList<Sms> messages = new ArrayList<>();
public SmsCommunicatorPlugin() {
MainApp.bus().register(this);
}
@Override
public String getFragmentClass() {
return SmsCommunicatorFragment.class.getName();
}
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public String getName() {
return MainApp.sResources.getString(R.string.smscommunicator);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
SmsCommunicatorPlugin.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
SmsCommunicatorPlugin.fragmentVisible = fragmentVisible;
}
@Subscribe
public void onStatusEvent(final EventNewSMS ev) {
Object[] pdus = (Object[]) ev.bundle.get("pdus");
// For every SMS message received
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
processSms(new Sms(message));
}
}
private void processSms(Sms receivedSms) {
if (!isEnabled(PluginBase.GENERAL)) {
log.debug("Ignoring SMS. Plugin disabled.");
return;
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String allowedNumbers = sharedPreferences.getString("smscommunicator_allowednumbers", "");
if (!allowedNumbers.contains(receivedSms.phoneNumber)) {
log.debug("Ignoring SMS from: " + receivedSms.phoneNumber + ". Sender not allowed");
return;
}
String reply = "";
messages.add(receivedSms);
log.debug(receivedSms.toString());
String[] splited = receivedSms.text.split("\\s+");
double amount = 0d;
String passCode = "";
if (splited.length > 0) {
switch (splited[0].toUpperCase()) {
case "RT":
Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.getDbHelper().resetTreatments();
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "RT " + q.size() + " receivers";
receivedSms.processed = true;
break;
case "RNSC":
restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "RNSC " + q.size() + " receivers";
receivedSms.processed = true;
break;
case "DANAR":
DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
if (danaRPlugin != null) reply = danaRPlugin.shortStatus();
receivedSms.processed = true;
break;
case "BOLUS":
if (new Date().getTime() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) {
reply = MainApp.sResources.getString(R.string.remotebolusnotallowed);
} else if (splited.length > 1) {
amount = SafeParse.stringToDouble(splited[1]);
amount = MainApp.getConfigBuilder().applyBolusConstraints(amount);
boolean remoteBolusingAllowed = sharedPreferences.getBoolean("smscommunicator_remotebolusingallowed", false);
if (amount > 0d && remoteBolusingAllowed) {
int startChar1 = 'A'; // on iphone 1st char is uppercase :)
passCode = Character.toString((char) (startChar1 + Math.random() * ('z' - 'a' + 1)));
int startChar2 = Math.random() > 0.5 ? 'a' : 'A';
passCode += Character.toString((char) (startChar2 + Math.random() * ('z' - 'a' + 1)));
int startChar3 = Math.random() > 0.5 ? 'a' : 'A';
passCode += Character.toString((char) (startChar3 + Math.random() * ('z' - 'a' + 1)));
reply = String.format(MainApp.sResources.getString(R.string.replywithcode), amount, passCode);
receivedSms.processed = true;
} else {
reply = MainApp.sResources.getString(R.string.remotebolusnotallowed);
}
}
break;
default: // expect passCode here
if (bolusWaitingForConfirmation != null && !bolusWaitingForConfirmation.processed &&
bolusWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - bolusWaitingForConfirmation.date.getTime() < 5 * 60 * 1000L) {
bolusWaitingForConfirmation.processed = true;
PumpInterface pumpInterface = MainApp.getConfigBuilder();
if (pumpInterface != null) {
danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRFragment.class);
PumpEnactResult result = pumpInterface.deliverTreatment(bolusWaitingForConfirmation.bolusRequested, 0, null);
if (result.success) {
reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), bolusWaitingForConfirmation.bolusRequested);
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
lastRemoteBolusTime = new Date();
} else {
reply = MainApp.sResources.getString(R.string.bolusfailed);
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
}
}
}
break;
}
}
if (!reply.equals("")) {
SmsManager smsManager = SmsManager.getDefault();
Sms newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
if (amount > 0d) {
newSms.bolusRequested = amount;
newSms.confirmCode = passCode;
bolusWaitingForConfirmation = newSms;
} else {
bolusWaitingForConfirmation = null;
newSms.processed = true;
}
smsManager.sendTextMessage(newSms.phoneNumber, null, newSms.text, null, null);
messages.add(newSms);
}
MainApp.bus().post(new EventSmsCommunicatorUpdateGui());
}
}

View file

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

View file

@ -1,63 +1,16 @@
package info.nightscout.androidaps.plugins.SourceNSClient;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.FragmentBase;
public class SourceNSClientFragment extends Fragment implements PluginBase, BgSourceInterface {
public class SourceNSClientFragment extends Fragment implements FragmentBase {
boolean fragmentEnabled = true;
private static SourceNSClientPlugin sourceNSClientPlugin = new SourceNSClientPlugin();
public SourceNSClientFragment() {
}
@Override
public int getType() {
return PluginBase.BGSOURCE;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.nsclient);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
}
public static SourceNSClientFragment newInstance() {
SourceNSClientFragment fragment = new SourceNSClientFragment();
return fragment;
public static SourceNSClientPlugin getPlugin() {
return sourceNSClientPlugin;
}
}

View file

@ -0,0 +1,55 @@
package info.nightscout.androidaps.plugins.SourceNSClient;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
/**
* Created by mike on 05.08.2016.
*/
public class SourceNSClientPlugin implements PluginBase, BgSourceInterface {
boolean fragmentEnabled = true;
@Override
public String getFragmentClass() {
return SourceNSClientFragment.class.getName();
}
@Override
public int getType() {
return PluginBase.BGSOURCE;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.nsclient);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
}
}

View file

@ -1,62 +1,15 @@
package info.nightscout.androidaps.plugins.SourceXdrip;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.FragmentBase;
public class SourceXdripFragment extends Fragment implements PluginBase, BgSourceInterface {
public class SourceXdripFragment extends Fragment implements FragmentBase {
boolean fragmentEnabled = true;
private static SourceXdripPlugin sourceXdripPlugin = new SourceXdripPlugin();
public SourceXdripFragment() {
}
@Override
public int getType() {
return PluginBase.BGSOURCE;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.xdrip);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
}
public static SourceXdripFragment newInstance() {
SourceXdripFragment fragment = new SourceXdripFragment();
return fragment;
public static SourceXdripPlugin getPlugin() {
return sourceXdripPlugin;
}
}

View file

@ -0,0 +1,56 @@
package info.nightscout.androidaps.plugins.SourceXdrip;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment;
/**
* Created by mike on 05.08.2016.
*/
public class SourceXdripPlugin implements PluginBase, BgSourceInterface {
@Override
public String getFragmentClass() {
return SourceNSClientFragment.class.getName();
}
private static boolean fragmentEnabled = true;
@Override
public int getType() {
return PluginBase.BGSOURCE;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.xdrip);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return false;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
SourceXdripPlugin.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
}
}

View file

@ -1,10 +1,7 @@
package info.nightscout.androidaps.plugins.TempBasals;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
@ -15,233 +12,38 @@ import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.TempBasalsInterface;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.TempBasals.events.EventTempBasalsUpdateGui;
import info.nightscout.utils.DecimalFormatter;
public class TempBasalsFragment extends Fragment implements PluginBase, TempBasalsInterface {
public class TempBasalsFragment extends Fragment implements FragmentBase {
private static Logger log = LoggerFactory.getLogger(TempBasalsFragment.class);
private static TempBasalsPlugin tempBasalsPlugin = new TempBasalsPlugin();
public static TempBasalsPlugin getPlugin() {
return tempBasalsPlugin;
}
RecyclerView recyclerView;
LinearLayoutManager llm;
TextView tempBasalTotalView;
public long lastCalculationTimestamp = 0;
public IobTotal lastCalculation;
private List<TempBasal> tempBasals;
private List<TempBasal> extendedBoluses;
private boolean useExtendedBoluses = false;
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
boolean visibleNow = false;
@Override
public String getName() {
return MainApp.instance().getString(R.string.tempbasals);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.TEMPBASAL;
}
private void initializeData() {
try {
Dao<TempBasal, Long> dao = MainApp.getDbHelper().getDaoTempBasals();
/*
// **************** TESTING CREATE FAKE RECORD *****************
TempBasal fake = new TempBasal();
fake.timeStart = new Date(new Date().getTime() - 45 * 40 * 1000);
fake.timeEnd = new Date(new Date().getTime() - new Double(Math.random() * 45d * 40 * 1000).longValue());
fake.duration = 30;
fake.percent = 150;
fake.isAbsolute = false;
fake.isExtended = false;
dao.createOrUpdate(fake);
// **************** TESTING CREATE FAKE RECORD *****************
*/
QueryBuilder<TempBasal, Long> queryBuilder = dao.queryBuilder();
queryBuilder.orderBy("timeIndex", false);
Where where = queryBuilder.where();
where.eq("isExtended", false);
queryBuilder.limit(30L);
PreparedQuery<TempBasal> preparedQuery = queryBuilder.prepare();
tempBasals = dao.query(preparedQuery);
QueryBuilder<TempBasal, Long> queryBuilderExt = dao.queryBuilder();
queryBuilderExt.orderBy("timeIndex", false);
Where whereExt = queryBuilderExt.where();
whereExt.eq("isExtended", true);
queryBuilderExt.limit(5L);
PreparedQuery<TempBasal> preparedQueryExt = queryBuilderExt.prepare();
extendedBoluses = dao.query(preparedQueryExt);
// Update ended
checkForExpiredExtended();
checkForExpiredTemps();
} catch (SQLException e) {
log.debug(e.getMessage(), e);
tempBasals = new ArrayList<TempBasal>();
extendedBoluses = new ArrayList<TempBasal>();
}
}
public void checkForExpiredTemps() {
checkForExpired(tempBasals);
}
public void checkForExpiredExtended() {
checkForExpired(extendedBoluses);
}
private void checkForExpired(List<TempBasal> list) {
long now = new Date().getTime();
for (int position = list.size() - 1; position >= 0; position--) {
TempBasal t = list.get(position);
boolean update = false;
if (t.timeEnd == null && t.getPlannedTimeEnd().getTime() < now) {
t.timeEnd = new Date(t.getPlannedTimeEnd().getTime());
if (Config.logTempBasalsCut)
log.debug("Add timeEnd to old record");
update = true;
}
if (position > 0) {
Date startofnewer = list.get(position - 1).timeStart;
if (t.timeEnd == null) {
t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd().getTime()));
if (Config.logTempBasalsCut)
log.debug("Add timeEnd to old record");
update = true;
} else if (t.timeEnd.getTime() > startofnewer.getTime()) {
t.timeEnd = startofnewer;
update = true;
}
}
if (update) {
try {
Dao<TempBasal, Long> dao = MainApp.getDbHelper().getDaoTempBasals();
dao.update(t);
} catch (SQLException e) {
e.printStackTrace();
}
if (Config.logTempBasalsCut) {
log.debug("Fixing unfinished temp end: " + t.log());
if (position > 0)
log.debug("Previous: " + list.get(position - 1).log());
}
}
}
}
/*
* Recalculate IOB if value is older than 1 minute
*/
public void updateTotalIOBIfNeeded() {
if (lastCalculationTimestamp > new Date().getTime() - 60 * 1000)
return;
updateTotalIOB();
}
@Override
public IobTotal getLastCalculation() {
return lastCalculation;
}
@Override
public void updateTotalIOB() {
checkForExpired(tempBasals);
checkForExpired(extendedBoluses);
Date now = new Date();
IobTotal total = new IobTotal();
for (Integer pos = 0; pos < tempBasals.size(); pos++) {
TempBasal t = tempBasals.get(pos);
IobTotal calc = t.iobCalc(now);
total.plus(calc);
}
if (useExtendedBoluses) {
for (Integer pos = 0; pos < extendedBoluses.size(); pos++) {
TempBasal t = extendedBoluses.get(pos);
IobTotal calc = t.iobCalc(now);
total.plus(calc);
}
}
lastCalculationTimestamp = new Date().getTime();
lastCalculation = total;
}
@Nullable
@Override
public TempBasal getTempBasal(Date time) {
checkForExpired(tempBasals);
for (TempBasal t : tempBasals) {
if (t.isInProgress(time)) return t;
}
return null;
}
@Override
public TempBasal getExtendedBolus(Date time) {
checkForExpired(extendedBoluses);
for (TempBasal t : extendedBoluses) {
if (t.isInProgress(time)) return t;
}
return null;
}
public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TempBasalsViewHolder> {
List<TempBasal> tempBasalList;
@ -331,25 +133,6 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
}
}
public TempBasalsFragment() {
super();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
registerBus();
initializeData();
updateGUI();
}
public static TempBasalsFragment newInstance() {
TempBasalsFragment fragment = new TempBasalsFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -360,7 +143,7 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getMergedList());
RecyclerViewAdapter adapter = new RecyclerViewAdapter(tempBasalsPlugin.getMergedList());
recyclerView.setAdapter(adapter);
tempBasalTotalView = (TextView) view.findViewById(R.id.tempbasals_totaltempiob);
@ -368,70 +151,37 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
return view;
}
private void registerBus() {
try {
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
@Override
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
List<TempBasal> getMergedList() {
if (useExtendedBoluses) {
List<TempBasal> merged = new ArrayList<TempBasal>();
merged.addAll(tempBasals);
merged.addAll(extendedBoluses);
class CustomComparator implements Comparator<TempBasal> {
public int compare(TempBasal object1, TempBasal object2) {
return (int) (object2.timeIndex - object1.timeIndex);
}
}
Collections.sort(merged, new CustomComparator());
return merged;
} else {
return tempBasals;
}
}
@Subscribe
public void onStatusEvent(final EventTempBasalChange ev) {
initializeData();
}
public void onStatusEvent(final EventPreferenceChange s) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
initializeData();
public void onStatusEvent(final EventTempBasalsUpdateGui s) {
updateGUI();
}
public void updateGUI() {
Activity activity = getActivity();
if (visibleNow && activity != null && recyclerView != null)
if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerView.swapAdapter(new RecyclerViewAdapter(getMergedList()), false);
if (lastCalculation != null) {
String totalText = DecimalFormatter.to2Decimal(lastCalculation.basaliob) + " U";
recyclerView.swapAdapter(new RecyclerViewAdapter(tempBasalsPlugin.getMergedList()), false);
if (tempBasalsPlugin.lastCalculation != null) {
String totalText = DecimalFormatter.to2Decimal(tempBasalsPlugin.lastCalculation.basaliob) + " U";
tempBasalTotalView.setText(totalText);
}
}
});
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
visibleNow = true;
updateTotalIOBIfNeeded();
updateGUI();
} else
visibleNow = false;
}
}

View file

@ -0,0 +1,271 @@
package info.nightscout.androidaps.plugins.TempBasals;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.TempBasalsInterface;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.TempBasals.events.EventTempBasalsUpdateGui;
/**
* Created by mike on 05.08.2016.
*/
public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
private static Logger log = LoggerFactory.getLogger(TempBasalsPlugin.class);
public static long lastCalculationTimestamp = 0;
public static IobTotal lastCalculation;
private static List<TempBasal> tempBasals;
private static List<TempBasal> extendedBoluses;
private static boolean useExtendedBoluses = false;
private static boolean fragmentEnabled = true;
private static boolean fragmentVisible = true;
public TempBasalsPlugin() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
initializeData();
MainApp.bus().post(new EventTempBasalsUpdateGui());
}
@Override
public String getFragmentClass() {
return TempBasalsFragment.class.getName();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.tempbasals);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.TEMPBASAL;
}
private void initializeData() {
try {
Dao<TempBasal, Long> dao = MainApp.getDbHelper().getDaoTempBasals();
/*
// **************** TESTING CREATE FAKE RECORD *****************
TempBasal fake = new TempBasal();
fake.timeStart = new Date(new Date().getTime() - 45 * 40 * 1000);
fake.timeEnd = new Date(new Date().getTime() - new Double(Math.random() * 45d * 40 * 1000).longValue());
fake.duration = 30;
fake.percent = 150;
fake.isAbsolute = false;
fake.isExtended = false;
dao.createOrUpdate(fake);
// **************** TESTING CREATE FAKE RECORD *****************
*/
QueryBuilder<TempBasal, Long> queryBuilder = dao.queryBuilder();
queryBuilder.orderBy("timeIndex", false);
Where where = queryBuilder.where();
where.eq("isExtended", false);
queryBuilder.limit(30L);
PreparedQuery<TempBasal> preparedQuery = queryBuilder.prepare();
tempBasals = dao.query(preparedQuery);
QueryBuilder<TempBasal, Long> queryBuilderExt = dao.queryBuilder();
queryBuilderExt.orderBy("timeIndex", false);
Where whereExt = queryBuilderExt.where();
whereExt.eq("isExtended", true);
queryBuilderExt.limit(5L);
PreparedQuery<TempBasal> preparedQueryExt = queryBuilderExt.prepare();
extendedBoluses = dao.query(preparedQueryExt);
// Update ended
checkForExpiredExtended();
checkForExpiredTemps();
} catch (SQLException e) {
log.debug(e.getMessage(), e);
tempBasals = new ArrayList<TempBasal>();
extendedBoluses = new ArrayList<TempBasal>();
}
}
public void checkForExpiredTemps() {
checkForExpired(tempBasals);
}
public void checkForExpiredExtended() {
checkForExpired(extendedBoluses);
}
private void checkForExpired(List<TempBasal> list) {
long now = new Date().getTime();
for (int position = list.size() - 1; position >= 0; position--) {
TempBasal t = list.get(position);
boolean update = false;
if (t.timeEnd == null && t.getPlannedTimeEnd().getTime() < now) {
t.timeEnd = new Date(t.getPlannedTimeEnd().getTime());
if (Config.logTempBasalsCut)
log.debug("Add timeEnd to old record");
update = true;
}
if (position > 0) {
Date startofnewer = list.get(position - 1).timeStart;
if (t.timeEnd == null) {
t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd().getTime()));
if (Config.logTempBasalsCut)
log.debug("Add timeEnd to old record");
update = true;
} else if (t.timeEnd.getTime() > startofnewer.getTime()) {
t.timeEnd = startofnewer;
update = true;
}
}
if (update) {
try {
Dao<TempBasal, Long> dao = MainApp.getDbHelper().getDaoTempBasals();
dao.update(t);
} catch (SQLException e) {
e.printStackTrace();
}
if (Config.logTempBasalsCut) {
log.debug("Fixing unfinished temp end: " + t.log());
if (position > 0)
log.debug("Previous: " + list.get(position - 1).log());
}
}
}
}
/*
* Recalculate IOB if value is older than 1 minute
*/
public void updateTotalIOBIfNeeded() {
if (lastCalculationTimestamp > new Date().getTime() - 60 * 1000)
return;
updateTotalIOB();
}
@Override
public IobTotal getLastCalculation() {
return lastCalculation;
}
@Override
public void updateTotalIOB() {
checkForExpired(tempBasals);
checkForExpired(extendedBoluses);
Date now = new Date();
IobTotal total = new IobTotal();
for (Integer pos = 0; pos < tempBasals.size(); pos++) {
TempBasal t = tempBasals.get(pos);
IobTotal calc = t.iobCalc(now);
total.plus(calc);
}
if (useExtendedBoluses) {
for (Integer pos = 0; pos < extendedBoluses.size(); pos++) {
TempBasal t = extendedBoluses.get(pos);
IobTotal calc = t.iobCalc(now);
total.plus(calc);
}
}
lastCalculationTimestamp = new Date().getTime();
lastCalculation = total;
}
@Nullable
@Override
public TempBasal getTempBasal(Date time) {
checkForExpired(tempBasals);
for (TempBasal t : tempBasals) {
if (t.isInProgress(time)) return t;
}
return null;
}
@Override
public TempBasal getExtendedBolus(Date time) {
checkForExpired(extendedBoluses);
for (TempBasal t : extendedBoluses) {
if (t.isInProgress(time)) return t;
}
return null;
}
List<TempBasal> getMergedList() {
if (useExtendedBoluses) {
List<TempBasal> merged = new ArrayList<TempBasal>();
merged.addAll(tempBasals);
merged.addAll(extendedBoluses);
class CustomComparator implements Comparator<TempBasal> {
public int compare(TempBasal object1, TempBasal object2) {
return (int) (object2.timeIndex - object1.timeIndex);
}
}
Collections.sort(merged, new CustomComparator());
return merged;
} else {
return tempBasals;
}
}
@Subscribe
public void onStatusEvent(final EventTempBasalChange ev) {
initializeData();
}
public void onStatusEvent(final EventPreferenceChange s) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false);
initializeData();
}
}

View file

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

View file

@ -36,15 +36,25 @@ import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.Treatments.events.EventTreatmentsUpdateGui;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DecimalFormatter;
public class TreatmentsFragment extends Fragment implements View.OnClickListener, PluginBase, TreatmentsInterface {
public class TreatmentsFragment extends Fragment implements View.OnClickListener, FragmentBase {
private static Logger log = LoggerFactory.getLogger(TreatmentsFragment.class);
private static TreatmentsPlugin treatmentsPlugin = new TreatmentsPlugin();
public static TreatmentsPlugin getPlugin() {
return treatmentsPlugin;
}
RecyclerView recyclerView;
LinearLayoutManager llm;
@ -52,140 +62,6 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
TextView activityTotal;
Button refreshFromNS;
private long lastCalculationTimestamp = 0;
private IobTotal lastCalculation;
private List<Treatment> treatments;
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
boolean visibleNow = false;
@Override
public String getName() {
return MainApp.instance().getString(R.string.treatments);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.TREATMENT;
}
private void initializeData() {
try {
Dao<Treatment, Long> dao = MainApp.getDbHelper().getDaoTreatments();
QueryBuilder<Treatment, Long> queryBuilder = dao.queryBuilder();
queryBuilder.orderBy("timeIndex", false);
queryBuilder.limit(30l);
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
treatments = dao.query(preparedQuery);
} catch (SQLException e) {
log.debug(e.getMessage(), e);
treatments = new ArrayList<Treatment>();
}
}
/*
* Recalculate IOB if value is older than 1 minute
*/
public void updateTotalIOBIfNeeded() {
if (lastCalculationTimestamp > new Date().getTime() - 60 * 1000)
return;
updateTotalIOB();
}
@Override
public IobTotal getLastCalculation() {
return lastCalculation;
}
@Override
public void updateTotalIOB() {
IobTotal total = new IobTotal();
if (MainApp.getConfigBuilder() == null || MainApp.getConfigBuilder().getActiveProfile() == null) // app not initialized yet
return;
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) {
lastCalculation = total;
return;
}
Double dia = profile.getDia();
Date now = new Date();
for (Integer pos = 0; pos < treatments.size(); pos++) {
Treatment t = treatments.get(pos);
Iob tIOB = t.iobCalc(now, dia);
total.iob += tIOB.iobContrib;
total.activity += tIOB.activityContrib;
Iob bIOB = t.iobCalc(now, dia / 2);
total.bolussnooze += bIOB.iobContrib;
}
lastCalculationTimestamp = new Date().getTime();
lastCalculation = total;
}
public class MealData {
public double boluses = 0d;
public double carbs = 0d;
}
@Override
public MealData getMealData() {
MealData result = new MealData();
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null)
return result;
for (Treatment treatment : treatments) {
long now = new Date().getTime();
long dia_ago = now - (new Double(profile.getDia() * 60 * 60 * 1000l)).longValue();
long t = treatment.created_at.getTime();
if (t > dia_ago && t <= now) {
if (treatment.carbs >= 1) {
result.carbs += treatment.carbs;
}
if (treatment.insulin >= 0.1) {
result.boluses += treatment.insulin;
}
}
}
return result;
}
@Override
public List<Treatment> getTreatments() {
return treatments;
}
public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TreatmentsViewHolder> {
List<Treatment> treatments;
@ -253,23 +129,6 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
}
}
public TreatmentsFragment() {
super();
registerBus();
initializeData();
updateGUI();
}
public static TreatmentsFragment newInstance() {
TreatmentsFragment fragment = new TreatmentsFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -280,7 +139,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(treatments);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.treatments);
recyclerView.setAdapter(adapter);
iobTotal = (TextView) view.findViewById(R.id.treatments_iobtotal);
@ -303,7 +162,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
builder.setPositiveButton(this.getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainApp.getDbHelper().resetTreatments();
initializeData();
treatmentsPlugin.initializeData();
updateGUI();
Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
@ -316,45 +175,41 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
}
}
private void registerBus() {
try {
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
@Override
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@Subscribe
public void onStatusEvent(final EventTreatmentsUpdateGui ev) {
updateGUI();
}
@Subscribe
public void onStatusEvent(final EventTreatmentChange ev) {
initializeData();
updateGUI();
}
public void updateGUI() {
Activity activity = getActivity();
if (visibleNow && activity != null && recyclerView != null)
if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerView.swapAdapter(new RecyclerViewAdapter(treatments), false);
if (lastCalculation != null)
iobTotal.setText(DecimalFormatter.to2Decimal(lastCalculation.iob) + " U");
if (lastCalculation != null)
activityTotal.setText(DecimalFormatter.to3Decimal(lastCalculation.activity) + " U");
recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.treatments), false);
if (TreatmentsPlugin.lastCalculation != null)
iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.lastCalculation.iob) + " U");
if (TreatmentsPlugin.lastCalculation != null)
activityTotal.setText(DecimalFormatter.to3Decimal(TreatmentsPlugin.lastCalculation.activity) + " U");
}
});
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
visibleNow = true;
updateTotalIOBIfNeeded();
updateGUI();
} else
visibleNow = false;
}
}

View file

@ -0,0 +1,175 @@
package info.nightscout.androidaps.plugins.Treatments;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.Treatments.events.EventTreatmentsUpdateGui;
import info.nightscout.client.data.NSProfile;
/**
* Created by mike on 05.08.2016.
*/
public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
private static Logger log = LoggerFactory.getLogger(TreatmentsPlugin.class);
public static long lastCalculationTimestamp = 0;
public static IobTotal lastCalculation;
public static List<Treatment> treatments;
private static boolean fragmentEnabled = true;
private static boolean fragmentVisible = true;
@Override
public String getFragmentClass() {
return TreatmentsFragment.class.getName();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.treatments);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.TREATMENT;
}
public TreatmentsPlugin() {
MainApp.bus().register(this);
initializeData();
MainApp.bus().post(new EventTreatmentsUpdateGui());
}
public void initializeData() {
try {
Dao<Treatment, Long> dao = MainApp.getDbHelper().getDaoTreatments();
QueryBuilder<Treatment, Long> queryBuilder = dao.queryBuilder();
queryBuilder.orderBy("timeIndex", false);
queryBuilder.limit(30l);
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
treatments = dao.query(preparedQuery);
} catch (SQLException e) {
log.debug(e.getMessage(), e);
treatments = new ArrayList<Treatment>();
}
}
/*
* Recalculate IOB if value is older than 1 minute
*/
public void updateTotalIOBIfNeeded() {
if (lastCalculationTimestamp > new Date().getTime() - 60 * 1000)
return;
updateTotalIOB();
}
@Override
public IobTotal getLastCalculation() {
return lastCalculation;
}
@Override
public void updateTotalIOB() {
IobTotal total = new IobTotal();
if (MainApp.getConfigBuilder() == null || MainApp.getConfigBuilder().getActiveProfile() == null) // app not initialized yet
return;
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) {
lastCalculation = total;
return;
}
Double dia = profile.getDia();
Date now = new Date();
for (Integer pos = 0; pos < treatments.size(); pos++) {
Treatment t = treatments.get(pos);
Iob tIOB = t.iobCalc(now, dia);
total.iob += tIOB.iobContrib;
total.activity += tIOB.activityContrib;
Iob bIOB = t.iobCalc(now, dia / 2);
total.bolussnooze += bIOB.iobContrib;
}
lastCalculationTimestamp = new Date().getTime();
lastCalculation = total;
}
public class MealData {
public double boluses = 0d;
public double carbs = 0d;
}
@Override
public MealData getMealData() {
MealData result = new MealData();
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null)
return result;
for (Treatment treatment : treatments) {
long now = new Date().getTime();
long dia_ago = now - (new Double(profile.getDia() * 60 * 60 * 1000l)).longValue();
long t = treatment.created_at.getTime();
if (t > dia_ago && t <= now) {
if (treatment.carbs >= 1) {
result.carbs += treatment.carbs;
}
if (treatment.insulin >= 0.1) {
result.boluses += treatment.insulin;
}
}
}
return result;
}
@Override
public List<Treatment> getTreatments() {
return treatments;
}
}

View file

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

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.VirtualPump;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
@ -12,31 +11,24 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.VirtualPump.events.EventVirtualPumpUpdateGui;
public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInterface {
public class VirtualPumpFragment extends Fragment implements FragmentBase {
private static Logger log = LoggerFactory.getLogger(VirtualPumpFragment.class);
Double defaultBasalValue = 0.2d;
private static VirtualPumpPlugin virtualPumpPlugin = new VirtualPumpPlugin();
Integer batteryPercent = 50;
Integer reservoirInUnits = 50;
public static VirtualPumpPlugin getPlugin() {
return virtualPumpPlugin;
}
TextView basaBasalRateView;
TextView tempBasalView;
@ -44,65 +36,21 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
TextView batteryView;
TextView reservoirView;
Handler loopHandler = new Handler();
Runnable refreshLoop = null;
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
boolean visibleNow = false;
@Override
public String getName() {
return MainApp.instance().getString(R.string.virtualpump);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.PUMP;
}
public static VirtualPumpFragment newInstance() {
VirtualPumpFragment fragment = new VirtualPumpFragment();
return fragment;
}
private static Handler sLoopHandler = new Handler();
private static Runnable sRefreshLoop = null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (refreshLoop == null) {
refreshLoop = new Runnable() {
if (sRefreshLoop == null) {
sRefreshLoop = new Runnable() {
@Override
public void run() {
updateGUI();
loopHandler.postDelayed(refreshLoop, 60 * 1000l);
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
};
loopHandler.postDelayed(refreshLoop, 60 * 1000l);
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
}
@ -121,289 +69,42 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
}
@Override
public boolean isTempBasalInProgress() {
return getTempBasal() != null;
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
}
@Override
public boolean isExtendedBoluslInProgress() {
return getExtendedBolus() != null;
public void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@Override
public void setNewBasalProfile(NSProfile profile) {
// Do nothing here. we are using MainApp.getConfigBuilder().getActiveProfile().getProfile();
}
@Override
public double getBaseBasalRate() {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null)
return defaultBasalValue;
return profile.getBasal(profile.secondsFromMidnight());
}
@Override
public double getTempBasalAbsoluteRate() {
if (!isTempBasalInProgress())
return 0;
if (getTempBasal().isAbsolute) {
return getTempBasal().absolute;
} else {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null)
return defaultBasalValue;
Double baseRate = profile.getBasal(profile.secondsFromMidnight());
Double tempRate = baseRate * (getTempBasal().percent / 100d);
return baseRate + tempRate;
}
}
@Override
public TempBasal getTempBasal() {
return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date());
}
@Override
public TempBasal getExtendedBolus() {
return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(new Date());
}
@Override
public double getTempBasalRemainingMinutes() {
if (!isTempBasalInProgress())
return 0;
return getTempBasal().getPlannedRemainingMinutes();
}
@Override
public TempBasal getTempBasal(Date time) {
return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(time);
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
PumpEnactResult result = new PumpEnactResult();
result.success = true;
result.bolusDelivered = insulin;
result.carbsDelivered = carbs;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpComm)
log.debug("Delivering treatment insulin: " + insulin + "U carbs: " + carbs + "g " + result);
@Subscribe
public void onStatusEvent(final EventVirtualPumpUpdateGui ev) {
updateGUI();
return result;
}
@Override
public void stopBolusDelivering() {
}
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
PumpEnactResult result = cancelTempBasal();
if (!result.success)
return result;
TempBasal tempBasal = new TempBasal();
tempBasal.timeStart = new Date();
tempBasal.isAbsolute = true;
tempBasal.absolute = absoluteRate;
tempBasal.duration = durationInMinutes;
result.success = true;
result.enacted = true;
result.isTempCancel = false;
result.absolute = absoluteRate;
result.duration = durationInMinutes;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(tempBasal);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Setting temp basal absolute: " + result);
updateGUI();
return result;
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult();
if (isTempBasalInProgress()) {
result = cancelTempBasal();
if (!result.success)
return result;
}
TempBasal tempBasal = new TempBasal();
tempBasal.timeStart = new Date();
tempBasal.isAbsolute = false;
tempBasal.percent = percent;
tempBasal.duration = durationInMinutes;
result.success = true;
result.enacted = true;
result.percent = percent;
result.isPercent = true;
result.isTempCancel = false;
result.duration = durationInMinutes;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(tempBasal);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Settings temp basal percent: " + result);
updateGUI();
return result;
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
PumpEnactResult result = cancelExtendedBolus();
if (!result.success)
return result;
TempBasal extendedBolus = new TempBasal();
extendedBolus.timeStart = new Date();
extendedBolus.isExtended = true;
extendedBolus.absolute = insulin * 60d / durationInMinutes;
extendedBolus.duration = durationInMinutes;
extendedBolus.isAbsolute = true;
result.success = true;
result.enacted = true;
result.bolusDelivered = insulin;
result.isTempCancel = false;
result.duration = durationInMinutes;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(extendedBolus);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.enacted = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Setting extended bolus: " + result);
updateGUI();
return result;
}
@Override
public PumpEnactResult cancelTempBasal() {
PumpEnactResult result = new PumpEnactResult();
result.success = true;
result.isTempCancel = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (isTempBasalInProgress()) {
result.enacted = true;
TempBasal tb = getTempBasal();
tb.timeEnd = new Date();
try {
MainApp.instance().getDbHelper().getDaoTempBasals().update(tb);
//tempBasal = null;
if (Config.logPumpComm)
log.debug("Canceling temp basal: " + result);
updateGUI();
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.enacted = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
}
return result;
}
@Override
public PumpEnactResult cancelExtendedBolus() {
PumpEnactResult result = new PumpEnactResult();
if (isExtendedBoluslInProgress()) {
TempBasal extendedBolus = getExtendedBolus();
extendedBolus.timeEnd = new Date();
try {
MainApp.instance().getDbHelper().getDaoTempBasals().update(extendedBolus);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
}
result.success = true;
result.enacted = true;
result.isTempCancel = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpComm)
log.debug("Canceling extended basal: " + result);
updateGUI();
return result;
}
@Override
public JSONObject getJSONStatus() {
JSONObject pump = new JSONObject();
JSONObject battery = new JSONObject();
JSONObject status = new JSONObject();
try {
battery.put("percent", batteryPercent);
status.put("status", "normal");
if (isTempBasalInProgress()) {
status.put("tempbasalpct", getTempBasal().percent);
status.put("tempbasalstart", DateUtil.toISOString(getTempBasal().timeStart));
status.put("tempbasalremainmin", getTempBasal().getPlannedRemainingMinutes());
}
status.put("timestamp", DateUtil.toISOString(new Date()));
pump.put("battery", battery);
pump.put("status", status);
pump.put("reservoir", reservoirInUnits);
pump.put("clock", DateUtil.toISOString(new Date()));
} catch (JSONException e) {
}
return pump;
}
@Override
public String deviceID() {
return "VirtualPump";
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
visibleNow = true;
updateGUI();
} else
visibleNow = false;
}
public void updateGUI() {
Activity activity = getActivity();
if (activity != null && visibleNow && basaBasalRateView != null)
if (activity != null && basaBasalRateView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
basaBasalRateView.setText(getBaseBasalRate() + "U");
if (isTempBasalInProgress()) {
tempBasalView.setText(getTempBasal().toString());
basaBasalRateView.setText(virtualPumpPlugin.getBaseBasalRate() + "U");
if (virtualPumpPlugin.isTempBasalInProgress()) {
tempBasalView.setText(virtualPumpPlugin.getTempBasal().toString());
} else {
tempBasalView.setText("");
}
if (isExtendedBoluslInProgress()) {
extendedBolusView.setText(getExtendedBolus().toString());
if (virtualPumpPlugin.isExtendedBoluslInProgress()) {
extendedBolusView.setText(virtualPumpPlugin.getExtendedBolus().toString());
} else {
extendedBolusView.setText("");
}
batteryView.setText(batteryPercent + "%");
reservoirView.setText(reservoirInUnits + "U");
batteryView.setText(VirtualPumpPlugin.batteryPercent + "%");
reservoirView.setText(VirtualPumpPlugin.reservoirInUnits + "U");
}
});
}

View file

@ -0,0 +1,330 @@
package info.nightscout.androidaps.plugins.VirtualPump;
import android.content.Context;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.VirtualPump.events.EventVirtualPumpUpdateGui;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
/**
* Created by mike on 05.08.2016.
*/
public class VirtualPumpPlugin implements PluginBase, PumpInterface {
private static Logger log = LoggerFactory.getLogger(VirtualPumpPlugin.class);
public static Double defaultBasalValue = 0.2d;
public static Integer batteryPercent = 50;
public static Integer reservoirInUnits = 50;
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
@Override
public String getFragmentClass() {
return VirtualPumpFragment.class.getName();
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.virtualpump);
}
@Override
public boolean isEnabled(int type) {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return fragmentVisible;
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
@Override
public int getType() {
return PluginBase.PUMP;
}
@Override
public boolean isTempBasalInProgress() {
return getTempBasal() != null;
}
@Override
public boolean isExtendedBoluslInProgress() {
return getExtendedBolus() != null;
}
@Override
public void setNewBasalProfile(NSProfile profile) {
// Do nothing here. we are using MainApp.getConfigBuilder().getActiveProfile().getProfile();
}
@Override
public double getBaseBasalRate() {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null)
return defaultBasalValue;
return profile.getBasal(profile.secondsFromMidnight());
}
@Override
public double getTempBasalAbsoluteRate() {
if (!isTempBasalInProgress())
return 0;
if (getTempBasal().isAbsolute) {
return getTempBasal().absolute;
} else {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null)
return defaultBasalValue;
Double baseRate = profile.getBasal(profile.secondsFromMidnight());
Double tempRate = baseRate * (getTempBasal().percent / 100d);
return baseRate + tempRate;
}
}
@Override
public TempBasal getTempBasal() {
return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date());
}
@Override
public TempBasal getExtendedBolus() {
return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(new Date());
}
@Override
public double getTempBasalRemainingMinutes() {
if (!isTempBasalInProgress())
return 0;
return getTempBasal().getPlannedRemainingMinutes();
}
@Override
public TempBasal getTempBasal(Date time) {
return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(time);
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) {
PumpEnactResult result = new PumpEnactResult();
result.success = true;
result.bolusDelivered = insulin;
result.carbsDelivered = carbs;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpComm)
log.debug("Delivering treatment insulin: " + insulin + "U carbs: " + carbs + "g " + result);
MainApp.bus().post(new EventVirtualPumpUpdateGui());
return result;
}
@Override
public void stopBolusDelivering() {
}
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
PumpEnactResult result = cancelTempBasal();
if (!result.success)
return result;
TempBasal tempBasal = new TempBasal();
tempBasal.timeStart = new Date();
tempBasal.isAbsolute = true;
tempBasal.absolute = absoluteRate;
tempBasal.duration = durationInMinutes;
result.success = true;
result.enacted = true;
result.isTempCancel = false;
result.absolute = absoluteRate;
result.duration = durationInMinutes;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(tempBasal);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Setting temp basal absolute: " + result);
MainApp.bus().post(new EventVirtualPumpUpdateGui());
return result;
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
PumpEnactResult result = new PumpEnactResult();
if (isTempBasalInProgress()) {
result = cancelTempBasal();
if (!result.success)
return result;
}
TempBasal tempBasal = new TempBasal();
tempBasal.timeStart = new Date();
tempBasal.isAbsolute = false;
tempBasal.percent = percent;
tempBasal.duration = durationInMinutes;
result.success = true;
result.enacted = true;
result.percent = percent;
result.isPercent = true;
result.isTempCancel = false;
result.duration = durationInMinutes;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(tempBasal);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Settings temp basal percent: " + result);
MainApp.bus().post(new EventVirtualPumpUpdateGui());
return result;
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
PumpEnactResult result = cancelExtendedBolus();
if (!result.success)
return result;
TempBasal extendedBolus = new TempBasal();
extendedBolus.timeStart = new Date();
extendedBolus.isExtended = true;
extendedBolus.absolute = insulin * 60d / durationInMinutes;
extendedBolus.duration = durationInMinutes;
extendedBolus.isAbsolute = true;
result.success = true;
result.enacted = true;
result.bolusDelivered = insulin;
result.isTempCancel = false;
result.duration = durationInMinutes;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(extendedBolus);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.enacted = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Setting extended bolus: " + result);
MainApp.bus().post(new EventVirtualPumpUpdateGui());
return result;
}
@Override
public PumpEnactResult cancelTempBasal() {
PumpEnactResult result = new PumpEnactResult();
result.success = true;
result.isTempCancel = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (isTempBasalInProgress()) {
result.enacted = true;
TempBasal tb = getTempBasal();
tb.timeEnd = new Date();
try {
MainApp.instance().getDbHelper().getDaoTempBasals().update(tb);
//tempBasal = null;
if (Config.logPumpComm)
log.debug("Canceling temp basal: " + result);
MainApp.bus().post(new EventVirtualPumpUpdateGui());
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.enacted = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
}
return result;
}
@Override
public PumpEnactResult cancelExtendedBolus() {
PumpEnactResult result = new PumpEnactResult();
if (isExtendedBoluslInProgress()) {
TempBasal extendedBolus = getExtendedBolus();
extendedBolus.timeEnd = new Date();
try {
MainApp.instance().getDbHelper().getDaoTempBasals().update(extendedBolus);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
}
result.success = true;
result.enacted = true;
result.isTempCancel = true;
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpComm)
log.debug("Canceling extended basal: " + result);
MainApp.bus().post(new EventVirtualPumpUpdateGui());
return result;
}
@Override
public JSONObject getJSONStatus() {
JSONObject pump = new JSONObject();
JSONObject battery = new JSONObject();
JSONObject status = new JSONObject();
try {
battery.put("percent", batteryPercent);
status.put("status", "normal");
if (isTempBasalInProgress()) {
status.put("tempbasalpct", getTempBasal().percent);
status.put("tempbasalstart", DateUtil.toISOString(getTempBasal().timeStart));
status.put("tempbasalremainmin", getTempBasal().getPlannedRemainingMinutes());
}
status.put("timestamp", DateUtil.toISOString(new Date()));
pump.put("battery", battery);
pump.put("status", status);
pump.put("reservoir", reservoirInUnits);
pump.put("clock", DateUtil.toISOString(new Date()));
} catch (JSONException e) {
}
return pump;
}
@Override
public String deviceID() {
return "VirtualPump";
}
}

View file

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

View file

@ -25,6 +25,7 @@ import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
import info.nightscout.utils.ToastUtils;
@ -38,13 +39,13 @@ public class KeepAliveReceiver extends BroadcastReceiver {
wl.acquire();
log.debug("KeepAlive received");
final DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class);
if (Config.DANAR && danaRFragment.isEnabled(PluginBase.PUMP)) {
if (danaRFragment.getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime() && !danaRFragment.isConnected() && !danaRFragment.isConnecting()) {
final DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
if (Config.DANAR && danaRPlugin.isEnabled(PluginBase.PUMP)) {
if (danaRPlugin.getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime() && !danaRPlugin.isConnected() && !danaRPlugin.isConnecting()) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
danaRFragment.doConnect("KeepAlive");
danaRPlugin.doConnect("KeepAlive");
}
});
t.start();

View file

@ -1,12 +1,12 @@
package info.nightscout.androidaps.tabs;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import info.nightscout.androidaps.interfaces.PluginBase;
@ -15,21 +15,23 @@ import info.nightscout.androidaps.interfaces.PluginBase;
*/
public class TabPageAdapter extends FragmentStatePagerAdapter {
ArrayList<PluginBase> fragmentList = new ArrayList<PluginBase>();
ArrayList<PluginBase> visibleFragmentList = new ArrayList<PluginBase>();
ArrayList<PluginBase> fragmentList = new ArrayList<>();
ArrayList<PluginBase> visibleFragmentList = new ArrayList<>();
FragmentManager fm;
Context context;
public TabPageAdapter(FragmentManager fm) {
public TabPageAdapter(FragmentManager fm, Context context) {
super(fm);
this.fm = fm;
this.context = context;
}
@Override
@Nullable
public Fragment getItem(int position) {
Fragment fragment = (Fragment) visibleFragmentList.get(position);
return fragment;
//Fragment fragment = (Fragment) visibleFragmentList.get(position);
return Fragment.instantiate(context, visibleFragmentList.get(position).getFragmentClass());
}
@Override
@ -42,8 +44,7 @@ public class TabPageAdapter extends FragmentStatePagerAdapter {
return visibleFragmentList.size();
}
public void registerNewFragment(Fragment fragment) {
PluginBase plugin = (PluginBase) fragment;
public void registerNewFragment(PluginBase plugin) {
fragmentList.add(plugin);
if (plugin.isVisibleInTabs(plugin.getType())) {
visibleFragmentList.add(plugin);

View file

@ -1,134 +0,0 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.nightscout.androidaps.plugins.LowSuspend.LowSuspendFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/lowsuspend_run"
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/openapsma_run" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="@color/linearBlockBackground"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_lastrun_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lowsuspend_lastrun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/linearBlockBackground"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_inputparameters_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_glucosestatus_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="5dp" />
<TextView
android:id="@+id/lowsuspend_glucosestatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_minbg_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="5dp" />
<TextView
android:id="@+id/lowsuspend_minbg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="@color/linearBlockBackground"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_result_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lowsuspend_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="@color/linearBlockBackground"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_request_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lowsuspend_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>

View file

@ -249,7 +249,6 @@
<string name="smscommunicator_remotebolusingallowed">Fernboli via SMS zulassen</string>
<string name="syncprofiletopump_title">Nightscout Profil zur Pumpe synchronisieren</string>
<string name="waitingforpumpresult">auf Pumpenergebnis warten</string>
<string name="bolusdelivered">Bolus abgegeben</string>
<string name="nobtadapter">Kein Bluetoothadapter gefunden</string>
<string name="remotebolusnotallowed">Remote Bolus nicht erlaubt</string>
<string formatted="false" name="replywithcode">Um Bolus %.2fU bitte mit %s antworten</string>

View file

@ -68,11 +68,6 @@
<string name="nochangerequested">No change requested</string>
<string name="openapsma_request_label">Request</string>
<string name="openapsma_minbg_label">Low target</string>
<string name="lowsuspend_lowmessage">LOW: Temp basal 0%</string>
<string name="lowsuspend_lowprojectedmessage">LOW PROJECTED: Temp basal 0%</string>
<string name="lowsuspend_cancelmessage">LowSuspend: Cancel low temp</string>
<string name="lowsuspend_low">Low</string>
<string name="lowsuspend_lowprojected">Low projected</string>
<string name="rate">Rate</string>
<string name="duration">Duration</string>
<string name="reason">Reason</string>
@ -82,7 +77,6 @@
<string name="minago">m ago</string>
<string name="configbuilder">Config Builder</string>
<string name="lowsuspend">Low Suspend</string>
<string name="objectives">Objectives</string>
<string name="openapsma">OpenAPS MA</string>
<string name="overview">Overview</string>
@ -115,7 +109,6 @@
<string name="cancel">Cancel</string>
<string name="noapsselected">NO APS SELECTED OR PROVIDED RESULT</string>
<string name="safety">Safety</string>
<string name="lowsuspend_low_title">Low BG threshold</string>
<string name="openapsma_disabled">Plugin is disabled</string>
<string name="constraints_violation">Constraints violation</string>
<string name="treatmentdeliveryerror">Bolus delivery error</string>

View file

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="lowsuspend"
android:title="@string/lowsuspend">
<EditTextPreference
android:title="@string/lowsuspend_low_title"
android:key="lowsuspend_lowthreshold"
android:inputType="numberDecimal">
</EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>