diff --git a/app/build.gradle b/app/build.gradle
index 2b6af5681e..d6f321e942 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -43,8 +43,8 @@ android {
applicationId "info.nightscout.androidaps"
minSdkVersion 21
targetSdkVersion 23
- versionCode 1400
- version "1.46"
+ versionCode 1500
+ version "1.51"
buildConfigField "String", "VERSION", '"' + version + '"'
buildConfigField "String", "BUILDVERSION", generateGitBuild()
}
@@ -173,6 +173,9 @@ dependencies {
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
compile(name: 'android-edittext-validator-v1.3.4-mod', ext: 'aar')
+ compile ('com.google.android:flexbox:0.3.0') {
+ exclude group: 'com.android.support'
+ }
compile('io.socket:socket.io-client:0.8.3') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
@@ -180,4 +183,4 @@ dependencies {
compile 'com.google.code.gson:gson:2.7'
compile 'com.google.guava:guava:20.0'
-}
\ No newline at end of file
+}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index ab45d3d4f5..7d792556b7 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -17,6 +17,7 @@
+
@@ -41,14 +42,11 @@
android:name=".plugins.Overview.Dialogs.BolusProgressHelperActivity"
android:theme="@style/Theme.AppCompat.Translucent" />
-
-
-
-
+
+
-
@@ -58,17 +56,6 @@
android:enabled="true"
android:exported="true">
-
-
-
-
-
-
-
-
-
-
-
@@ -79,7 +66,6 @@
-
@@ -116,11 +102,15 @@
android:name=".Services.DataService"
android:exported="false" />
+
+
+
-
-
\ No newline at end of file
diff --git a/app/src/main/assets/logback.xml b/app/src/main/assets/logback.xml
index 41ed7bdefe..c34ff08b80 100644
--- a/app/src/main/assets/logback.xml
+++ b/app/src/main/assets/logback.xml
@@ -6,7 +6,7 @@
- ${EXT_FILES_DIR}/AndroidAPS._%d{yyyy-MM-dd}.%i.zip
+ ${EXT_FILES_DIR}/AndroidAPS._%d{yyyy-MM-dd}_%d{HH-mm-ss, aux}_.%i.zip
5MB
@@ -15,7 +15,7 @@
120
- %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
+ %d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line]: %msg%n
@@ -24,7 +24,7 @@
%logger{0}
- [%thread] %-5level %logger{36} - %msg%n
+ [%thread] %-5level [%file:%line]: %msg%n
diff --git a/app/src/main/java/info/nightscout/androidaps/Config.java b/app/src/main/java/info/nightscout/androidaps/Config.java
index 7f094281f8..e6c06583c9 100644
--- a/app/src/main/java/info/nightscout/androidaps/Config.java
+++ b/app/src/main/java/info/nightscout/androidaps/Config.java
@@ -4,6 +4,8 @@ package info.nightscout.androidaps;
* Created by mike on 07.06.2016.
*/
public class Config {
+ public static int SUPPORTEDNSVERSION = 1000; // 0.10.00
+
// MAIN FUCTIONALITY
public static final boolean APS = BuildConfig.APS;
// PLUGINS
@@ -11,15 +13,15 @@ public class Config {
public static final boolean LOOPENABLED = APS;
public static final boolean WEAR = BuildConfig.WEAR;
+ public static final boolean NSCLIENT = BuildConfig.NSCLIENTOLNY;
public static final boolean DANAR = true && BuildConfig.PUMPDRIVERS;
- public static final boolean DANARKOREAN = true && BuildConfig.PUMPDRIVERS;
+ public static final boolean DANARv2 = true && BuildConfig.PUMPDRIVERS;
public static final boolean ACTION = !BuildConfig.NSCLIENTOLNY;
public static final boolean VIRTUALPUMP = !BuildConfig.NSCLIENTOLNY;
public static final boolean MDI = !BuildConfig.NSCLIENTOLNY;
public static final boolean OTHERPROFILES = !BuildConfig.NSCLIENTOLNY;
- public static final boolean TEMPBASALS = !BuildConfig.NSCLIENTOLNY;
public static final boolean SAFETY = !BuildConfig.NSCLIENTOLNY;
public static final boolean SMSCOMMUNICATORENABLED = !BuildConfig.NSCLIENTOLNY;
@@ -29,18 +31,16 @@ public class Config {
public static final boolean detailedLog = true;
public static final boolean logFunctionCalls = true;
- public static final boolean logIncommingBG = true;
public static final boolean logIncommingData = true;
public static final boolean logAPSResult = true;
public static final boolean logPumpComm = true;
public static final boolean logPrefsChange = true;
public static final boolean logConfigBuilder = true;
public static final boolean logConstraintsChanges = true;
- public static final boolean logTempBasalsCut = true;
public static final boolean logNSUpload = true;
public static final boolean logPumpActions = true;
- public static final boolean logSMSComm = true;
public static final boolean logCongigBuilderActions = true;
+ public static final boolean logAutosensData = true;
// DanaR specific
public static final boolean logDanaBTComm = true;
diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java
index 51f27d84a2..e361e4cc8c 100644
--- a/app/src/main/java/info/nightscout/androidaps/Constants.java
+++ b/app/src/main/java/info/nightscout/androidaps/Constants.java
@@ -33,14 +33,6 @@ public class Constants {
public static final int CPP_MIN_PERCENTAGE = 50;
public static final int CPP_MAX_PERCENTAGE = 200;
- // Defaults for settings
- public static final Double MAX_BG_DEFAULT_MGDL = 180d;
- public static final Double MAX_BG_DEFAULT_MMOL = 10d;
- public static final Double MIN_BG_DEFAULT_MGDL = 100d;
- public static final Double MIN_BG_DEFAULT_MMOL = 5d;
- public static final Double TARGET_BG_DEFAULT_MGDL = 150d;
- public static final Double TARGET_BG_DEFAULT_MMOL = 7d;
-
// Very Hard Limits Ranges
// First value is the Lowest and second value is the Highest a Limit can define
public static final int[] VERY_HARD_LIMIT_MIN_BG = {72,180};
diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java
index 9ae5081dbc..528cbd21bd 100644
--- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java
+++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java
@@ -19,11 +19,11 @@ import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
-import android.util.AttributeSet;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
+import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
@@ -35,10 +35,14 @@ import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import info.nightscout.androidaps.Services.AlarmSoundService;
+import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
+import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
+import info.nightscout.androidaps.plugins.Overview.events.EventSetWakeLock;
import info.nightscout.androidaps.tabs.SlidingTabLayout;
import info.nightscout.androidaps.tabs.TabPageAdapter;
import info.nightscout.utils.ImportExportPrefs;
@@ -59,6 +63,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
ImageButton menuButton;
+ protected PowerManager.WakeLock mWakeLock;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -75,13 +80,28 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
Manifest.permission.WRITE_EXTERNAL_STORAGE}, CASE_STORAGE);
}
askForBatteryOptimizationPermission();
+ checkUpgradeToProfileTarget();
if (Config.logFunctionCalls)
log.debug("onCreate");
+ onStatusEvent(new EventSetWakeLock(SP.getBoolean("lockscreen", false)));
+
registerBus();
setUpTabs(false);
}
+ @Subscribe
+ public void onStatusEvent(final EventSetWakeLock ev) {
+ final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+ if (ev.lock) {
+ mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "AAPS");
+ mWakeLock.acquire();
+ } else {
+ if (mWakeLock != null)
+ mWakeLock.release();
+ }
+ }
+
@Subscribe
public void onStatusEvent(final EventRefreshGui ev) {
String lang = SP.getString("language", "en");
@@ -91,10 +111,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
public void run() {
recreate();
try { // activity may be destroyed
- setUpTabs(ev.isSwitchToLast());
+ setUpTabs(true);
} catch (IllegalStateException e) {
e.printStackTrace();
}
+ boolean lockScreen = BuildConfig.NSCLIENTOLNY && SP.getBoolean("lockscreen", false);
+ if (lockScreen)
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+ else
+ getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
@@ -131,6 +156,29 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
}
}
+ private void checkUpgradeToProfileTarget() { // TODO: can be removed in the future
+ boolean oldKeyExists = SP.contains("openapsma_min_bg");
+ if (oldKeyExists) {
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ String oldRange = SP.getDouble("openapsma_min_bg", 0d) + " - " + SP.getDouble("openapsma_max_bg", 0d);
+ String newRange = "";
+ if (profile != null) {
+ newRange = profile.getTargetLow() + " - " + profile.getTargetHigh();
+ }
+ String message = "Target range is changed in current version.\n\nIt's not taken from preferences but from profile.\n\n!!! REVIEW YOUR SETTINGS !!!";
+ message += "\n\nOld settings: " + oldRange;
+ message += "\nProfile settings: " + newRange;
+ OKDialog.show(this, "Target range change", message, new Runnable() {
+ @Override
+ public void run() {
+ SP.remove("openapsma_min_bg");
+ SP.remove("openapsma_max_bg");
+ SP.remove("openapsma_target_bg");
+ }
+ });
+ }
+ }
+
//check for sms permission if enable in prefernces
@Subscribe
public void onStatusEvent(final EventPreferenceChange ev) {
@@ -155,6 +203,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
askForSMSPermissions();
}
+ @Override
+ public void onDestroy() {
+ if (mWakeLock != null)
+ mWakeLock.release();
+ super.onDestroy();
+ }
+
private void askForBatteryOptimizationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final String packageName = getPackageName();
@@ -296,11 +351,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
case R.id.nav_about:
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle(getString(R.string.app_name) + " " + BuildConfig.VERSION);
- if (BuildConfig.NSCLIENTOLNY)
+ if (Config.NSCLIENT)
builder.setIcon(R.mipmap.yellowowl);
else
builder.setIcon(R.mipmap.blueowl);
- builder.setMessage("Build: " + BuildConfig.BUILDVERSION);
+ String message = "Build: " + BuildConfig.BUILDVERSION + "\n";
+ message += MainApp.sResources.getString(R.string.configbuilder_nightscoutversion_label) + " " + ConfigBuilderPlugin.nightscoutVersionName;
+ builder.setMessage(message);
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java
index fab46087be..ac9aa93c7b 100644
--- a/app/src/main/java/info/nightscout/androidaps/MainApp.java
+++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java
@@ -2,8 +2,10 @@ package info.nightscout.androidaps;
import android.app.Application;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.res.Resources;
import android.support.annotation.Nullable;
+import android.support.v4.content.LocalBroadcastManager;
import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.answers.Answers;
@@ -17,44 +19,57 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
+import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.db.DatabaseHelper;
+import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.Actions.ActionsFragment;
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
-import info.nightscout.androidaps.plugins.InsulinFastactingProlonged.InsulinFastactingProlongedFragment;
-import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorFragment;
-import info.nightscout.androidaps.plugins.ProfileCircadianPercentage.CircadianPercentageProfileFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
-import info.nightscout.androidaps.plugins.PumpDanaR.DanaRFragment;
-import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanFragment;
-import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment;
-import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfileFragment;
-import info.nightscout.androidaps.plugins.Loop.LoopFragment;
-import info.nightscout.androidaps.plugins.PumpMDI.MDIFragment;
-import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment;
-import info.nightscout.androidaps.plugins.ProfileNS.NSProfileFragment;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesFragment;
+import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin;
+import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment;
+import info.nightscout.androidaps.plugins.InsulinFastactingProlonged.InsulinFastactingProlongedFragment;
+import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefFreePeakFragment;
+import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefRapidActingFragment;
+import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefUltraRapidActingFragment;
+import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
+import info.nightscout.androidaps.plugins.Loop.LoopFragment;
+import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment;
+import info.nightscout.androidaps.plugins.NSClientInternal.receivers.AckAlarmReceiver;
import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAFragment;
import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAFragment;
import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
-import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyFragment;
-import info.nightscout.androidaps.plugins.ProfileSimple.SimpleProfileFragment;
-import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment;
-import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpFragment;
-import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gFragment;
-import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment;
-import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment;
-import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment;
-import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangeFragment;
-import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
-import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpFragment;
-import info.nightscout.androidaps.plugins.Wear.WearFragment;
import info.nightscout.androidaps.plugins.Persistentnotification.PersistentNotificationPlugin;
-import info.nightscout.androidaps.plugins.XDripStatusline.StatuslineFragment;
+import info.nightscout.androidaps.plugins.ProfileCircadianPercentage.CircadianPercentageProfileFragment;
+import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfileFragment;
+import info.nightscout.androidaps.plugins.ProfileNS.NSProfileFragment;
+import info.nightscout.androidaps.plugins.ProfileSimple.SimpleProfileFragment;
+import info.nightscout.androidaps.plugins.PumpDanaR.DanaRFragment;
+import info.nightscout.androidaps.plugins.PumpDanaR.services.DanaRExecutionService;
+import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanFragment;
+import info.nightscout.androidaps.plugins.PumpDanaRKorean.services.DanaRKoreanExecutionService;
+import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Fragment;
+import info.nightscout.androidaps.plugins.PumpDanaRv2.services.DanaRv2ExecutionService;
+import info.nightscout.androidaps.plugins.PumpMDI.MDIPlugin;
+import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
+import info.nightscout.androidaps.plugins.SensitivityAAPS.SensitivityAAPSPlugin;
+import info.nightscout.androidaps.plugins.SensitivityOref0.SensitivityOref0Plugin;
+import info.nightscout.androidaps.plugins.SensitivityWeightedAverage.SensitivityWeightedAveragePlugin;
+import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment;
+import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpPlugin;
+import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gPlugin;
+import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin;
+import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin;
+import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
+import info.nightscout.androidaps.plugins.Wear.WearFragment;
import info.nightscout.androidaps.plugins.XDripStatusline.StatuslinePlugin;
+import info.nightscout.androidaps.receivers.DataReceiver;
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
+import info.nightscout.androidaps.receivers.NSAlarmReceiver;
+import info.nightscout.utils.NSUpload;
import io.fabric.sdk.android.Fabric;
@@ -71,6 +86,11 @@ public class MainApp extends Application {
private static ArrayList pluginsList = null;
+ private static DataReceiver dataReceiver = new DataReceiver();
+ private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
+ private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver();
+ private LocalBroadcastManager lbm;
+
@Override
public void onCreate() {
super.onCreate();
@@ -80,25 +100,32 @@ public class MainApp extends Application {
log.info("Version: " + BuildConfig.VERSION_NAME);
log.info("BuildVersion: " + BuildConfig.BUILDVERSION);
- Answers.getInstance().logCustom(new CustomEvent("AppStart"));
-
sBus = new Bus(ThreadEnforcer.ANY);
sInstance = this;
sResources = getResources();
+ registerLocalBroadcastReceiver();
+
if (pluginsList == null) {
pluginsList = new ArrayList<>();
// Register all tabs in app here
pluginsList.add(OverviewFragment.getPlugin());
- pluginsList.add(IobCobCalculatorFragment.getPlugin());
+ pluginsList.add(IobCobCalculatorPlugin.getPlugin());
if (Config.ACTION) pluginsList.add(ActionsFragment.getPlugin());
pluginsList.add(InsulinFastactingFragment.getPlugin());
pluginsList.add(InsulinFastactingProlongedFragment.getPlugin());
+ pluginsList.add(InsulinOrefRapidActingFragment.getPlugin());
+ pluginsList.add(InsulinOrefUltraRapidActingFragment.getPlugin());
+ pluginsList.add(InsulinOrefFreePeakFragment.getPlugin());
+ pluginsList.add(SensitivityOref0Plugin.getPlugin());
+ //pluginsList.add(SensitivityAAPSPlugin.getPlugin());
+ //pluginsList.add(SensitivityWeightedAveragePlugin.getPlugin());
if (Config.DANAR) pluginsList.add(DanaRFragment.getPlugin());
- if (Config.DANARKOREAN) pluginsList.add(DanaRKoreanFragment.getPlugin());
+ if (Config.DANAR) pluginsList.add(DanaRKoreanFragment.getPlugin());
+ if (Config.DANARv2) pluginsList.add(DanaRv2Fragment.getPlugin());
pluginsList.add(CareportalFragment.getPlugin());
- if (Config.MDI) pluginsList.add(MDIFragment.getPlugin());
- if (Config.VIRTUALPUMP) pluginsList.add(VirtualPumpFragment.getPlugin());
+ if (Config.MDI) pluginsList.add(MDIPlugin.getPlugin());
+ if (Config.VIRTUALPUMP) pluginsList.add(VirtualPumpPlugin.getInstance());
if (Config.LOOPENABLED) pluginsList.add(LoopFragment.getPlugin());
if (Config.OPENAPSENABLED) pluginsList.add(OpenAPSMAFragment.getPlugin());
if (Config.OPENAPSENABLED) pluginsList.add(OpenAPSAMAFragment.getPlugin());
@@ -107,19 +134,20 @@ public class MainApp extends Application {
if (Config.OTHERPROFILES) pluginsList.add(LocalProfileFragment.getPlugin());
if (Config.OTHERPROFILES)
pluginsList.add(CircadianPercentageProfileFragment.getPlugin());
- if (Config.APS) pluginsList.add(TempTargetRangeFragment.getPlugin());
pluginsList.add(TreatmentsFragment.getPlugin());
- if (Config.TEMPBASALS) pluginsList.add(TempBasalsFragment.getPlugin());
- if (Config.SAFETY) pluginsList.add(SafetyFragment.getPlugin());
+ if (Config.SAFETY) pluginsList.add(SafetyPlugin.getPlugin());
if (Config.APS) pluginsList.add(ObjectivesFragment.getPlugin());
- pluginsList.add(SourceXdripFragment.getPlugin());
- pluginsList.add(SourceNSClientFragment.getPlugin());
- pluginsList.add(SourceMM640gFragment.getPlugin());
- pluginsList.add(SourceGlimpFragment.getPlugin());
+ if (!Config.NSCLIENT)
+ pluginsList.add(SourceXdripPlugin.getPlugin());
+ pluginsList.add(SourceNSClientPlugin.getPlugin());
+ if (!Config.NSCLIENT)
+ pluginsList.add(SourceMM640gPlugin.getPlugin());
+ if (!Config.NSCLIENT)
+ pluginsList.add(SourceGlimpPlugin.getPlugin());
if (Config.SMSCOMMUNICATORENABLED) pluginsList.add(SmsCommunicatorFragment.getPlugin());
if (Config.WEAR) pluginsList.add(WearFragment.getPlugin(this));
- pluginsList.add(StatuslineFragment.getPlugin(this));
+ pluginsList.add(StatuslinePlugin.getPlugin(this));
pluginsList.add(new PersistentNotificationPlugin(this));
pluginsList.add(NSClientInternalFragment.getPlugin());
@@ -127,7 +155,12 @@ public class MainApp extends Application {
MainApp.getConfigBuilder().initialize();
}
- MainApp.getConfigBuilder().uploadAppStart();
+ NSUpload.uploadAppStart();
+ if (MainApp.getConfigBuilder().isClosedModeEnabled())
+ Answers.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop"));
+ else
+ Answers.getInstance().logCustom(new CustomEvent("AppStart"));
+
startKeepAliveService();
@@ -144,14 +177,38 @@ public class MainApp extends Application {
}
});
t.start();
+
+ }
+
+ private void registerLocalBroadcastReceiver() {
+ lbm = LocalBroadcastManager.getInstance(this);
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_TREATMENT));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_CHANGED_TREATMENT));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_REMOVED_TREATMENT));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_SGV));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_PROFILE));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_STATUS));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_MBG));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_DEVICESTATUS));
+ lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_CAL));
+
+ //register alarms
+ lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_ALARM));
+ lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_ANNOUNCEMENT));
+ lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_CLEAR_ALARM));
+ lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_URGENT_ALARM));
+
+ //register ack alarm
+ lbm.registerReceiver(ackAlarmReciever, new IntentFilter(Intents.ACTION_ACK_ALARM));
}
private void startKeepAliveService() {
if (keepAliveReceiver == null) {
keepAliveReceiver = new KeepAliveReceiver();
if (Config.DANAR) {
- startService(new Intent(this, info.nightscout.androidaps.plugins.PumpDanaR.Services.ExecutionService.class));
- startService(new Intent(this, info.nightscout.androidaps.plugins.PumpDanaRKorean.Services.ExecutionService.class));
+ startService(new Intent(this, DanaRExecutionService.class));
+ startService(new Intent(this, DanaRKoreanExecutionService.class));
+ startService(new Intent(this, DanaRv2ExecutionService.class));
}
keepAliveReceiver.setAlarm(this);
}
@@ -207,6 +264,19 @@ public class MainApp extends Application {
return newList;
}
+ @Nullable
+ public static InsulinInterface getInsulinIterfaceById(int id) {
+ if (pluginsList != null) {
+ for (PluginBase p : pluginsList) {
+ if (p.getType() == PluginBase.INSULIN && ((InsulinInterface) p).getId() == id)
+ return (InsulinInterface) p;
+ }
+ } else {
+ log.error("InsulinInterface not found");
+ }
+ return null;
+ }
+
public static ArrayList getSpecificPluginsVisibleInList(int type) {
ArrayList newList = new ArrayList<>();
@@ -252,11 +322,11 @@ public class MainApp extends Application {
}
@Nullable
- public static PluginBase getSpecificPlugin(Class pluginClass) {
+ public static T getSpecificPlugin(Class pluginClass) {
if (pluginsList != null) {
for (PluginBase p : pluginsList) {
- if (p.getClass() == pluginClass)
- return p;
+ if (pluginClass.isAssignableFrom(p.getClass()))
+ return (T) p;
}
} else {
log.error("pluginsList=null");
diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java
index ed7f780547..5c3372123e 100644
--- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java
+++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java
@@ -14,15 +14,22 @@ import android.preference.PreferenceManager;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
+import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefFreePeakPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.BluetoothDevicePreference;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin;
import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin;
+import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin;
import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
+import info.nightscout.androidaps.plugins.SensitivityAAPS.SensitivityAAPSPlugin;
+import info.nightscout.androidaps.plugins.SensitivityOref0.SensitivityOref0Plugin;
+import info.nightscout.androidaps.plugins.SensitivityWeightedAverage.SensitivityWeightedAveragePlugin;
import info.nightscout.androidaps.plugins.Wear.WearPlugin;
import info.nightscout.androidaps.plugins.XDripStatusline.StatuslinePlugin;
import info.nightscout.utils.LocaleHelper;
+import info.nightscout.utils.OKDialog;
+import info.nightscout.utils.SP;
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
MyPreferenceFragment myPreferenceFragment;
@@ -42,10 +49,13 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
String lang = sharedPreferences.getString("language", "en");
LocaleHelper.setLocale(getApplicationContext(), lang);
recreate();
- MainApp.bus().post(new EventRefreshGui(true));
+ MainApp.bus().post(new EventRefreshGui());
}
if (key.equals("short_tabtitles")) {
- MainApp.bus().post(new EventRefreshGui(true));
+ MainApp.bus().post(new EventRefreshGui());
+ }
+ if (key.equals("openapsama_useautosens") && SP.getBoolean("openapsama_useautosens", false)) {
+ OKDialog.show(this, MainApp.sResources.getString(R.string.configbuilder_sensitivity), MainApp.sResources.getString(R.string.sensitivity_warning), null);
}
updatePrefSummary(myPreferenceFragment.getPreference(key));
}
@@ -87,8 +97,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
super.onCreate(savedInstanceState);
if (Config.ALLPREFERENCES) {
addPreferencesFromResource(R.xml.pref_password);
- addPreferencesFromResource(R.xml.pref_age);
}
+ addPreferencesFromResource(R.xml.pref_age);
addPreferencesFromResource(R.xml.pref_language);
if (Config.ALLPREFERENCES) {
addPreferencesFromResource(R.xml.pref_quickwizard);
@@ -104,24 +114,38 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
if (MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class) != null && MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class).isEnabled(PluginBase.APS))
addPreferencesFromResource(R.xml.pref_openapsama);
}
+ if (MainApp.getSpecificPlugin(SensitivityAAPSPlugin.class) != null && MainApp.getSpecificPlugin(SensitivityAAPSPlugin.class).isEnabled(PluginBase.SENSITIVITY)
+ || MainApp.getSpecificPlugin(SensitivityWeightedAveragePlugin.class) != null && MainApp.getSpecificPlugin(SensitivityWeightedAveragePlugin.class).isEnabled(PluginBase.SENSITIVITY))
+ addPreferencesFromResource(R.xml.pref_absorption_aaps);
+ if (MainApp.getSpecificPlugin(SensitivityOref0Plugin.class) != null && MainApp.getSpecificPlugin(SensitivityOref0Plugin.class).isEnabled(PluginBase.SENSITIVITY))
+ addPreferencesFromResource(R.xml.pref_absorption_oref0);
if (Config.ALLPREFERENCES) {
addPreferencesFromResource(R.xml.pref_profile);
}
if (Config.DANAR) {
- DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
- DanaRKoreanPlugin danaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class);
+ DanaRPlugin danaRPlugin = MainApp.getSpecificPlugin(DanaRPlugin.class);
+ DanaRKoreanPlugin danaRKoreanPlugin = MainApp.getSpecificPlugin(DanaRKoreanPlugin.class);
+ DanaRv2Plugin danaRv2Plugin = MainApp.getSpecificPlugin(DanaRv2Plugin.class);
if (danaRPlugin.isEnabled(PluginBase.PUMP) || danaRKoreanPlugin.isEnabled(PluginBase.PUMP)) {
addPreferencesFromResource(R.xml.pref_danar);
}
- if (danaRPlugin.isEnabled(PluginBase.PROFILE) || danaRKoreanPlugin.isEnabled(PluginBase.PROFILE)) {
+ if (danaRv2Plugin != null && danaRv2Plugin.isEnabled(PluginBase.PUMP)) {
+ addPreferencesFromResource(R.xml.pref_danarv2);
+ }
+ if (danaRPlugin.isEnabled(PluginBase.PROFILE) || danaRKoreanPlugin.isEnabled(PluginBase.PROFILE) || danaRv2Plugin != null && danaRv2Plugin.isEnabled(PluginBase.PROFILE)) {
addPreferencesFromResource(R.xml.pref_danarprofile);
}
}
- VirtualPumpPlugin virtualPumpPlugin = (VirtualPumpPlugin) MainApp.getSpecificPlugin(VirtualPumpPlugin.class);
+ VirtualPumpPlugin virtualPumpPlugin = MainApp.getSpecificPlugin(VirtualPumpPlugin.class);
if (virtualPumpPlugin != null && virtualPumpPlugin.isEnabled(PluginBase.PUMP)) {
addPreferencesFromResource(R.xml.pref_virtualpump);
}
- NSClientInternalPlugin nsClientInternalPlugin = (NSClientInternalPlugin) MainApp.getSpecificPlugin(NSClientInternalPlugin.class);
+ InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin = MainApp.getSpecificPlugin(InsulinOrefFreePeakPlugin.class);
+ if(insulinOrefFreePeakPlugin.isEnabled(PluginBase.INSULIN)){
+ addPreferencesFromResource(R.xml.pref_insulinoreffreepeak);
+ }
+
+ NSClientInternalPlugin nsClientInternalPlugin = MainApp.getSpecificPlugin(NSClientInternalPlugin.class);
if (nsClientInternalPlugin != null && nsClientInternalPlugin.isEnabled(PluginBase.GENERAL)) {
addPreferencesFromResource(R.xml.pref_nsclientinternal);
}
@@ -129,17 +153,17 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
addPreferencesFromResource(R.xml.pref_smscommunicator);
if (Config.ALLPREFERENCES) {
addPreferencesFromResource(R.xml.pref_others);
- addPreferencesFromResource(R.xml.pref_advanced);
}
+ addPreferencesFromResource(R.xml.pref_advanced);
if (Config.WEAR) {
- WearPlugin wearPlugin = (WearPlugin) MainApp.getSpecificPlugin(WearPlugin.class);
+ WearPlugin wearPlugin = MainApp.getSpecificPlugin(WearPlugin.class);
if (wearPlugin != null && wearPlugin.isEnabled(PluginBase.GENERAL)) {
addPreferencesFromResource(R.xml.pref_wear);
}
}
- StatuslinePlugin statuslinePlugin = (StatuslinePlugin) MainApp.getSpecificPlugin(StatuslinePlugin.class);
+ StatuslinePlugin statuslinePlugin = MainApp.getSpecificPlugin(StatuslinePlugin.class);
if (statuslinePlugin != null && statuslinePlugin.isEnabled(PluginBase.GENERAL)) {
addPreferencesFromResource(R.xml.pref_xdripstatus);
}
diff --git a/app/src/main/java/info/nightscout/androidaps/Services/AlarmSoundService.java b/app/src/main/java/info/nightscout/androidaps/Services/AlarmSoundService.java
new file mode 100644
index 0000000000..41587e79b9
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/Services/AlarmSoundService.java
@@ -0,0 +1,73 @@
+package info.nightscout.androidaps.Services;
+
+import android.app.Service;
+import android.content.Intent;
+import android.content.res.AssetFileDescriptor;
+import android.media.MediaPlayer;
+import android.os.IBinder;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+
+public class AlarmSoundService extends Service {
+ private static Logger log = LoggerFactory.getLogger(AlarmSoundService.class);
+
+ MediaPlayer player;
+ int resourceId = R.raw.error;
+
+ public AlarmSoundService() {
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ // TODO: Return the communication channel to the service.
+ throw new UnsupportedOperationException("Not yet implemented");
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ log.debug("onCreate");
+ }
+
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ if (player != null && player.isPlaying())
+ player.stop();
+ log.debug("onStartCommand");
+ if (intent != null && intent.hasExtra("soundid"))
+ resourceId = intent.getIntExtra("soundid", R.raw.error);
+
+ player = new MediaPlayer();
+ AssetFileDescriptor afd = MainApp.sResources.openRawResourceFd(resourceId);
+ if (afd == null)
+ return START_STICKY;
+ try {
+ player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
+ afd.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ player.setLooping(true); // Set looping
+ player.setVolume(100, 100);
+
+ try {
+ player.prepare();
+ player.start();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return START_STICKY;
+ }
+
+ @Override
+ public void onDestroy() {
+ player.stop();
+ player.release();
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java
index e60a89e6a0..39eb023cb3 100644
--- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java
+++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java
@@ -2,58 +2,42 @@ package info.nightscout.androidaps.Services;
import android.app.IntentService;
import android.content.Intent;
-import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
import android.os.Bundle;
-import android.preference.PreferenceManager;
import android.provider.Telephony;
-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 org.json.JSONArray;
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 java.util.List;
-
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.BgReading;
-import info.nightscout.androidaps.db.DanaRHistoryRecord;
-import info.nightscout.androidaps.db.TempTarget;
-import info.nightscout.androidaps.db.Treatment;
-import info.nightscout.androidaps.events.EventNewBG;
+import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.events.EventNewBasalProfile;
-import info.nightscout.androidaps.interfaces.InsulinInterface;
-import info.nightscout.androidaps.interfaces.PluginBase;
-import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
-import info.nightscout.androidaps.plugins.PumpDanaR.History.DanaRNSHistorySync;
-import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment;
-import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
+import info.nightscout.androidaps.plugins.NSClientInternal.data.NSMbg;
+import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
+import info.nightscout.androidaps.data.ProfileStore;
+import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
-import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorPlugin;
+import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
+import info.nightscout.androidaps.plugins.PumpDanaR.activities.DanaRNSHistorySync;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS;
import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpPlugin;
import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gPlugin;
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin;
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin;
-import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange;
import info.nightscout.androidaps.receivers.DataReceiver;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
+import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus;
+import info.nightscout.utils.BundleLogger;
import info.nightscout.utils.SP;
@@ -73,7 +57,7 @@ public class DataService extends IntentService {
@Override
protected void onHandleIntent(final Intent intent) {
if (Config.logFunctionCalls)
- log.debug("onHandleIntent " + intent);
+ log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras()));
if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceXdripPlugin.class)) {
xDripEnabled = true;
@@ -97,7 +81,7 @@ public class DataService extends IntentService {
glimpEnabled = true;
}
- boolean isNSProfile = ConfigBuilderPlugin.getActiveProfile().getClass().equals(NSProfilePlugin.class);
+ boolean isNSProfile = ConfigBuilderPlugin.getActiveProfileInterface().getClass().equals(NSProfilePlugin.class);
boolean nsUploadOnly = SP.getBoolean(R.string.key_ns_upload_only, false);
@@ -180,25 +164,10 @@ public class DataService extends IntentService {
bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE);
bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME);
- bgReading.battery_level = bundle.getInt(Intents.EXTRA_SENSOR_BATTERY);
- bgReading.timeIndex = bundle.getLong(Intents.EXTRA_TIMESTAMP);
+ bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP);
bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW);
- if (bgReading.timeIndex < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) {
- if (Config.logIncommingBG)
- log.debug("Ignoring old XDRIPREC BG " + bgReading.toString());
- return;
- }
-
- if (Config.logIncommingBG)
- log.debug("XDRIPREC BG " + bgReading.toString());
-
- try {
- MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- MainApp.bus().post(new EventNewBG());
+ MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP");
}
private void handleNewDataFromGlimp(Intent intent) {
@@ -209,20 +178,10 @@ public class DataService extends IntentService {
bgReading.value = bundle.getDouble("mySGV");
bgReading.direction = bundle.getString("myTrend");
- bgReading.battery_level = bundle.getInt("myBatLvl");
- bgReading.timeIndex = bundle.getLong("myTimestamp");
+ bgReading.date = bundle.getLong("myTimestamp");
bgReading.raw = 0;
- if (Config.logIncommingBG)
- log.debug(bundle.toString());
- log.debug("GLIMP BG " + bgReading.toString());
-
- try {
- MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- MainApp.bus().post(new EventNewBG());
+ MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP");
}
private void handleNewDataFromMM640g(Intent intent) {
@@ -247,23 +206,10 @@ public class DataService extends IntentService {
bgReading.value = json_object.getDouble("sgv");
bgReading.direction = json_object.getString("direction");
- bgReading.timeIndex = json_object.getLong("date");
+ bgReading.date = json_object.getLong("date");
bgReading.raw = json_object.getDouble("sgv");
- if (bgReading.timeIndex < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) {
- if (Config.logIncommingBG)
- log.debug("Ignoring old MM640g BG " + bgReading.toString());
- return;
- }
-
- if (Config.logIncommingBG)
- log.debug("MM640g BG " + bgReading.toString());
-
- try {
- MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading);
- } catch (SQLException e) {
- e.printStackTrace();
- }
+ MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g");
break;
default:
log.debug("Unknown entries type: " + type);
@@ -274,7 +220,6 @@ public class DataService extends IntentService {
}
}
}
- MainApp.bus().post(new EventNewBG());
}
private void handleNewDataFromNSClient(Intent intent) {
@@ -285,19 +230,27 @@ public class DataService extends IntentService {
if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) {
- if (Config.logIncommingData)
- log.debug("Received status: " + bundles);
if (bundles.containsKey("nsclientversioncode")) {
ConfigBuilderPlugin.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203
ConfigBuilderPlugin.nightscoutVersionName = bundles.getString("nightscoutversionname");
ConfigBuilderPlugin.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117
ConfigBuilderPlugin.nsClientVersionName = bundles.getString("nsclientversionname");
log.debug("Got versions: NSClient: " + ConfigBuilderPlugin.nsClientVersionName + " Nightscout: " + ConfigBuilderPlugin.nightscoutVersionName);
- if (ConfigBuilderPlugin.nsClientVersionCode < 121) {
- Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.sResources.getString(R.string.unsupportedclientver), Notification.URGENT);
+ try {
+ if (ConfigBuilderPlugin.nsClientVersionCode < MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode) {
+ Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.sResources.getString(R.string.unsupportedclientver), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(notification));
+ } else {
+ MainApp.bus().post(new EventDismissNotification(Notification.OLD_NSCLIENT));
+ }
+ } catch (PackageManager.NameNotFoundException e) {
+ e.printStackTrace();
+ }
+ if (ConfigBuilderPlugin.nightscoutVersionCode < Config.SUPPORTEDNSVERSION) {
+ Notification notification = new Notification(Notification.OLD_NS, MainApp.sResources.getString(R.string.unsupportednsversion), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
} else {
- MainApp.bus().post(new EventDismissNotification(Notification.OLD_NSCLIENT));
+ MainApp.bus().post(new EventDismissNotification(Notification.OLD_NS));
}
} else {
Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.sResources.getString(R.string.unsupportedclientver), Notification.URGENT);
@@ -306,18 +259,15 @@ public class DataService extends IntentService {
if (bundles.containsKey("status")) {
try {
JSONObject statusJson = new JSONObject(bundles.getString("status"));
- if (statusJson.has("settings")) {
- JSONObject settings = statusJson.getJSONObject("settings");
- if (settings.has("thresholds")) {
- JSONObject thresholds = settings.getJSONObject("thresholds");
- if (thresholds.has("bgTargetTop")) {
- OverviewPlugin.bgTargetHigh = thresholds.getDouble("bgTargetTop");
- }
- if (thresholds.has("bgTargetBottom")) {
- OverviewPlugin.bgTargetLow = thresholds.getDouble("bgTargetBottom");
- }
- }
- }
+ NSSettingsStatus.getInstance().setData(statusJson);
+ if (Config.logIncommingData)
+ log.debug("Received status: " + statusJson.toString());
+ Double targetHigh = NSSettingsStatus.getInstance().getThreshold("bgTargetTop");
+ Double targetlow = NSSettingsStatus.getInstance().getThreshold("bgTargetBottom");
+ if (targetHigh != null)
+ OverviewPlugin.bgTargetHigh = targetHigh;
+ if (targetlow != null)
+ OverviewPlugin.bgTargetLow = targetlow;
} catch (JSONException e) {
e.printStackTrace();
}
@@ -326,8 +276,8 @@ public class DataService extends IntentService {
if (intent.getAction().equals(Intents.ACTION_NEW_DEVICESTATUS)) {
try {
if (bundles.containsKey("devicestatus")) {
- String devicestatusesstring = bundles.getString("devicestatus");
JSONObject devicestatusJson = new JSONObject(bundles.getString("devicestatus"));
+ NSDeviceStatus.getInstance().setData(devicestatusJson);
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesPlugin.pumpStatusIsAvailableInNS = true;
@@ -337,8 +287,9 @@ public class DataService extends IntentService {
if (bundles.containsKey("devicestatuses")) {
String devicestatusesstring = bundles.getString("devicestatuses");
JSONArray jsonArray = new JSONArray(devicestatusesstring);
- if (jsonArray.length() > 0) {
- JSONObject devicestatusJson = jsonArray.getJSONObject(0);
+ for (int i = 0; i < jsonArray.length(); i++) {
+ JSONObject devicestatusJson = jsonArray.getJSONObject(i);
+ NSDeviceStatus.getInstance().setData(devicestatusJson);
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesPlugin.pumpStatusIsAvailableInNS = true;
@@ -355,25 +306,12 @@ public class DataService extends IntentService {
try {
String activeProfile = bundles.getString("activeprofile");
String profile = bundles.getString("profile");
- NSProfile nsProfile = new NSProfile(new JSONObject(profile), activeProfile);
- MainApp.bus().post(new EventNewBasalProfile(nsProfile, "NSClient"));
+ ProfileStore profileStore = new ProfileStore(new JSONObject(profile));
+ NSProfilePlugin.storeNewProfile(profileStore);
+ MainApp.bus().post(new EventNewBasalProfile());
- PumpInterface pump = MainApp.getConfigBuilder();
- if (pump != null) {
- SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
- if (SP.getBoolean("syncprofiletopump", false)) {
- if (pump.setNewBasalProfile(nsProfile) == PumpInterface.SUCCESS) {
- SmsCommunicatorPlugin smsCommunicatorPlugin = (SmsCommunicatorPlugin) MainApp.getSpecificPlugin(SmsCommunicatorPlugin.class);
- if (smsCommunicatorPlugin != null && smsCommunicatorPlugin.isEnabled(PluginBase.GENERAL)) {
- smsCommunicatorPlugin.sendNotificationToAllNumbers(MainApp.sResources.getString(R.string.profile_set_ok));
- }
- }
- }
- } else {
- log.error("No active pump selected");
- }
if (Config.logIncommingData)
- log.debug("Received profile: " + activeProfile + " " + profile);
+ log.debug("Received profileStore: " + activeProfile + " " + profile);
} catch (JSONException e) {
e.printStackTrace();
}
@@ -382,7 +320,7 @@ public class DataService extends IntentService {
try {
if (bundles.containsKey("treatment")) {
String trstring = bundles.getString("treatment");
- handleAddedTreatment(trstring);
+ handleAddChangeDataFromNS(trstring);
}
if (bundles.containsKey("treatments")) {
String trstring = bundles.getString("treatments");
@@ -390,7 +328,7 @@ public class DataService extends IntentService {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String trstr = trJson.toString();
- handleAddedTreatment(trstr);
+ handleAddChangeDataFromNS(trstr);
}
}
} catch (Exception e) {
@@ -403,7 +341,7 @@ public class DataService extends IntentService {
try {
if (bundles.containsKey("treatment")) {
String trstring = bundles.getString("treatment");
- handleChangedTreatment(trstring);
+ handleAddChangeDataFromNS(trstring);
}
if (bundles.containsKey("treatments")) {
String trstring = bundles.getString("treatments");
@@ -411,7 +349,7 @@ public class DataService extends IntentService {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String trstr = trJson.toString();
- handleChangedTreatment(trstr);
+ handleAddChangeDataFromNS(trstr);
}
}
} catch (Exception e) {
@@ -425,8 +363,7 @@ public class DataService extends IntentService {
String trstring = bundles.getString("treatment");
JSONObject trJson = new JSONObject(trstring);
String _id = trJson.getString("_id");
- MainApp.getDbHelper().delete(_id);
- handleRemoveTempTargetRecord(trJson);
+ handleRemovedRecordFromNS(_id);
}
if (bundles.containsKey("treatments")) {
@@ -435,8 +372,7 @@ public class DataService extends IntentService {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String _id = trJson.getString("_id");
- MainApp.getDbHelper().delete(_id);
- handleRemoveTempTargetRecord(trJson);
+ handleRemovedRecordFromNS(_id);
}
}
} catch (Exception e) {
@@ -451,14 +387,7 @@ public class DataService extends IntentService {
JSONObject sgvJson = new JSONObject(sgvstring);
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
- if (bgReading.timeIndex < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) {
- if (Config.logIncommingData)
- log.debug("Ignoring old BG: " + bgReading.toString());
- return;
- }
- MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading);
- if (Config.logIncommingData)
- log.debug("ADD: Stored new BG: " + bgReading.toString());
+ MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
}
if (bundles.containsKey("sgvs")) {
@@ -468,248 +397,129 @@ public class DataService extends IntentService {
JSONObject sgvJson = jsonArray.getJSONObject(i);
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
- if (bgReading.timeIndex < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) {
- if (Config.logIncommingData)
- log.debug("Ignoring old BG: " + bgReading.toString());
- } else {
- MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading);
- if (Config.logIncommingData)
- log.debug("ADD: Stored new BG: " + bgReading.toString());
- }
+ MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
}
}
} catch (Exception e) {
e.printStackTrace();
}
- MainApp.bus().post(new EventNewBG());
}
if (intent.getAction().equals(Intents.ACTION_NEW_MBG)) {
- log.error("Not implemented yet"); // TODO implemeng MBGS
- }
- }
+ try {
+ if (bundles.containsKey("mbg")) {
+ String mbgstring = bundles.getString("mbg");
+ JSONObject mbgJson = new JSONObject(mbgstring);
+ NSMbg nsMbg = new NSMbg(mbgJson);
+ CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
+ MainApp.getDbHelper().createOrUpdate(careportalEvent);
+ if (Config.logIncommingData)
+ log.debug("Adding/Updating new MBG: " + careportalEvent.log());
+ }
- private void handleAddedTreatment(String trstring) throws JSONException, SQLException {
- JSONObject trJson = new JSONObject(trstring);
- handleDanaRHistoryRecords(trJson); // update record _id in history
- handleAddChangeTempTargetRecord(trJson);
- if (!trJson.has("insulin") && !trJson.has("carbs")) {
- if (Config.logIncommingData)
- log.debug("ADD: Uninterested treatment: " + trstring);
- return;
- }
-
- Treatment stored = null;
- String _id = trJson.getString("_id");
-
- if (trJson.has("timeIndex")) {
- if (Config.logIncommingData)
- log.debug("ADD: timeIndex found: " + trstring);
- stored = MainApp.getDbHelper().findTreatmentByTimeIndex(trJson.getLong("timeIndex"));
- } else {
- stored = MainApp.getDbHelper().findTreatmentById(_id);
- }
-
- if (stored != null) {
- if (Config.logIncommingData)
- log.debug("ADD: Existing treatment: " + trstring);
- if (trJson.has("timeIndex")) {
- stored._id = _id;
- int updated = MainApp.getDbHelper().update(stored);
- if (Config.logIncommingData)
- log.debug("Records updated: " + updated);
- }
- } else {
- if (Config.logIncommingData)
- log.debug("ADD: New treatment: " + trstring);
- InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin();
- if (insulinInterface == null) insulinInterface = InsulinFastactingFragment.getPlugin();
- Treatment treatment = new Treatment(insulinInterface);
- treatment._id = _id;
- treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
- treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
- treatment.created_at = new Date(trJson.getLong("mills"));
- if (trJson.has("eventType")) {
- treatment.mealBolus = true;
- if (trJson.get("eventType").equals("Correction Bolus"))
- treatment.mealBolus = false;
- double carbs = treatment.carbs;
- if (trJson.has("boluscalc")) {
- JSONObject boluscalc = trJson.getJSONObject("boluscalc");
- if (boluscalc.has("carbs")) {
- carbs = Math.max(boluscalc.getDouble("carbs"), carbs);
+ if (bundles.containsKey("mbgs")) {
+ String sgvstring = bundles.getString("mbgs");
+ JSONArray jsonArray = new JSONArray(sgvstring);
+ for (int i = 0; i < jsonArray.length(); i++) {
+ JSONObject mbgJson = jsonArray.getJSONObject(i);
+ NSMbg nsMbg = new NSMbg(mbgJson);
+ CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
+ MainApp.getDbHelper().createOrUpdate(careportalEvent);
+ if (Config.logIncommingData)
+ log.debug("Adding/Updating new MBG: " + careportalEvent.log());
}
}
- if (carbs <= 0)
- treatment.mealBolus = false;
+ } catch (Exception e) {
+ e.printStackTrace();
}
- treatment.setTimeIndex(treatment.getTimeIndex());
- MainApp.getDbHelper().createOrUpdate(treatment);
- if (Config.logIncommingData)
- log.debug("ADD: Stored treatment: " + treatment.log());
}
}
- private void handleChangedTreatment(String trstring) throws JSONException, SQLException {
+ private void handleRemovedRecordFromNS(String _id) {
+ MainApp.getDbHelper().deleteTreatmentById(_id);
+ MainApp.getDbHelper().deleteTempTargetById(_id);
+ MainApp.getDbHelper().deleteTempBasalById(_id);
+ MainApp.getDbHelper().deleteExtendedBolusById(_id);
+ MainApp.getDbHelper().deleteCareportalEventById(_id);
+ MainApp.getDbHelper().deleteProfileSwitchById(_id);
+ }
+
+ private void handleAddChangeDataFromNS(String trstring) throws JSONException {
JSONObject trJson = new JSONObject(trstring);
handleDanaRHistoryRecords(trJson); // update record _id in history
handleAddChangeTempTargetRecord(trJson);
- if (!trJson.has("insulin") && !trJson.has("carbs")) {
- if (Config.logIncommingData)
- log.debug("CHANGE: Uninterested treatment: " + trstring);
+ handleAddChangeTempBasalRecord(trJson);
+ handleAddChangeExtendedBolusRecord(trJson);
+ handleAddChangeCareportalEventRecord(trJson);
+ handleAddChangeTreatmentRecord(trJson);
+ handleAddChangeProfileSwitchRecord(trJson);
+ }
+
+ public void handleDanaRHistoryRecords(JSONObject trJson) {
+ if (trJson.has(DanaRNSHistorySync.DANARSIGNATURE)) {
+ MainApp.getDbHelper().updateDanaRHistoryRecordId(trJson);
+ }
+ }
+
+ public void handleAddChangeTreatmentRecord(JSONObject trJson) throws JSONException {
+ if (trJson.has("insulin") || trJson.has("carbs")) {
+ MainApp.getDbHelper().createTreatmentFromJsonIfNotExists(trJson);
return;
}
- String _id = trJson.getString("_id");
-
- Treatment stored;
-
- if (trJson.has("timeIndex")) {
- if (Config.logIncommingData)
- log.debug("ADD: timeIndex found: " + trstring);
- stored = MainApp.getDbHelper().findTreatmentByTimeIndex(trJson.getLong("timeIndex"));
- } else {
- stored = MainApp.getDbHelper().findTreatmentById(_id);
- }
-
- if (stored != null) {
- if (Config.logIncommingData)
- log.debug("CHANGE: Removing old: " + trstring);
- MainApp.getDbHelper().delete(_id);
- }
-
- if (Config.logIncommingData)
- log.debug("CHANGE: Adding new treatment: " + trstring);
- InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin();
- if (insulinInterface == null) insulinInterface = InsulinFastactingFragment.getPlugin();
- Treatment treatment = new Treatment(insulinInterface);
- treatment._id = _id;
- treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
- treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
- //treatment.created_at = DateUtil.fromISODateString(trJson.getString("created_at"));
- treatment.created_at = new Date(trJson.getLong("mills"));
- if (trJson.has("eventType")) {
- treatment.mealBolus = true;
- if (trJson.get("eventType").equals("Correction Bolus"))
- treatment.mealBolus = false;
- double carbs = treatment.carbs;
- if (trJson.has("boluscalc")) {
- JSONObject boluscalc = trJson.getJSONObject("boluscalc");
- if (boluscalc.has("carbs")) {
- carbs = Math.max(boluscalc.getDouble("carbs"), carbs);
- }
- }
- if (carbs <= 0)
- treatment.mealBolus = false;
- }
- treatment.setTimeIndex(treatment.getTimeIndex());
- Dao.CreateOrUpdateStatus status = MainApp.getDbHelper().createOrUpdate(treatment);
- if (Config.logIncommingData)
- log.debug("Records updated: " + status.getNumLinesChanged());
- if (Config.logIncommingData)
- log.debug("CHANGE: Stored treatment: " + treatment.log());
}
- public void handleDanaRHistoryRecords(JSONObject trJson) throws JSONException, SQLException {
- if (trJson.has(DanaRNSHistorySync.DANARSIGNATURE)) {
- Dao daoHistoryRecords = MainApp.getDbHelper().getDaoDanaRHistory();
- QueryBuilder queryBuilder = daoHistoryRecords.queryBuilder();
- Where where = queryBuilder.where();
- where.ge("bytes", trJson.get(DanaRNSHistorySync.DANARSIGNATURE));
- PreparedQuery preparedQuery = queryBuilder.prepare();
- List list = daoHistoryRecords.query(preparedQuery);
- if (list.size() == 0) {
- // Record does not exists. Ignore
- } else if (list.size() == 1) {
- DanaRHistoryRecord record = list.get(0);
- if (record.get_id() == null || record.get_id() != trJson.getString("_id")) {
- if (Config.logIncommingData)
- log.debug("Updating _id in DanaR history database: " + trJson.getString("_id"));
- record.set_id(trJson.getString("_id"));
- daoHistoryRecords.update(record);
- } else {
- // already set
- }
+ public void handleAddChangeTempTargetRecord(JSONObject trJson) throws JSONException {
+ if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.TEMPORARYTARGET)) {
+ MainApp.getDbHelper().createTemptargetFromJsonIfNotExists(trJson);
+ }
+ }
+
+ public void handleAddChangeTempBasalRecord(JSONObject trJson) throws JSONException {
+ if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.TEMPBASAL)) {
+ MainApp.getDbHelper().createTempBasalFromJsonIfNotExists(trJson);
+ }
+ }
+
+ public void handleAddChangeExtendedBolusRecord(JSONObject trJson) throws JSONException {
+ if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.COMBOBOLUS)) {
+ MainApp.getDbHelper().createExtendedBolusFromJsonIfNotExists(trJson);
+ }
+ }
+
+ public void handleAddChangeCareportalEventRecord(JSONObject trJson) throws JSONException {
+ if (trJson.has("insulin") && trJson.getDouble("insulin") > 0)
+ return;
+ if (trJson.has("carbs") && trJson.getDouble("carbs") > 0)
+ return;
+ if (trJson.has("eventType") && (
+ trJson.getString("eventType").equals(CareportalEvent.SITECHANGE) ||
+ trJson.getString("eventType").equals(CareportalEvent.INSULINCHANGE) ||
+ trJson.getString("eventType").equals(CareportalEvent.SENSORCHANGE) ||
+ trJson.getString("eventType").equals(CareportalEvent.BGCHECK) ||
+ trJson.getString("eventType").equals(CareportalEvent.NOTE) ||
+ trJson.getString("eventType").equals(CareportalEvent.NONE) ||
+ trJson.getString("eventType").equals(CareportalEvent.ANNOUNCEMENT) ||
+ trJson.getString("eventType").equals(CareportalEvent.QUESTION) ||
+ trJson.getString("eventType").equals(CareportalEvent.EXERCISE) ||
+ trJson.getString("eventType").equals(CareportalEvent.OPENAPSOFFLINE) ||
+ trJson.getString("eventType").equals(CareportalEvent.PUMPBATTERYCHANGE)
+ )) {
+ MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(trJson);
+ }
+
+ if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.ANNOUNCEMENT)) {
+ long date = trJson.getLong("mills");
+ long now = System.currentTimeMillis();
+ if (date > now - 15 * 60 * 1000L && trJson.has("notes")) {
+ Notification announcement = new Notification(Notification.NSANNOUNCEMENT, trJson.getString("notes"), Notification.ANNOUNCEMENT, 60);
+ MainApp.bus().post(new EventNewNotification(announcement));
}
}
}
- /*
- {
- "_id": "58795998aa86647ba4d68ce7",
- "enteredBy": "",
- "eventType": "Temporary Target",
- "reason": "Eating Soon",
- "targetTop": 80,
- "targetBottom": 80,
- "duration": 120,
- "created_at": "2017-01-13T22:50:00.782Z",
- "carbs": null,
- "insulin": null
-}
- */
-
- public void handleAddChangeTempTargetRecord(JSONObject trJson) throws JSONException, SQLException {
- if (trJson.has("eventType") && trJson.getString("eventType").equals("Temporary Target")) {
- if (Config.logIncommingData)
- log.debug("Processing TempTarget record: " + trJson.toString());
- Dao daoTempTargets = MainApp.getDbHelper().getDaoTempTargets();
- QueryBuilder queryBuilder = daoTempTargets.queryBuilder();
- Where where = queryBuilder.where();
- where.eq("_id", trJson.getString("_id")).or().eq("timeIndex", trJson.getLong("mills"));
- PreparedQuery preparedQuery = queryBuilder.prepare();
- List list = daoTempTargets.query(preparedQuery);
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
- if (profile == null) return; // no profile data, better ignore than do something wrong
- String units = profile.getUnits();
- if (list.size() == 0) {
- // Record does not exists. add
- TempTarget newRecord = new TempTarget();
- newRecord.timeStart = new Date(trJson.getLong("mills"));
- newRecord.duration = trJson.getInt("duration");
- newRecord.low = NSProfile.toMgdl(trJson.getDouble("targetBottom"), units);
- newRecord.high = NSProfile.toMgdl(trJson.getDouble("targetTop"), units);
- newRecord.reason = trJson.getString("reason");
- newRecord._id = trJson.getString("_id");
- newRecord.setTimeIndex(newRecord.getTimeIndex());
- daoTempTargets.createIfNotExists(newRecord);
- if (Config.logIncommingData)
- log.debug("Adding TempTarget record to database: " + newRecord.log());
- MainApp.bus().post(new EventTempTargetRangeChange());
- } else if (list.size() == 1) {
- if (Config.logIncommingData)
- log.debug("Updating TempTarget record in database: " + trJson.getString("_id"));
- TempTarget record = list.get(0);
- record.timeStart = new Date(trJson.getLong("mills"));
- record.duration = trJson.getInt("duration");
- record.low = NSProfile.toMgdl(trJson.getDouble("targetBottom"), units);
- record.high = NSProfile.toMgdl(trJson.getDouble("targetTop"), units);
- record.reason = trJson.getString("reason");
- record._id = trJson.getString("_id");
- daoTempTargets.update(record);
- MainApp.bus().post(new EventTempTargetRangeChange());
- }
- }
- }
-
- public void handleRemoveTempTargetRecord(JSONObject trJson) throws JSONException, SQLException {
- if (trJson.has("_id")) {
- Dao daoTempTargets = MainApp.getDbHelper().getDaoTempTargets();
- QueryBuilder queryBuilder = daoTempTargets.queryBuilder();
- Where where = queryBuilder.where();
- where.eq("_id", trJson.getString("_id"));
- PreparedQuery preparedQuery = queryBuilder.prepare();
- List list = daoTempTargets.query(preparedQuery);
-
- if (list.size() == 1) {
- TempTarget record = list.get(0);
- if (Config.logIncommingData)
- log.debug("Removing TempTarget record from database: " + record.log());
- daoTempTargets.delete(record);
- MainApp.bus().post(new EventTempTargetRangeChange());
- } else {
- if (Config.logIncommingData)
- log.debug("TempTarget not found database: " + trJson.toString());
- }
+ public void handleAddChangeProfileSwitchRecord(JSONObject trJson) throws JSONException {
+ if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.PROFILESWITCH)) {
+ MainApp.getDbHelper().createProfileSwitchFromJsonIfNotExists(trJson);
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java
index 1b7b0875e5..77ef5139ae 100644
--- a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java
+++ b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java
@@ -12,12 +12,17 @@ public interface Intents {
String ACTION_NEW_CAL = "info.nightscout.client.NEW_CAL";
String ACTION_NEW_STATUS = "info.nightscout.client.NEW_STATUS";
String ACTION_QUEUE_STATUS = "info.nightscout.client.QUEUE_STATUS";
+ String ACTION_ANNOUNCEMENT = "info.nightscout.client.ANNOUNCEMENT";
+ String ACTION_ALARM = "info.nightscout.client.ALARM";
+ String ACTION_URGENT_ALARM = "info.nightscout.client.URGENT_ALARM";
+ String ACTION_CLEAR_ALARM = "info.nightscout.client.CLEAR_ALARM";
// App -> NSClient
String ACTION_DATABASE = "info.nightscout.client.DBACCESS";
String ACTION_RESTART = "info.nightscout.client.RESTART";
String ACTION_RESEND = "info.nightscout.client.RESEND";
+ String ACTION_ACK_ALARM = "info.nightscout.client.ACK_ALARM";
// xDrip -> App
String RECEIVER_PERMISSION = "com.eveningoutpost.dexdrip.permissions.RECEIVE_BG_ESTIMATE";
diff --git a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java
new file mode 100644
index 0000000000..2d928a5541
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java
@@ -0,0 +1,32 @@
+package info.nightscout.androidaps.data;
+
+import android.content.Context;
+
+import org.json.JSONObject;
+
+import java.util.Date;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.db.CareportalEvent;
+import info.nightscout.androidaps.db.Source;
+import info.nightscout.androidaps.interfaces.InsulinInterface;
+
+/**
+ * Created by mike on 29.05.2017.
+ */
+
+public class DetailedBolusInfo {
+ public long date = System.currentTimeMillis();
+ public String eventType = CareportalEvent.MEALBOLUS;
+ public double insulin = 0;
+ public double carbs = 0;
+ public int source = Source.NONE;
+ public boolean isValid = true;
+ public double glucose = 0; // Bg value in current units
+ public String glucoseType = ""; // NS values: Manual, Finger, Sensor
+ public int carbTime = 0; // time shift of carbs in minutes
+ public JSONObject boluscalc = null; // additional bolus wizard info
+ public Context context = null; // context for progress dialog
+ public long pumpId = 0; // id of record if comming from pump history (not a newly created treatment)
+ public boolean isSMB = false; // is a Super-MicroBolus
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java b/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java
index c4caf2f1af..1623028a82 100644
--- a/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java
+++ b/app/src/main/java/info/nightscout/androidaps/data/GlucoseStatus.java
@@ -66,19 +66,17 @@ public class GlucoseStatus {
@Nullable
public static GlucoseStatus getGlucoseStatusData() {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainApp.instance());
-
// load 45min
- long fromtime = (long) (new Date().getTime() - 60 * 1000L * 45);
+ long fromtime = (long) (System.currentTimeMillis() - 60 * 1000L * 45);
List data = MainApp.getDbHelper().getBgreadingsDataFromTime(fromtime, false);
int sizeRecords = data.size();
- if (sizeRecords < 1 || data.get(0).timeIndex < new Date().getTime() - 7 * 60 * 1000L) {
+ if (sizeRecords < 1 || data.get(0).date < System.currentTimeMillis() - 7 * 60 * 1000L) {
return null;
}
BgReading now = data.get(0);
- long now_date = now.timeIndex;
+ long now_date = now.date;
double change;
if (sizeRecords < 2) {
@@ -98,7 +96,7 @@ public class GlucoseStatus {
for (int i = 1; i < data.size(); i++) {
if (data.get(i).value > 38) {
BgReading then = data.get(i);
- long then_date = then.timeIndex;
+ long then_date = then.date;
double avgdelta = 0;
long minutesago;
@@ -143,48 +141,6 @@ public class GlucoseStatus {
return status.round();
}
- /*
- * Return last BgReading from database or null if db is empty
- */
- @Nullable
- public static BgReading lastBg() {
- List bgList = null;
-
- try {
- Dao daoBgReadings = MainApp.getDbHelper().getDaoBgReadings();
- QueryBuilder queryBuilder = daoBgReadings.queryBuilder();
- queryBuilder.orderBy("timeIndex", false);
- queryBuilder.limit(1L);
- queryBuilder.where().gt("value", 38);
- PreparedQuery preparedQuery = queryBuilder.prepare();
- bgList = daoBgReadings.query(preparedQuery);
-
- } catch (SQLException e) {
- log.debug(e.getMessage(), e);
- }
- if (bgList != null && bgList.size() > 0)
- return bgList.get(0);
- else
- return null;
- }
-
- /*
- * Return bg reading if not old ( <9 min )
- * or null if older
- */
- @Nullable
- public static BgReading actualBg() {
- BgReading lastBg = lastBg();
-
- if (lastBg == null)
- return null;
-
- if (lastBg.timeIndex > new Date().getTime() - 9 * 60 * 1000)
- return lastBg;
-
- return null;
- }
-
public static double average(ArrayList array) {
double sum = 0d;
diff --git a/app/src/main/java/info/nightscout/androidaps/data/Intervals.java b/app/src/main/java/info/nightscout/androidaps/data/Intervals.java
new file mode 100644
index 0000000000..108b4060e0
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/data/Intervals.java
@@ -0,0 +1,90 @@
+package info.nightscout.androidaps.data;
+
+import android.support.annotation.Nullable;
+import android.support.v4.util.LongSparseArray;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import info.nightscout.androidaps.interfaces.Interval;
+
+/**
+ * Created by mike on 09.05.2017.
+ */
+
+// Zero duration means end of interval
+
+public abstract class Intervals {
+
+ LongSparseArray rawData = new LongSparseArray(); // oldest at index 0
+
+ public synchronized Intervals reset() {
+ rawData = new LongSparseArray();
+ return this;
+ }
+
+ protected abstract void merge();
+
+ /**
+ * The List must be sorted by `T.start()` in ascending order
+ *
+ * */
+ public synchronized void add(List list) {
+ for (T interval : list) {
+ rawData.put(interval.start(), interval);
+ }
+ merge();
+ }
+
+
+
+ public synchronized List getList() {
+ List list = new ArrayList<>();
+ for (int i = 0; i < rawData.size(); i++)
+ list.add(rawData.valueAt(i));
+ return list;
+ }
+
+ public synchronized List getReversedList() {
+ List list = new ArrayList<>();
+ for (int i = rawData.size() -1; i>=0; i--)
+ list.add(rawData.valueAt(i));
+ return list;
+ }
+
+ protected synchronized int binarySearch(long value) {
+ int lo = 0;
+ int hi = rawData.size() - 1;
+
+ while (lo <= hi) {
+ final int mid = (lo + hi) >>> 1;
+ final Interval midVal = rawData.valueAt(mid);
+
+ if (midVal.before(value)) {
+ lo = mid + 1;
+ } else if (midVal.after(value)) {
+ hi = mid - 1;
+ } else if (midVal.match(value)) {
+ return mid; // value found
+ }
+ }
+ return ~lo; // value not present
+ }
+
+ public abstract T getValueByInterval(long time);
+
+ public synchronized int size() {
+ return rawData.size();
+ }
+
+ public synchronized T get(int index) {
+ return rawData.valueAt(index);
+ }
+
+ public synchronized T getReversed(int index) {
+ return rawData.valueAt(size() - 1 - index);
+ }
+
+
+
+}
\ No newline at end of file
diff --git a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java
index ed3096c945..bd924d35b5 100644
--- a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java
+++ b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java
@@ -1,26 +1,29 @@
package info.nightscout.androidaps.data;
-import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
-import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.Round;
public class IobTotal {
- public Double iob;
- public Double activity;
- public Double bolussnooze;
- public Double basaliob;
- public Double netbasalinsulin;
- public Double hightempinsulin;
+ public double iob;
+ public double activity;
+ public double bolussnooze;
+ public double basaliob;
+ public double netbasalinsulin;
+ public double hightempinsulin;
- public Double netInsulin = 0d; // for calculations from temp basals only
- public Double netRatio = 0d; // net ratio at start of temp basal
+ // oref1
+ public double microBolusInsulin;
+ public double microBolusIOB;
+
+ public double netInsulin = 0d; // for calculations from temp basals only
+ public double netRatio = 0d; // net ratio at start of temp basal
+
+ public double extendedBolusInsulin = 0d; // total insulin for extended bolus
long time;
@@ -31,6 +34,8 @@ public class IobTotal {
this.basaliob = 0d;
this.netbasalinsulin = 0d;
this.hightempinsulin = 0d;
+ this.microBolusInsulin = 0d;
+ this.microBolusIOB = 0d;
this.time = time;
}
@@ -42,7 +47,9 @@ public class IobTotal {
netbasalinsulin += other.netbasalinsulin;
hightempinsulin += other.hightempinsulin;
netInsulin += other.netInsulin;
- netRatio += other.netRatio;
+ extendedBolusInsulin += other.extendedBolusInsulin;
+ microBolusInsulin += other.microBolusInsulin;
+ microBolusIOB += other.microBolusIOB;
return this;
}
@@ -54,6 +61,8 @@ public class IobTotal {
result.basaliob = basalIob.basaliob;
result.netbasalinsulin = basalIob.netbasalinsulin;
result.hightempinsulin = basalIob.hightempinsulin;
+ result.microBolusInsulin = bolusIOB.microBolusInsulin + basalIob.microBolusInsulin;
+ result.microBolusIOB = bolusIOB.microBolusIOB + basalIob.microBolusIOB;
return result;
}
@@ -64,6 +73,8 @@ public class IobTotal {
this.basaliob = Round.roundTo(this.basaliob, 0.001);
this.netbasalinsulin = Round.roundTo(this.netbasalinsulin, 0.001);
this.hightempinsulin = Round.roundTo(this.hightempinsulin, 0.001);
+ this.microBolusInsulin = Round.roundTo(this.microBolusInsulin, 0.001);
+ this.microBolusIOB = Round.roundTo(this.microBolusIOB, 0.001);
return this;
}
diff --git a/app/src/main/java/info/nightscout/androidaps/data/NonOverlappingIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/NonOverlappingIntervals.java
new file mode 100644
index 0000000000..385eeb9594
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/data/NonOverlappingIntervals.java
@@ -0,0 +1,30 @@
+package info.nightscout.androidaps.data;
+
+
+import android.support.annotation.Nullable;
+
+import info.nightscout.androidaps.interfaces.Interval;
+
+/**
+ * Created by adrian on 15/07/17.
+ */
+
+public class NonOverlappingIntervals extends Intervals {
+
+ protected synchronized void merge() {
+ for (int index = 0; index < rawData.size() - 1; index++) {
+ Interval i = rawData.valueAt(index);
+ long startOfNewer = rawData.valueAt(index + 1).start();
+ if (i.originalEnd() > startOfNewer) {
+ i.cutEndTo(startOfNewer);
+ }
+ }
+ }
+
+ @Nullable
+ public synchronized T getValueByInterval(long time) {
+ int index = binarySearch(time);
+ if (index >= 0) return rawData.valueAt(index);
+ return null;
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java
new file mode 100644
index 0000000000..070426cca0
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java
@@ -0,0 +1,43 @@
+package info.nightscout.androidaps.data;
+
+
+import android.support.annotation.Nullable;
+
+import info.nightscout.androidaps.interfaces.Interval;
+
+/**
+ * Created by adrian on 15/07/17.
+ */
+
+public class OverlappingIntervals extends Intervals {
+
+ protected synchronized void merge() {
+ boolean needToCut = false;
+ long cutTime = 0;
+
+ for (int index = rawData.size()-1; index >= 0; index--) { //begin with newest
+ Interval cur = rawData.valueAt(index);
+ if (cur.isEndingEvent()){
+ needToCut = true;
+ cutTime = cur.start();
+ } else {
+ //event that is no EndingEvent might need to be stopped by an ending event
+ if(needToCut&&cur.end() > cutTime){
+ cur.cutEndTo(cutTime);
+ }
+ }
+ }
+ }
+
+ @Nullable
+ public synchronized T getValueByInterval(long time) {
+ for (int index = rawData.size()-1; index >= 0; index--) { //begin with newest
+ T cur = rawData.valueAt(index);
+ if (cur.match(time)){
+ return cur;
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java
new file mode 100644
index 0000000000..984d92c4e5
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java
@@ -0,0 +1,413 @@
+package info.nightscout.androidaps.data;
+
+import android.support.v4.util.LongSparseArray;
+
+import com.crashlytics.android.Crashlytics;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.text.DecimalFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+import info.nightscout.androidaps.Constants;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.plugins.Overview.Notification;
+import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
+import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
+import info.nightscout.utils.DecimalFormatter;
+import info.nightscout.utils.ToastUtils;
+
+public class Profile {
+ private static Logger log = LoggerFactory.getLogger(Profile.class);
+
+ private JSONObject json;
+ private String units = null;
+ double dia = Constants.defaultDIA;
+ TimeZone timeZone = TimeZone.getDefault();
+ JSONArray isf;
+ private LongSparseArray isf_v = null; // oldest at index 0
+ JSONArray ic;
+ private LongSparseArray ic_v = null; // oldest at index 0
+ JSONArray basal;
+ private LongSparseArray basal_v = null; // oldest at index 0
+ JSONArray targetLow;
+ JSONArray targetHigh;
+
+ public Profile(JSONObject json, String units) {
+ this(json);
+ if (this.units == null) {
+ if (units != null)
+ this.units = units;
+ else {
+ Crashlytics.log("Profile failover failed too");
+ this.units = Constants.MGDL;
+ }
+ }
+ }
+
+ public Profile(JSONObject json) {
+ this.json = json;
+ try {
+ if (json.has("units"))
+ units = json.getString("units").toLowerCase();
+ if (json.has("dia"))
+ dia = json.getDouble("dia");
+ if (json.has("dia"))
+ dia = json.getDouble("dia");
+ if (json.has("timezone"))
+ timeZone = TimeZone.getTimeZone(json.getString("timezone"));
+ isf = json.getJSONArray("sens");
+ if (getIsf(0) == null) {
+ int defaultISF = units.equals(Constants.MGDL) ? 400 : 20;
+ isf = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultISF + "\",\"timeAsSeconds\":\"0\"}]");
+ Notification noisf = new Notification(Notification.ISF_MISSING, MainApp.sResources.getString(R.string.isfmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(noisf));
+ } else {
+ MainApp.bus().post(new EventDismissNotification(Notification.ISF_MISSING));
+ }
+ ic = json.getJSONArray("carbratio");
+ if (getIc(0) == null) {
+ int defaultIC = 25;
+ ic = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultIC + "\",\"timeAsSeconds\":\"0\"}]");
+ Notification noic = new Notification(Notification.IC_MISSING, MainApp.sResources.getString(R.string.icmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(noic));
+ } else {
+ MainApp.bus().post(new EventDismissNotification(Notification.IC_MISSING));
+ }
+ basal = json.getJSONArray("basal");
+ if (getBasal(0) == null) {
+ double defaultBasal = 0.1d;
+ basal = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultBasal + "\",\"timeAsSeconds\":\"0\"}]");
+ Notification nobasal = new Notification(Notification.BASAL_MISSING, MainApp.sResources.getString(R.string.basalmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(nobasal));
+ } else {
+ MainApp.bus().post(new EventDismissNotification(Notification.BASAL_MISSING));
+ }
+ targetLow = json.getJSONArray("target_low");
+ if (getTargetLow(0) == null) {
+ double defaultLow = units.equals(Constants.MGDL) ? 120 : 6;
+ targetLow = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultLow + "\",\"timeAsSeconds\":\"0\"}]");
+ Notification notarget = new Notification(Notification.TARGET_MISSING, MainApp.sResources.getString(R.string.targetmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(notarget));
+ } else {
+ MainApp.bus().post(new EventDismissNotification(Notification.TARGET_MISSING));
+ }
+ targetHigh = json.getJSONArray("target_high");
+ if (getTargetHigh(0) == null) {
+ double defaultHigh = units.equals(Constants.MGDL) ? 160 : 8;
+ targetHigh = new JSONArray("[{\"time\":\"00:00\",\"value\":\"" + defaultHigh + "\",\"timeAsSeconds\":\"0\"}]");
+ Notification notarget = new Notification(Notification.TARGET_MISSING, MainApp.sResources.getString(R.string.targetmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(notarget));
+ } else {
+ MainApp.bus().post(new EventDismissNotification(Notification.TARGET_MISSING));
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.invalidprofile));
+ }
+ }
+
+ public String log() {
+ String ret = "\n";
+ for (Integer hour = 0; hour < 24; hour++) {
+ double value = getBasal((Integer) (hour * 60 * 60));
+ ret += "NS basal value for " + hour + ":00 is " + value + "\n";
+ }
+ ret += "NS units: " + getUnits();
+ return ret;
+ }
+
+ public JSONObject getData() {
+ if (!json.has("units"))
+ try {
+ json.put("units", units);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return json;
+ }
+
+ public double getDia() {
+ return dia;
+ }
+
+ // mmol or mg/dl
+ public String getUnits() {
+ return units;
+ }
+
+ public TimeZone getTimeZone() {
+ return timeZone;
+ }
+
+ private LongSparseArray convertToSparseArray(JSONArray array) {
+ LongSparseArray sparse = new LongSparseArray<>();
+ for (Integer index = 0; index < array.length(); index++) {
+ try {
+ JSONObject o = array.getJSONObject(index);
+ long tas = o.getLong("timeAsSeconds");
+ Double value = o.getDouble("value");
+ sparse.put(tas, value);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ return sparse;
+ }
+
+ private Double getValueToTime(JSONArray array, Integer timeAsSeconds) {
+ Double lastValue = null;
+
+ for (Integer index = 0; index < array.length(); index++) {
+ try {
+ JSONObject o = array.getJSONObject(index);
+ Integer tas = o.getInt("timeAsSeconds");
+ Double value = o.getDouble("value");
+ if (lastValue == null) lastValue = value;
+ if (timeAsSeconds < tas) {
+ break;
+ }
+ lastValue = value;
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ return lastValue;
+ }
+
+ private Double getValueToTime(LongSparseArray array, long timeAsSeconds) {
+ Double lastValue = null;
+
+ for (Integer index = 0; index < array.size(); index++) {
+ long tas = array.keyAt(index);
+ double value = array.valueAt(index);
+ if (lastValue == null) lastValue = value;
+ if (timeAsSeconds < tas) {
+ break;
+ }
+ lastValue = value;
+ }
+ return lastValue;
+ }
+
+ private String getValuesList(JSONArray array, JSONArray array2, DecimalFormat format, String units) {
+ String retValue = "";
+
+ for (Integer index = 0; index < array.length(); index++) {
+ try {
+ JSONObject o = array.getJSONObject(index);
+ retValue += o.getString("time");
+ retValue += " ";
+ retValue += format.format(o.getDouble("value"));
+ if (array2 != null) {
+ JSONObject o2 = array2.getJSONObject(index);
+ retValue += " - ";
+ retValue += format.format(o2.getDouble("value"));
+ }
+ retValue += " " + units;
+ if (index + 1 < array.length())
+ retValue += "\n";
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ return retValue;
+ }
+
+ public Double getIsf() {
+ return getIsf(secondsFromMidnight(System.currentTimeMillis()));
+ }
+
+ public Double getIsf(long time) {
+ return getIsf(secondsFromMidnight(time));
+ }
+
+ public Double getIsf(Integer timeAsSeconds) {
+ if (isf_v == null)
+ isf_v = convertToSparseArray(isf);
+ return getValueToTime(isf_v, timeAsSeconds);
+ }
+
+ public String getIsfList() {
+ return getValuesList(isf, null, new DecimalFormat("0.0"), getUnits() + "/U");
+ }
+
+ public Double getIc() {
+ return getIc(secondsFromMidnight(System.currentTimeMillis()));
+ }
+
+ public Double getIc(long time) {
+ return getIc(secondsFromMidnight(time));
+ }
+
+ public Double getIc(Integer timeAsSeconds) {
+ if (ic_v == null)
+ ic_v = convertToSparseArray(ic);
+ return getValueToTime(ic_v, timeAsSeconds);
+ }
+
+ public String getIcList() {
+ return getValuesList(ic, null, new DecimalFormat("0.0"), " g/U");
+ }
+
+ public Double getBasal() {
+ return getBasal(secondsFromMidnight(System.currentTimeMillis()));
+ }
+
+ public Double getBasal(long time) {
+ return getBasal(secondsFromMidnight(time));
+ }
+
+ public Double getBasal(Integer timeAsSeconds) {
+ if (basal_v == null)
+ basal_v = convertToSparseArray(basal);
+ return getValueToTime(basal_v, timeAsSeconds);
+ }
+
+ public String getBasalList() {
+ return getValuesList(basal, null, new DecimalFormat("0.00"), "U");
+ }
+
+ public class BasalValue {
+ public BasalValue(Integer timeAsSeconds, Double value) {
+ this.timeAsSeconds = timeAsSeconds;
+ this.value = value;
+ }
+
+ public Integer timeAsSeconds;
+ public Double value;
+ }
+
+ public BasalValue[] getBasalValues() {
+ try {
+ BasalValue[] ret = new BasalValue[basal.length()];
+
+ for (Integer index = 0; index < basal.length(); index++) {
+ JSONObject o = basal.getJSONObject(index);
+ Integer tas = o.getInt("timeAsSeconds");
+ Double value = o.getDouble("value");
+ ret[index] = new BasalValue(tas, value);
+ }
+ return ret;
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return new BasalValue[0];
+ }
+
+ public Double getTargetLow() {
+ return getTargetLow(secondsFromMidnight(System.currentTimeMillis()));
+ }
+
+ public Double getTargetLow(long time) {
+ return getTargetLow(secondsFromMidnight(time));
+ }
+
+ public Double getTargetLow(Integer timeAsSeconds) {
+ return getValueToTime(targetLow, timeAsSeconds);
+ }
+
+ public Double getTargetHigh() {
+ return getTargetHigh(secondsFromMidnight(System.currentTimeMillis()));
+ }
+
+ public Double getTargetHigh(long time) {
+ return getTargetHigh(secondsFromMidnight(time));
+ }
+
+ public Double getTargetHigh(Integer timeAsSeconds) {
+ return getValueToTime(targetHigh, timeAsSeconds);
+ }
+
+ public String getTargetList() {
+ return getValuesList(targetLow, targetHigh, new DecimalFormat("0.0"), getUnits());
+ }
+
+ public double getMaxDailyBasal() {
+ Double max = 0d;
+ for (Integer hour = 0; hour < 24; hour++) {
+ double value = getBasal((Integer) (hour * 60 * 60));
+ if (value > max) max = value;
+ }
+ return max;
+ }
+
+ public static Integer secondsFromMidnight() {
+ Calendar c = Calendar.getInstance();
+ long now = c.getTimeInMillis();
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ c.set(Calendar.MILLISECOND, 0);
+ long passed = now - c.getTimeInMillis();
+ return (int) (passed / 1000);
+ }
+
+ public static Integer secondsFromMidnight(Date date) {
+ Calendar c = Calendar.getInstance();
+ long now = date.getTime();
+ c.setTime(date);
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ c.set(Calendar.MILLISECOND, 0);
+ long passed = now - c.getTimeInMillis();
+ return (int) (passed / 1000);
+ }
+
+ public static Integer secondsFromMidnight(long date) {
+ Calendar c = Calendar.getInstance();
+ c.setTimeInMillis(date);
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ c.set(Calendar.MILLISECOND, 0);
+ long passed = date - c.getTimeInMillis();
+ return (int) (passed / 1000);
+ }
+
+ public static Double toMgdl(Double value, String units) {
+ if (units.equals(Constants.MGDL)) return value;
+ else return value * Constants.MMOLL_TO_MGDL;
+ }
+
+ public static Double toMmol(Double value, String units) {
+ if (units.equals(Constants.MGDL)) return value * Constants.MGDL_TO_MMOLL;
+ else return value;
+ }
+
+ public static Double fromMgdlToUnits(Double value, String units) {
+ if (units.equals(Constants.MGDL)) return value;
+ else return value * Constants.MGDL_TO_MMOLL;
+ }
+
+ public static Double toUnits(Double valueInMgdl, Double valueInMmol, String units) {
+ if (units.equals(Constants.MGDL)) return valueInMgdl;
+ else return valueInMmol;
+ }
+
+ public static String toUnitsString(Double valueInMgdl, Double valueInMmol, String units) {
+ if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(valueInMgdl);
+ else return DecimalFormatter.to1Decimal(valueInMmol);
+ }
+
+ // targets are stored in mg/dl but profile vary
+ public static String toTargetRangeString(double low, double high, String sourceUnits, String units) {
+ double lowMgdl = toMgdl(low, sourceUnits);
+ double highMgdl = toMgdl(high, sourceUnits);
+ double lowMmol = toMmol(low, sourceUnits);
+ double highMmol = toMmol(high, sourceUnits);
+ if (low == high)
+ return toUnitsString(lowMgdl, lowMmol, units);
+ else
+ return toUnitsString(lowMgdl, lowMmol, units) + " - " + toUnitsString(highMgdl, highMmol, units);
+
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java
new file mode 100644
index 0000000000..3633f1079e
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java
@@ -0,0 +1,109 @@
+package info.nightscout.androidaps.data;
+
+import android.support.annotation.Nullable;
+import android.support.v4.util.LongSparseArray;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import info.nightscout.androidaps.interfaces.Interval;
+
+/**
+ * Created by mike on 09.05.2017.
+ */
+
+// Zero duration means profile is valid until is chaged
+// When no interval match the lastest record without duration is used
+
+public class ProfileIntervals {
+
+ private LongSparseArray rawData = new LongSparseArray<>(); // oldest at index 0
+
+ public synchronized ProfileIntervals reset() {
+ rawData = new LongSparseArray<>();
+ return this;
+ }
+
+ public synchronized void add(T newInterval) {
+ rawData.put(newInterval.start(), newInterval);
+ merge();
+ }
+
+ public synchronized void add(List list) {
+ for (T interval : list) {
+ rawData.put(interval.start(), interval);
+ }
+ merge();
+ }
+
+ private synchronized void merge() {
+ for (int index = 0; index < rawData.size() - 1; index++) {
+ Interval i = rawData.valueAt(index);
+ long startOfNewer = rawData.valueAt(index + 1).start();
+ if (i.originalEnd() > startOfNewer) {
+ i.cutEndTo(startOfNewer);
+ }
+ }
+ }
+
+ @Nullable
+ public synchronized Interval getValueToTime(long time) {
+ int index = binarySearch(time);
+ if (index >= 0) return rawData.valueAt(index);
+ return null;
+ }
+
+ public synchronized List getList() {
+ List list = new ArrayList<>();
+ for (int i = 0; i < rawData.size(); i++)
+ list.add(rawData.valueAt(i));
+ return list;
+ }
+
+ public synchronized List getReversedList() {
+ List list = new ArrayList<>();
+ for (int i = rawData.size() -1; i>=0; i--)
+ list.add(rawData.valueAt(i));
+ return list;
+ }
+
+ private synchronized int binarySearch(long value) {
+ if (rawData.size() == 0)
+ return -1;
+ int lo = 0;
+ int hi = rawData.size() - 1;
+
+ while (lo <= hi) {
+ final int mid = (lo + hi) >>> 1;
+ final Interval midVal = rawData.valueAt(mid);
+
+ if (midVal.match(value)) {
+ return mid; // value found
+ } else if (midVal.before(value)) {
+ lo = mid + 1;
+ } else if (midVal.after(value)) {
+ hi = mid - 1;
+ }
+ }
+ // not found, try nearest older with duration 0
+ lo = lo - 1;
+ while (lo >= 0 && lo < rawData.size()) {
+ if (rawData.valueAt(lo).isEndingEvent())
+ return lo;
+ lo--;
+ }
+ return -1; // value not present
+ }
+
+ public synchronized int size() {
+ return rawData.size();
+ }
+
+ public synchronized T get(int index) {
+ return rawData.valueAt(index);
+ }
+
+ public synchronized T getReversed(int index) {
+ return rawData.valueAt(size() - 1 - index);
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java b/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java
new file mode 100644
index 0000000000..3e2217454f
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java
@@ -0,0 +1,118 @@
+package info.nightscout.androidaps.data;
+
+import android.support.annotation.Nullable;
+import android.support.v4.util.ArrayMap;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import info.nightscout.androidaps.Constants;
+
+/**
+ * Created by mike on 01.06.2017.
+ */
+
+public class ProfileStore {
+ private static Logger log = LoggerFactory.getLogger(ProfileStore.class);
+ private JSONObject json = null;
+ private String units = Constants.MGDL;
+
+ ArrayMap cachedObjects = new ArrayMap<>();
+
+ public ProfileStore(JSONObject json) {
+ this.json = json;
+ getDefaultProfile(); // initialize units
+ }
+
+ public JSONObject getData() {
+ return json;
+ }
+
+ @Nullable
+ public Profile getDefaultProfile() {
+ Profile profile = null;
+ try {
+ String defaultProfileName = json.getString("defaultProfile");
+ JSONObject store = json.getJSONObject("store");
+ if (store.has(defaultProfileName)) {
+ profile = cachedObjects.get(defaultProfileName);
+ if (profile == null) {
+ if (store.has("units"))
+ units = store.getString("units");
+ profile = new Profile(store.getJSONObject(defaultProfileName), units);
+ units = profile.getUnits();
+ cachedObjects.put(defaultProfileName, profile);
+ }
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return profile;
+ }
+
+ @Nullable
+ public String getDefaultProfileName() {
+ String defaultProfileName = null;
+ try {
+ defaultProfileName = json.getString("defaultProfile");
+ JSONObject store = json.getJSONObject("store");
+ if (store.has(defaultProfileName)) {
+ return defaultProfileName;
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return defaultProfileName;
+ }
+
+ public String getUnits() {
+ return units;
+ }
+
+ @Nullable
+ public Profile getSpecificProfile(String profileName) {
+ Profile profile = null;
+ try {
+ JSONObject store = json.getJSONObject("store");
+ if (store.has(profileName)) {
+ profile = cachedObjects.get(profileName);
+ if (profile == null) {
+ if (store.has("units"))
+ units = store.getString("units");
+ profile = new Profile(store.getJSONObject(profileName), units);
+ units = profile.getUnits();
+ cachedObjects.put(profileName, profile);
+ }
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return profile;
+ }
+
+ public ArrayList getProfileList() {
+ ArrayList ret = new ArrayList();
+
+ JSONObject store;
+ try {
+ store = json.getJSONObject("store");
+ Iterator> keys = store.keys();
+
+ while (keys.hasNext()) {
+ String profileName = (String) keys.next();
+ ret.add(profileName);
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+
+ return ret;
+ }
+
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java
index a2a5b91870..bfddeff320 100644
--- a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java
+++ b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java
@@ -8,7 +8,6 @@ import org.json.JSONObject;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.Round;
@@ -25,7 +24,7 @@ public class PumpEnactResult extends Object {
public boolean isTempCancel = false; // if true we are caceling temp basal
// Result of treatment delivery
public Double bolusDelivered = 0d; // real value of delivered insulin
- public Integer carbsDelivered = 0; // real value of delivered carbs
+ public Double carbsDelivered = 0d; // real value of delivered carbs
public boolean queued = false;
@@ -94,7 +93,7 @@ public class PumpEnactResult extends Object {
result.put("duration", 0);
} else if (isPercent) {
// Nightscout is expecting absolute value
- Double abs = Round.roundTo(MainApp.getConfigBuilder().getActiveProfile().getProfile().getBasal(NSProfile.secondsFromMidnight()) * percent / 100, 0.01);
+ Double abs = Round.roundTo(MainApp.getConfigBuilder().getProfile().getBasal() * percent / 100, 0.01);
result.put("rate", abs);
result.put("duration", duration);
} else {
diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java
index 8a0aaf96e1..cbe1850958 100644
--- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java
+++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java
@@ -2,50 +2,54 @@ package info.nightscout.androidaps.db;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
-import com.jjoe64.graphview.series.DataPointInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
+import java.util.Objects;
import info.nightscout.androidaps.Constants;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
+import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries;
+import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
+import info.nightscout.utils.SP;
@DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS)
-public class BgReading implements DataPointInterface {
+public class BgReading implements DataPointWithLabelInterface {
private static Logger log = LoggerFactory.getLogger(BgReading.class);
- public long getTimeIndex() {
- return timeIndex;
- }
+ @DatabaseField(id = true)
+ public long date;
- public void setTimeIndex(long timeIndex) {
- this.timeIndex = timeIndex;
- }
-
- @DatabaseField(id = true, useGetSet = true)
- public long timeIndex;
+ @DatabaseField
+ public boolean isValid = true;
@DatabaseField
public double value;
-
@DatabaseField
public String direction;
-
@DatabaseField
public double raw;
@DatabaseField
- public int battery_level;
+ public int source = Source.NONE;
+ @DatabaseField
+ public String _id = null; // NS _id
- public static String units = Constants.MGDL;
+ public boolean isPrediction = false; // true when drawing predictions as bg points
- public BgReading() {}
+ public BgReading() {
+ }
public BgReading(NSSgv sgv) {
- timeIndex = sgv.getMills();
+ date = sgv.getMills();
value = sgv.getMgdl();
raw = sgv.getFiltered() != null ? sgv.getFiltered() : value;
direction = sgv.getDirection();
@@ -63,9 +67,11 @@ public class BgReading implements DataPointInterface {
else return DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL);
}
- public String directionToSymbol() {
+ public String directionToSymbol() {
String symbol = "";
- if (direction.compareTo("DoubleDown") == 0) {
+ if (direction == null) {
+ symbol = "??";
+ } else if (direction.compareTo("DoubleDown") == 0) {
symbol = "\u21ca";
} else if (direction.compareTo("SingleDown") == 0) {
symbol = "\u2193";
@@ -101,23 +107,108 @@ public class BgReading implements DataPointInterface {
@Override
public String toString() {
return "BgReading{" +
- "timeIndex=" + timeIndex +
- ", date=" + new Date(timeIndex) +
+ "date=" + date +
+ ", date=" + new Date(date).toLocaleString() +
", value=" + value +
", direction=" + direction +
", raw=" + raw +
- ", battery_level=" + battery_level +
'}';
}
+ public boolean isDataChanging(BgReading other) {
+ if (date != other.date) {
+ log.error("Comparing different");
+ return false;
+ }
+ if (value != other.value)
+ return true;
+ return false;
+ }
+
+ public boolean isEqual(BgReading other) {
+ if (date != other.date) {
+ log.error("Comparing different");
+ return false;
+ }
+ if (value != other.value)
+ return false;
+ if (raw != other.raw)
+ return false;
+ if (!direction.equals(other.direction))
+ return false;
+ if (!Objects.equals(_id, other._id))
+ return false;
+ return true;
+ }
+
+ public void copyFrom(BgReading other) {
+ if (date != other.date) {
+ log.error("Copying different");
+ return;
+ }
+ value = other.value;
+ raw = other.raw;
+ direction = other.direction;
+ _id = other._id;
+ }
+
+ // ------------------ DataPointWithLabelInterface ------------------
@Override
public double getX() {
- return timeIndex;
+ return date;
}
@Override
public double getY() {
+ String units = MainApp.getConfigBuilder().getProfileUnits();
return valueToUnits(units);
}
+ @Override
+ public void setY(double y) {
+
+ }
+
+ @Override
+ public String getLabel() {
+ return null;
+ }
+
+ @Override
+ public long getDuration() {
+ return 0;
+ }
+
+ @Override
+ public PointsWithLabelGraphSeries.Shape getShape() {
+ return PointsWithLabelGraphSeries.Shape.POINT;
+ }
+
+ @Override
+ public float getSize() {
+ boolean isTablet = MainApp.sResources.getBoolean(R.bool.isTablet);
+ return isTablet ? 8 : 5;
+ }
+
+ @Override
+ public int getColor() {
+ String units = MainApp.getConfigBuilder().getProfileUnits();
+ Double lowLine = SP.getDouble("low_mark", 0d);
+ Double highLine = SP.getDouble("high_mark", 0d);
+ if (lowLine < 1) {
+ lowLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units);
+ }
+ if (highLine < 1) {
+ highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units);
+ }
+ int color = MainApp.sResources.getColor(R.color.inrange);
+ if (isPrediction)
+ color = MainApp.sResources.getColor(R.color.prediction);
+ else if (valueToUnits(units) < lowLine)
+ color = MainApp.sResources.getColor(R.color.low);
+ else if (valueToUnits(units) > highLine)
+ color = MainApp.sResources.getColor(R.color.high);
+ return color;
+ }
+
}
diff --git a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java
new file mode 100644
index 0000000000..f65da51176
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java
@@ -0,0 +1,245 @@
+package info.nightscout.androidaps.db;
+
+import android.graphics.Color;
+
+import com.j256.ormlite.field.DatabaseField;
+import com.j256.ormlite.table.DatabaseTable;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.EnumSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import info.nightscout.androidaps.Constants;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.androidaps.plugins.NSClientInternal.data.NSMbg;
+import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries;
+import info.nightscout.utils.DateUtil;
+import info.nightscout.utils.Translator;
+
+@DatabaseTable(tableName = DatabaseHelper.DATABASE_CAREPORTALEVENTS)
+public class CareportalEvent implements DataPointWithLabelInterface {
+ private static Logger log = LoggerFactory.getLogger(CareportalEvent.class);
+
+ @DatabaseField(id = true)
+ public long date;
+
+ @DatabaseField
+ public boolean isValid = true;
+
+ @DatabaseField
+ public int source = Source.NONE;
+ @DatabaseField
+ public String _id;
+
+ @DatabaseField
+ public String eventType;
+ @DatabaseField
+ public String json;
+
+ public static final String CARBCORRECTION = "Carb Correction";
+ public static final String BOLUSWIZARD = "Bolus Wizard";
+ public static final String CORRECTIONBOLUS = "Correction Bolus";
+ public static final String MEALBOLUS = "Meal Bolus";
+ public static final String COMBOBOLUS = "Combo Bolus";
+ public static final String TEMPBASAL = "Temp Basal";
+ public static final String TEMPORARYTARGET = "Temporary Target";
+ public static final String PROFILESWITCH = "Profile Switch";
+ public static final String SITECHANGE = "Site Change";
+ public static final String INSULINCHANGE = "Insulin Change";
+ public static final String SENSORCHANGE = "Sensor Change";
+ public static final String PUMPBATTERYCHANGE = "Pump Battery Change";
+ public static final String BGCHECK = "BG Check";
+ public static final String ANNOUNCEMENT = "Announcement";
+ public static final String NOTE = "Note";
+ public static final String QUESTION = "Question";
+ public static final String EXERCISE = "Exercise";
+ public static final String OPENAPSOFFLINE = "OpenAPS Offline";
+ public static final String NONE = "";
+
+ public static final String MBG = "Mbg"; // comming from entries
+
+ public CareportalEvent() {
+ }
+
+ public CareportalEvent(NSMbg mbg) {
+ date = mbg.date;
+ eventType = MBG;
+ json = mbg.json;
+ }
+
+ public long getMillisecondsFromStart() {
+ return System.currentTimeMillis() - date;
+ }
+
+ public long getHoursFromStart() {
+ return (System.currentTimeMillis() - date) / (60 * 1000);
+ }
+
+ public String age() {
+ Map diff = computeDiff(date, System.currentTimeMillis());
+ if (OverviewFragment.shorttextmode)
+ return diff.get(TimeUnit.DAYS) +"d" + diff.get(TimeUnit.HOURS) + "h";
+ else
+ return diff.get(TimeUnit.DAYS) + " " + MainApp.sResources.getString(R.string.days) + " " + diff.get(TimeUnit.HOURS) + " " + MainApp.sResources.getString(R.string.hours);
+ }
+
+ public String log() {
+ return "CareportalEvent{" +
+ "date= " + date +
+ ", date= " + DateUtil.dateAndTimeString(date) +
+ ", isValid= " + isValid +
+ ", _id= " + _id +
+ ", eventType= " + eventType +
+ ", json= " + json +
+ "}";
+ }
+
+ //Map:{DAYS=1, HOURS=3, MINUTES=46, SECONDS=40, MILLISECONDS=0, MICROSECONDS=0, NANOSECONDS=0}
+ public static Map computeDiff(long date1, long date2) {
+ long diffInMillies = date2 - date1;
+ List units = new ArrayList(EnumSet.allOf(TimeUnit.class));
+ Collections.reverse(units);
+ Map result = new LinkedHashMap();
+ long milliesRest = diffInMillies;
+ for (TimeUnit unit : units) {
+ long diff = unit.convert(milliesRest, TimeUnit.MILLISECONDS);
+ long diffInMilliesForUnit = unit.toMillis(diff);
+ milliesRest = milliesRest - diffInMilliesForUnit;
+ result.put(unit, diff);
+ }
+ return result;
+ }
+
+ // -------- DataPointWithLabelInterface -------
+
+ @Override
+ public double getX() {
+ return date;
+ }
+
+ double yValue = 0;
+
+ @Override
+ public double getY() {
+ String units = MainApp.getConfigBuilder().getProfileUnits();
+ if (eventType.equals(MBG)) {
+ double mbg = 0d;
+ try {
+ JSONObject object = new JSONObject(json);
+ mbg = object.getDouble("mgdl");
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return Profile.fromMgdlToUnits(mbg, units);
+ }
+
+ double glucose = 0d;
+ try {
+ JSONObject object = new JSONObject(json);
+ if (object.has("glucose")) {
+ glucose = object.getDouble("glucose");
+ units = object.getString("units");
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ if (glucose != 0d) {
+ double mmol = 0d;
+ double mgdl = 0;
+ if (units.equals(Constants.MGDL)) {
+ mgdl = glucose;
+ mmol = glucose * Constants.MGDL_TO_MMOLL;
+ }
+ if (units.equals(Constants.MMOL)) {
+ mmol = glucose;
+ mgdl = glucose * Constants.MMOLL_TO_MGDL;
+ }
+ return Profile.toUnits(mgdl, mmol, units);
+ }
+
+ return yValue;
+ }
+
+ @Override
+ public void setY(double y) {
+ yValue = y;
+ }
+
+ @Override
+ public String getLabel() {
+ try {
+ JSONObject object = new JSONObject(json);
+ if (object.has("notes"))
+ return object.getString("notes");
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return Translator.translate(eventType);
+ }
+
+ @Override
+ public long getDuration() {
+ try {
+ JSONObject object = new JSONObject(json);
+ if (object.has("duration"))
+ return object.getInt("duration") * 60 * 1000L;
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return 0;
+ }
+
+ @Override
+ public PointsWithLabelGraphSeries.Shape getShape() {
+ switch (eventType) {
+ case CareportalEvent.MBG:
+ return PointsWithLabelGraphSeries.Shape.MBG;
+ case CareportalEvent.BGCHECK:
+ return PointsWithLabelGraphSeries.Shape.BGCHECK;
+ case CareportalEvent.ANNOUNCEMENT:
+ return PointsWithLabelGraphSeries.Shape.ANNOUNCEMENT;
+ case CareportalEvent.OPENAPSOFFLINE:
+ return PointsWithLabelGraphSeries.Shape.OPENAPSOFFLINE;
+ case CareportalEvent.EXERCISE:
+ return PointsWithLabelGraphSeries.Shape.EXERCISE;
+ }
+ if (getDuration() > 0)
+ return PointsWithLabelGraphSeries.Shape.GENERALWITHDURATION;
+ return PointsWithLabelGraphSeries.Shape.GENERAL;
+ }
+
+ @Override
+ public float getSize() {
+ boolean isTablet = MainApp.sResources.getBoolean(R.bool.isTablet);
+ return isTablet ? 12 : 10;
+ }
+
+ @Override
+ public int getColor() {
+ if (eventType.equals(ANNOUNCEMENT))
+ return MainApp.sResources.getColor(R.color.notificationAnnouncement);
+ if (eventType.equals(MBG))
+ return Color.RED;
+ if (eventType.equals(BGCHECK))
+ return Color.RED;
+ if (eventType.equals(EXERCISE))
+ return Color.BLUE;
+ if (eventType.equals(OPENAPSOFFLINE))
+ return Color.GRAY;
+ return Color.GRAY;
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/db/DanaRHistoryRecord.java b/app/src/main/java/info/nightscout/androidaps/db/DanaRHistoryRecord.java
index 48b976548b..5c2e4f4985 100644
--- a/app/src/main/java/info/nightscout/androidaps/db/DanaRHistoryRecord.java
+++ b/app/src/main/java/info/nightscout/androidaps/db/DanaRHistoryRecord.java
@@ -8,38 +8,38 @@ import java.util.Date;
@DatabaseTable(tableName = DatabaseHelper.DATABASE_DANARHISTORY)
public class DanaRHistoryRecord {
- @DatabaseField(useGetSet = true)
- private String _id;
+ @DatabaseField
+ public String _id;
- @DatabaseField(useGetSet = true)
- private byte recordCode;
+ @DatabaseField
+ public byte recordCode;
- @DatabaseField(id = true, useGetSet = true)
- private String bytes;
+ @DatabaseField(id = true)
+ public String bytes;
- @DatabaseField(useGetSet = true)
- private long recordDate;
+ @DatabaseField
+ public long recordDate;
- @DatabaseField(useGetSet = true)
- private double recordValue;
+ @DatabaseField
+ public double recordValue;
- @DatabaseField(useGetSet = true)
- private String bolusType;
+ @DatabaseField
+ public String bolusType;
- @DatabaseField(useGetSet = true)
- private String stringRecordValue;
+ @DatabaseField
+ public String stringRecordValue;
- @DatabaseField(useGetSet = true)
- private int recordDuration;
+ @DatabaseField
+ public int recordDuration;
- @DatabaseField(useGetSet = true)
- private double recordDailyBasal;
+ @DatabaseField
+ public double recordDailyBasal;
- @DatabaseField(useGetSet = true)
- private double recordDailyBolus;
+ @DatabaseField
+ public double recordDailyBolus;
- @DatabaseField(useGetSet = true)
- private String recordAlarm;
+ @DatabaseField
+ public String recordAlarm;
public DanaRHistoryRecord() {
this.recordDate = 0;
@@ -50,74 +50,6 @@ public class DanaRHistoryRecord {
this._id = null;
}
- public void setRecordDate(Date dtRecordDate) {
- this.recordDate = dtRecordDate.getTime();
- }
-
- public long getRecordDate() {
- return this.recordDate;
- }
-
- public void setRecordDate(long dtRecordDate) {
- this.recordDate = dtRecordDate;
- }
-
- public double getRecordValue() {
- return this.recordValue;
- }
-
- public void setRecordValue(double dRecordValue) {
- this.recordValue = dRecordValue;
- }
-
- public String getBolusType() {
- return this.bolusType;
- }
-
- public void setBolusType(String strRecordType) {
- this.bolusType = strRecordType;
- }
-
- public String getStringRecordValue() {
- return this.stringRecordValue;
- }
-
- public void setStringRecordValue(String strRecordValue) {
- this.stringRecordValue = strRecordValue;
- }
-
- public byte getRecordCode() {
- return this.recordCode;
- }
-
- public void setRecordCode(byte cRecordCode) {
- this.recordCode = cRecordCode;
- }
-
- public int getRecordDuration() {
- return this.recordDuration;
- }
-
- public void setRecordDuration(int dRecordDuraion) {
- this.recordDuration = dRecordDuraion;
- }
-
- public double getRecordDailyBasal() {
- return this.recordDailyBasal;
- }
-
- public void setRecordDailyBasal(double dRecordDailyBasal) {
- this.recordDailyBasal = dRecordDailyBasal;
- }
-
- public double getRecordDailyBolus() {
- return this.recordDailyBolus;
- }
-
- public void setRecordDailyBolus(double dRecordDailyBolus) {
- this.recordDailyBolus = dRecordDailyBolus;
- }
-
public int getRecordLevel(double dExLow, double dLow, double dHigh, double dExHigh) {
if (this.recordValue < dExLow)
return 0;
@@ -128,34 +60,10 @@ public class DanaRHistoryRecord {
return this.recordValue < dExHigh ? 3 : 4;
}
- public String getRecordAlarm() {
- return this.recordAlarm;
- }
-
- public void setRecordAlarm(String strAlarm) {
- this.recordAlarm = strAlarm;
- }
-
- public String get_id() {
- return this._id;
- }
-
- public void set_id(String _id) {
- this._id = _id;
- }
-
public void setBytes(byte[] raw) {
this.bytes = bytesToHex(raw);
}
- public void setBytes(String bytes) {
- this.bytes = bytes;
- }
-
- public String getBytes() {
- return this.bytes;
- }
-
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java
index 17c01bd9ff..2b4cc87f20 100644
--- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java
+++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java
@@ -6,6 +6,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.Nullable;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
+import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
@@ -13,12 +14,13 @@ import com.j256.ormlite.stmt.Where;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
+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 java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -28,43 +30,80 @@ import java.util.concurrent.TimeUnit;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.androidaps.events.EventCareportalEventChange;
+import info.nightscout.androidaps.events.EventExtendedBolusChange;
+import info.nightscout.androidaps.events.EventNewBG;
+import info.nightscout.androidaps.events.EventProfileSwitchChange;
+import info.nightscout.androidaps.events.EventRefreshOverview;
+import info.nightscout.androidaps.events.EventReloadProfileSwitchData;
+import info.nightscout.androidaps.events.EventReloadTempBasalData;
+import info.nightscout.androidaps.events.EventReloadTreatmentData;
+import info.nightscout.androidaps.events.EventTempBasalChange;
+import info.nightscout.androidaps.events.EventTempTargetChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData;
+import info.nightscout.androidaps.plugins.PumpDanaR.activities.DanaRNSHistorySync;
+import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class);
public static final String DATABASE_NAME = "AndroidAPSDb";
public static final String DATABASE_BGREADINGS = "BgReadings";
- public static final String DATABASE_TEMPBASALS = "TempBasals";
+ public static final String DATABASE_TEMPORARYBASALS = "TemporaryBasals";
+ public static final String DATABASE_EXTENDEDBOLUSES = "ExtendedBoluses";
public static final String DATABASE_TEMPTARGETS = "TempTargets";
public static final String DATABASE_TREATMENTS = "Treatments";
public static final String DATABASE_DANARHISTORY = "DanaRHistory";
public static final String DATABASE_DBREQUESTS = "DBRequests";
+ public static final String DATABASE_CAREPORTALEVENTS = "CareportalEvents";
+ public static final String DATABASE_PROFILESWITCHES = "ProfileSwitches";
- private static final int DATABASE_VERSION = 6;
+ private static final int DATABASE_VERSION = 8;
- private static Long latestTreatmentChange = null;
+ private static Long earliestDataChange = null;
- private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
- private static ScheduledFuture> scheduledPost = null;
+ private static final ScheduledExecutorService bgWorker = Executors.newSingleThreadScheduledExecutor();
+ private static ScheduledFuture> scheduledBgPost = null;
+
+ private static final ScheduledExecutorService treatmentsWorker = Executors.newSingleThreadScheduledExecutor();
+ private static ScheduledFuture> scheduledTratmentPost = null;
+
+ private static final ScheduledExecutorService tempBasalsWorker = Executors.newSingleThreadScheduledExecutor();
+ private static ScheduledFuture> scheduledTemBasalsPost = null;
+
+ private static final ScheduledExecutorService tempTargetWorker = Executors.newSingleThreadScheduledExecutor();
+ private static ScheduledFuture> scheduledTemTargetPost = null;
+
+ private static final ScheduledExecutorService extendedBolusWorker = Executors.newSingleThreadScheduledExecutor();
+ private static ScheduledFuture> scheduledExtendedBolusPost = null;
+
+ private static final ScheduledExecutorService careportalEventWorker = Executors.newSingleThreadScheduledExecutor();
+ private static ScheduledFuture> scheduledCareportalEventPost = null;
+
+ private static final ScheduledExecutorService profileSwitchEventWorker = Executors.newSingleThreadScheduledExecutor();
+ private static ScheduledFuture> scheduledProfileSwitchEventPost = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
onCreate(getWritableDatabase(), getConnectionSource());
+ //onUpgrade(getWritableDatabase(), getConnectionSource(), 1,1);
}
-
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
log.info("onCreate");
- TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
TableUtils.createTableIfNotExists(connectionSource, DbRequest.class);
+ TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
+ TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
+ TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
+ TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
} catch (SQLException e) {
log.error("Can't create database", e);
throw new RuntimeException(e);
@@ -74,14 +113,23 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
- log.info(DatabaseHelper.class.getName(), "onUpgrade");
- TableUtils.dropTable(connectionSource, TempBasal.class, true);
- TableUtils.dropTable(connectionSource, TempTarget.class, true);
- TableUtils.dropTable(connectionSource, Treatment.class, true);
- TableUtils.dropTable(connectionSource, BgReading.class, true);
- TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
- TableUtils.dropTable(connectionSource, DbRequest.class, true);
- onCreate(database, connectionSource);
+ if (oldVersion == 7 && newVersion == 8) {
+ log.debug("Upgrading database from v7 to v8");
+ TableUtils.dropTable(connectionSource, Treatment.class, true);
+ TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
+ } else {
+ log.info(DatabaseHelper.class.getName(), "onUpgrade");
+ TableUtils.dropTable(connectionSource, TempTarget.class, true);
+ TableUtils.dropTable(connectionSource, Treatment.class, true);
+ TableUtils.dropTable(connectionSource, BgReading.class, true);
+ TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
+ TableUtils.dropTable(connectionSource, DbRequest.class, true);
+ TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
+ TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
+ TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
+ TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
+ onCreate(database, connectionSource);
+ }
} catch (SQLException e) {
log.error("Can't drop databases", e);
throw new RuntimeException(e);
@@ -99,53 +147,96 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void cleanUpDatabases() {
// TODO: call it somewhere
log.debug("Before BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_BGREADINGS));
- getWritableDatabase().delete(DATABASE_BGREADINGS, "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
+ getWritableDatabase().delete(DATABASE_BGREADINGS, "date" + " < '" + (System.currentTimeMillis() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
log.debug("After BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_BGREADINGS));
- log.debug("Before TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPBASALS));
- getWritableDatabase().delete(DATABASE_TEMPBASALS, "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
- log.debug("After TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPBASALS));
-
log.debug("Before TempTargets size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPTARGETS));
- getWritableDatabase().delete(DATABASE_TEMPTARGETS, "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
+ getWritableDatabase().delete(DATABASE_TEMPTARGETS, "date" + " < '" + (System.currentTimeMillis() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
log.debug("After TempTargets size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPTARGETS));
log.debug("Before Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TREATMENTS));
- getWritableDatabase().delete(DATABASE_TREATMENTS, "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
+ getWritableDatabase().delete(DATABASE_TREATMENTS, "date" + " < '" + (System.currentTimeMillis() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
log.debug("After Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TREATMENTS));
log.debug("Before History size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_DANARHISTORY));
- getWritableDatabase().delete(DATABASE_DANARHISTORY, "recordDate" + " < '" + (new Date().getTime() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
+ getWritableDatabase().delete(DATABASE_DANARHISTORY, "recordDate" + " < '" + (System.currentTimeMillis() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
log.debug("After History size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_DANARHISTORY));
+
+ log.debug("Before TemporaryBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPORARYBASALS));
+ getWritableDatabase().delete(DATABASE_TEMPORARYBASALS, "recordDate" + " < '" + (System.currentTimeMillis() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
+ log.debug("After TemporaryBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPORARYBASALS));
+
+ log.debug("Before ExtendedBoluses size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_EXTENDEDBOLUSES));
+ getWritableDatabase().delete(DATABASE_EXTENDEDBOLUSES, "recordDate" + " < '" + (System.currentTimeMillis() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
+ log.debug("After ExtendedBoluses size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_EXTENDEDBOLUSES));
+
+ log.debug("Before CareportalEvent size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_CAREPORTALEVENTS));
+ getWritableDatabase().delete(DATABASE_CAREPORTALEVENTS, "recordDate" + " < '" + (System.currentTimeMillis() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
+ log.debug("After CareportalEvent size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_CAREPORTALEVENTS));
+
+ log.debug("Before ProfileSwitch size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_PROFILESWITCHES));
+ getWritableDatabase().delete(DATABASE_PROFILESWITCHES, "recordDate" + " < '" + (System.currentTimeMillis() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
+ log.debug("After ProfileSwitch size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_PROFILESWITCHES));
}
+ public long size(String database) {
+ return DatabaseUtils.queryNumEntries(getReadableDatabase(), database);
+ }
+
+ // --------------------- DB resets ---------------------
+
public void resetDatabases() {
try {
- TableUtils.dropTable(connectionSource, TempBasal.class, true);
TableUtils.dropTable(connectionSource, TempTarget.class, true);
TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.dropTable(connectionSource, BgReading.class, true);
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
- //DbRequests can be cleared from NSClient fragment
- TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
+ TableUtils.dropTable(connectionSource, DbRequest.class, true);
+ TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
+ TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
+ TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
+ TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
- latestTreatmentChange = 0L;
+ TableUtils.createTableIfNotExists(connectionSource, DbRequest.class);
+ TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
+ TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
+ TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
+ TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
+ updateEarliestDataChange(0);
} catch (SQLException e) {
e.printStackTrace();
}
+ VirtualPumpPlugin.setFakingStatus(true);
+ scheduleBgChange(); // trigger refresh
+ scheduleTemporaryBasalChange();
+ scheduleTreatmentChange();
+ scheduleExtendedBolusChange();
+ scheduleTemporaryTargetChange();
+ scheduleCareportalEventChange();
+ scheduleProfileSwitchChange();
+ new java.util.Timer().schedule(
+ new java.util.TimerTask() {
+ @Override
+ public void run() {
+ MainApp.bus().post(new EventRefreshOverview("resetDatabases"));
+ }
+ },
+ 3000
+ );
}
public void resetTreatments() {
try {
TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
- latestTreatmentChange = 0L;
+ updateEarliestDataChange(0);
} catch (SQLException e) {
e.printStackTrace();
}
+ scheduleTreatmentChange();
}
public void resetTempTargets() {
@@ -155,13 +246,55 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
} catch (SQLException e) {
e.printStackTrace();
}
+ scheduleTemporaryTargetChange();
}
- public Dao getDaoTempBasals() throws SQLException {
- return getDao(TempBasal.class);
+ public void resetTemporaryBasals() {
+ try {
+ TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
+ TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
+ updateEarliestDataChange(0);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ VirtualPumpPlugin.setFakingStatus(false);
+ scheduleTemporaryBasalChange();
}
- public Dao getDaoTempTargets() throws SQLException {
+ public void resetExtededBoluses() {
+ try {
+ TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
+ TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
+ updateEarliestDataChange(0);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleExtendedBolusChange();
+ }
+
+ public void resetCareportalEvents() {
+ try {
+ TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
+ TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleCareportalEventChange();
+ }
+
+ public void resetProfileSwitch() {
+ try {
+ TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
+ TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleProfileSwitchChange();
+ }
+
+ // ------------------ getDao -------------------------------------------
+
+ private Dao getDaoTempTargets() throws SQLException {
return getDao(TempTarget.class);
}
@@ -169,30 +302,130 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return getDao(Treatment.class);
}
- public Dao getDaoBgReadings() throws SQLException {
+ private Dao getDaoBgReadings() throws SQLException {
return getDao(BgReading.class);
}
- public Dao getDaoDanaRHistory() throws SQLException {
+ private Dao getDaoDanaRHistory() throws SQLException {
return getDao(DanaRHistoryRecord.class);
}
- public Dao getDaoDbRequest() throws SQLException {
+ private Dao getDaoDbRequest() throws SQLException {
return getDao(DbRequest.class);
}
- public long size(String database) {
- return DatabaseUtils.queryNumEntries(getReadableDatabase(), database);
+ private Dao getDaoTemporaryBasal() throws SQLException {
+ return getDao(TemporaryBasal.class);
}
+ private Dao getDaoExtendedBolus() throws SQLException {
+ return getDao(ExtendedBolus.class);
+ }
+
+ private Dao getDaoCareportalEvents() throws SQLException {
+ return getDao(CareportalEvent.class);
+ }
+
+ private Dao getDaoProfileSwitch() throws SQLException {
+ return getDao(ProfileSwitch.class);
+ }
+
+ public long roundDateToSec(long date) {
+ return date - date % 1000;
+ }
+ // ------------------- BgReading handling -----------------------
+
+ public void createIfNotExists(BgReading bgReading, String from) {
+ try {
+ bgReading.date = roundDateToSec(bgReading.date);
+ BgReading old = getDaoBgReadings().queryForId(bgReading.date);
+ if (old == null) {
+ getDaoBgReadings().create(bgReading);
+ log.debug("BG: New record from: " + from + " " + bgReading.toString());
+ scheduleBgChange();
+ return;
+ }
+ if (!old.isEqual(bgReading)) {
+ old.copyFrom(bgReading);
+ getDaoBgReadings().update(old);
+ log.debug("BG: Updating record from: " + from + " " + old.toString());
+ scheduleBgChange();
+ return;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void scheduleBgChange() {
+ class PostRunnable implements Runnable {
+ public void run() {
+ log.debug("Firing EventNewBg");
+ MainApp.bus().post(new EventNewBG());
+ scheduledBgPost = null;
+ }
+ }
+ // prepare task for execution in 1 sec
+ // cancel waiting task to prevent sending multiple posts
+ if (scheduledBgPost != null)
+ scheduledBgPost.cancel(false);
+ Runnable task = new PostRunnable();
+ final int sec = 1;
+ scheduledBgPost = bgWorker.schedule(task, sec, TimeUnit.SECONDS);
+
+ }
+
+ /*
+ * Return last BgReading from database or null if db is empty
+ */
+ @Nullable
+ public static BgReading lastBg() {
+ List bgList = null;
+
+ try {
+ Dao daoBgReadings = MainApp.getDbHelper().getDaoBgReadings();
+ QueryBuilder queryBuilder = daoBgReadings.queryBuilder();
+ queryBuilder.orderBy("date", false);
+ queryBuilder.limit(1L);
+ queryBuilder.where().gt("value", 38);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ bgList = daoBgReadings.query(preparedQuery);
+
+ } catch (SQLException e) {
+ log.debug(e.getMessage(), e);
+ }
+ if (bgList != null && bgList.size() > 0)
+ return bgList.get(0);
+ else
+ return null;
+ }
+
+ /*
+ * Return bg reading if not old ( <9 min )
+ * or null if older
+ */
+ @Nullable
+ public static BgReading actualBg() {
+ BgReading lastBg = lastBg();
+
+ if (lastBg == null)
+ return null;
+
+ if (lastBg.date > System.currentTimeMillis() - 9 * 60 * 1000)
+ return lastBg;
+
+ return null;
+ }
+
+
public List getBgreadingsDataFromTime(long mills, boolean ascending) {
try {
Dao daoBgreadings = getDaoBgReadings();
List bgReadings;
QueryBuilder queryBuilder = daoBgreadings.queryBuilder();
- queryBuilder.orderBy("timeIndex", ascending);
+ queryBuilder.orderBy("date", ascending);
Where where = queryBuilder.where();
- where.ge("timeIndex", mills).and().gt("value", 38);
+ where.ge("date", mills).and().gt("value", 38);
PreparedQuery preparedQuery = queryBuilder.prepare();
bgReadings = daoBgreadings.query(preparedQuery);
return bgReadings;
@@ -202,7 +435,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList();
}
- // DbRequests handling
+ // ------------- DbRequests handling -------------------
public void create(DbRequest dbr) {
try {
@@ -258,83 +491,131 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
}
}
- // TREATMENT HANDLING
-
- public boolean affectingIobCob(Treatment t) {
- Treatment existing = findTreatmentByTimeIndex(t.timeIndex);
- if (existing == null)
- return true;
- if (existing.insulin == t.insulin && existing.carbs == t.carbs)
- return false;
- return true;
+ public CloseableIterator getDbRequestInterator() {
+ try {
+ return getDaoDbRequest().closeableIterator();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return null;
+ }
}
- public int update(Treatment treatment) {
- int updated = 0;
+ // -------------------- TREATMENT HANDLING -------------------
+
+ // return true if new record is created
+ public boolean createOrUpdate(Treatment treatment) {
try {
- boolean historyChange = affectingIobCob(treatment);
- updated = getDaoTreatments().update(treatment);
- if (historyChange)
- latestTreatmentChange = treatment.getTimeIndex();
+ Treatment old;
+ treatment.date = roundDateToSec(treatment.date);
+
+ if (treatment.source == Source.PUMP) {
+ // check for changed from pump change in NS
+ QueryBuilder queryBuilder = getDaoTreatments().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("pumpId", treatment.pumpId);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoTreatments().query(preparedQuery);
+ if (trList.size() > 0) {
+ // do nothing, pump history record cannot be changed
+ return false;
+ }
+ getDaoTreatments().create(treatment);
+ log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
+ updateEarliestDataChange(treatment.date);
+ scheduleTreatmentChange();
+ return true;
+ }
+ if (treatment.source == Source.NIGHTSCOUT) {
+ old = getDaoTreatments().queryForId(treatment.date);
+ if (old != null) {
+ if (!old.isEqual(treatment)) {
+ boolean historyChange = old.isDataChanging(treatment);
+ long oldDate = old.date;
+ getDaoTreatments().delete(old); // need to delete/create because date may change too
+ old.copyFrom(treatment);
+ getDaoTreatments().create(old);
+ log.debug("TREATMENT: Updating record by date from: " + Source.getString(treatment.source) + " " + old.toString());
+ if (historyChange) {
+ updateEarliestDataChange(oldDate);
+ updateEarliestDataChange(old.date);
+ }
+ scheduleTreatmentChange();
+ return true;
+ }
+ return false;
+ }
+ // find by NS _id
+ if (treatment._id != null) {
+ QueryBuilder queryBuilder = getDaoTreatments().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", treatment._id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoTreatments().query(preparedQuery);
+ if (trList.size() > 0) {
+ old = trList.get(0);
+ if (!old.isEqual(treatment)) {
+ boolean historyChange = old.isDataChanging(treatment);
+ long oldDate = old.date;
+ getDaoTreatments().delete(old); // need to delete/create because date may change too
+ old.copyFrom(treatment);
+ getDaoTreatments().create(old);
+ log.debug("TREATMENT: Updating record by _id from: " + Source.getString(treatment.source) + " " + old.toString());
+ if (historyChange) {
+ updateEarliestDataChange(oldDate);
+ updateEarliestDataChange(old.date);
+ }
+ scheduleTreatmentChange();
+ return true;
+ }
+ }
+ }
+ getDaoTreatments().create(treatment);
+ log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
+ updateEarliestDataChange(treatment.date);
+ scheduleTreatmentChange();
+ return true;
+ }
+ if (treatment.source == Source.USER) {
+ getDaoTreatments().create(treatment);
+ log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
+ updateEarliestDataChange(treatment.date);
+ scheduleTreatmentChange();
+ return true;
+ }
} catch (SQLException e) {
e.printStackTrace();
}
- scheduleTreatmentChange();
- return updated;
- }
-
- public Dao.CreateOrUpdateStatus createOrUpdate(Treatment treatment) {
- Dao.CreateOrUpdateStatus status = null;
- try {
- boolean historyChange = affectingIobCob(treatment);
- status = getDaoTreatments().createOrUpdate(treatment);
- if (historyChange)
- latestTreatmentChange = treatment.getTimeIndex();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- scheduleTreatmentChange();
- return status;
- }
-
- public void create(Treatment treatment) {
- try {
- getDaoTreatments().create(treatment);
- latestTreatmentChange = treatment.getTimeIndex();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- scheduleTreatmentChange();
+ return false;
}
public void delete(Treatment treatment) {
try {
getDaoTreatments().delete(treatment);
- latestTreatmentChange = treatment.getTimeIndex();
+ updateEarliestDataChange(treatment.date);
} catch (SQLException e) {
e.printStackTrace();
}
scheduleTreatmentChange();
}
- public int delete(String _id) {
- Treatment stored = findTreatmentById(_id);
- int removed = 0;
- if (stored != null) {
- log.debug("REMOVE: Existing treatment (removing): " + _id);
- try {
- removed = getDaoTreatments().delete(stored);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if (Config.logIncommingData)
- log.debug("Records removed: " + removed);
- latestTreatmentChange = stored.getTimeIndex();
- scheduleTreatmentChange();
- } else {
- log.debug("REMOVE: Not stored treatment (ignoring): " + _id);
+ public void update(Treatment treatment) {
+ try {
+ getDaoTreatments().update(treatment);
+ updateEarliestDataChange(treatment.date);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleTreatmentChange();
+ }
+
+ public void deleteTreatmentById(String _id) {
+ Treatment stored = findTreatmentById(_id);
+ if (stored != null) {
+ log.debug("TREATMENT: Removing Treatment record from database: " + stored.toString());
+ delete(stored);
+ updateEarliestDataChange(stored.date);
+ scheduleTreatmentChange();
}
- return removed;
}
@Nullable
@@ -360,47 +641,35 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return null;
}
- @Nullable
- public Treatment findTreatmentByTimeIndex(Long timeIndex) {
- try {
- QueryBuilder qb = null;
- Dao daoTreatments = getDaoTreatments();
- QueryBuilder queryBuilder = daoTreatments.queryBuilder();
- Where where = queryBuilder.where();
- where.eq("timeIndex", timeIndex);
- queryBuilder.limit(10L);
- PreparedQuery preparedQuery = queryBuilder.prepare();
- List trList = daoTreatments.query(preparedQuery);
- if (trList.size() != 1) {
- log.debug("Treatment findTreatmentByTimeIndex query size: " + trList.size());
- return null;
- } else {
- log.debug("Treatment findTreatmentByTimeIndex found: " + trList.get(0).log());
- return trList.get(0);
- }
- } catch (SQLException e) {
- e.printStackTrace();
+ private void updateEarliestDataChange(long newDate) {
+ if (earliestDataChange == null) {
+ earliestDataChange = newDate;
+ return;
+ }
+ if (newDate < earliestDataChange) {
+ earliestDataChange = newDate;
}
- return null;
}
- static public void scheduleTreatmentChange() {
+ private static void scheduleTreatmentChange() {
class PostRunnable implements Runnable {
public void run() {
+ log.debug("Firing EventTreatmentChange");
+ MainApp.bus().post(new EventReloadTreatmentData());
MainApp.bus().post(new EventTreatmentChange());
- if (latestTreatmentChange != null)
- MainApp.bus().post(new EventNewHistoryData(latestTreatmentChange));
- latestTreatmentChange = null;
- scheduledPost = null;
+ if (earliestDataChange != null)
+ MainApp.bus().post(new EventNewHistoryData(earliestDataChange));
+ earliestDataChange = null;
+ scheduledTratmentPost = null;
}
}
- // prepare task for execution in 5 sec
+ // prepare task for execution in 1 sec
// cancel waiting task to prevent sending multiple posts
- if (scheduledPost != null)
- scheduledPost.cancel(false);
+ if (scheduledTratmentPost != null)
+ scheduledTratmentPost.cancel(false);
Runnable task = new PostRunnable();
- final int sec = 5;
- scheduledPost = worker.schedule(task, sec, TimeUnit.SECONDS);
+ final int sec = 1;
+ scheduledTratmentPost = treatmentsWorker.schedule(task, sec, TimeUnit.SECONDS);
}
@@ -409,9 +678,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
Dao daoTreatments = getDaoTreatments();
List treatments;
QueryBuilder queryBuilder = daoTreatments.queryBuilder();
- queryBuilder.orderBy("timeIndex", ascending);
+ queryBuilder.orderBy("date", ascending);
Where where = queryBuilder.where();
- where.ge("timeIndex", mills);
+ where.ge("date", mills);
PreparedQuery preparedQuery = queryBuilder.prepare();
treatments = daoTreatments.query(preparedQuery);
return treatments;
@@ -421,24 +690,45 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList();
}
- public void delete(TempBasal tempBasal) {
+ public void createTreatmentFromJsonIfNotExists(JSONObject trJson) {
try {
- getDaoTempBasals().delete(tempBasal);
- latestTreatmentChange = tempBasal.getTimeIndex();
- } catch (SQLException e) {
+ Treatment treatment = new Treatment();
+ treatment.source = Source.NIGHTSCOUT;
+ treatment.date = roundDateToSec(trJson.getLong("mills"));
+ treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
+ treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
+ treatment.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0;
+ treatment._id = trJson.getString("_id");
+ if (trJson.has("isSMB"))
+ treatment.isSMB = trJson.getBoolean("isSMB");
+ if (trJson.has("eventType")) {
+ treatment.mealBolus = !trJson.get("eventType").equals("Correction Bolus");
+ double carbs = treatment.carbs;
+ if (trJson.has("boluscalc")) {
+ JSONObject boluscalc = trJson.getJSONObject("boluscalc");
+ if (boluscalc.has("carbs")) {
+ carbs = Math.max(boluscalc.getDouble("carbs"), carbs);
+ }
+ }
+ if (carbs <= 0)
+ treatment.mealBolus = false;
+ }
+ createOrUpdate(treatment);
+ } catch (JSONException e) {
e.printStackTrace();
}
- scheduleTreatmentChange();
}
+ // ---------------- TempTargets handling ---------------
+
public List getTemptargetsDataFromTime(long mills, boolean ascending) {
try {
Dao daoTempTargets = getDaoTempTargets();
List tempTargets;
QueryBuilder queryBuilder = daoTempTargets.queryBuilder();
- queryBuilder.orderBy("timeIndex", ascending);
+ queryBuilder.orderBy("date", ascending);
Where where = queryBuilder.where();
- where.ge("timeIndex", mills);
+ where.ge("date", mills);
PreparedQuery preparedQuery = queryBuilder.prepare();
tempTargets = daoTempTargets.query(preparedQuery);
return tempTargets;
@@ -448,22 +738,938 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList();
}
-
- public List getTempbasalsDataFromTime(long mills, boolean ascending, boolean isExtended) {
+ public boolean createOrUpdate(TempTarget tempTarget) {
try {
- Dao daoTempbasals = getDaoTempBasals();
- List tempbasals;
- QueryBuilder queryBuilder = daoTempbasals.queryBuilder();
- queryBuilder.orderBy("timeIndex", ascending);
+ TempTarget old;
+ tempTarget.date = roundDateToSec(tempTarget.date);
+
+ if (tempTarget.source == Source.NIGHTSCOUT) {
+ old = getDaoTempTargets().queryForId(tempTarget.date);
+ if (old != null) {
+ if (!old.isEqual(tempTarget)) {
+ getDaoTempTargets().delete(old); // need to delete/create because date may change too
+ old.copyFrom(tempTarget);
+ getDaoTempTargets().create(old);
+ log.debug("TEMPTARGET: Updating record by date from: " + Source.getString(tempTarget.source) + " " + old.toString());
+ scheduleTemporaryTargetChange();
+ return true;
+ }
+ return false;
+ }
+ // find by NS _id
+ if (tempTarget._id != null) {
+ QueryBuilder queryBuilder = getDaoTempTargets().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", tempTarget._id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoTempTargets().query(preparedQuery);
+ if (trList.size() > 0) {
+ old = trList.get(0);
+ if (!old.isEqual(tempTarget)) {
+ getDaoTempTargets().delete(old); // need to delete/create because date may change too
+ old.copyFrom(tempTarget);
+ getDaoTempTargets().create(old);
+ log.debug("TEMPTARGET: Updating record by _id from: " + Source.getString(tempTarget.source) + " " + old.toString());
+ scheduleTemporaryTargetChange();
+ return true;
+ }
+ }
+ }
+ getDaoTempTargets().create(tempTarget);
+ log.debug("TEMPTARGET: New record from: " + Source.getString(tempTarget.source) + " " + tempTarget.toString());
+ scheduleTemporaryTargetChange();
+ return true;
+ }
+ if (tempTarget.source == Source.USER) {
+ getDaoTempTargets().create(tempTarget);
+ log.debug("TEMPTARGET: New record from: " + Source.getString(tempTarget.source) + " " + tempTarget.toString());
+ scheduleTemporaryTargetChange();
+ return true;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ public void delete(TempTarget tempTarget) {
+ try {
+ getDaoTempTargets().delete(tempTarget);
+ scheduleTemporaryTargetChange();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void scheduleTemporaryTargetChange() {
+ class PostRunnable implements Runnable {
+ public void run() {
+ log.debug("Firing EventTempTargetChange");
+ MainApp.bus().post(new EventTempTargetChange());
+ scheduledTemTargetPost = null;
+ }
+ }
+ // prepare task for execution in 1 sec
+ // cancel waiting task to prevent sending multiple posts
+ if (scheduledTemTargetPost != null)
+ scheduledTemTargetPost.cancel(false);
+ Runnable task = new PostRunnable();
+ final int sec = 1;
+ scheduledTemTargetPost = tempTargetWorker.schedule(task, sec, TimeUnit.SECONDS);
+
+ }
+
+ /*
+ {
+ "_id": "58795998aa86647ba4d68ce7",
+ "enteredBy": "",
+ "eventType": "Temporary Target",
+ "reason": "Eating Soon",
+ "targetTop": 80,
+ "targetBottom": 80,
+ "duration": 120,
+ "created_at": "2017-01-13T22:50:00.782Z",
+ "carbs": null,
+ "insulin": null
+}
+ */
+
+ public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
+ try {
+ String units = MainApp.getConfigBuilder().getProfileUnits();
+ TempTarget tempTarget = new TempTarget();
+ tempTarget.date = trJson.getLong("mills");
+ tempTarget.durationInMinutes = trJson.getInt("duration");
+ tempTarget.low = Profile.toMgdl(trJson.getDouble("targetBottom"), units);
+ tempTarget.high = Profile.toMgdl(trJson.getDouble("targetTop"), units);
+ tempTarget.reason = trJson.getString("reason");
+ tempTarget._id = trJson.getString("_id");
+ tempTarget.source = Source.NIGHTSCOUT;
+ createOrUpdate(tempTarget);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void deleteTempTargetById(String _id) {
+ TempTarget stored = findTempTargetById(_id);
+ if (stored != null) {
+ log.debug("TEMPTARGET: Removing TempTarget record from database: " + stored.toString());
+ delete(stored);
+ scheduleTemporaryTargetChange();
+ }
+ }
+
+ public TempTarget findTempTargetById(String _id) {
+ try {
+ QueryBuilder queryBuilder = getDaoTempTargets().queryBuilder();
Where where = queryBuilder.where();
- where.ge("timeIndex", mills).and().eq("isExtended", isExtended);
- PreparedQuery preparedQuery = queryBuilder.prepare();
- tempbasals = daoTempbasals.query(preparedQuery);
+ where.eq("_id", _id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoTempTargets().query(preparedQuery);
+
+ if (list.size() == 1) {
+ return list.get(0);
+ } else {
+ return null;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ // ----------------- DanaRHistory handling --------------------
+
+ public void createOrUpdate(DanaRHistoryRecord record) {
+ try {
+ getDaoDanaRHistory().createOrUpdate(record);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public List getDanaRHistoryRecordsByType(byte type) {
+ List historyList;
+ try {
+ QueryBuilder queryBuilder = getDaoDanaRHistory().queryBuilder();
+ queryBuilder.orderBy("recordDate", false);
+ Where where = queryBuilder.where();
+ where.eq("recordCode", type);
+ queryBuilder.limit(200L);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ historyList = getDaoDanaRHistory().query(preparedQuery);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ historyList = new ArrayList<>();
+ }
+ return historyList;
+ }
+
+ public void updateDanaRHistoryRecordId(JSONObject trJson) {
+ try {
+ QueryBuilder queryBuilder = getDaoDanaRHistory().queryBuilder();
+ Where where = queryBuilder.where();
+ where.ge("bytes", trJson.get(DanaRNSHistorySync.DANARSIGNATURE));
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoDanaRHistory().query(preparedQuery);
+ if (list.size() == 0) {
+ // Record does not exists. Ignore
+ } else if (list.size() == 1) {
+ DanaRHistoryRecord record = list.get(0);
+ if (record._id == null || !record._id.equals(trJson.getString("_id"))) {
+ if (Config.logIncommingData)
+ log.debug("Updating _id in DanaR history database: " + trJson.getString("_id"));
+ record._id = trJson.getString("_id");
+ getDaoDanaRHistory().update(record);
+ } else {
+ // already set
+ }
+ }
+ } catch (SQLException | JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ // ------------ TemporaryBasal handling ---------------
+
+ //return true if new record was created
+ public boolean createOrUpdate(TemporaryBasal tempBasal) {
+ try {
+ TemporaryBasal old;
+ tempBasal.date = roundDateToSec(tempBasal.date);
+
+ if (tempBasal.source == Source.PUMP) {
+ // check for changed from pump change in NS
+ QueryBuilder queryBuilder = getDaoTemporaryBasal().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("pumpId", tempBasal.pumpId);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoTemporaryBasal().query(preparedQuery);
+ if (trList.size() > 0) {
+ // do nothing, pump history record cannot be changed
+ log.debug("TEMPBASAL: Already exists from: " + Source.getString(tempBasal.source) + " " + tempBasal.toString());
+ return false;
+ }
+ getDaoTemporaryBasal().create(tempBasal);
+ log.debug("TEMPBASAL: New record from: " + Source.getString(tempBasal.source) + " " + tempBasal.toString());
+ updateEarliestDataChange(tempBasal.date);
+ scheduleTemporaryBasalChange();
+ return true;
+ }
+ if (tempBasal.source == Source.NIGHTSCOUT) {
+ old = getDaoTemporaryBasal().queryForId(tempBasal.date);
+ if (old != null) {
+ if (!old.isAbsolute && tempBasal.isAbsolute) { // converted to absolute by "ns_sync_use_absolute"
+ // so far ignore, do not convert back because it may not be accurate
+ return false;
+ }
+ if (!old.isEqual(tempBasal)) {
+ long oldDate = old.date;
+ getDaoTemporaryBasal().delete(old); // need to delete/create because date may change too
+ old.copyFrom(tempBasal);
+ getDaoTemporaryBasal().create(old);
+ log.debug("TEMPBASAL: Updating record by date from: " + Source.getString(tempBasal.source) + " " + old.toString());
+ updateEarliestDataChange(oldDate);
+ updateEarliestDataChange(old.date);
+ scheduleTemporaryBasalChange();
+ return true;
+ }
+ return false;
+ }
+ // find by NS _id
+ if (tempBasal._id != null) {
+ QueryBuilder queryBuilder = getDaoTemporaryBasal().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", tempBasal._id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoTemporaryBasal().query(preparedQuery);
+ if (trList.size() > 0) {
+ old = trList.get(0);
+ if (!old.isEqual(tempBasal)) {
+ long oldDate = old.date;
+ getDaoTemporaryBasal().delete(old); // need to delete/create because date may change too
+ old.copyFrom(tempBasal);
+ getDaoTemporaryBasal().create(old);
+ log.debug("TEMPBASAL: Updating record by _id from: " + Source.getString(tempBasal.source) + " " + old.toString());
+ updateEarliestDataChange(oldDate);
+ updateEarliestDataChange(old.date);
+ scheduleTemporaryBasalChange();
+ return true;
+ }
+ }
+ }
+ getDaoTemporaryBasal().create(tempBasal);
+ log.debug("TEMPBASAL: New record from: " + Source.getString(tempBasal.source) + " " + tempBasal.toString());
+ updateEarliestDataChange(tempBasal.date);
+ scheduleTemporaryBasalChange();
+ return true;
+ }
+ if (tempBasal.source == Source.USER) {
+ getDaoTemporaryBasal().create(tempBasal);
+ log.debug("TEMPBASAL: New record from: " + Source.getString(tempBasal.source) + " " + tempBasal.toString());
+ updateEarliestDataChange(tempBasal.date);
+ scheduleTemporaryBasalChange();
+ return true;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ public void delete(TemporaryBasal tempBasal) {
+ try {
+ getDaoTemporaryBasal().delete(tempBasal);
+ updateEarliestDataChange(tempBasal.date);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleTemporaryBasalChange();
+ }
+
+ public List getTemporaryBasalsDataFromTime(long mills, boolean ascending) {
+ try {
+ List tempbasals;
+ QueryBuilder queryBuilder = getDaoTemporaryBasal().queryBuilder();
+ queryBuilder.orderBy("date", ascending);
+ Where where = queryBuilder.where();
+ where.ge("date", mills);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ tempbasals = getDaoTemporaryBasal().query(preparedQuery);
return tempbasals;
} catch (SQLException e) {
e.printStackTrace();
}
- return new ArrayList();
+ return new ArrayList();
}
+ private static void scheduleTemporaryBasalChange() {
+ class PostRunnable implements Runnable {
+ public void run() {
+ log.debug("Firing EventTempBasalChange");
+ MainApp.bus().post(new EventReloadTempBasalData());
+ MainApp.bus().post(new EventTempBasalChange());
+ if (earliestDataChange != null)
+ MainApp.bus().post(new EventNewHistoryData(earliestDataChange));
+ earliestDataChange = null;
+ scheduledTemBasalsPost = null;
+ }
+ }
+ // prepare task for execution in 1 sec
+ // cancel waiting task to prevent sending multiple posts
+ if (scheduledTemBasalsPost != null)
+ scheduledTemBasalsPost.cancel(false);
+ Runnable task = new PostRunnable();
+ final int sec = 1;
+ scheduledTemBasalsPost = tempBasalsWorker.schedule(task, sec, TimeUnit.SECONDS);
+
+ }
+
+ /*
+ {
+ "_id": "59232e1ddd032d04218dab00",
+ "eventType": "Temp Basal",
+ "duration": 60,
+ "percent": -50,
+ "created_at": "2017-05-22T18:29:57Z",
+ "enteredBy": "AndroidAPS",
+ "notes": "Basal Temp Start 50% 60.0 min",
+ "NSCLIENT_ID": 1495477797863,
+ "mills": 1495477797000,
+ "mgdl": 194.5,
+ "endmills": 1495481397000
+ }
+ */
+
+ public void createTempBasalFromJsonIfNotExists(JSONObject trJson) {
+ try {
+ if (trJson.has("originalExtendedAmount")) { // extended bolus uploaded as temp basal
+ ExtendedBolus extendedBolus = new ExtendedBolus();
+ extendedBolus.source = Source.NIGHTSCOUT;
+ extendedBolus.date = trJson.getLong("mills");
+ extendedBolus.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0;
+ extendedBolus.durationInMinutes = trJson.getInt("duration");
+ extendedBolus.insulin = trJson.getDouble("originalExtendedAmount");
+ extendedBolus._id = trJson.getString("_id");
+ if (!VirtualPumpPlugin.getFakingStatus()) {
+ VirtualPumpPlugin.setFakingStatus(true);
+ updateEarliestDataChange(0);
+ scheduleTemporaryBasalChange();
+ }
+ createOrUpdate(extendedBolus);
+ } else if (trJson.has("isFakedTempBasal")) { // extended bolus end uploaded as temp basal end
+ ExtendedBolus extendedBolus = new ExtendedBolus();
+ extendedBolus.source = Source.NIGHTSCOUT;
+ extendedBolus.date = trJson.getLong("mills");
+ extendedBolus.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0;
+ extendedBolus.durationInMinutes = 0;
+ extendedBolus.insulin = 0;
+ extendedBolus._id = trJson.getString("_id");
+ if (!VirtualPumpPlugin.getFakingStatus()) {
+ VirtualPumpPlugin.setFakingStatus(true);
+ updateEarliestDataChange(0);
+ scheduleTemporaryBasalChange();
+ }
+ createOrUpdate(extendedBolus);
+ } else {
+ TemporaryBasal tempBasal = new TemporaryBasal();
+ tempBasal.date = trJson.getLong("mills");
+ tempBasal.source = Source.NIGHTSCOUT;
+ tempBasal.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0;
+ if (trJson.has("duration")) {
+ tempBasal.durationInMinutes = trJson.getInt("duration");
+ }
+ if (trJson.has("percent")) {
+ tempBasal.percentRate = trJson.getInt("percent") + 100;
+ tempBasal.isAbsolute = false;
+ }
+ if (trJson.has("absolute")) {
+ tempBasal.absoluteRate = trJson.getDouble("absolute");
+ tempBasal.isAbsolute = true;
+ }
+ tempBasal._id = trJson.getString("_id");
+ createOrUpdate(tempBasal);
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void deleteTempBasalById(String _id) {
+ TemporaryBasal stored = findTempBasalById(_id);
+ if (stored != null) {
+ log.debug("TEMPBASAL: Removing TempBasal record from database: " + stored.toString());
+ delete(stored);
+ updateEarliestDataChange(stored.date);
+ scheduleTemporaryBasalChange();
+ }
+ }
+
+ public TemporaryBasal findTempBasalById(String _id) {
+ try {
+ QueryBuilder queryBuilder = null;
+ queryBuilder = getDaoTemporaryBasal().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", _id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoTemporaryBasal().query(preparedQuery);
+
+ if (list.size() != 1) {
+ return null;
+ } else {
+ return list.get(0);
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ // ------------ ExtendedBolus handling ---------------
+
+ public boolean createOrUpdate(ExtendedBolus extendedBolus) {
+ try {
+ ExtendedBolus old;
+ extendedBolus.date = roundDateToSec(extendedBolus.date);
+
+ if (extendedBolus.source == Source.PUMP) {
+ // check for changed from pump change in NS
+ QueryBuilder queryBuilder = getDaoExtendedBolus().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("pumpId", extendedBolus.pumpId);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoExtendedBolus().query(preparedQuery);
+ if (trList.size() > 0) {
+ // do nothing, pump history record cannot be changed
+ return false;
+ }
+ getDaoExtendedBolus().create(extendedBolus);
+ log.debug("EXTENDEDBOLUS: New record from: " + Source.getString(extendedBolus.source) + " " + extendedBolus.toString());
+ updateEarliestDataChange(extendedBolus.date);
+ scheduleExtendedBolusChange();
+ return true;
+ }
+ if (extendedBolus.source == Source.NIGHTSCOUT) {
+ old = getDaoExtendedBolus().queryForId(extendedBolus.date);
+ if (old != null) {
+ if (!old.isEqual(extendedBolus)) {
+ long oldDate = old.date;
+ getDaoExtendedBolus().delete(old); // need to delete/create because date may change too
+ old.copyFrom(extendedBolus);
+ getDaoExtendedBolus().create(old);
+ log.debug("EXTENDEDBOLUS: Updating record by date from: " + Source.getString(extendedBolus.source) + " " + old.toString());
+ updateEarliestDataChange(oldDate);
+ updateEarliestDataChange(old.date);
+ scheduleExtendedBolusChange();
+ return true;
+ }
+ return false;
+ }
+ // find by NS _id
+ if (extendedBolus._id != null) {
+ QueryBuilder queryBuilder = getDaoExtendedBolus().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", extendedBolus._id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoExtendedBolus().query(preparedQuery);
+ if (trList.size() > 0) {
+ old = trList.get(0);
+ if (!old.isEqual(extendedBolus)) {
+ long oldDate = old.date;
+ getDaoExtendedBolus().delete(old); // need to delete/create because date may change too
+ old.copyFrom(extendedBolus);
+ getDaoExtendedBolus().create(old);
+ log.debug("EXTENDEDBOLUS: Updating record by _id from: " + Source.getString(extendedBolus.source) + " " + old.toString());
+ updateEarliestDataChange(oldDate);
+ updateEarliestDataChange(old.date);
+ scheduleExtendedBolusChange();
+ return true;
+ }
+ }
+ }
+ getDaoExtendedBolus().create(extendedBolus);
+ log.debug("EXTENDEDBOLUS: New record from: " + Source.getString(extendedBolus.source) + " " + extendedBolus.toString());
+ updateEarliestDataChange(extendedBolus.date);
+ scheduleExtendedBolusChange();
+ return true;
+ }
+ if (extendedBolus.source == Source.USER) {
+ getDaoExtendedBolus().create(extendedBolus);
+ log.debug("EXTENDEDBOLUS: New record from: " + Source.getString(extendedBolus.source) + " " + extendedBolus.toString());
+ updateEarliestDataChange(extendedBolus.date);
+ scheduleExtendedBolusChange();
+ return true;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ public void delete(ExtendedBolus extendedBolus) {
+ try {
+ getDaoExtendedBolus().delete(extendedBolus);
+ updateEarliestDataChange(extendedBolus.date);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleExtendedBolusChange();
+ }
+
+ public List getExtendedBolusDataFromTime(long mills, boolean ascending) {
+ try {
+ List extendedBoluses;
+ QueryBuilder queryBuilder = getDaoExtendedBolus().queryBuilder();
+ queryBuilder.orderBy("date", ascending);
+ Where where = queryBuilder.where();
+ where.ge("date", mills);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ extendedBoluses = getDaoExtendedBolus().query(preparedQuery);
+ return extendedBoluses;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return new ArrayList();
+ }
+
+ public void deleteExtendedBolusById(String _id) {
+ ExtendedBolus stored = findExtendedBolusById(_id);
+ if (stored != null) {
+ log.debug("EXTENDEDBOLUS: Removing ExtendedBolus record from database: " + stored.toString());
+ delete(stored);
+ updateEarliestDataChange(stored.date);
+ scheduleExtendedBolusChange();
+ }
+ }
+
+ public ExtendedBolus findExtendedBolusById(String _id) {
+ try {
+ QueryBuilder queryBuilder = null;
+ queryBuilder = getDaoExtendedBolus().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", _id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoExtendedBolus().query(preparedQuery);
+
+ if (list.size() == 1) {
+ return list.get(0);
+ } else {
+ return null;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /*
+{
+ "_id": "5924898d577eb0880e355337",
+ "eventType": "Combo Bolus",
+ "duration": 120,
+ "splitNow": 0,
+ "splitExt": 100,
+ "enteredinsulin": 1,
+ "relative": 1,
+ "created_at": "2017-05-23T19:12:14Z",
+ "enteredBy": "AndroidAPS",
+ "NSCLIENT_ID": 1495566734628,
+ "mills": 1495566734000,
+ "mgdl": 106
}
+ */
+
+ public void createExtendedBolusFromJsonIfNotExists(JSONObject trJson) {
+ try {
+ QueryBuilder queryBuilder = null;
+ queryBuilder = getDaoExtendedBolus().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoExtendedBolus().query(preparedQuery);
+ ExtendedBolus extendedBolus;
+ if (list.size() == 0) {
+ extendedBolus = new ExtendedBolus();
+ extendedBolus.source = Source.NIGHTSCOUT;
+ if (Config.logIncommingData)
+ log.debug("Adding ExtendedBolus record to database: " + trJson.toString());
+ // Record does not exists. add
+ } else if (list.size() == 1) {
+ extendedBolus = list.get(0);
+ if (Config.logIncommingData)
+ log.debug("Updating ExtendedBolus record in database: " + trJson.toString());
+ } else {
+ log.error("Something went wrong");
+ return;
+ }
+ extendedBolus.date = trJson.getLong("mills");
+ extendedBolus.durationInMinutes = trJson.getInt("duration");
+ extendedBolus.insulin = trJson.getDouble("relative");
+ extendedBolus._id = trJson.getString("_id");
+ createOrUpdate(extendedBolus);
+ } catch (SQLException | JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void scheduleExtendedBolusChange() {
+ class PostRunnable implements Runnable {
+ public void run() {
+ log.debug("Firing EventExtendedBolusChange");
+ MainApp.bus().post(new EventReloadTreatmentData());
+ MainApp.bus().post(new EventExtendedBolusChange());
+ if (earliestDataChange != null)
+ MainApp.bus().post(new EventNewHistoryData(earliestDataChange));
+ earliestDataChange = null;
+ scheduledExtendedBolusPost = null;
+ }
+ }
+ // prepare task for execution in 1 sec
+ // cancel waiting task to prevent sending multiple posts
+ if (scheduledExtendedBolusPost != null)
+ scheduledExtendedBolusPost.cancel(false);
+ Runnable task = new PostRunnable();
+ final int sec = 1;
+ scheduledExtendedBolusPost = extendedBolusWorker.schedule(task, sec, TimeUnit.SECONDS);
+
+ }
+
+
+ // ------------ CareportalEvent handling ---------------
+
+ public void createOrUpdate(CareportalEvent careportalEvent) {
+ careportalEvent.date = careportalEvent.date - careportalEvent.date % 1000;
+ try {
+ getDaoCareportalEvents().createOrUpdate(careportalEvent);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleCareportalEventChange();
+ }
+
+ public void delete(CareportalEvent careportalEvent) {
+ try {
+ getDaoCareportalEvents().delete(careportalEvent);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ scheduleCareportalEventChange();
+ }
+
+ @Nullable
+ public CareportalEvent getLastCareportalEvent(String event) {
+ try {
+ List careportalEvents;
+ QueryBuilder queryBuilder = getDaoCareportalEvents().queryBuilder();
+ queryBuilder.orderBy("date", false);
+ Where where = queryBuilder.where();
+ where.eq("eventType", event);
+ queryBuilder.limit(1L);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ careportalEvents = getDaoCareportalEvents().query(preparedQuery);
+ if (careportalEvents.size() == 1)
+ return careportalEvents.get(0);
+ else
+ return null;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public List getCareportalEventsFromTime(long mills, boolean ascending) {
+ try {
+ List careportalEvents;
+ QueryBuilder queryBuilder = getDaoCareportalEvents().queryBuilder();
+ queryBuilder.orderBy("date", ascending);
+ Where where = queryBuilder.where();
+ where.ge("date", mills);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ careportalEvents = getDaoCareportalEvents().query(preparedQuery);
+ return careportalEvents;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return new ArrayList();
+ }
+
+ public void deleteCareportalEventById(String _id) {
+ try {
+ QueryBuilder queryBuilder = null;
+ queryBuilder = getDaoCareportalEvents().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", _id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoCareportalEvents().query(preparedQuery);
+
+ if (list.size() == 1) {
+ CareportalEvent record = list.get(0);
+ if (Config.logIncommingData)
+ log.debug("Removing CareportalEvent record from database: " + record.log());
+ delete(record);
+ } else {
+ if (Config.logIncommingData)
+ log.debug("CareportalEvent not found database: " + _id);
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void createCareportalEventFromJsonIfNotExists(JSONObject trJson) {
+ try {
+ QueryBuilder queryBuilder = null;
+ queryBuilder = getDaoCareportalEvents().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoCareportalEvents().query(preparedQuery);
+ CareportalEvent careportalEvent;
+ if (list.size() == 0) {
+ careportalEvent = new CareportalEvent();
+ careportalEvent.source = Source.NIGHTSCOUT;
+ if (Config.logIncommingData)
+ log.debug("Adding CareportalEvent record to database: " + trJson.toString());
+ // Record does not exists. add
+ } else if (list.size() == 1) {
+ careportalEvent = list.get(0);
+ if (Config.logIncommingData)
+ log.debug("Updating CareportalEvent record in database: " + trJson.toString());
+ } else {
+ log.error("Something went wrong");
+ return;
+ }
+ careportalEvent.date = trJson.getLong("mills");
+ careportalEvent.eventType = trJson.getString("eventType");
+ careportalEvent.json = trJson.toString();
+ careportalEvent._id = trJson.getString("_id");
+ createOrUpdate(careportalEvent);
+ } catch (SQLException | JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void scheduleCareportalEventChange() {
+ class PostRunnable implements Runnable {
+ public void run() {
+ log.debug("Firing scheduleCareportalEventChange");
+ MainApp.bus().post(new EventCareportalEventChange());
+ scheduledCareportalEventPost = null;
+ }
+ }
+ // prepare task for execution in 1 sec
+ // cancel waiting task to prevent sending multiple posts
+ if (scheduledCareportalEventPost != null)
+ scheduledCareportalEventPost.cancel(false);
+ Runnable task = new PostRunnable();
+ final int sec = 1;
+ scheduledCareportalEventPost = careportalEventWorker.schedule(task, sec, TimeUnit.SECONDS);
+
+ }
+
+ // ---------------- ProfileSwitch handling ---------------
+
+ public List getProfileSwitchData(boolean ascending) {
+ try {
+ Dao daoProfileSwitch = getDaoProfileSwitch();
+ List profileSwitches;
+ QueryBuilder queryBuilder = daoProfileSwitch.queryBuilder();
+ queryBuilder.orderBy("date", ascending);
+ queryBuilder.limit(20L);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ profileSwitches = daoProfileSwitch.query(preparedQuery);
+ return profileSwitches;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return new ArrayList();
+ }
+
+ public boolean createOrUpdate(ProfileSwitch profileSwitch) {
+ try {
+ ProfileSwitch old;
+ profileSwitch.date = roundDateToSec(profileSwitch.date);
+
+ if (profileSwitch.source == Source.NIGHTSCOUT) {
+ old = getDaoProfileSwitch().queryForId(profileSwitch.date);
+ if (old != null) {
+ if (!old.isEqual(profileSwitch)) {
+ getDaoProfileSwitch().delete(old); // need to delete/create because date may change too
+ old.copyFrom(profileSwitch);
+ getDaoProfileSwitch().create(old);
+ log.debug("PROFILESWITCH: Updating record by date from: " + Source.getString(profileSwitch.source) + " " + old.toString());
+ scheduleProfileSwitchChange();
+ return true;
+ }
+ return false;
+ }
+ // find by NS _id
+ if (profileSwitch._id != null) {
+ QueryBuilder queryBuilder = getDaoProfileSwitch().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", profileSwitch._id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List trList = getDaoProfileSwitch().query(preparedQuery);
+ if (trList.size() > 0) {
+ old = trList.get(0);
+ if (!old.isEqual(profileSwitch)) {
+ getDaoProfileSwitch().delete(old); // need to delete/create because date may change too
+ old.copyFrom(profileSwitch);
+ getDaoProfileSwitch().create(old);
+ log.debug("PROFILESWITCH: Updating record by _id from: " + Source.getString(profileSwitch.source) + " " + old.toString());
+ scheduleProfileSwitchChange();
+ return true;
+ }
+ }
+ }
+ getDaoProfileSwitch().create(profileSwitch);
+ log.debug("PROFILESWITCH: New record from: " + Source.getString(profileSwitch.source) + " " + profileSwitch.toString());
+ scheduleProfileSwitchChange();
+ return true;
+ }
+ if (profileSwitch.source == Source.USER) {
+ getDaoProfileSwitch().create(profileSwitch);
+ log.debug("PROFILESWITCH: New record from: " + Source.getString(profileSwitch.source) + " " + profileSwitch.toString());
+ scheduleProfileSwitchChange();
+ return true;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ public void delete(ProfileSwitch profileSwitch) {
+ try {
+ getDaoProfileSwitch().delete(profileSwitch);
+ scheduleProfileSwitchChange();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void scheduleProfileSwitchChange() {
+ class PostRunnable implements Runnable {
+ public void run() {
+ log.debug("Firing EventProfileSwitchChange");
+ MainApp.bus().post(new EventReloadProfileSwitchData());
+ MainApp.bus().post(new EventProfileSwitchChange());
+ scheduledProfileSwitchEventPost = null;
+ }
+ }
+ // prepare task for execution in 1 sec
+ // cancel waiting task to prevent sending multiple posts
+ if (scheduledProfileSwitchEventPost != null)
+ scheduledProfileSwitchEventPost.cancel(false);
+ Runnable task = new PostRunnable();
+ final int sec = 1;
+ scheduledProfileSwitchEventPost = profileSwitchEventWorker.schedule(task, sec, TimeUnit.SECONDS);
+
+ }
+
+ /*
+{
+ "_id":"592fa43ed97496a80da913d2",
+ "created_at":"2017-06-01T05:20:06Z",
+ "eventType":"Profile Switch",
+ "profile":"2016 +30%",
+ "units":"mmol",
+ "enteredBy":"sony",
+ "NSCLIENT_ID":1496294454309,
+}
+ */
+
+ public void createProfileSwitchFromJsonIfNotExists(JSONObject trJson) {
+ try {
+ ProfileSwitch profileSwitch = new ProfileSwitch();
+ profileSwitch.date = trJson.getLong("mills");
+ if (trJson.has("duration"))
+ profileSwitch.durationInMinutes = trJson.getInt("duration");
+ profileSwitch._id = trJson.getString("_id");
+ profileSwitch.profileName = trJson.getString("profile");
+ profileSwitch.isCPP = trJson.has("CircadianPercentageProfile");
+ profileSwitch.source = Source.NIGHTSCOUT;
+ if (trJson.has("timeshift"))
+ profileSwitch.timeshift = trJson.getInt("timeshift");
+ if (trJson.has("percentage"))
+ profileSwitch.percentage = trJson.getInt("percentage");
+ if (trJson.has("profileJson"))
+ profileSwitch.profileJson = trJson.getString("profileJson");
+ if (trJson.has("profilePlugin"))
+ profileSwitch.profilePlugin = trJson.getString("profilePlugin");
+ createOrUpdate(profileSwitch);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void deleteProfileSwitchById(String _id) {
+ ProfileSwitch stored = findProfileSwitchById(_id);
+ if (stored != null) {
+ log.debug("PROFILESWITCH: Removing ProfileSwitch record from database: " + stored.toString());
+ delete(stored);
+ scheduleTemporaryTargetChange();
+ }
+ }
+
+ public ProfileSwitch findProfileSwitchById(String _id) {
+ try {
+ QueryBuilder queryBuilder = getDaoProfileSwitch().queryBuilder();
+ Where where = queryBuilder.where();
+ where.eq("_id", _id);
+ PreparedQuery preparedQuery = queryBuilder.prepare();
+ List list = getDaoProfileSwitch().query(preparedQuery);
+
+ if (list.size() == 1) {
+ return list.get(0);
+ } else {
+ return null;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+}
\ No newline at end of file
diff --git a/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java b/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java
index 0ecc5d4f32..203436661c 100644
--- a/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java
+++ b/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java
@@ -19,15 +19,7 @@ import org.slf4j.LoggerFactory;
public class DbRequest {
private static Logger log = LoggerFactory.getLogger(DbRequest.class);
- public String getNsClientID() {
- return nsClientID;
- }
-
- public void setNsClientID(String nsClientID) {
- this.nsClientID = nsClientID;
- }
-
- @DatabaseField(id = true, useGetSet = true)
+ @DatabaseField(id = true)
public String nsClientID = null;
@DatabaseField
diff --git a/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java b/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java
new file mode 100644
index 0000000000..0bd929030f
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java
@@ -0,0 +1,283 @@
+package info.nightscout.androidaps.db;
+
+/**
+ * Created by mike on 21.05.2017.
+ */
+
+import android.graphics.Color;
+
+import com.j256.ormlite.field.DatabaseField;
+import com.j256.ormlite.table.DatabaseTable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+import java.util.Objects;
+
+import info.nightscout.androidaps.Constants;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.data.Iob;
+import info.nightscout.androidaps.data.IobTotal;
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.androidaps.interfaces.InsulinInterface;
+import info.nightscout.androidaps.interfaces.Interval;
+import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries;
+import info.nightscout.utils.DateUtil;
+import info.nightscout.utils.DecimalFormatter;
+import info.nightscout.utils.Round;
+
+/**
+ * Created by mike on 21.05.2017.
+ */
+
+@DatabaseTable(tableName = DatabaseHelper.DATABASE_EXTENDEDBOLUSES)
+public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
+ private static Logger log = LoggerFactory.getLogger(ExtendedBolus.class);
+
+ @DatabaseField(id = true)
+ public long date;
+
+ @DatabaseField
+ public boolean isValid = true;
+
+ @DatabaseField(index = true)
+ public long pumpId = 0;
+
+ @DatabaseField
+ public int source = Source.NONE;
+ @DatabaseField
+ public String _id = null; // NS _id
+
+ @DatabaseField
+ public double insulin = 0d;
+ @DatabaseField
+ public int durationInMinutes = 0; // duration == 0 means end of extended bolus
+
+ @DatabaseField
+ public int insulinInterfaceID = InsulinInterface.FASTACTINGINSULIN;
+ @DatabaseField
+ public double dia = Constants.defaultDIA;
+
+ public ExtendedBolus() {
+ }
+
+ public ExtendedBolus(long date) {
+ this.date = date;
+ }
+
+ public boolean isEqual(ExtendedBolus other) {
+ if (date != other.date) {
+ return false;
+ }
+ if (durationInMinutes != other.durationInMinutes)
+ return false;
+ if (insulin != other.insulin)
+ return false;
+ if (pumpId != other.pumpId)
+ return false;
+ if (!Objects.equals(_id, other._id))
+ return false;
+ return true;
+ }
+
+ public void copyFrom(ExtendedBolus t) {
+ date = t.date;
+ _id = t._id;
+ durationInMinutes = t.durationInMinutes;
+ insulin = t.insulin;
+ pumpId = t.pumpId;
+ }
+
+ // -------- Interval interface ---------
+
+ Long cuttedEnd = null;
+
+ public long durationInMsec() {
+ return durationInMinutes * 60 * 1000L;
+ }
+
+ public long start() {
+ return date;
+ }
+
+ // planned end time at time of creation
+ public long originalEnd() {
+ return date + durationInMinutes * 60 * 1000L;
+ }
+
+ // end time after cut
+ public long end() {
+ if (cuttedEnd != null)
+ return cuttedEnd;
+ return originalEnd();
+ }
+
+ public void cutEndTo(long end) {
+ cuttedEnd = end;
+ }
+
+ public boolean match(long time) {
+ if (start() <= time && end() >= time)
+ return true;
+ return false;
+ }
+
+ public boolean before(long time) {
+ if (end() < time)
+ return true;
+ return false;
+ }
+
+ public boolean after(long time) {
+ if (start() > time)
+ return true;
+ return false;
+ }
+
+ @Override
+ public boolean isInProgress() {
+ return match(System.currentTimeMillis());
+ }
+
+ @Override
+ public boolean isEndingEvent() {
+ return durationInMinutes == 0;
+ }
+
+ // -------- Interval interface end ---------
+
+ public String log() {
+ return "Bolus{" +
+ "date= " + date +
+ ", date= " + DateUtil.dateAndTimeString(date) +
+ ", isValid=" + isValid +
+ ", _id= " + _id +
+ ", pumpId= " + pumpId +
+ ", insulin= " + insulin +
+ ", durationInMinutes= " + durationInMinutes +
+ "}";
+ }
+
+ public double absoluteRate() {
+ return Round.roundTo(insulin / durationInMinutes * 60, 0.01);
+ }
+
+ public double insulinSoFar() {
+ return absoluteRate() * getRealDuration() / 60d;
+ }
+
+ public IobTotal iobCalc(long time) {
+ IobTotal result = new IobTotal(time);
+ InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
+
+ int realDuration = getDurationToTime(time);
+
+ if (realDuration > 0) {
+ double dia_ago = time - dia * 60 * 60 * 1000;
+ int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
+ double spacing = realDuration / aboutFiveMinIntervals;
+
+ for (long j = 0L; j < aboutFiveMinIntervals; j++) {
+ // find middle of the interval
+ long calcdate = (long) (date + j * spacing * 60 * 1000 + 0.5d * spacing * 60 * 1000);
+
+ if (calcdate > dia_ago && calcdate <= time) {
+ double tempBolusSize = absoluteRate() * spacing / 60d;
+
+ Treatment tempBolusPart = new Treatment();
+ tempBolusPart.insulin = tempBolusSize;
+ tempBolusPart.date = calcdate;
+
+ Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia);
+ result.iob += aIOB.iobContrib;
+ result.activity += aIOB.activityContrib;
+ result.extendedBolusInsulin += tempBolusPart.insulin;
+ }
+ }
+ }
+ return result;
+ }
+
+ public int getRealDuration() {
+ return getDurationToTime(System.currentTimeMillis());
+ }
+
+ private int getDurationToTime(long time) {
+ long endTime = Math.min(time, end());
+ long msecs = endTime - date;
+ return Math.round(msecs / 60f / 1000);
+ }
+
+ public int getPlannedRemainingMinutes() {
+ float remainingMin = (end() - System.currentTimeMillis()) / 1000f / 60;
+ return (remainingMin < 0) ? 0 : Math.round(remainingMin);
+ }
+
+ public String toString() {
+ return "E " + DecimalFormatter.to2Decimal(absoluteRate()) + "U/h @" +
+ DateUtil.timeString(date) +
+ " " + getRealDuration() + "/" + durationInMinutes + "min";
+ }
+
+ public String toStringShort() {
+ return "E " + DecimalFormatter.to2Decimal(absoluteRate()) + "U/h ";
+ }
+
+ public String toStringMedium() {
+ return "E " + DecimalFormatter.to2Decimal(absoluteRate()) + "U/h ("
+ + getRealDuration() + "/" + durationInMinutes + ") ";
+ }
+
+ public String toStringTotal() {
+ return DecimalFormatter.to2Decimal(insulin) + "U ( " +
+ DecimalFormatter.to2Decimal(absoluteRate()) + " U/h )";
+ }
+
+ // -------- DataPointWithLabelInterface --------
+ @Override
+ public double getX() {
+ return date;
+ }
+
+ // default when no sgv around available
+ private double yValue = 0;
+
+
+ @Override
+ public double getY() {
+ return yValue;
+ }
+
+ @Override
+ public void setY(double y) {
+ yValue = y;
+ }
+
+ @Override
+ public String getLabel() {
+ return toStringTotal();
+ }
+
+ @Override
+ public long getDuration() {
+ return durationInMinutes * 60 * 1000L;
+ }
+
+ @Override
+ public PointsWithLabelGraphSeries.Shape getShape() {
+ return PointsWithLabelGraphSeries.Shape.EXTENDEDBOLUS;
+ }
+
+ @Override
+ public float getSize() {
+ return 10;
+ }
+
+ @Override
+ public int getColor() {
+ return Color.CYAN;
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java
new file mode 100644
index 0000000000..fb99934382
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java
@@ -0,0 +1,217 @@
+package info.nightscout.androidaps.db;
+
+import android.graphics.Color;
+
+import com.j256.ormlite.field.DatabaseField;
+import com.j256.ormlite.table.DatabaseTable;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+import java.util.Objects;
+
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.androidaps.interfaces.Interval;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries;
+import info.nightscout.utils.DateUtil;
+
+@DatabaseTable(tableName = DatabaseHelper.DATABASE_PROFILESWITCHES)
+public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
+ private static Logger log = LoggerFactory.getLogger(ProfileSwitch.class);
+
+ @DatabaseField(id = true)
+ public long date;
+
+ @DatabaseField
+ public boolean isValid = true;
+
+ @DatabaseField
+ public int source = Source.NONE;
+ @DatabaseField
+ public String _id = null; // NS _id
+
+ @DatabaseField
+ public boolean isCPP = false; // CPP NS="CircadianPercentageProfile"
+ @DatabaseField
+ public int timeshift = 0; // CPP NS="timeshift"
+ @DatabaseField
+ public int percentage = 100; // CPP NS="percentage"
+
+ @DatabaseField
+ public String profileName = null;
+
+ @DatabaseField
+ public String profileJson = null;
+
+ @DatabaseField
+ public String profilePlugin = null; // NSProfilePlugin.class.getName();
+
+ @DatabaseField
+ public int durationInMinutes = 0;
+
+ private Profile profile = null;
+
+ public Profile getProfileObject() {
+ if (profile == null)
+ try {
+ profile = new Profile(new JSONObject(profileJson));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return profile;
+ }
+
+ public boolean isEqual(ProfileSwitch other) {
+ if (date != other.date) {
+ return false;
+ }
+ if (durationInMinutes != other.durationInMinutes)
+ return false;
+ if (percentage != other.percentage)
+ return false;
+ if (timeshift != other.timeshift)
+ return false;
+ if (isCPP != other.isCPP)
+ return false;
+ if (!Objects.equals(_id, other._id))
+ return false;
+ if (!Objects.equals(profilePlugin, other.profilePlugin))
+ return false;
+ if (!Objects.equals(profileJson, other.profileJson))
+ return false;
+ if (!Objects.equals(profileName, other.profileName))
+ return false;
+ return true;
+ }
+
+ public void copyFrom(ProfileSwitch t) {
+ date = t.date;
+ _id = t._id;
+ durationInMinutes = t.durationInMinutes;
+ percentage = t.percentage;
+ timeshift = t.timeshift;
+ isCPP = t.isCPP;
+ profilePlugin = t.profilePlugin;
+ profileJson = t.profileJson;
+ profileName = t.profileName;
+ }
+
+ // -------- Interval interface ---------
+
+ Long cuttedEnd = null;
+
+ public long durationInMsec() {
+ return durationInMinutes * 60 * 1000L;
+ }
+
+ public long start() {
+ return date;
+ }
+
+ // planned end time at time of creation
+ public long originalEnd() {
+ return date + durationInMinutes * 60 * 1000L;
+ }
+
+ // end time after cut
+ public long end() {
+ if (cuttedEnd != null)
+ return cuttedEnd;
+ return originalEnd();
+ }
+
+ public void cutEndTo(long end) {
+ cuttedEnd = end;
+ }
+
+ public boolean match(long time) {
+ if (start() <= time && end() >= time)
+ return true;
+ return false;
+ }
+
+ public boolean before(long time) {
+ if (end() < time)
+ return true;
+ return false;
+ }
+
+ public boolean after(long time) {
+ if (start() > time)
+ return true;
+ return false;
+ }
+
+ @Override
+ public boolean isInProgress() {
+ return match(System.currentTimeMillis());
+ }
+
+ @Override
+ public boolean isEndingEvent() {
+ return durationInMinutes == 0;
+ }
+
+ // -------- Interval interface end ---------
+
+ // ----------------- DataPointInterface --------------------
+ @Override
+ public double getX() {
+ return date;
+ }
+
+ // default when no sgv around available
+ private double yValue = 0;
+
+ @Override
+ public double getY() {
+ return yValue;
+ }
+
+ @Override
+ public void setY(double y) {
+ yValue = y;
+ }
+
+ @Override
+ public String getLabel() {
+ return profileName;
+ }
+
+ @Override
+ public long getDuration() {
+ return 0;
+ }
+
+ @Override
+ public PointsWithLabelGraphSeries.Shape getShape() {
+ return PointsWithLabelGraphSeries.Shape.PROFILE;
+ }
+
+ @Override
+ public float getSize() {
+ return 10;
+ }
+
+ @Override
+ public int getColor() {
+ return Color.CYAN;
+ }
+
+ public String toString() {
+ return "ProfileSwitch{" +
+ "date=" + date +
+ "date=" + DateUtil.dateAndTimeString(date) +
+ ", isValid=" + isValid +
+ ", duration=" + durationInMinutes +
+ ", profileName=" + profileName +
+ ", percentage=" + percentage +
+ ", timeshift=" + timeshift +
+ '}';
+ }
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/db/Source.java b/app/src/main/java/info/nightscout/androidaps/db/Source.java
new file mode 100644
index 0000000000..c126794769
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/db/Source.java
@@ -0,0 +1,24 @@
+package info.nightscout.androidaps.db;
+
+/**
+ * Created by mike on 21.05.2017.
+ */
+
+public class Source {
+ public final static int NONE = 0;
+ public final static int PUMP = 1; // Pump history
+ public final static int NIGHTSCOUT = 2; // created in NS
+ public final static int USER = 3; // created by user or driver not using history
+
+ public static String getString(int source) {
+ switch (source) {
+ case PUMP:
+ return "PUMP";
+ case NIGHTSCOUT:
+ return "NIGHTSCOUT";
+ case USER:
+ return "USER";
+ }
+ return "NONE";
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java
deleted file mode 100644
index d5a6c01235..0000000000
--- a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java
+++ /dev/null
@@ -1,301 +0,0 @@
-package info.nightscout.androidaps.db;
-
-import com.j256.ormlite.field.DatabaseField;
-import com.j256.ormlite.table.DatabaseTable;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Date;
-
-import info.nightscout.androidaps.MainApp;
-import info.nightscout.androidaps.data.Iob;
-import info.nightscout.androidaps.data.IobTotal;
-import info.nightscout.androidaps.interfaces.InsulinInterface;
-import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
-import info.nightscout.utils.DateUtil;
-import info.nightscout.utils.DecimalFormatter;
-
-@DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPBASALS)
-public class TempBasal {
- private static Logger log = LoggerFactory.getLogger(TempBasal.class);
-
- public long getTimeIndex() {
- return timeStart.getTime();
- }
-
- public void setTimeIndex(long timeIndex) {
- this.timeIndex = timeIndex;
- }
-
- @DatabaseField(id = true, useGetSet = true)
- public long timeIndex;
-
- @DatabaseField
- public Date timeStart;
-
- @DatabaseField
- public Date timeEnd;
-
- @DatabaseField
- public int percent; // In % of current basal. 100% == current basal
-
- @DatabaseField
- public Double absolute; // Absolute value in U
-
- @DatabaseField
- public int duration; // in minutes
-
- @DatabaseField
- public boolean isExtended = false; // true if set as extended bolus
-
- @DatabaseField
- public boolean isAbsolute = false; // true if if set as absolute value in U
-
-
- public IobTotal iobCalc(long time) {
- IobTotal result = new IobTotal(time);
- NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
- InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
-
- if (profile == null)
- return result;
-
- int realDuration = getDurationToTime(time);
- Double netBasalAmount = 0d;
-
- if (realDuration > 0) {
- Double netBasalRate = 0d;
-
- Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;
- int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
- double tempBolusSpacing = realDuration / aboutFiveMinIntervals;
-
- for (Long j = 0L; j < aboutFiveMinIntervals; j++) {
- // find middle of the interval
- Long date = (long) (timeStart.getTime() + j * tempBolusSpacing * 60 * 1000 + 0.5d * tempBolusSpacing * 60 * 1000);
-
- Double basalRate = profile.getBasal(NSProfile.secondsFromMidnight(date));
-
- if (basalRate == null)
- continue;
- if (isExtended) {
- netBasalRate = this.absolute;
- } else {
- if (this.isAbsolute) {
- netBasalRate = this.absolute - basalRate;
- } else {
- netBasalRate = (this.percent - 100) / 100d * basalRate;
- }
- }
-
- if (date > dia_ago && date <= time) {
- double tempBolusSize = netBasalRate * tempBolusSpacing / 60d;
- netBasalAmount += tempBolusSize;
-
- Treatment tempBolusPart = new Treatment(insulinInterface);
- tempBolusPart.insulin = tempBolusSize;
- tempBolusPart.created_at = new Date(date);
-
- Iob aIOB = insulinInterface.iobCalc(tempBolusPart, time, profile.getDia());
- result.basaliob += aIOB.iobContrib;
- result.activity += aIOB.activityContrib;
- result.netbasalinsulin += tempBolusPart.insulin;
- if (tempBolusPart.insulin > 0) {
- result.hightempinsulin += tempBolusPart.insulin;
- }
- }
- result.netRatio = netBasalRate; // ratio at the end of interval
- }
- }
- result.netInsulin = netBasalAmount;
- return result;
- }
-
-/*
- public IobTotal old_iobCalc(long time) {
- IobTotal result = new IobTotal(time);
- NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
- InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
-
- if (profile == null)
- return result;
-
- Double basalRate = profile.getBasal(NSProfile.secondsFromMidnight(time));
-
- if (basalRate == null)
- return result;
-
- int realDuration = getDurationToTime(time);
-
- if (realDuration > 0) {
- Double netBasalRate = 0d;
- Double tempBolusSize = 0.05;
-
- if (isExtended) {
- netBasalRate = this.absolute;
- } else {
- if (this.isAbsolute) {
- netBasalRate = this.absolute - basalRate;
- } else {
- netBasalRate = (this.percent - 100) / 100d * basalRate;
- }
- }
-
- result.netRatio = netBasalRate;
- Double netBasalAmount = Math.round(netBasalRate * realDuration * 10 / 6) / 100d;
- result.netInsulin = netBasalAmount;
- if (netBasalAmount < 0.1) {
- tempBolusSize = 0.01;
- }
- if (netBasalRate < 0) {
- tempBolusSize = -tempBolusSize;
- }
- Long tempBolusCount = Math.round(netBasalAmount / tempBolusSize);
- if (tempBolusCount > 0) {
- Long tempBolusSpacing = realDuration / tempBolusCount;
- for (Long j = 0L; j < tempBolusCount; j++) {
- Treatment tempBolusPart = new Treatment(insulinInterface);
- tempBolusPart.insulin = tempBolusSize;
- Long date = this.timeStart.getTime() + j * tempBolusSpacing * 60 * 1000;
- tempBolusPart.created_at = new Date(date);
-
- Iob aIOB = insulinInterface.iobCalc(tempBolusPart, time, profile.getDia());
- result.basaliob += aIOB.iobContrib;
- result.activity += aIOB.activityContrib;
- Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;
- if (date > dia_ago && date <= time) {
- result.netbasalinsulin += tempBolusPart.insulin;
- if (tempBolusPart.insulin > 0) {
- result.hightempinsulin += tempBolusPart.insulin;
- }
- }
- }
- }
- }
- return result;
- }
-*/
-
- // Determine end of basal
- public long getTimeEnd() {
- long tempBasalTimePlannedEnd = getPlannedTimeEnd();
- long now = new Date().getTime();
-
- if (timeEnd != null && timeEnd.getTime() < tempBasalTimePlannedEnd) {
- tempBasalTimePlannedEnd = timeEnd.getTime();
- }
-
- if (now < tempBasalTimePlannedEnd)
- tempBasalTimePlannedEnd = now;
-
- return tempBasalTimePlannedEnd;
- }
-
- public long getPlannedTimeEnd() {
- return timeStart.getTime() + 60 * 1_000 * duration;
- }
-
- public int getRealDuration() {
- long msecs = getTimeEnd() - timeStart.getTime();
- return Math.round(msecs / 60f / 1000);
- }
-
- public int getDurationToTime(long time) {
- long endTime = Math.min(time, getTimeEnd());
- long msecs = endTime - timeStart.getTime();
- return Math.round(msecs / 60f / 1000);
- }
-
- public long getMillisecondsFromStart() {
- return new Date().getTime() - timeStart.getTime();
- }
-
- public int getPlannedRemainingMinutes() {
- if (timeEnd != null) return 0;
- float remainingMin = (getPlannedTimeEnd() - new Date().getTime()) / 1000f / 60;
- return (remainingMin < 0) ? 0 : Math.round(remainingMin);
- }
-
- public boolean isInProgress() {
- return isInProgress(new Date());
- }
-
- public double tempBasalConvertedToAbsolute(Date time) {
- if (isExtended) {
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
- double absval = profile.getBasal(NSProfile.secondsFromMidnight(time)) + absolute;
- return absval;
- } else {
- if (isAbsolute) return absolute;
- else {
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
- double absval = profile.getBasal(NSProfile.secondsFromMidnight(time)) * percent / 100;
- return absval;
- }
- }
- }
-
- public boolean isInProgress(Date time) {
- if (timeStart.getTime() > time.getTime()) return false; // in the future
- if (timeEnd == null) { // open end
- if (timeStart.getTime() < time.getTime() && getPlannedTimeEnd() > time.getTime())
- return true; // in interval
- return false;
- }
- // closed end
- if (timeStart.getTime() < time.getTime() && timeEnd.getTime() > time.getTime())
- return true; // in interval
- return false;
- }
-
- public String log() {
- return "TempBasal{" +
- "timeIndex=" + timeIndex +
- ", timeStart=" + timeStart +
- ", timeEnd=" + timeEnd +
- ", percent=" + percent +
- ", absolute=" + absolute +
- ", duration=" + duration +
- ", isAbsolute=" + isAbsolute +
- ", isExtended=" + isExtended +
- '}';
- }
-
- public String toString() {
- String extended = isExtended ? "E " : "";
-
- if (isAbsolute) {
- return extended + DecimalFormatter.to2Decimal(absolute) + "U/h @" +
- DateUtil.timeString(timeStart) +
- " " + getRealDuration() + "/" + duration + "min";
- } else { // percent
- return percent + "% @" +
- DateUtil.timeString(timeStart) +
- " " + getRealDuration() + "/" + duration + "min";
- }
- }
-
- public String toStringShort() {
- String extended = isExtended ? "E" : "";
-
- if (isAbsolute) {
- return extended + DecimalFormatter.to2Decimal(absolute) + "U/h ";
- } else { // percent
- return percent + "% ";
- }
- }
-
- public String toStringMedium() {
- String extended = isExtended ? "E" : "";
-
- if (isAbsolute) {
- return extended + DecimalFormatter.to2Decimal(absolute) + "U/h ("
- + getRealDuration() + "/" + duration + ") ";
- } else { // percent
- return percent + "% (" + getRealDuration() + "/" + duration + ") ";
- }
- }
-
-}
diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java b/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java
index 051a1d3d40..3bbdcbc06c 100644
--- a/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java
+++ b/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java
@@ -7,29 +7,27 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
+import java.util.Objects;
import info.nightscout.androidaps.Constants;
-import info.nightscout.androidaps.MainApp;
-import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin;
+import info.nightscout.androidaps.interfaces.Interval;
+import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
@DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPTARGETS)
-public class TempTarget {
+public class TempTarget implements Interval {
private static Logger log = LoggerFactory.getLogger(TempTarget.class);
- public long getTimeIndex() {
- return timeStart.getTime() - timeStart.getTime() % 1000;
- }
-
- public void setTimeIndex(long timeIndex) {
- this.timeIndex = timeIndex;
- }
-
- @DatabaseField(id = true, useGetSet = true)
- public long timeIndex;
+ @DatabaseField(id = true)
+ public long date;
@DatabaseField
- public Date timeStart;
+ public boolean isValid = true;
+
+ @DatabaseField
+ public int source = Source.NONE;
+ @DatabaseField
+ public String _id = null; // NS _id
@DatabaseField
public double low; // in mgdl
@@ -41,15 +39,92 @@ public class TempTarget {
public String reason;
@DatabaseField
- public int duration; // in minutes
+ public int durationInMinutes;
- @DatabaseField
- public String _id; // NS _id
-
- public Date getPlannedTimeEnd() {
- return new Date(timeStart.getTime() + 60 * 1_000 * duration);
+ public boolean isEqual(TempTarget other) {
+ if (date != other.date) {
+ return false;
+ }
+ if (durationInMinutes != other.durationInMinutes)
+ return false;
+ if (low != other.low)
+ return false;
+ if (high != other.high)
+ return false;
+ if (!Objects.equals(reason, other.reason))
+ return false;
+ if (!Objects.equals(_id, other._id))
+ return false;
+ return true;
}
+ public void copyFrom(TempTarget t) {
+ date = t.date;
+ _id = t._id;
+ durationInMinutes = t.durationInMinutes;
+ low = t.low;
+ high = t.high;
+ reason = t.reason;
+ }
+
+ // -------- Interval interface ---------
+
+ Long cuttedEnd = null;
+
+ public long durationInMsec() {
+ return durationInMinutes * 60 * 1000L;
+ }
+
+ public long start() {
+ return date;
+ }
+
+ // planned end time at time of creation
+ public long originalEnd() {
+ return date + durationInMinutes * 60 * 1000L;
+ }
+
+ // end time after cut
+ public long end() {
+ if (cuttedEnd != null)
+ return cuttedEnd;
+ return originalEnd();
+ }
+
+ public void cutEndTo(long end) {
+ cuttedEnd = end;
+ }
+
+ public boolean match(long time) {
+ if (start() <= time && end() >= time)
+ return true;
+ return false;
+ }
+
+ public boolean before(long time) {
+ if (end() < time)
+ return true;
+ return false;
+ }
+
+ public boolean after(long time) {
+ if (start() > time)
+ return true;
+ return false;
+ }
+
+ @Override
+ public boolean isInProgress() {
+ return match(System.currentTimeMillis());
+ }
+
+ @Override
+ public boolean isEndingEvent() {
+ return durationInMinutes == 0;
+ }
+
+ // -------- Interval interface end ---------
+
public String lowValueToUnitsToString(String units) {
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(low);
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
@@ -60,15 +135,12 @@ public class TempTarget {
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
}
- public boolean isInProgress() {
- return ((TempTargetRangePlugin) MainApp.getSpecificPlugin(TempTargetRangePlugin.class)).getTempTargetInProgress(new Date().getTime()) == this;
- }
-
- public String log() {
- return "TempTarget{" +
- "timeIndex=" + timeIndex +
- ", timeStart=" + timeStart +
- ", duration=" + duration +
+ public String toString() {
+ return "TemporaryTarget{" +
+ "date=" + date +
+ "date=" + DateUtil.dateAndTimeString(date) +
+ ", isValid=" + isValid +
+ ", duration=" + durationInMinutes +
", reason=" + reason +
", low=" + low +
", high=" + high +
diff --git a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java
new file mode 100644
index 0000000000..3c8f95f5af
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java
@@ -0,0 +1,375 @@
+package info.nightscout.androidaps.db;
+
+import com.j256.ormlite.field.DatabaseField;
+import com.j256.ormlite.table.DatabaseTable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+import java.util.Objects;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.data.Iob;
+import info.nightscout.androidaps.data.IobTotal;
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.androidaps.interfaces.InsulinInterface;
+import info.nightscout.androidaps.interfaces.Interval;
+import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
+import info.nightscout.utils.DateUtil;
+import info.nightscout.utils.DecimalFormatter;
+import info.nightscout.utils.SP;
+
+/**
+ * Created by mike on 21.05.2017.
+ */
+
+@DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPORARYBASALS)
+public class TemporaryBasal implements Interval {
+ private static Logger log = LoggerFactory.getLogger(TemporaryBasal.class);
+
+ @DatabaseField(id = true)
+ public long date;
+
+ @DatabaseField
+ public boolean isValid = true;
+
+ @DatabaseField(index = true)
+ public long pumpId = 0;
+
+ @DatabaseField
+ public int source = Source.NONE;
+ @DatabaseField
+ public String _id = null; // NS _id
+
+ @DatabaseField
+ public int durationInMinutes = 0; // duration == 0 means end of temp basal
+ @DatabaseField
+ public boolean isAbsolute = false;
+
+ public boolean isFakeExtended = false;
+
+ @DatabaseField
+ public int percentRate = 0;
+ @DatabaseField
+ public double absoluteRate = 0d;
+
+ public double netExtendedRate = 0d;
+
+ public TemporaryBasal() {
+ }
+
+ public TemporaryBasal(long date) {
+ this.date = date;
+ }
+
+ public TemporaryBasal(ExtendedBolus extendedBolus) {
+ double basal = MainApp.getConfigBuilder().getProfile(extendedBolus.date).getBasal(extendedBolus.date);
+ this.date = extendedBolus.date;
+ this.isValid = extendedBolus.isValid;
+ this.source = extendedBolus.source;
+ this._id = extendedBolus._id;
+ this.durationInMinutes = extendedBolus.durationInMinutes;
+ this.isAbsolute = true;
+ this.isFakeExtended = true;
+ this.netExtendedRate = extendedBolus.absoluteRate();
+ this.absoluteRate = basal + extendedBolus.absoluteRate();
+ }
+
+ public TemporaryBasal clone() {
+ TemporaryBasal t = new TemporaryBasal();
+ t.date = date;
+ t.isValid = isValid;
+ t.source = source;
+ t._id = _id;
+ t.pumpId = pumpId;
+ t.durationInMinutes = durationInMinutes;
+ t.isAbsolute = isAbsolute;
+ t.percentRate = percentRate;
+ t.absoluteRate = absoluteRate;
+ return t;
+ }
+
+ public boolean isEqual(TemporaryBasal other) {
+ if (date != other.date) {
+ return false;
+ }
+ if (durationInMinutes != other.durationInMinutes)
+ return false;
+ if (isAbsolute != other.isAbsolute)
+ return false;
+ if (percentRate != other.percentRate)
+ return false;
+ if (absoluteRate != other.absoluteRate)
+ return false;
+ if (netExtendedRate != other.netExtendedRate)
+ return false;
+ if (isFakeExtended != other.isFakeExtended)
+ return false;
+ if (pumpId != other.pumpId)
+ return false;
+ if (!Objects.equals(_id, other._id))
+ return false;
+ return true;
+ }
+
+ public void copyFrom(TemporaryBasal t) {
+ date = t.date;
+ _id = t._id;
+ durationInMinutes = t.durationInMinutes;
+ isAbsolute = t.isAbsolute;
+ percentRate = t.percentRate;
+ absoluteRate = t.absoluteRate;
+ pumpId = t.pumpId;
+ isFakeExtended = t.isFakeExtended;
+ netExtendedRate = t.netExtendedRate;
+ }
+
+ // -------- Interval interface ---------
+
+ Long cuttedEnd = null;
+
+ public long durationInMsec() {
+ return durationInMinutes * 60 * 1000L;
+ }
+
+ public long start() {
+ return date;
+ }
+
+ // planned end time at time of creation
+ public long originalEnd() {
+ return date + durationInMinutes * 60 * 1000L;
+ }
+
+ // end time after cut
+ public long end() {
+ if (cuttedEnd != null)
+ return cuttedEnd;
+ return originalEnd();
+ }
+
+ public void cutEndTo(long end) {
+ cuttedEnd = end;
+ }
+
+ public boolean match(long time) {
+ if (start() <= time && end() >= time)
+ return true;
+ return false;
+ }
+
+ public boolean before(long time) {
+ if (end() < time)
+ return true;
+ return false;
+ }
+
+ public boolean after(long time) {
+ if (start() > time)
+ return true;
+ return false;
+ }
+
+ @Override
+ public boolean isInProgress() {
+ return match(System.currentTimeMillis());
+ }
+
+ @Override
+ public boolean isEndingEvent() {
+ return durationInMinutes == 0;
+ }
+
+ // -------- Interval interface end ---------
+
+ public IobTotal iobCalc(long time) {
+
+ if(isFakeExtended){
+ log.error("iobCalc should only be called on Extended boluses separately");
+ return new IobTotal(time);
+ }
+
+ IobTotal result = new IobTotal(time);
+ Profile profile = MainApp.getConfigBuilder().getProfile(time);
+ InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
+
+ int realDuration = getDurationToTime(time);
+ double netBasalAmount = 0d;
+
+ if (realDuration > 0) {
+ double netBasalRate = 0d;
+ double dia = profile.getDia();
+ double dia_ago = time - dia * 60 * 60 * 1000;
+ int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
+ double tempBolusSpacing = realDuration / aboutFiveMinIntervals;
+
+ for (long j = 0L; j < aboutFiveMinIntervals; j++) {
+ // find middle of the interval
+ long calcdate = (long) (date + j * tempBolusSpacing * 60 * 1000 + 0.5d * tempBolusSpacing * 60 * 1000);
+
+ Double basalRate = profile.getBasal(calcdate);
+
+ if (basalRate == null)
+ continue;
+ if (isAbsolute) {
+ netBasalRate = absoluteRate - basalRate;
+ } else {
+ netBasalRate = (percentRate - 100) / 100d * basalRate;
+ }
+
+ if (calcdate > dia_ago && calcdate <= time) {
+ double tempBolusSize = netBasalRate * tempBolusSpacing / 60d;
+ netBasalAmount += tempBolusSize;
+
+ Treatment tempBolusPart = new Treatment();
+ tempBolusPart.insulin = tempBolusSize;
+ tempBolusPart.date = calcdate;
+
+ Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia);
+ result.basaliob += aIOB.iobContrib;
+ result.activity += aIOB.activityContrib;
+ result.netbasalinsulin += tempBolusPart.insulin;
+ if (tempBolusPart.insulin > 0) {
+ result.hightempinsulin += tempBolusPart.insulin;
+ }
+ }
+ result.netRatio = netBasalRate; // ratio at the end of interval
+ }
+ }
+ result.netInsulin = netBasalAmount;
+ return result;
+ }
+
+ public int getRealDuration() {
+ return getDurationToTime(System.currentTimeMillis());
+ }
+
+ private int getDurationToTime(long time) {
+ long endTime = Math.min(time, end());
+ long msecs = endTime - date;
+ return Math.round(msecs / 60f / 1000);
+ }
+
+ public int getPlannedRemainingMinutes() {
+ float remainingMin = (end() - System.currentTimeMillis()) / 1000f / 60;
+ return (remainingMin < 0) ? 0 : Math.round(remainingMin);
+ }
+
+ public double tempBasalConvertedToAbsolute(long time) {
+ if(isFakeExtended){
+ return MainApp.getConfigBuilder().getProfile(time).getBasal(time) + netExtendedRate;
+ } else if (isAbsolute) {
+ return absoluteRate;
+ } else {
+ return MainApp.getConfigBuilder().getProfile(time).getBasal(time) * percentRate / 100;
+ }
+ }
+
+ public String toString() {
+ return "TemporaryBasal{" +
+ "date=" + date +
+ ", date=" + DateUtil.dateAndTimeString(date) +
+ ", isValid=" + isValid +
+ ", pumpId=" + pumpId +
+ ", _id=" + _id +
+ ", percentRate=" + percentRate +
+ ", absoluteRate=" + absoluteRate +
+ ", durationInMinutes=" + durationInMinutes +
+ ", isAbsolute=" + isAbsolute +
+ ", isFakeExtended=" + isFakeExtended +
+ ", netExtendedRate=" + netExtendedRate +
+ '}';
+ }
+
+ public String toStringFull() {
+ if(isFakeExtended){
+
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ Double currentBasalRate = profile.getBasal();
+ double rate = (currentBasalRate == null)?0d:(currentBasalRate+netExtendedRate);
+ return getCalcuatedPercentageIfNeeded() + DecimalFormatter.to2Decimal(rate) + "U/h ("+DecimalFormatter.to2Decimal(netExtendedRate)+"E) @" +
+ DateUtil.timeString(date) +
+ " " + getRealDuration() + "/" + durationInMinutes + "'";
+ } else if (isAbsolute) {
+ return DecimalFormatter.to2Decimal(absoluteRate) + "U/h @" +
+ DateUtil.timeString(date) +
+ " " + getRealDuration() + "/" + durationInMinutes + "'";
+ } else { // percent
+ return percentRate + "% @" +
+ DateUtil.timeString(date) +
+ " " + getRealDuration() + "/" + durationInMinutes + "'";
+ }
+ }
+
+ public String toStringShort() {
+ if (isAbsolute || isFakeExtended) {
+
+ double rate = 0d;
+ if (isFakeExtended) {
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ Double currentBasalRate = profile.getBasal();
+ rate = (currentBasalRate == null)?0d:(currentBasalRate+netExtendedRate);
+ } else if (isAbsolute){
+ rate = absoluteRate;
+ }
+
+ if(SP.getBoolean(R.string.key_danar_visualizeextendedaspercentage, false) && SP.getBoolean(R.string.key_danar_useextended, false)){
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ if(profile != null) {
+ double basal = profile.getBasal();
+ if(basal != 0){
+ return Math.round(rate*100d/basal) + "% ";
+ }
+ }
+ }
+ return DecimalFormatter.to2Decimal(rate) + "U/h ";
+ } else { // percent
+ return percentRate + "% ";
+ }
+ }
+
+ private String getCalcuatedPercentageIfNeeded(){
+ if (isAbsolute || isFakeExtended) {
+
+ double rate = 0d;
+ if (isFakeExtended) {
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ Double currentBasalRate = profile.getBasal();
+ rate = (currentBasalRate == null)?0d:(currentBasalRate+netExtendedRate);
+ } else if (isAbsolute){
+ rate = absoluteRate;
+ }
+
+ if(SP.getBoolean(R.string.key_danar_visualizeextendedaspercentage, false) && SP.getBoolean(R.string.key_danar_useextended, false)){
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ if(profile != null) {
+ double basal = profile.getBasal();
+ if(basal != 0){
+ return Math.round(rate*100d/basal) + "% ";
+ }
+ }
+ }
+ }
+ return "";
+ }
+
+ public String toStringVeryShort() {
+ if (isAbsolute || isFakeExtended) {
+
+ double rate = 0d;
+ if (isFakeExtended) {
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ Double currentBasalRate = profile.getBasal();
+ rate = (currentBasalRate == null)?0d:(currentBasalRate+netExtendedRate);
+ } else if (isAbsolute){
+ rate = absoluteRate;
+ }
+ return DecimalFormatter.to2Decimal(rate) + "U/h ";
+ } else { // percent
+ return percentRate + "% ";
+ }
+ }
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java
index dc933e59ee..9de76e6bf8 100644
--- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java
+++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java
@@ -1,15 +1,14 @@
package info.nightscout.androidaps.db;
+import android.graphics.Color;
+
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
-import org.json.JSONException;
-import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.Date;
-import java.util.List;
+import java.util.Objects;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
@@ -17,7 +16,7 @@ import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
+import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
@@ -25,81 +24,95 @@ import info.nightscout.utils.DecimalFormatter;
public class Treatment implements DataPointWithLabelInterface {
private static Logger log = LoggerFactory.getLogger(Treatment.class);
- public long getTimeIndex() {
- return created_at.getTime();
- }
+ @DatabaseField(id = true)
+ public long date;
- public void setTimeIndex(long timeIndex) {
- this.timeIndex = timeIndex;
- }
+ @DatabaseField
+ public boolean isValid = true;
- @DatabaseField(id = true, useGetSet = true)
- public long timeIndex;
+ @DatabaseField(index = true)
+ public long pumpId = 0;
+ @DatabaseField
+ public int source = Source.NONE;
@DatabaseField
public String _id;
@DatabaseField
- public Date created_at;
-
+ public double insulin = 0d;
@DatabaseField
- public Double insulin = 0d;
-
- @DatabaseField
- public int insulinType = InsulinInterface.FASTACTINGINSULIN;
-
- @DatabaseField
- public double dia = Constants.defaultDIA;
-
- @DatabaseField
- public Double carbs = 0d;
-
+ public double carbs = 0d;
@DatabaseField
public boolean mealBolus = true; // true for meal bolus , false for correction bolus
+ @DatabaseField
+ public boolean isSMB = false;
+
+ @DatabaseField
+ public int insulinInterfaceID = InsulinInterface.FASTACTINGINSULIN; // currently unused, will be used in the future
+ @DatabaseField
+ public double dia = Constants.defaultDIA; // currently unused, will be used in the future
public Treatment() {
- InsulinInterface insulin = MainApp.getConfigBuilder().getActiveInsulin();
- if (insulin != null) {
- insulinType = insulin.getId();
- dia = insulin.getDia();
- } else {
- insulinType = InsulinInterface.FASTACTINGINSULIN;
- dia = Constants.defaultDIA;
- }
}
- public Treatment(InsulinInterface insulin) {
- insulinType = insulin.getId();
- dia = insulin.getDia();
- }
-
- public void copyFrom(Treatment t) {
- this._id = t._id;
- this.created_at = t.created_at;
- this.insulin = t.insulin;
- this.carbs = t.carbs;
- this.mealBolus = t.mealBolus;
- }
-
- public long getMillisecondsFromStart() {
- return new Date().getTime() - created_at.getTime();
- }
-
- public String log() {
+ public String toString() {
return "Treatment{" +
- "timeIndex: " + timeIndex +
- ", _id: " + _id +
- ", insulin: " + insulin +
- ", carbs: " + carbs +
- ", mealBolus: " + mealBolus +
- ", created_at: " +
+ "date= " + date +
+ ", date= " + DateUtil.dateAndTimeString(date) +
+ ", isValid= " + isValid +
+ ", isSMB= " + isSMB +
+ ", _id= " + _id +
+ ", pumpId= " + pumpId +
+ ", insulin= " + insulin +
+ ", carbs= " + carbs +
+ ", mealBolus= " + mealBolus +
"}";
}
- // DataPointInterface
+ public boolean isDataChanging(Treatment other) {
+ if (date != other.date) {
+ return true;
+ }
+ if (insulin != other.insulin)
+ return true;
+ if (carbs != other.carbs)
+ return true;
+ return false;
+ }
+
+ public boolean isEqual(Treatment other) {
+ if (date != other.date) {
+ return false;
+ }
+ if (insulin != other.insulin)
+ return false;
+ if (carbs != other.carbs)
+ return false;
+ if (mealBolus != other.mealBolus)
+ return false;
+ if (pumpId != other.pumpId)
+ return false;
+ if (isSMB != other.isSMB)
+ return false;
+ if (!Objects.equals(_id, other._id))
+ return false;
+ return true;
+ }
+
+ public void copyFrom(Treatment t) {
+ date = t.date;
+ _id = t._id;
+ insulin = t.insulin;
+ carbs = t.carbs;
+ mealBolus = t.mealBolus;
+ pumpId = t.pumpId;
+ isSMB = t.isSMB;
+ }
+
+ // ----------------- DataPointInterface --------------------
@Override
public double getX() {
- return timeIndex;
+ return date;
}
// default when no sgv around available
@@ -115,36 +128,45 @@ public class Treatment implements DataPointWithLabelInterface {
String label = "";
if (insulin > 0) label += DecimalFormatter.to2Decimal(insulin) + "U";
if (carbs > 0)
- label += (label.equals("") ? "" : " ") + DecimalFormatter.to0Decimal(carbs) + "g";
+ label += "~" + DecimalFormatter.to0Decimal(carbs) + "g";
return label;
}
- public void setYValue(List bgReadingsArray) {
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
- if (profile == null) return;
- for (int r = bgReadingsArray.size() - 1; r >= 0; r--) {
- BgReading reading = bgReadingsArray.get(r);
- if (reading.timeIndex > timeIndex) continue;
- yValue = NSProfile.fromMgdlToUnits(reading.value, profile.getUnits());
- break;
- }
+ @Override
+ public long getDuration() {
+ return 0;
}
- public void sendToNSClient() {
- JSONObject data = new JSONObject();
- try {
- if (mealBolus)
- data.put("eventType", "Meal Bolus");
- else
- data.put("eventType", "Correction Bolus");
- if (insulin != 0d) data.put("insulin", insulin);
- if (carbs != 0d) data.put("carbs", carbs.intValue());
- data.put("created_at", DateUtil.toISOString(created_at));
- data.put("timeIndex", timeIndex);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
+ @Override
+ public PointsWithLabelGraphSeries.Shape getShape() {
+ return PointsWithLabelGraphSeries.Shape.BOLUS;
}
+ @Override
+ public float getSize() {
+ return 10;
+ }
+
+ @Override
+ public int getColor() {
+ if (isValid)
+ return Color.CYAN;
+ else
+ return MainApp.instance().getResources().getColor(android.R.color.holo_red_light);
+ }
+
+ @Override
+ public void setY(double y) {
+ yValue = y;
+ }
+
+ // ----------------- DataPointInterface end --------------------
+
+ public Iob iobCalc(long time, double dia) {
+ if (!isValid)
+ return new Iob();
+
+ InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
+ return insulinInterface.iobCalcForTreatment(this, time, dia);
+ }
}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java
new file mode 100644
index 0000000000..d86fe679fb
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java
@@ -0,0 +1,8 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 25.05.2017.
+ */
+
+public class EventCareportalEventChange {
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.java
new file mode 100644
index 0000000000..78dee60388
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.java
@@ -0,0 +1,8 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 15.05.2017.
+ */
+
+public class EventExtendedBolusChange {
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java
index b3750f59c8..1309625b98 100644
--- a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java
@@ -1,16 +1,7 @@
package info.nightscout.androidaps.events;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
-
/**
* Created by mike on 04.06.2016.
*/
public class EventNewBasalProfile {
- public NSProfile newNSProfile = null;
- public String from = "";
-
- public EventNewBasalProfile(NSProfile newProfile, String from) {
- newNSProfile = newProfile;
- this.from = from;
- }
}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventProfileSwitchChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventProfileSwitchChange.java
new file mode 100644
index 0000000000..476a571d70
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventProfileSwitchChange.java
@@ -0,0 +1,8 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 02.06.2017.
+ */
+
+public class EventProfileSwitchChange {
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshGui.java b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshGui.java
index cb57c361fc..f20dfb9329 100644
--- a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshGui.java
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshGui.java
@@ -4,15 +4,4 @@ package info.nightscout.androidaps.events;
* Created by mike on 13.06.2016.
*/
public class EventRefreshGui {
-
- public boolean isSwitchToLast() {
- return switchToLast;
- }
-
- private final boolean switchToLast;
-
- public EventRefreshGui(boolean switchToLast){
- this.switchToLast = switchToLast;
- }
-
}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.java b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.java
new file mode 100644
index 0000000000..bf14f5f585
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.java
@@ -0,0 +1,13 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 16.06.2017.
+ */
+
+public class EventRefreshOverview {
+ public String from;
+
+ public EventRefreshOverview(String from) {
+ this.from = from;
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.java b/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.java
new file mode 100644
index 0000000000..20073a7cf0
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.java
@@ -0,0 +1,8 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 12.06.2017.
+ */
+
+public class EventReloadProfileSwitchData {
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.java b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.java
new file mode 100644
index 0000000000..38b1b86186
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.java
@@ -0,0 +1,8 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 29.05.2017.
+ */
+
+public class EventReloadTempBasalData {
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.java b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.java
new file mode 100644
index 0000000000..a488a9c7a4
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.java
@@ -0,0 +1,8 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 29.05.2017.
+ */
+
+public class EventReloadTreatmentData {
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.java
new file mode 100644
index 0000000000..d0ef42d830
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.java
@@ -0,0 +1,8 @@
+package info.nightscout.androidaps.events;
+
+/**
+ * Created by mike on 13.01.2017.
+ */
+
+public class EventTempTargetChange {
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/DanaRInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/DanaRInterface.java
new file mode 100644
index 0000000000..abd1d8e304
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/DanaRInterface.java
@@ -0,0 +1,9 @@
+package info.nightscout.androidaps.interfaces;
+
+/**
+ * Created by mike on 12.06.2017.
+ */
+
+public interface DanaRInterface {
+ boolean loadHistory(byte type);
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java
index 6b50178652..0803f65373 100644
--- a/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java
@@ -12,10 +12,14 @@ import info.nightscout.androidaps.db.Treatment;
public interface InsulinInterface {
final int FASTACTINGINSULIN = 0;
final int FASTACTINGINSULINPROLONGED = 1;
+ final int OREF_RAPID_ACTING = 2;
+ final int OREF_ULTRA_RAPID_ACTING = 3;
+ final int OREF_FREE_PEAK = 4;
+
int getId();
String getFriendlyName();
String getComment();
double getDia();
- public Iob iobCalc(Treatment treatment, long time, Double dia);
+ public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia);
}
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/Interval.java b/app/src/main/java/info/nightscout/androidaps/interfaces/Interval.java
new file mode 100644
index 0000000000..f3b4111642
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/Interval.java
@@ -0,0 +1,24 @@
+package info.nightscout.androidaps.interfaces;
+
+/**
+ * Created by mike on 21.05.2017.
+ */
+
+public interface Interval {
+ long durationInMsec();
+ long start();
+
+ // planned end time at time of creation
+ long originalEnd();
+
+ // end time after cut
+ long end();
+
+ void cutEndTo(long end);
+ boolean match(long time);
+ boolean before(long time);
+ boolean after(long time);
+
+ boolean isInProgress();
+ boolean isEndingEvent();
+}
\ No newline at end of file
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java
index efbd51ea72..bf5e66f2a1 100644
--- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java
@@ -8,7 +8,7 @@ import java.util.Date;
public interface PluginBase {
int GENERAL = 1;
int TREATMENT = 2;
- int TEMPBASAL = 3;
+ int SENSITIVITY = 3;
int PROFILE = 4;
int APS = 5;
int PUMP = 6;
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java
index 0924723776..e3b368fe86 100644
--- a/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java
@@ -2,12 +2,14 @@ package info.nightscout.androidaps.interfaces;
import android.support.annotation.Nullable;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
+import info.nightscout.androidaps.data.ProfileStore;
/**
* Created by mike on 14.06.2016.
*/
public interface ProfileInterface {
@Nullable
- NSProfile getProfile();
+ ProfileStore getProfile();
+ String getUnits();
+ String getProfileName();
}
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java
index 78a111d468..d37693d494 100644
--- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java
@@ -6,29 +6,29 @@ package info.nightscout.androidaps.interfaces;
public class PumpDescription {
public static final int NONE = 0;
- public static final int PERCENT = 1;
- public static final int ABSOLUTE = 2;
- public static final int EXTENDED = 4;
+ public static final int PERCENT = 0x01;
+ public static final int ABSOLUTE = 0x02;
public boolean isBolusCapable = true;
public double bolusStep = 0.1d;
public boolean isExtendedBolusCapable = true;
public double extendedBolusStep = 0.1d;
+ public double extendedBolusDurationStep = 30;
+ public double extendedBolusMaxDuration = 12 * 60;
public boolean isTempBasalCapable = true;
- public int lowTempBasalStyle = PERCENT;
- public int highTempBasalStyle = PERCENT;
- public double maxHighTempPercent = 200;
- public double maxHighTempAbsolute = 0; // zero = no limit
- public double lowTempPercentStep = 10;
- public double lowTempAbsoluteStep = 0.05d;
- public int lowTempPercentDuration = 30;
- public int lowTempAbsoluteDuration = 30;
- public double highTempPercentStep = 10;
- public double highTempAbsoluteStep = 0.05d;
- public int highTempPercentDuration = 30;
- public int highTempAbsoluteDuration = 30;
+ public int tempBasalStyle = PERCENT;
+
+ public int maxTempPercent = 200;
+ public int tempPercentStep = 10;
+
+ public double maxTempAbsolute = 10;
+ public double tempAbsoluteStep = 0.05d;
+
+ public int tempDurationStep = 60;
+ public int tempMaxDuration = 12 * 60;
+
public boolean isSetBasalProfileCapable = true;
public double basalStep = 0.01d;
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java
index b9c7e18650..2db7564de7 100644
--- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java
@@ -1,14 +1,12 @@
package info.nightscout.androidaps.interfaces;
-import android.content.Context;
-
import org.json.JSONObject;
import java.util.Date;
+import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.PumpEnactResult;
-import info.nightscout.androidaps.db.TempBasal;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
+import info.nightscout.androidaps.data.Profile;
/**
* Created by mike on 04.06.2016.
@@ -19,32 +17,26 @@ public interface PumpInterface {
boolean isSuspended();
boolean isBusy();
- boolean isTempBasalInProgress();
- boolean isExtendedBoluslInProgress();
-
// Upload to pump new basal profile
int SUCCESS = 0;
int FAILED = 1;
int NOT_NEEDED = 2;
- int setNewBasalProfile(NSProfile profile);
- boolean isThisProfileSet(NSProfile profile);
+ int setNewBasalProfile(Profile profile);
+ boolean isThisProfileSet(Profile profile);
Date lastDataTime();
void refreshDataFromPump(String reason);
double getBaseBasalRate(); // base basal rate, not temp basal
- double getTempBasalAbsoluteRate();
- double getTempBasalRemainingMinutes();
- TempBasal getTempBasal(Date time);
- TempBasal getTempBasal();
- TempBasal getExtendedBolus();
- PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context);
+ PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo);
void stopBolusDelivering();
- PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes);
+ PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean force);
PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes);
PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes);
- PumpEnactResult cancelTempBasal();
+ //some pumps might set a very short temp close to 100% as cancelling a temp can be noisy
+ //when the cancel request is requested by the user (forced), the pump should always do a real cancel
+ PumpEnactResult cancelTempBasal(boolean force);
PumpEnactResult cancelExtendedBolus();
// Status to be passed to NS
@@ -56,4 +48,6 @@ public interface PumpInterface {
// Short info for SMS, Wear etc
String shortStatus(boolean veryShort);
+
+ boolean isFakingTempsByExtendedBoluses();
}
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java
new file mode 100644
index 0000000000..deb649c21f
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java
@@ -0,0 +1,11 @@
+package info.nightscout.androidaps.interfaces;
+
+import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult;
+
+/**
+ * Created by mike on 24.06.2017.
+ */
+
+public interface SensitivityInterface {
+ AutosensResult detectSensitivity(long fromTime, long toTime);
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java
deleted file mode 100644
index aa01d77adb..0000000000
--- a/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package info.nightscout.androidaps.interfaces;
-
-import java.util.Date;
-
-import info.nightscout.androidaps.db.TempBasal;
-import info.nightscout.androidaps.data.IobTotal;
-
-/**
- * Created by mike on 14.06.2016.
- */
-public interface TempBasalsInterface {
- void updateTotalIOB();
- IobTotal getLastCalculation();
- IobTotal getCalculationToTime(long time);
-
- TempBasal getTempBasal (Date time);
- TempBasal getExtendedBolus (Date time);
-
- long oldestDataAvaialable();
-}
diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java
index 9ecacf9e22..5e8b4e52e8 100644
--- a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java
+++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java
@@ -2,19 +2,63 @@ package info.nightscout.androidaps.interfaces;
import java.util.List;
-import info.nightscout.androidaps.data.MealData;
-import info.nightscout.androidaps.db.Treatment;
+import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.IobTotal;
+import info.nightscout.androidaps.data.MealData;
+import info.nightscout.androidaps.db.ExtendedBolus;
+import info.nightscout.androidaps.db.ProfileSwitch;
+import info.nightscout.androidaps.db.TempTarget;
+import info.nightscout.androidaps.db.TemporaryBasal;
+import info.nightscout.androidaps.db.Treatment;
+import info.nightscout.androidaps.data.Intervals;
+import info.nightscout.androidaps.data.ProfileIntervals;
/**
* Created by mike on 14.06.2016.
*/
public interface TreatmentsInterface {
- void updateTotalIOB();
- IobTotal getLastCalculation();
- IobTotal getCalculationToTime(long time);
+ void updateTotalIOBTreatments();
+ void updateTotalIOBTempBasals();
+
+ IobTotal getLastCalculationTreatments();
+ IobTotal getCalculationToTimeTreatments(long time);
+ IobTotal getLastCalculationTempBasals();
+ IobTotal getCalculationToTimeTempBasals(long time);
+
MealData getMealData();
- List getTreatments();
- List getTreatments5MinBack(long time);
+
+ List getTreatmentsFromHistory();
+ List getTreatments5MinBackFromHistory(long time);
+
+ // real basals (not faked by extended bolus)
+ boolean isInHistoryRealTempBasalInProgress();
+ TemporaryBasal getRealTempBasalFromHistory(long time);
+
+ boolean addToHistoryTempBasal(TemporaryBasal tempBasal);
+
+ // basal that can be faked by extended boluses
+ boolean isTempBasalInProgress();
+ TemporaryBasal getTempBasalFromHistory(long time);
+ double getTempBasalAbsoluteRateHistory();
+ double getTempBasalRemainingMinutesFromHistory();
+ Intervals getTemporaryBasalsFromHistory();
+
+ boolean isInHistoryExtendedBoluslInProgress();
+ ExtendedBolus getExtendedBolusFromHistory(long time);
+ Intervals getExtendedBolusesFromHistory();
+
+ boolean addToHistoryExtendedBolus(ExtendedBolus extendedBolus);
+
+ boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo);
+
+ TempTarget getTempTargetFromHistory(long time);
+ Intervals getTempTargetsFromHistory();
+
+ ProfileSwitch getProfileSwitchFromHistory(long time);
+ ProfileIntervals getProfileSwitchesFromHistory();
+ void addToHistoryProfileSwitch(ProfileSwitch profileSwitch);
+
+ long oldestDataAvailable();
+
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java
index 582feb89a6..849f875567 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java
@@ -3,6 +3,8 @@ package info.nightscout.androidaps.plugins.Actions;
import android.app.Activity;
import android.os.Bundle;
+import android.os.Handler;
+import android.os.HandlerThread;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
@@ -10,23 +12,32 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
+import com.crashlytics.android.answers.Answers;
+import com.crashlytics.android.answers.CustomEvent;
import com.squareup.otto.Subscribe;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.db.ExtendedBolus;
+import info.nightscout.androidaps.db.TemporaryBasal;
+import info.nightscout.androidaps.events.EventExtendedBolusChange;
import info.nightscout.androidaps.events.EventInitializationChanged;
-import info.nightscout.androidaps.events.EventRefreshGui;
+import info.nightscout.androidaps.events.EventRefreshOverview;
+import info.nightscout.androidaps.events.EventTempBasalChange;
+import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog;
import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog;
import info.nightscout.androidaps.plugins.Actions.dialogs.NewTempBasalDialog;
+import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
+import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
/**
* A simple {@link Fragment} subclass.
*/
-public class ActionsFragment extends Fragment implements View.OnClickListener {
+public class ActionsFragment extends SubscriberFragment implements View.OnClickListener {
static ActionsPlugin actionsPlugin = new ActionsPlugin();
@@ -37,10 +48,21 @@ public class ActionsFragment extends Fragment implements View.OnClickListener {
Button profileSwitch;
Button tempTarget;
Button extendedBolus;
+ Button extendedBolusCancel;
Button tempBasal;
+ Button tempBasalCancel;
Button fill;
+ private static Handler sHandler;
+ private static HandlerThread sHandlerThread;
+
public ActionsFragment() {
+ super();
+ if (sHandlerThread == null) {
+ sHandlerThread = new HandlerThread(ActionsFragment.class.getSimpleName());
+ sHandlerThread.start();
+ sHandler = new Handler(sHandlerThread.getLooper());
+ }
}
@@ -52,63 +74,103 @@ public class ActionsFragment extends Fragment implements View.OnClickListener {
profileSwitch = (Button) view.findViewById(R.id.actions_profileswitch);
tempTarget = (Button) view.findViewById(R.id.actions_temptarget);
extendedBolus = (Button) view.findViewById(R.id.actions_extendedbolus);
+ extendedBolusCancel = (Button) view.findViewById(R.id.actions_extendedbolus_cancel);
tempBasal = (Button) view.findViewById(R.id.actions_settempbasal);
+ tempBasalCancel = (Button) view.findViewById(R.id.actions_canceltempbasal);
fill = (Button) view.findViewById(R.id.actions_fill);
profileSwitch.setOnClickListener(this);
tempTarget.setOnClickListener(this);
extendedBolus.setOnClickListener(this);
+ extendedBolusCancel.setOnClickListener(this);
tempBasal.setOnClickListener(this);
+ tempBasalCancel.setOnClickListener(this);
fill.setOnClickListener(this);
- updateGUIIfVisible();
+ updateGUI();
return view;
}
- @Override
- public void onPause() {
- super.onPause();
- MainApp.bus().unregister(this);
- }
-
- @Override
- public void onResume() {
- super.onResume();
- MainApp.bus().register(this);
- }
-
@Subscribe
public void onStatusEvent(final EventInitializationChanged ev) {
- updateGUIIfVisible();
+ updateGUI();
}
@Subscribe
- public void onStatusEvent(final EventRefreshGui ev) {
- updateGUIIfVisible();
+ public void onStatusEvent(final EventRefreshOverview ev) {
+ updateGUI();
}
- void updateGUIIfVisible() {
+ @Subscribe
+ public void onStatusEvent(final EventExtendedBolusChange ev) {
+ updateGUI();
+ }
+
+ @Subscribe
+ public void onStatusEvent(final EventTempBasalChange ev) {
+ updateGUI();
+ }
+
+ @Override
+ protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
- if (!MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable || !MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended())
+ if (MainApp.getConfigBuilder().getActiveProfileInterface().getProfile() == null) {
+ tempTarget.setVisibility(View.GONE);
+ profileSwitch.setVisibility(View.GONE);
+ extendedBolus.setVisibility(View.GONE);
+ extendedBolusCancel.setVisibility(View.GONE);
+ tempBasal.setVisibility(View.GONE);
+ tempBasalCancel.setVisibility(View.GONE);
+ fill.setVisibility(View.GONE);
+ return;
+ }
+ boolean allowProfileSwitch = MainApp.getConfigBuilder().getActiveProfileInterface().getProfile().getProfileList().size() > 1;
+ if (!MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable || !MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended() || !allowProfileSwitch)
profileSwitch.setVisibility(View.GONE);
else
profileSwitch.setVisibility(View.VISIBLE);
- if (!MainApp.getConfigBuilder().getPumpDescription().isExtendedBolusCapable || !MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended())
+
+
+ if (!MainApp.getConfigBuilder().getPumpDescription().isExtendedBolusCapable || !MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended() || MainApp.getConfigBuilder().isFakingTempsByExtendedBoluses()) {
extendedBolus.setVisibility(View.GONE);
- else
- extendedBolus.setVisibility(View.VISIBLE);
- if (!MainApp.getConfigBuilder().getPumpDescription().isTempBasalCapable || !MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended())
+ extendedBolusCancel.setVisibility(View.GONE);
+ } else {
+ if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) {
+ extendedBolus.setVisibility(View.GONE);
+ extendedBolusCancel.setVisibility(View.VISIBLE);
+ ExtendedBolus running = MainApp.getConfigBuilder().getExtendedBolusFromHistory(System.currentTimeMillis());
+ extendedBolusCancel.setText(MainApp.instance().getString(R.string.cancel) + " " + running.toString());
+ } else {
+ extendedBolus.setVisibility(View.VISIBLE);
+ extendedBolusCancel.setVisibility(View.GONE);
+ }
+ }
+
+
+ if (!MainApp.getConfigBuilder().getPumpDescription().isTempBasalCapable || !MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended()) {
tempBasal.setVisibility(View.GONE);
- else
- tempBasal.setVisibility(View.VISIBLE);
+ tempBasalCancel.setVisibility(View.GONE);
+ } else {
+ if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
+ tempBasal.setVisibility(View.GONE);
+ tempBasalCancel.setVisibility(View.VISIBLE);
+ final TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis());
+ tempBasalCancel.setText(MainApp.instance().getString(R.string.cancel) + "\n" + activeTemp.toStringShort());
+ } else {
+ tempBasal.setVisibility(View.VISIBLE);
+ tempBasalCancel.setVisibility(View.GONE);
+ }
+ }
+
if (!MainApp.getConfigBuilder().getPumpDescription().isRefillingCapable || !MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended())
fill.setVisibility(View.GONE);
else
fill.setVisibility(View.VISIBLE);
+
if (!Config.APS)
tempTarget.setVisibility(View.GONE);
else
@@ -121,25 +183,48 @@ public class ActionsFragment extends Fragment implements View.OnClickListener {
@Override
public void onClick(View view) {
FragmentManager manager = getFragmentManager();
+ final PumpInterface pump = MainApp.getConfigBuilder();
switch (view.getId()) {
case R.id.actions_profileswitch:
NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog();
- final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, false, false, false, true, false, false);
+ final OptionsToShow profileswitch = CareportalFragment.profileswitch;
profileswitch.executeProfileSwitch = true;
- newDialog.setOptions(profileswitch);
+ newDialog.setOptions(profileswitch, R.string.careportal_profileswitch);
newDialog.show(manager, "NewNSTreatmentDialog");
break;
case R.id.actions_temptarget:
NewNSTreatmentDialog newTTDialog = new NewNSTreatmentDialog();
- final OptionsToShow temptarget = new OptionsToShow(R.id.careportal_temporarytarget, R.string.careportal_temporarytarget, false, false, false, false, true, false, false, false, false, true);
+ final OptionsToShow temptarget = CareportalFragment.temptarget;
temptarget.executeTempTarget = true;
- newTTDialog.setOptions(temptarget);
+ newTTDialog.setOptions(temptarget, R.string.careportal_temporarytarget);
newTTDialog.show(manager, "NewNSTreatmentDialog");
break;
case R.id.actions_extendedbolus:
NewExtendedBolusDialog newExtendedDialog = new NewExtendedBolusDialog();
newExtendedDialog.show(manager, "NewExtendedDialog");
break;
+ case R.id.actions_extendedbolus_cancel:
+ if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) {
+ sHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ pump.cancelExtendedBolus();
+ Answers.getInstance().logCustom(new CustomEvent("CancelExtended"));
+ }
+ });
+ }
+ break;
+ case R.id.actions_canceltempbasal:
+ if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
+ sHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ pump.cancelTempBasal(true);
+ Answers.getInstance().logCustom(new CustomEvent("CancelTemp"));
+ }
+ });
+ }
+ break;
case R.id.actions_settempbasal:
NewTempBasalDialog newTempDialog = new NewTempBasalDialog();
newTempDialog.show(manager, "NewTempDialog");
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java
index dd623c38e9..8a4d4001cb 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java
@@ -26,7 +26,9 @@ import java.text.DecimalFormat;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.PumpEnactResult;
+import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.utils.DecimalFormatter;
@@ -67,8 +69,8 @@ public class FillDialog extends DialogFragment implements OnClickListener {
insulin = (TextView) view.findViewById(R.id.treatments_newtreatment_insulinamount);
Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit);
-
- editInsulin = new PlusMinusEditText(view, R.id.treatments_newtreatment_insulinamount, R.id.treatments_newtreatment_insulinamount_plus, R.id.treatments_newtreatment_insulinamount_minus, 0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false);
+ double bolusstep = MainApp.getConfigBuilder().getPumpDescription().bolusStep;
+ editInsulin = new PlusMinusEditText(view, R.id.treatments_newtreatment_insulinamount, R.id.treatments_newtreatment_insulinamount_plus, R.id.treatments_newtreatment_insulinamount_minus, 0d, 0d, maxInsulin, bolusstep, new DecimalFormat("0.00"), false);
//setup preset buttons
Button button1 = (Button) view.findViewById(R.id.fill_preset_button1);
@@ -159,7 +161,12 @@ public class FillDialog extends DialogFragment implements OnClickListener {
mHandler.post(new Runnable() {
@Override
public void run() {
- PumpEnactResult result = pump.deliverTreatment(MainApp.getConfigBuilder().getActiveInsulin(), finalInsulinAfterConstraints, 0, context, false);
+ DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
+ detailedBolusInfo.insulin = finalInsulinAfterConstraints;
+ detailedBolusInfo.context = context;
+ detailedBolusInfo.source = Source.USER;
+ detailedBolusInfo.isValid = false; // do not count it in IOB (for pump history)
+ PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo);
if (!result.success) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror));
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java
index ee726efb4d..c85d5c4540 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java
@@ -29,15 +29,8 @@ import info.nightscout.utils.SafeParse;
public class NewExtendedBolusDialog extends DialogFragment implements View.OnClickListener {
- Button okButton;
- EditText insulinEdit;
- RadioButton h05Radio;
- RadioButton h10Radio;
- RadioButton h20Radio;
- RadioButton h30Radio;
- RadioButton h40Radio;
-
PlusMinusEditText editInsulin;
+ PlusMinusEditText editDuration;
Handler mHandler;
public static HandlerThread mHandlerThread;
@@ -54,18 +47,16 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli
getDialog().setTitle(getString(R.string.overview_extendedbolus_button));
View view = inflater.inflate(R.layout.overview_newextendedbolus_dialog, container, false);
- okButton = (Button) view.findViewById(R.id.overview_newextendedbolus_okbutton);
- insulinEdit = (EditText) view.findViewById(R.id.overview_newextendedbolus_insulin);
- h05Radio = (RadioButton) view.findViewById(R.id.overview_newextendedbolus_05h);
- h10Radio = (RadioButton) view.findViewById(R.id.overview_newextendedbolus_1h);
- h20Radio = (RadioButton) view.findViewById(R.id.overview_newextendedbolus_2h);
- h30Radio = (RadioButton) view.findViewById(R.id.overview_newextendedbolus_3h);
- h40Radio = (RadioButton) view.findViewById(R.id.overview_newextendedbolus_4h);
Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit);
editInsulin = new PlusMinusEditText(view, R.id.overview_newextendedbolus_insulin, R.id.overview_newextendedbolus_insulin_plus, R.id.overview_newextendedbolus_insulin_minus, 0d, 0d, maxInsulin, 0.1d, new DecimalFormat("0.00"), false);
- okButton.setOnClickListener(this);
+ double extendedDurationStep = MainApp.getConfigBuilder().getPumpDescription().extendedBolusDurationStep;
+ double extendedMaxDuration = MainApp.getConfigBuilder().getPumpDescription().extendedBolusMaxDuration;
+ editDuration = new PlusMinusEditText(view, R.id.overview_newextendedbolus_duration, R.id.overview_newextendedbolus_duration_plus, R.id.overview_newextendedbolus_duration_minus, extendedDurationStep, extendedDurationStep, extendedMaxDuration, extendedDurationStep, new DecimalFormat("0"), false);
+
+ view.findViewById(R.id.ok).setOnClickListener(this);
+ view.findViewById(R.id.cancel).setOnClickListener(this);
return view;
}
@@ -79,14 +70,10 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli
@Override
public void onClick(View view) {
switch (view.getId()) {
- case R.id.overview_newextendedbolus_okbutton:
+ case R.id.ok:
try {
- Double insulin = SafeParse.stringToDouble(insulinEdit.getText().toString());
- int durationInMinutes = 30;
- if (h10Radio.isChecked()) durationInMinutes = 60;
- if (h20Radio.isChecked()) durationInMinutes = 120;
- if (h30Radio.isChecked()) durationInMinutes = 180;
- if (h40Radio.isChecked()) durationInMinutes = 240;
+ Double insulin = SafeParse.stringToDouble(editInsulin.getText());
+ int durationInMinutes = SafeParse.stringToInt(editDuration.getText());
String confirmMessage = getString(R.string.setextendedbolusquestion);
@@ -130,6 +117,10 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli
} catch (Exception e) {
e.printStackTrace();
}
+ break;
+ case R.id.cancel:
+ dismiss();
+ break;
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java
index 6930a0de21..9582dced87 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java
@@ -11,45 +11,39 @@ import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.Button;
-import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
+import android.widget.RelativeLayout;
import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
import java.text.DecimalFormat;
-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.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.utils.NumberPicker;
import info.nightscout.utils.PlusMinusEditText;
import info.nightscout.utils.SafeParse;
public class NewTempBasalDialog extends DialogFragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
- Button okButton;
- EditText basalPercentEdit;
- EditText basalAbsoluteEdit;
RadioButton percentRadio;
RadioButton absoluteRadio;
RadioGroup basalTypeRadioGroup;
- RadioButton h05Radio;
- RadioButton h10Radio;
- RadioButton h20Radio;
- RadioButton h30Radio;
- RadioButton h40Radio;
+ LinearLayout typeSelectorLayout;
LinearLayout percentLayout;
LinearLayout absoluteLayout;
- PlusMinusEditText basalPercentPM;
- PlusMinusEditText basalAbsolutePM;
+ NumberPicker basalPercent;
+ NumberPicker basalAbsolute;
+ NumberPicker duration;
Handler mHandler;
public static HandlerThread mHandlerThread;
@@ -66,75 +60,85 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi
getDialog().setTitle(getString(R.string.overview_tempbasal_button));
View view = inflater.inflate(R.layout.overview_newtempbasal_dialog, container, false);
- okButton = (Button) view.findViewById(R.id.overview_newtempbasal_okbutton);
- basalPercentEdit = (EditText) view.findViewById(R.id.overview_newtempbasal_basalpercentinput);
- basalAbsoluteEdit = (EditText) view.findViewById(R.id.overview_newtempbasal_basalabsoluteinput);
+
percentLayout = (LinearLayout) view.findViewById(R.id.overview_newtempbasal_percent_layout);
absoluteLayout = (LinearLayout) view.findViewById(R.id.overview_newtempbasal_absolute_layout);
percentRadio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_percent_radio);
basalTypeRadioGroup = (RadioGroup) view.findViewById(R.id.overview_newtempbasal_radiogroup);
absoluteRadio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_absolute_radio);
- h05Radio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_05h);
- h10Radio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_1h);
- h20Radio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_2h);
- h30Radio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_3h);
- h40Radio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_4h);
+ typeSelectorLayout = (LinearLayout) view.findViewById(R.id.overview_newtempbasal_typeselector_layout);
- Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit);
- basalPercentPM = new PlusMinusEditText(view, R.id.overview_newtempbasal_basalpercentinput, R.id.overview_newtempbasal_basalpercent_plus, R.id.overview_newtempbasal_basalpercent_minus, 100d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true);
+ PumpDescription pumpDescription = MainApp.getConfigBuilder().getPumpDescription();
- Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit);
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
- Double currentBasal = 0d;
- if (profile != null) currentBasal = profile.getBasal(NSProfile.secondsFromMidnight());
- basalAbsolutePM = new PlusMinusEditText(view, R.id.overview_newtempbasal_basalabsoluteinput, R.id.overview_newtempbasal_basalabsolute_plus, R.id.overview_newtempbasal_basalabsolute_minus, currentBasal, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true);
+ basalPercent = (NumberPicker) view.findViewById(R.id.overview_newtempbasal_basalpercentinput);
+ double maxTempPercent = pumpDescription.maxTempPercent;
+ double tempPercentStep = pumpDescription.tempPercentStep;
+ basalPercent.setParams(100d, 0d, maxTempPercent, tempPercentStep, new DecimalFormat("0"), true);
- absoluteLayout.setVisibility(View.GONE);
- okButton.setOnClickListener(this);
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ Double currentBasal = profile != null ? profile.getBasal() : 0d;
+ basalAbsolute = (NumberPicker) view.findViewById(R.id.overview_newtempbasal_basalabsoluteinput);
+ basalAbsolute.setParams(currentBasal, 0d, pumpDescription.maxTempAbsolute, pumpDescription.tempAbsoluteStep, new DecimalFormat("0.00"), true);
+
+ double tempDurationStep = pumpDescription.tempDurationStep;
+ double tempMaxDuration = pumpDescription.tempMaxDuration;
+ duration = (NumberPicker) view.findViewById(R.id.overview_newtempbasal_duration);
+ duration.setParams(tempDurationStep, tempDurationStep, tempMaxDuration, tempDurationStep, new DecimalFormat("0"), false);
+
+ if ((pumpDescription.tempBasalStyle & PumpDescription.PERCENT) == PumpDescription.PERCENT && (pumpDescription.tempBasalStyle & PumpDescription.ABSOLUTE) == PumpDescription.ABSOLUTE) {
+ // Both allowed
+ typeSelectorLayout.setVisibility(View.VISIBLE);
+ } else {
+ typeSelectorLayout.setVisibility(View.GONE);
+ }
+
+ if ((pumpDescription.tempBasalStyle & PumpDescription.PERCENT) == PumpDescription.PERCENT) {
+ percentRadio.setChecked(true);
+ absoluteRadio.setChecked(false);
+ percentLayout.setVisibility(View.VISIBLE);
+ absoluteLayout.setVisibility(View.GONE);
+ } else if ((pumpDescription.tempBasalStyle & PumpDescription.ABSOLUTE) == PumpDescription.ABSOLUTE) {
+ percentRadio.setChecked(false);
+ absoluteRadio.setChecked(true);
+ percentLayout.setVisibility(View.GONE);
+ absoluteLayout.setVisibility(View.VISIBLE);
+ }
+
+ view.findViewById(R.id.ok).setOnClickListener(this);
+ view.findViewById(R.id.cancel).setOnClickListener(this);
basalTypeRadioGroup.setOnCheckedChangeListener(this);
return view;
}
- @Override
- public void onResume() {
- super.onResume();
- if (getDialog() != null)
- getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- }
-
@Override
public void onClick(View view) {
switch (view.getId()) {
- case R.id.overview_newtempbasal_okbutton:
+ case R.id.ok:
try {
- int basalPercent = 0;
- Double basalAbsolute = 0d;
+ int percent = 0;
+ Double absolute = 0d;
final boolean setAsPercent = percentRadio.isChecked();
- int durationInMinutes = 30;
- if (h10Radio.isChecked()) durationInMinutes = 60;
- if (h20Radio.isChecked()) durationInMinutes = 120;
- if (h30Radio.isChecked()) durationInMinutes = 180;
- if (h40Radio.isChecked()) durationInMinutes = 240;
+ int durationInMinutes = SafeParse.stringToInt(duration.getText());
String confirmMessage = getString(R.string.setbasalquestion);
if (setAsPercent) {
- int basalPercentInput = SafeParse.stringToDouble(basalPercentEdit.getText().toString()).intValue();
- basalPercent = MainApp.getConfigBuilder().applyBasalConstraints(basalPercentInput);
- confirmMessage += "\n" + basalPercent + "% ";
+ int basalPercentInput = SafeParse.stringToInt(basalPercent.getText());
+ percent = MainApp.getConfigBuilder().applyBasalConstraints(basalPercentInput);
+ confirmMessage += "\n" + percent + "% ";
confirmMessage += "\n" + getString(R.string.duration) + " " + durationInMinutes + "min ?";
- if (basalPercent != basalPercentInput)
+ if (percent != basalPercentInput)
confirmMessage += "\n" + getString(R.string.constraintapllied);
} else {
- Double basalAbsoluteInput = SafeParse.stringToDouble(basalAbsoluteEdit.getText().toString());
- basalAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(basalAbsoluteInput);
- confirmMessage += "\n" + basalAbsolute + " U/h ";
+ Double basalAbsoluteInput = SafeParse.stringToDouble(basalAbsolute.getText());
+ absolute = MainApp.getConfigBuilder().applyBasalConstraints(basalAbsoluteInput);
+ confirmMessage += "\n" + absolute + " U/h ";
confirmMessage += "\n" + getString(R.string.duration) + " " + durationInMinutes + "min ?";
- if (basalAbsolute - basalAbsoluteInput != 0d)
+ if (absolute - basalAbsoluteInput != 0d)
confirmMessage += "\n" + getString(R.string.constraintapllied);
}
- final int finalBasalPercent = basalPercent;
- final Double finalBasal = basalAbsolute;
+ final int finalBasalPercent = percent;
+ final Double finalBasal = absolute;
final int finalDurationInMinutes = durationInMinutes;
final Context context = getContext();
@@ -151,7 +155,7 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi
if (setAsPercent) {
result = pump.setTempBasalPercent(finalBasalPercent, finalDurationInMinutes);
} else {
- result = pump.setTempBasalAbsolute(finalBasal, finalDurationInMinutes);
+ result = pump.setTempBasalAbsolute(finalBasal, finalDurationInMinutes, false);
}
if (!result.success) {
if (context instanceof Activity) {
@@ -178,6 +182,10 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi
} catch (Exception e) {
e.printStackTrace();
}
+ break;
+ case R.id.cancel:
+ dismiss();
+ break;
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java
index 7c603ecaed..6ddda8cd68 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java
@@ -1,20 +1,38 @@
package info.nightscout.androidaps.plugins.Careportal;
+import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
+import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.TextView;
+import com.squareup.otto.Subscribe;
+
+import info.nightscout.androidaps.BuildConfig;
+import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.db.CareportalEvent;
+import info.nightscout.androidaps.events.EventCareportalEventChange;
import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog;
+import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
+import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
-public class CareportalFragment extends Fragment implements View.OnClickListener {
+public class CareportalFragment extends SubscriberFragment implements View.OnClickListener {
static CareportalPlugin careportalPlugin;
+ TextView iage;
+ TextView cage;
+ TextView sage;
+ TextView pbage;
+
+ View statsLayout;
+
static public CareportalPlugin getPlugin() {
if (careportalPlugin == null) {
careportalPlugin = new CareportalPlugin();
@@ -23,25 +41,26 @@ public class CareportalFragment extends Fragment implements View.OnClickListener
}
// bg,insulin,carbs,prebolus,duration,percent,absolute,profile,split,temptarget
- final OptionsToShow bgcheck = new OptionsToShow(R.id.careportal_bgcheck, R.string.careportal_bgcheck, true, true, true, false, false, false, false, false, false, false);
- final OptionsToShow snackbolus = new OptionsToShow(R.id.careportal_snackbolus, R.string.careportal_snackbolus, true, true, true, true, false, false, false, false, false, false);
- final OptionsToShow mealbolus = new OptionsToShow(R.id.careportal_mealbolus, R.string.careportal_mealbolus, true, true, true, true, false, false, false, false, false, false);
- final OptionsToShow correctionbolus = new OptionsToShow(R.id.careportal_correctionbolus, R.string.careportal_correctionbolus, true, true, true, true, false, false, false, false, false, false);
- final OptionsToShow carbcorrection = new OptionsToShow(R.id.careportal_carbscorrection, R.string.careportal_carbscorrection, true, false, true, false, false, false, false, false, false, false);
- final OptionsToShow combobolus = new OptionsToShow(R.id.careportal_combobolus, R.string.careportal_combobolus, true, true, true, true, true, false, false, false, true, false);
- final OptionsToShow announcement = new OptionsToShow(R.id.careportal_announcement, R.string.careportal_announcement, true, false, false, false, false, false, false, false, false, false);
- final OptionsToShow note = new OptionsToShow(R.id.careportal_note, R.string.careportal_note, true, false, false, false, true, false, false, false, false, false);
- final OptionsToShow question = new OptionsToShow(R.id.careportal_question, R.string.careportal_question, true, false, false, false, false, false, false, false, false, false);
- final OptionsToShow exercise = new OptionsToShow(R.id.careportal_exercise, R.string.careportal_exercise, false, false, false, false, true, false, false, false, false, false);
- final OptionsToShow sitechange = new OptionsToShow(R.id.careportal_pumpsitechange, R.string.careportal_pumpsitechange, true, true, false, false, false, false, false, false, false, false);
- final OptionsToShow sensorstart = new OptionsToShow(R.id.careportal_cgmsensorstart, R.string.careportal_cgmsensorstart, true, false, false, false, false, false, false, false, false, false);
- final OptionsToShow sensorchange = new OptionsToShow(R.id.careportal_cgmsensorinsert, R.string.careportal_cgmsensorinsert, true, false, false, false, false, false, false, false, false, false);
- final OptionsToShow insulinchange = new OptionsToShow(R.id.careportal_insulincartridgechange, R.string.careportal_insulincartridgechange, true, false, false, false, false, false, false, false, false, false);
- final OptionsToShow tempbasalstart = new OptionsToShow(R.id.careportal_tempbasalstart, R.string.careportal_tempbasalstart, true, false, false, false, true, true, true, false, false, false);
- final OptionsToShow tempbasalend = new OptionsToShow(R.id.careportal_tempbasalend, R.string.careportal_tempbasalend, true, false, false, false, false, false, false, false, false, false);
- final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, false, false, false, true, false, false);
- final OptionsToShow openapsoffline = new OptionsToShow(R.id.careportal_openapsoffline, R.string.careportal_openapsoffline, false, false, false, false, true, false, false, false, false, false);
- final OptionsToShow temptarget = new OptionsToShow(R.id.careportal_temporarytarget, R.string.careportal_temporarytarget, false, false, false, false, true, false, false, false, false, true);
+ public static final OptionsToShow bgcheck = new OptionsToShow(R.id.careportal_bgcheck, R.string.careportal_bgcheck, true, true, true, false, false, false, false, false, false, false);
+ public static final OptionsToShow snackbolus = new OptionsToShow(R.id.careportal_snackbolus, R.string.careportal_snackbolus, true, true, true, true, false, false, false, false, false, false);
+ public static final OptionsToShow mealbolus = new OptionsToShow(R.id.careportal_mealbolus, R.string.careportal_mealbolus, true, true, true, true, false, false, false, false, false, false);
+ public static final OptionsToShow correctionbolus = new OptionsToShow(R.id.careportal_correctionbolus, R.string.careportal_correctionbolus, true, true, true, true, false, false, false, false, false, false);
+ public static final OptionsToShow carbcorrection = new OptionsToShow(R.id.careportal_carbscorrection, R.string.careportal_carbscorrection, true, false, true, false, false, false, false, false, false, false);
+ public static final OptionsToShow combobolus = new OptionsToShow(R.id.careportal_combobolus, R.string.careportal_combobolus, true, true, true, true, true, false, false, false, true, false);
+ public static final OptionsToShow announcement = new OptionsToShow(R.id.careportal_announcement, R.string.careportal_announcement, true, false, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow note = new OptionsToShow(R.id.careportal_note, R.string.careportal_note, true, false, false, false, true, false, false, false, false, false);
+ public static final OptionsToShow question = new OptionsToShow(R.id.careportal_question, R.string.careportal_question, true, false, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow exercise = new OptionsToShow(R.id.careportal_exercise, R.string.careportal_exercise, false, false, false, false, true, false, false, false, false, false);
+ public static final OptionsToShow sitechange = new OptionsToShow(R.id.careportal_pumpsitechange, R.string.careportal_pumpsitechange, true, true, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow sensorstart = new OptionsToShow(R.id.careportal_cgmsensorstart, R.string.careportal_cgmsensorstart, true, false, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow sensorchange = new OptionsToShow(R.id.careportal_cgmsensorinsert, R.string.careportal_cgmsensorinsert, true, false, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow insulinchange = new OptionsToShow(R.id.careportal_insulincartridgechange, R.string.careportal_insulincartridgechange, true, false, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow pumpbatterychange = new OptionsToShow(R.id.careportal_pumpbatterychange, R.string.careportal_pumpbatterychange, true, false, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow tempbasalstart = new OptionsToShow(R.id.careportal_tempbasalstart, R.string.careportal_tempbasalstart, true, false, false, false, true, true, true, false, false, false);
+ public static final OptionsToShow tempbasalend = new OptionsToShow(R.id.careportal_tempbasalend, R.string.careportal_tempbasalend, true, false, false, false, false, false, false, false, false, false);
+ public static final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, true, false, false, true, false, false);
+ public static final OptionsToShow openapsoffline = new OptionsToShow(R.id.careportal_openapsoffline, R.string.careportal_openapsoffline, false, false, false, false, true, false, false, false, false, false);
+ public static final OptionsToShow temptarget = new OptionsToShow(R.id.careportal_temporarytarget, R.string.careportal_temporarytarget, false, false, false, false, true, false, false, false, false, true);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -57,6 +76,7 @@ public class CareportalFragment extends Fragment implements View.OnClickListener
view.findViewById(R.id.careportal_carbscorrection).setOnClickListener(this);
view.findViewById(R.id.careportal_exercise).setOnClickListener(this);
view.findViewById(R.id.careportal_insulincartridgechange).setOnClickListener(this);
+ view.findViewById(R.id.careportal_pumpbatterychange).setOnClickListener(this);
view.findViewById(R.id.careportal_mealbolus).setOnClickListener(this);
view.findViewById(R.id.careportal_note).setOnClickListener(this);
view.findViewById(R.id.careportal_profileswitch).setOnClickListener(this);
@@ -67,70 +87,90 @@ public class CareportalFragment extends Fragment implements View.OnClickListener
view.findViewById(R.id.careportal_tempbasalstart).setOnClickListener(this);
view.findViewById(R.id.careportal_openapsoffline).setOnClickListener(this);
view.findViewById(R.id.careportal_temporarytarget).setOnClickListener(this);
+
+ iage = (TextView) view.findViewById(R.id.careportal_insulinage);
+ cage = (TextView) view.findViewById(R.id.careportal_canulaage);
+ sage = (TextView) view.findViewById(R.id.careportal_sensorage);
+ pbage = (TextView) view.findViewById(R.id.careportal_pbage);
+
+ statsLayout = (View) view.findViewById(R.id.careportal_stats);
+
+ if (BuildConfig.NSCLIENTOLNY)
+ statsLayout.setVisibility(View.GONE); // visible on overview
+
+ updateGUI();
return view;
}
@Override
public void onClick(View view) {
- FragmentManager manager = getFragmentManager();
+ action(view.getId(), getFragmentManager());
+ }
+
+ public static void action(int id, FragmentManager manager) {
NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog();
- switch (view.getId()) {
+ switch (id) {
case R.id.careportal_bgcheck:
- newDialog.setOptions(bgcheck);
+ newDialog.setOptions(bgcheck, R.string.careportal_bgcheck);
break;
case R.id.careportal_announcement:
- newDialog.setOptions(announcement);
+ newDialog.setOptions(announcement, R.string.careportal_announcement);
break;
case R.id.careportal_cgmsensorinsert:
- newDialog.setOptions(sensorchange);
+ newDialog.setOptions(sensorchange, R.string.careportal_cgmsensorinsert);
break;
case R.id.careportal_cgmsensorstart:
- newDialog.setOptions(sensorstart);
+ newDialog.setOptions(sensorstart, R.string.careportal_cgmsensorstart);
break;
case R.id.careportal_combobolus:
- newDialog.setOptions(combobolus);
+ newDialog.setOptions(combobolus, R.string.careportal_combobolus);
break;
case R.id.careportal_correctionbolus:
- newDialog.setOptions(correctionbolus);
+ newDialog.setOptions(correctionbolus, R.string.careportal_correctionbolus);
break;
case R.id.careportal_carbscorrection:
- newDialog.setOptions(carbcorrection);
+ newDialog.setOptions(carbcorrection, R.string.careportal_carbscorrection);
break;
case R.id.careportal_exercise:
- newDialog.setOptions(exercise);
+ newDialog.setOptions(exercise, R.string.careportal_exercise);
break;
case R.id.careportal_insulincartridgechange:
- newDialog.setOptions(insulinchange);
+ newDialog.setOptions(insulinchange, R.string.careportal_insulincartridgechange);
+ break;
+ case R.id.careportal_pumpbatterychange:
+ newDialog.setOptions(pumpbatterychange, R.string.careportal_pumpbatterychange);
break;
case R.id.careportal_mealbolus:
- newDialog.setOptions(mealbolus);
+ newDialog.setOptions(mealbolus, R.string.careportal_mealbolus);
break;
case R.id.careportal_note:
- newDialog.setOptions(note);
+ newDialog.setOptions(note, R.string.careportal_note);
break;
case R.id.careportal_profileswitch:
- newDialog.setOptions(profileswitch);
+ profileswitch.executeProfileSwitch = false;
+ newDialog.setOptions(profileswitch, R.string.careportal_profileswitch);
break;
case R.id.careportal_pumpsitechange:
- newDialog.setOptions(sitechange);
+ newDialog.setOptions(sitechange, R.string.careportal_pumpsitechange);
break;
case R.id.careportal_question:
- newDialog.setOptions(question);
+ newDialog.setOptions(question, R.string.careportal_question);
break;
case R.id.careportal_snackbolus:
- newDialog.setOptions(snackbolus);
+ newDialog.setOptions(snackbolus, R.string.careportal_snackbolus);
break;
case R.id.careportal_tempbasalstart:
- newDialog.setOptions(tempbasalstart);
+ newDialog.setOptions(tempbasalstart, R.string.careportal_tempbasalstart);
break;
case R.id.careportal_tempbasalend:
- newDialog.setOptions(tempbasalend);
+ newDialog.setOptions(tempbasalend, R.string.careportal_tempbasalend);
break;
case R.id.careportal_openapsoffline:
- newDialog.setOptions(openapsoffline);
+ newDialog.setOptions(openapsoffline, R.string.careportal_openapsoffline);
break;
case R.id.careportal_temporarytarget:
- newDialog.setOptions(temptarget);
+ temptarget.executeTempTarget = false;
+ newDialog.setOptions(temptarget, R.string.careportal_temporarytarget);
break;
default:
newDialog = null;
@@ -139,4 +179,45 @@ public class CareportalFragment extends Fragment implements View.OnClickListener
newDialog.show(manager, "NewNSTreatmentDialog");
}
+ @Subscribe
+ public void onStatusEvent(final EventCareportalEventChange c) {
+ updateGUI();
+ }
+
+ @Override
+ protected void updateGUI() {
+ Activity activity = getActivity();
+ updateAge(activity, sage, iage, cage, pbage);
+ }
+
+ public static void updateAge(Activity activity, final TextView sage, final TextView iage, final TextView cage, final TextView pbage) {
+ if (activity != null) {
+ activity.runOnUiThread(
+ new Runnable() {
+ @Override
+ public void run() {
+ CareportalEvent careportalEvent;
+ String notavailable = OverviewFragment.shorttextmode ? "-" : MainApp.sResources.getString(R.string.notavailable);
+ if (sage != null) {
+ careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SENSORCHANGE);
+ sage.setText(careportalEvent != null ? careportalEvent.age() : notavailable);
+ }
+ if (iage != null) {
+ careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.INSULINCHANGE);
+ iage.setText(careportalEvent != null ? careportalEvent.age() : notavailable);
+ }
+ if (cage != null) {
+ careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SITECHANGE);
+ cage.setText(careportalEvent != null ? careportalEvent.age() : notavailable);
+ }
+ if (pbage != null) {
+ careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.PUMPBATTERYCHANGE);
+ pbage.setText(careportalEvent != null ? careportalEvent.age() : notavailable);
+ }
+ }
+ }
+ );
+ }
+ }
+
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java
index e6981083d0..fed3ff1a43 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java
@@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.Careportal;
+import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
@@ -57,7 +58,7 @@ public class CareportalPlugin implements PluginBase {
@Override
public boolean showInList(int type) {
- return true;
+ return !Config.NSCLIENT;
}
@Override
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java
index 9c89fdd948..b10ab5dab8 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java
@@ -7,7 +7,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.app.DialogFragment;
-import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
@@ -16,18 +15,15 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
-import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
-import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
-import com.j256.ormlite.dao.Dao;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.RadialPickerLayout;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;
@@ -37,7 +33,6 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
@@ -47,73 +42,68 @@ import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.GlucoseStatus;
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.androidaps.data.ProfileStore;
+import info.nightscout.androidaps.db.CareportalEvent;
+import info.nightscout.androidaps.db.ProfileSwitch;
+import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
-import info.nightscout.androidaps.plugins.ProfileCircadianPercentage.CircadianPercentageProfilePlugin;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
-import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange;
+import info.nightscout.androidaps.plugins.ProfileCircadianPercentage.CircadianPercentageProfilePlugin;
import info.nightscout.utils.DateUtil;
-import info.nightscout.utils.PlusMinusEditText;
+import info.nightscout.utils.NSUpload;
+import info.nightscout.utils.NumberPicker;
import info.nightscout.utils.SP;
import info.nightscout.utils.SafeParse;
-import info.nightscout.utils.ToastUtils;
import info.nightscout.utils.Translator;
public class NewNSTreatmentDialog extends DialogFragment implements View.OnClickListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
private static Logger log = LoggerFactory.getLogger(NewNSTreatmentDialog.class);
- private FragmentActivity context;
+ private Activity context;
private static OptionsToShow options;
+ private static String event;
- NSProfile profile;
+ Profile profile;
+ ProfileStore profileStore;
String units;
- RelativeLayout layoutBg;
+ TextView eventTypeText;
+ LinearLayout layoutBg;
LinearLayout layoutBgSource;
- RelativeLayout layoutInsulin;
- RelativeLayout layoutCarbs;
- RelativeLayout layoutSplit;
- RelativeLayout layoutDuration;
- RelativeLayout layoutPercent;
- RelativeLayout layoutAbsolute;
- RelativeLayout layoutCarbTime;
- RelativeLayout layoutProfile;
+ LinearLayout layoutInsulin;
+ LinearLayout layoutCarbs;
+ LinearLayout layoutSplit;
+ LinearLayout layoutDuration;
+ LinearLayout layoutPercent;
+ LinearLayout layoutAbsolute;
+ LinearLayout layoutCarbTime;
+ LinearLayout layoutProfile;
LinearLayout layoutTempTarget;
- Button dateButton;
- Button timeButton;
- Button okButton;
- Button cancelButton;
+ TextView dateButton;
+ TextView timeButton;
TextView bgUnitsView;
RadioButton meterRadioButton;
RadioButton sensorRadioButton;
RadioButton otherRadioButton;
EditText notesEdit;
- EditText bgInputEdit;
- EditText insulinEdit;
- EditText carbsEdit;
- EditText percentEdit;
- EditText absoluteEdit;
- EditText durationeEdit;
- EditText carbTimeEdit;
- EditText splitEdit;
Spinner profileSpinner;
- EditText low;
- EditText high;
Spinner reasonSpinner;
- PlusMinusEditText editBg;
- PlusMinusEditText editCarbs;
- PlusMinusEditText editInsulin;
- PlusMinusEditText editSplit;
- PlusMinusEditText editDuration;
- PlusMinusEditText editPercent;
- PlusMinusEditText editAbsolute;
- PlusMinusEditText editCarbTime;
+ NumberPicker editBg;
+ NumberPicker editCarbs;
+ NumberPicker editInsulin;
+ NumberPicker editSplit;
+ NumberPicker editDuration;
+ NumberPicker editPercent;
+ NumberPicker editAbsolute;
+ NumberPicker editCarbTime;
+ NumberPicker editTemptarget;
Date eventTime;
@@ -121,8 +111,9 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
private static HandlerThread sHandlerThread;
- public void setOptions(OptionsToShow options) {
+ public void setOptions(OptionsToShow options, int event) {
this.options = options;
+ this.event = MainApp.sResources.getString(event);
}
public NewNSTreatmentDialog() {
@@ -136,10 +127,16 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
@Override
public void onAttach(Activity activity) {
- context = (FragmentActivity) activity;
+ context = activity;
super.onAttach(activity);
}
+ @Override
+ public void onDetach() {
+ super.onDetach();
+ this.context = null;
+ }
+
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@@ -147,29 +144,118 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
setStyle(DialogFragment.STYLE_NORMAL, getTheme());
View view = inflater.inflate(R.layout.careportal_newnstreatment_dialog, container, false);
- layoutBg = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_bg_layout);
+ layoutBg = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_bg_layout);
layoutBgSource = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_bgsource_layout);
- layoutInsulin = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_insulin_layout);
- layoutCarbs = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_carbs_layout);
- layoutSplit = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_split_layout);
- layoutDuration = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_duration_layout);
- layoutPercent = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_percent_layout);
- layoutAbsolute = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_absolute_layout);
- layoutCarbTime = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_carbtime_layout);
- layoutProfile = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_profile_layout);
+ layoutInsulin = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_insulin_layout);
+ layoutCarbs = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_carbs_layout);
+ layoutSplit = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_split_layout);
+ layoutDuration = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_duration_layout);
+ layoutPercent = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_percent_layout);
+ layoutAbsolute = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_absolute_layout);
+ layoutCarbTime = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_carbtime_layout);
+ layoutProfile = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_profile_layout);
layoutTempTarget = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_temptarget_layout);
+ eventTypeText = (TextView) view.findViewById(R.id.careportal_newnstreatment_eventtype);
+ eventTypeText.setText(event);
bgUnitsView = (TextView) view.findViewById(R.id.careportal_newnstreatment_bgunits);
meterRadioButton = (RadioButton) view.findViewById(R.id.careportal_newnstreatment_meter);
sensorRadioButton = (RadioButton) view.findViewById(R.id.careportal_newnstreatment_sensor);
otherRadioButton = (RadioButton) view.findViewById(R.id.careportal_newnstreatment_other);
profileSpinner = (Spinner) view.findViewById(R.id.careportal_newnstreatment_profile);
- bgInputEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_bginput);
- insulinEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_insulininput);
- carbsEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_carbsinput);
- percentEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_percentinput);
- percentEdit.addTextChangedListener(new TextWatcher() {
+ notesEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_notes);
+
+ reasonSpinner = (Spinner) view.findViewById(R.id.careportal_newnstreatment_temptarget_reason);
+
+ eventTime = new Date();
+ dateButton = (TextView) view.findViewById(R.id.careportal_newnstreatment_eventdate);
+ timeButton = (TextView) view.findViewById(R.id.careportal_newnstreatment_eventtime);
+ dateButton.setText(DateUtil.dateString(eventTime));
+ timeButton.setText(DateUtil.timeString(eventTime));
+ dateButton.setOnClickListener(this);
+ timeButton.setOnClickListener(this);
+
+ view.findViewById(R.id.ok).setOnClickListener(this);
+ view.findViewById(R.id.cancel).setOnClickListener(this);
+
+ // profile
+ profile = MainApp.getConfigBuilder().getProfile();
+ profileStore = ConfigBuilderPlugin.getActiveProfileInterface().getProfile();
+ ArrayList profileList;
+ units = profile != null ? profile.getUnits() : Constants.MGDL;
+ profileList = profileStore.getProfileList();
+ ArrayAdapter adapter = new ArrayAdapter(getContext(),
+ R.layout.spinner_centered, profileList);
+ profileSpinner.setAdapter(adapter);
+ // set selected to actual profile
+ for (int p = 0; p < profileList.size(); p++) {
+ if (profileList.get(p).equals(MainApp.getConfigBuilder().getProfileName()))
+ profileSpinner.setSelection(p);
+ }
+
+ // temp target
+ ArrayList reasonList = new ArrayList();
+ reasonList.add(MainApp.sResources.getString(R.string.eatingsoon));
+ reasonList.add(MainApp.sResources.getString(R.string.activity));
+ reasonList.add(MainApp.sResources.getString(R.string.manual));
+ ArrayAdapter adapterReason = new ArrayAdapter(getContext(),
+ R.layout.spinner_centered, reasonList);
+ reasonSpinner.setAdapter(adapterReason);
+
+ // bg
+ bgUnitsView.setText(units);
+
+ Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile != null ? profile.getUnits() : Constants.MGDL);
+ editBg = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_bginput);
+ editTemptarget = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_temptarget);
+ if (profile == null) {
+ editBg.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false);
+ editTemptarget.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false);
+ } else if (profile.getUnits().equals(Constants.MMOL)) {
+ editBg.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false);
+ editTemptarget.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false);
+ } else {
+ editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
+ editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
+ }
+ editBg.addTextChangedListener(new TextWatcher() {
+ public void afterTextChanged(Editable s) {
+ }
+
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+ }
+
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
+ if (sensorRadioButton.isChecked()) meterRadioButton.setChecked(true);
+ }
+ });
+
+ sensorRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile.getUnits());
+ editBg.setValue(bg);
+ }
+ });
+
+ Integer maxCarbs = MainApp.getConfigBuilder().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit);
+ editCarbs = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbsinput);
+ editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false);
+
+ Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit);
+ editInsulin = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_insulininput);
+ editInsulin.setParams(0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false);
+
+ editSplit = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_splitinput);
+ editSplit.setParams(100d, 0d, 100d, 5d, new DecimalFormat("0"), true);
+ editDuration = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_durationinput);
+ editDuration.setParams(0d, 0d, 24 * 60d, 10d, new DecimalFormat("0"), false);
+
+ Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit);
+ editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput);
+ editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true);
+ editPercent.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@@ -186,8 +272,11 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
layoutAbsolute.setVisibility(View.GONE);
}
});
- absoluteEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_absoluteinput);
- absoluteEdit.addTextChangedListener(new TextWatcher() {
+
+ Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit);
+ editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput);
+ editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true);
+ editAbsolute.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@@ -204,115 +293,10 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
layoutAbsolute.setVisibility(View.VISIBLE);
}
});
- durationeEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_durationinput);
- carbTimeEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_carbtimeinput);
- notesEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_notes);
- splitEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_splitinput);
- reasonSpinner = (Spinner) view.findViewById(R.id.careportal_newnstreatment_temptarget_reason);
- low = (EditText) view.findViewById(R.id.careportal_temptarget_low);
- high = (EditText) view.findViewById(R.id.careportal_temptarget_high);
+ editCarbTime = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbtimeinput);
+ editCarbTime.setParams(0d, -60d, 60d, 5d, new DecimalFormat("0"), false);
- eventTime = new Date();
- dateButton = (Button) view.findViewById(R.id.careportal_newnstreatment_eventdate);
- timeButton = (Button) view.findViewById(R.id.careportal_newnstreatment_eventtime);
- dateButton.setText(DateUtil.dateString(eventTime));
- timeButton.setText(DateUtil.timeString(eventTime));
- dateButton.setOnClickListener(this);
- timeButton.setOnClickListener(this);
-
- okButton = (Button) view.findViewById(R.id.ok);
- okButton.setOnClickListener(this);
- cancelButton = (Button) view.findViewById(R.id.cancel);
- cancelButton.setOnClickListener(this);
-
- // profile
- profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
- ArrayList profileList;
- units = Constants.MGDL;
- if (profile == null) {
- ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), context.getString(R.string.noprofile));
- profileList = new ArrayList();
- } else {
- units = profile.getUnits();
- profileList = profile.getProfileList();
- }
- ArrayAdapter adapter = new ArrayAdapter(getContext(),
- R.layout.spinner_centered, profileList);
- profileSpinner.setAdapter(adapter);
- if (profile != null) {
- // set selected to actual profile
- for (int p = 0; p < profileList.size(); p++) {
- if (profileList.get(p).equals(profile.getActiveProfile()))
- profileSpinner.setSelection(p);
- }
- }
-
- // temp target
- ArrayList reasonList = new ArrayList();
- reasonList.add(MainApp.sResources.getString(R.string.eatingsoon));
- reasonList.add(MainApp.sResources.getString(R.string.activity));
- reasonList.add(MainApp.sResources.getString(R.string.manual));
- ArrayAdapter adapterReason = new ArrayAdapter(getContext(),
- R.layout.spinner_centered, reasonList);
- reasonSpinner.setAdapter(adapterReason);
-
- // bg
- bgUnitsView.setText(units);
-
- // Set BG if not old
-// BgReading lastBg = MainApp.getDbHelper().lastBg();
-// Double lastBgValue = 0d;
-// if (lastBg != null) {
-// lastBgValue = lastBg.valueToUnits(units);
-// sensorRadioButton.setChecked(true);
-// } else {
-// meterRadioButton.setChecked(true);
-// }
-
- Double bg = NSProfile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile != null ? profile.getUnits() : Constants.MGDL);
- if (profile == null)
- editBg = new PlusMinusEditText(view, R.id.careportal_newnstreatment_bginput, R.id.careportal_newnstreatment_bg_plus, R.id.careportal_newnstreatment_bg_minus, bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false);
- else if (profile.getUnits().equals(Constants.MMOL))
- editBg = new PlusMinusEditText(view, R.id.careportal_newnstreatment_bginput, R.id.careportal_newnstreatment_bg_plus, R.id.careportal_newnstreatment_bg_minus, bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false);
- else
- editBg = new PlusMinusEditText(view, R.id.careportal_newnstreatment_bginput, R.id.careportal_newnstreatment_bg_plus, R.id.careportal_newnstreatment_bg_minus, bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
- bgInputEdit.addTextChangedListener(new TextWatcher() {
-
- public void afterTextChanged(Editable s) {}
-
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
-
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- if (sensorRadioButton.isChecked()) meterRadioButton.setChecked(true);
- }
- });
- sensorRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
- if (profile == null) return;
- Double bg = NSProfile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile.getUnits());
- editBg.setValue(bg);
- }
- });
-
- Integer maxCarbs = MainApp.getConfigBuilder().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit);
- editCarbs = new PlusMinusEditText(view, R.id.careportal_newnstreatment_carbsinput, R.id.careportal_newnstreatment_carbs_plus, R.id.careportal_newnstreatment_carbs_minus, 0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false);
-
- Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit);
- editInsulin = new PlusMinusEditText(view, R.id.careportal_newnstreatment_insulininput, R.id.careportal_newnstreatment_insulin_plus, R.id.careportal_newnstreatment_insulin_minus, 0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false);
-
- editSplit = new PlusMinusEditText(view, R.id.careportal_newnstreatment_splitinput, R.id.careportal_newnstreatment_split_plus, R.id.careportal_newnstreatment_split_minus, 100d, 0d, 100d, 5d, new DecimalFormat("0"), true);
- editDuration = new PlusMinusEditText(view, R.id.careportal_newnstreatment_durationinput, R.id.careportal_newnstreatment_duration_plus, R.id.careportal_newnstreatment_duration_minus, 0d, 0d, 24 * 60d, 10d, new DecimalFormat("0"), false);
-
- Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit);
- editPercent = new PlusMinusEditText(view, R.id.careportal_newnstreatment_percentinput, R.id.careportal_newnstreatment_percent_plus, R.id.careportal_newnstreatment_percent_minus, 0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true);
-
- Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit);
- editAbsolute = new PlusMinusEditText(view, R.id.careportal_newnstreatment_absoluteinput, R.id.careportal_newnstreatment_absolute_plus, R.id.careportal_newnstreatment_absolute_minus, 0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true);
-
- editCarbTime = new PlusMinusEditText(view, R.id.careportal_newnstreatment_carbtimeinput, R.id.careportal_newnstreatment_carbtime_plus, R.id.careportal_newnstreatment_carbtime_minus, 0d, -60d, 60d, 5d, new DecimalFormat("0"), false);
showOrHide(layoutBg, options.bg);
showOrHide(layoutBgSource, options.bg);
@@ -329,13 +313,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
return view;
}
- @Override
- public void onResume() {
- super.onResume();
- if (getDialog() != null)
- getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- }
-
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
@@ -353,7 +330,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
dpd.show(context.getFragmentManager(), "Datepickerdialog");
break;
case R.id.careportal_newnstreatment_eventtime:
- android.text.format.DateFormat df = new android.text.format.DateFormat();
TimePickerDialog tpd = TimePickerDialog.newInstance(
this,
calendar.get(Calendar.HOUR_OF_DAY),
@@ -404,22 +380,22 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
data.put("created_at", DateUtil.toISOString(eventTime));
switch (options.eventType) {
case R.id.careportal_bgcheck:
- data.put("eventType", "BG Check");
+ data.put("eventType", CareportalEvent.BGCHECK);
break;
case R.id.careportal_announcement:
- data.put("eventType", "Announcement");
+ data.put("eventType", CareportalEvent.ANNOUNCEMENT);
data.put("isAnnouncement", true);
break;
case R.id.careportal_cgmsensorinsert:
- data.put("eventType", "Sensor Change");
+ data.put("eventType", CareportalEvent.SENSORCHANGE);
break;
case R.id.careportal_cgmsensorstart:
data.put("eventType", "Sensor Start");
break;
case R.id.careportal_combobolus:
- data.put("splitNow", SafeParse.stringToDouble(splitEdit.getText().toString()));
- data.put("splitExt", 100 - SafeParse.stringToDouble(splitEdit.getText().toString()));
- data.put("eventType", "Combo Bolus");
+ data.put("splitNow", SafeParse.stringToDouble(editSplit.getText()));
+ data.put("splitExt", 100 - SafeParse.stringToDouble(editSplit.getText()));
+ data.put("eventType", CareportalEvent.COMBOBOLUS);
break;
case R.id.careportal_correctionbolus:
data.put("eventType", "Correction Bolus");
@@ -428,78 +404,82 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
data.put("eventType", "Carb Correction");
break;
case R.id.careportal_exercise:
- data.put("eventType", "Exercise");
+ data.put("eventType", CareportalEvent.EXERCISE);
break;
case R.id.careportal_insulincartridgechange:
- data.put("eventType", "Insulin Change");
+ data.put("eventType", CareportalEvent.INSULINCHANGE);
+ break;
+ case R.id.careportal_pumpbatterychange:
+ data.put("eventType", CareportalEvent.PUMPBATTERYCHANGE);
break;
case R.id.careportal_mealbolus:
data.put("eventType", "Meal Bolus");
break;
case R.id.careportal_note:
- data.put("eventType", "Note");
+ data.put("eventType", CareportalEvent.NOTE);
break;
case R.id.careportal_profileswitch:
- data.put("eventType", "Profile Switch");
+ data.put("eventType", CareportalEvent.PROFILESWITCH);
+ allowZeroDuration = true;
break;
case R.id.careportal_pumpsitechange:
- data.put("eventType", "Site Change");
+ data.put("eventType", CareportalEvent.SITECHANGE);
break;
case R.id.careportal_question:
- data.put("eventType", "Question");
+ data.put("eventType", CareportalEvent.QUESTION);
break;
case R.id.careportal_snackbolus:
data.put("eventType", "Snack Bolus");
break;
case R.id.careportal_tempbasalstart:
- data.put("eventType", "Temp Basal");
+ data.put("eventType", CareportalEvent.TEMPBASAL);
break;
case R.id.careportal_tempbasalend:
- data.put("eventType", "Temp Basal");
+ data.put("eventType", CareportalEvent.TEMPBASAL);
break;
case R.id.careportal_openapsoffline:
- data.put("eventType", "OpenAPS Offline");
+ data.put("eventType", CareportalEvent.OPENAPSOFFLINE);
break;
case R.id.careportal_temporarytarget:
- data.put("eventType", "Temporary Target");
+ data.put("eventType", CareportalEvent.TEMPORARYTARGET);
if (!reasonSpinner.getSelectedItem().toString().equals(""))
data.put("reason", reasonSpinner.getSelectedItem().toString());
- if (SafeParse.stringToDouble(low.getText().toString()) != 0d)
- data.put("targetBottom", SafeParse.stringToDouble(low.getText().toString()));
- if (SafeParse.stringToDouble(high.getText().toString()) != 0d)
- data.put("targetTop", SafeParse.stringToDouble(high.getText().toString()));
+ if (SafeParse.stringToDouble(editTemptarget.getText()) != 0d) {
+ data.put("targetBottom", SafeParse.stringToDouble(editTemptarget.getText()));
+ data.put("targetTop", SafeParse.stringToDouble(editTemptarget.getText()));
+ }
allowZeroDuration = true;
break;
}
- if (SafeParse.stringToDouble(bgInputEdit.getText().toString()) != 0d) {
- data.put("glucose", SafeParse.stringToDouble(bgInputEdit.getText().toString()));
+ if (SafeParse.stringToDouble(editBg.getText()) != 0d) {
+ data.put("glucose", SafeParse.stringToDouble(editBg.getText()));
if (meterRadioButton.isChecked()) data.put("glucoseType", "Finger");
if (sensorRadioButton.isChecked()) data.put("glucoseType", "Sensor");
if (otherRadioButton.isChecked()) data.put("glucoseType", "Manual");
}
- if (SafeParse.stringToDouble(carbsEdit.getText().toString()) != 0d)
- data.put("carbs", SafeParse.stringToDouble(carbsEdit.getText().toString()));
- if (SafeParse.stringToDouble(insulinEdit.getText().toString()) != 0d)
- data.put("insulin", SafeParse.stringToDouble(insulinEdit.getText().toString()));
- if (allowZeroDuration || SafeParse.stringToDouble(durationeEdit.getText().toString()) != 0d)
- data.put("duration", SafeParse.stringToDouble(durationeEdit.getText().toString()));
+ if (SafeParse.stringToDouble(editCarbs.getText()) != 0d)
+ data.put("carbs", SafeParse.stringToDouble(editCarbs.getText()));
+ if (SafeParse.stringToDouble(editInsulin.getText()) != 0d)
+ data.put("insulin", SafeParse.stringToDouble(editInsulin.getText()));
+ if (allowZeroDuration || SafeParse.stringToDouble(editDuration.getText()) != 0d)
+ data.put("duration", SafeParse.stringToDouble(editDuration.getText()));
if (layoutPercent.getVisibility() != View.GONE)
- data.put("percent", SafeParse.stringToDouble(percentEdit.getText().toString()));
+ data.put("percent", SafeParse.stringToDouble(editPercent.getText()));
if (layoutAbsolute.getVisibility() != View.GONE)
- data.put("absolute", SafeParse.stringToDouble(absoluteEdit.getText().toString()));
+ data.put("absolute", SafeParse.stringToDouble(editAbsolute.getText()));
if (options.profile && profileSpinner.getSelectedItem() != null)
data.put("profile", profileSpinner.getSelectedItem().toString());
- if (SafeParse.stringToDouble(carbTimeEdit.getText().toString()) != 0d)
- data.put("preBolus", SafeParse.stringToDouble(carbTimeEdit.getText().toString()));
+ if (SafeParse.stringToDouble(editCarbTime.getText()) != 0d)
+ data.put("preBolus", SafeParse.stringToDouble(editCarbTime.getText()));
if (!notesEdit.getText().toString().equals(""))
data.put("notes", notesEdit.getText().toString());
data.put("units", units);
if (!enteredBy.equals("")) data.put("enteredBy", enteredBy);
if (options.eventType == R.id.careportal_combobolus) {
- Double enteredInsulin = SafeParse.stringToDouble(insulinEdit.getText().toString());
+ Double enteredInsulin = SafeParse.stringToDouble(editInsulin.getText());
data.put("enteredinsulin", enteredInsulin);
- data.put("insulin", enteredInsulin * SafeParse.stringToDouble(splitEdit.getText().toString()) / 100);
- data.put("relative", enteredInsulin * (100 - SafeParse.stringToDouble(splitEdit.getText().toString())) / 100 / SafeParse.stringToDouble(durationeEdit.getText().toString()) * 60);
+ data.put("insulin", enteredInsulin * SafeParse.stringToDouble(editInsulin.getText()) / 100);
+ data.put("relative", enteredInsulin * (100 - SafeParse.stringToDouble(editSplit.getText())) / 100 / SafeParse.stringToDouble(editDuration.getText()) * 60);
}
} catch (JSONException e) {
e.printStackTrace();
@@ -613,61 +593,36 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
public void onClick(DialogInterface dialog, int id) {
if (options.executeProfileSwitch) {
if (data.has("profile")) {
- sHandler.post(new Runnable() {
- @Override
- public void run() {
- try {
- String profile = data.getString("profile");
- NSProfile nsProfile = ConfigBuilderPlugin.getActiveProfile().getProfile();
- nsProfile.setActiveProfile(profile);
- PumpInterface pump = MainApp.getConfigBuilder();
- if (pump != null) {
- pump.setNewBasalProfile(nsProfile);
- log.debug("Setting new profile: " + profile);
- MainApp.bus().post(new EventNewBasalProfile(nsProfile, "NewNSTreatmentDialog"));
- } else {
- log.error("No active pump selected");
- }
- if (ConfigBuilderPlugin.getActiveProfile() instanceof CircadianPercentageProfilePlugin) {
- CircadianPercentageProfilePlugin cpp = (CircadianPercentageProfilePlugin) ConfigBuilderPlugin.getActiveProfile();
- data.put("CircadianPercentageProfile", true);
- data.put("timeshift", cpp.timeshift);
- data.put("percentage", cpp.percentage);
- }
- ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
- Answers.getInstance().logCustom(new CustomEvent("ProfileSwitch"));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- });
+ try {
+ doProfileSwitch(profileStore, data.getString("profile"), data.getInt("duration"));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
}
} else if (options.executeTempTarget) {
try {
- if ((data.has("targetBottom") && data.has("targetTop")) || (data.has("duration")&& data.getInt("duration") == 0)) {
+ if ((data.has("targetBottom") && data.has("targetTop")) || (data.has("duration") && data.getInt("duration") == 0)) {
sHandler.post(new Runnable() {
@Override
public void run() {
try {
TempTarget tempTarget = new TempTarget();
- tempTarget.timeStart = eventTime;
- tempTarget.duration = data.getInt("duration");
+ tempTarget.date = eventTime.getTime();
+ tempTarget.durationInMinutes = data.getInt("duration");
tempTarget.reason = data.getString("reason");
- if(tempTarget.duration != 0) {
- tempTarget.low = NSProfile.toMgdl(data.getDouble("targetBottom"), ConfigBuilderPlugin.getActiveProfile().getProfile().getUnits());
- tempTarget.high = NSProfile.toMgdl(data.getDouble("targetTop"), ConfigBuilderPlugin.getActiveProfile().getProfile().getUnits());
+ tempTarget.source = Source.USER;
+ if (tempTarget.durationInMinutes != 0) {
+ tempTarget.low = Profile.toMgdl(data.getDouble("targetBottom"), profile.getUnits());
+ tempTarget.high = Profile.toMgdl(data.getDouble("targetTop"), profile.getUnits());
} else {
tempTarget.low = 0;
tempTarget.high = 0;
}
- tempTarget.setTimeIndex(tempTarget.getTimeIndex());
- Dao dao = MainApp.getDbHelper().getDaoTempTargets();
- log.debug("Creating new TempTarget db record: " + tempTarget.log());
- dao.createIfNotExists(tempTarget);
- MainApp.bus().post(new EventTempTargetRangeChange());
- ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
+ log.debug("Creating new TempTarget db record: " + tempTarget.toString());
+ MainApp.getDbHelper().createOrUpdate(tempTarget);
+ NSUpload.uploadCareportalEntryToNS(data);
Answers.getInstance().logCustom(new CustomEvent("TempTarget"));
- } catch (JSONException | SQLException e) {
+ } catch (JSONException e) {
e.printStackTrace();
}
}
@@ -677,7 +632,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
e.printStackTrace();
}
} else {
- ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
+ NSUpload.uploadCareportalEntryToNS(data);
Answers.getInstance().logCustom(new CustomEvent("NSTreatment"));
}
}
@@ -686,4 +641,35 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
builder.show();
}
+ public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration) {
+ sHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ ProfileSwitch profileSwitch = new ProfileSwitch();
+ profileSwitch.date = System.currentTimeMillis();
+ profileSwitch.source = Source.USER;
+ profileSwitch.profileName = profileName;
+ profileSwitch.profileJson = profileStore.getSpecificProfile(profileName).getData().toString();
+ profileSwitch.profilePlugin = ConfigBuilderPlugin.getActiveProfileInterface().getClass().getName();
+ profileSwitch.durationInMinutes = duration;
+ if (ConfigBuilderPlugin.getActiveProfileInterface() instanceof CircadianPercentageProfilePlugin) {
+ CircadianPercentageProfilePlugin cpp = (CircadianPercentageProfilePlugin) ConfigBuilderPlugin.getActiveProfileInterface();
+ profileSwitch.isCPP = true;
+ profileSwitch.timeshift = cpp.timeshift;
+ profileSwitch.percentage = cpp.percentage;
+ }
+ MainApp.getConfigBuilder().addToHistoryProfileSwitch(profileSwitch);
+
+ PumpInterface pump = MainApp.getConfigBuilder();
+ if (pump != null) {
+ pump.setNewBasalProfile(profileStore.getSpecificProfile(profileName));
+ MainApp.bus().post(new EventNewBasalProfile());
+ } else {
+ log.error("No active pump selected");
+ }
+ Answers.getInstance().logCustom(new CustomEvent("ProfileSwitch"));
+ }
+ });
+ }
+
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Common/SubscriberFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Common/SubscriberFragment.java
new file mode 100644
index 0000000000..74abc837fe
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Common/SubscriberFragment.java
@@ -0,0 +1,22 @@
+package info.nightscout.androidaps.plugins.Common;
+
+import android.support.v4.app.Fragment;
+
+import info.nightscout.androidaps.MainApp;
+
+abstract public class SubscriberFragment extends Fragment {
+ @Override
+ public void onPause() {
+ super.onPause();
+ MainApp.bus().unregister(this);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ MainApp.bus().register(this);
+ updateGUI();
+ }
+
+ protected abstract void updateGUI();
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java
index ff6f69ca9f..be9341b860 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java
@@ -34,9 +34,11 @@ import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface;
+import info.nightscout.androidaps.interfaces.SensitivityInterface;
import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingPlugin;
import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
+import info.nightscout.androidaps.plugins.SensitivityOref0.SensitivityOref0Plugin;
import info.nightscout.utils.PasswordProtection;
@@ -49,32 +51,32 @@ public class ConfigBuilderFragment extends Fragment {
}
ListView insulinListView;
+ ListView sensitivityListView;
ListView bgsourceListView;
+ TextView bgsourceLabel;
ListView pumpListView;
TextView pumpLabel;
ListView loopListView;
TextView loopLabel;
ListView treatmentsListView;
- ListView tempsListView;
- TextView tempsLabel;
+ TextView treatmentsLabel;
ListView profileListView;
+ TextView profileLabel;
ListView apsListView;
TextView apsLabel;
ListView constraintsListView;
TextView constraintsLabel;
ListView generalListView;
- TextView nsclientVerView;
- TextView nightscoutVerView;
LinearLayout mainLayout;
Button unlock;
PluginCustomAdapter insulinDataAdapter = null;
+ PluginCustomAdapter sensivityDataAdapter = null;
PluginCustomAdapter bgsourceDataAdapter = null;
PluginCustomAdapter pumpDataAdapter = null;
PluginCustomAdapter loopDataAdapter = null;
PluginCustomAdapter treatmentsDataAdapter = null;
- PluginCustomAdapter tempsDataAdapter = null;
PluginCustomAdapter profileDataAdapter = null;
PluginCustomAdapter apsDataAdapter = null;
PluginCustomAdapter constraintsDataAdapter = null;
@@ -96,31 +98,26 @@ public class ConfigBuilderFragment extends Fragment {
smallWidth = screen_width < Constants.SMALL_WIDTH;
insulinListView = (ListView) view.findViewById(R.id.configbuilder_insulinlistview);
+ sensitivityListView = (ListView) view.findViewById(R.id.configbuilder_sensitivitylistview);
bgsourceListView = (ListView) view.findViewById(R.id.configbuilder_bgsourcelistview);
+ bgsourceLabel = (TextView) view.findViewById(R.id.configbuilder_bgsourcelabel);
pumpListView = (ListView) view.findViewById(R.id.configbuilder_pumplistview);
pumpLabel = (TextView) view.findViewById(R.id.configbuilder_pumplabel);
loopListView = (ListView) view.findViewById(R.id.configbuilder_looplistview);
loopLabel = (TextView) view.findViewById(R.id.configbuilder_looplabel);
treatmentsListView = (ListView) view.findViewById(R.id.configbuilder_treatmentslistview);
- tempsListView = (ListView) view.findViewById(R.id.configbuilder_tempslistview);
- tempsLabel = (TextView) view.findViewById(R.id.configbuilder_tempslabel);
+ treatmentsLabel = (TextView) view.findViewById(R.id.configbuilder_treatmentslabel);
profileListView = (ListView) view.findViewById(R.id.configbuilder_profilelistview);
+ profileLabel = (TextView) view.findViewById(R.id.configbuilder_profilelabel);
apsListView = (ListView) view.findViewById(R.id.configbuilder_apslistview);
apsLabel = (TextView) view.findViewById(R.id.configbuilder_apslabel);
constraintsListView = (ListView) view.findViewById(R.id.configbuilder_constraintslistview);
constraintsLabel = (TextView) view.findViewById(R.id.configbuilder_constraintslabel);
generalListView = (ListView) view.findViewById(R.id.configbuilder_generallistview);
- nsclientVerView = (TextView) view.findViewById(R.id.configbuilder_nsclientversion);
- nightscoutVerView = (TextView) view.findViewById(R.id.configbuilder_nightscoutversion);
mainLayout = (LinearLayout) view.findViewById(R.id.configbuilder_mainlayout);
unlock = (Button) view.findViewById(R.id.configbuilder_unlock);
- 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();
if (PasswordProtection.isLocked("settings_password")) {
@@ -149,6 +146,8 @@ public class ConfigBuilderFragment extends Fragment {
setListViewHeightBasedOnChildren(insulinListView);
bgsourceDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(BgSourceInterface.class, PluginBase.BGSOURCE), PluginBase.BGSOURCE);
bgsourceListView.setAdapter(bgsourceDataAdapter);
+ if (MainApp.getSpecificPluginsVisibleInList(PluginBase.BGSOURCE).size() == 0)
+ bgsourceLabel.setVisibility(View.GONE);
setListViewHeightBasedOnChildren(bgsourceListView);
pumpDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInList(PluginBase.PUMP), PluginBase.PUMP);
pumpListView.setAdapter(pumpDataAdapter);
@@ -162,21 +161,23 @@ public class ConfigBuilderFragment extends Fragment {
loopLabel.setVisibility(View.GONE);
treatmentsDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInList(PluginBase.TREATMENT), PluginBase.TREATMENT);
treatmentsListView.setAdapter(treatmentsDataAdapter);
+ if (MainApp.getSpecificPluginsVisibleInList(PluginBase.TREATMENT).size() == 0)
+ treatmentsLabel.setVisibility(View.GONE);
setListViewHeightBasedOnChildren(treatmentsListView);
- tempsDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInList(PluginBase.TEMPBASAL), PluginBase.TEMPBASAL);
- tempsListView.setAdapter(tempsDataAdapter);
- setListViewHeightBasedOnChildren(tempsListView);
- if (MainApp.getSpecificPluginsVisibleInList(PluginBase.TEMPBASAL).size() == 0)
- tempsLabel.setVisibility(View.GONE);
- profileDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(ProfileInterface.class, PluginBase.BGSOURCE), PluginBase.PROFILE);
+ profileDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(ProfileInterface.class, PluginBase.PROFILE), PluginBase.PROFILE);
profileListView.setAdapter(profileDataAdapter);
+ if (MainApp.getSpecificPluginsVisibleInList(PluginBase.PROFILE).size() == 0)
+ profileLabel.setVisibility(View.GONE);
setListViewHeightBasedOnChildren(profileListView);
apsDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInList(PluginBase.APS), PluginBase.APS);
apsListView.setAdapter(apsDataAdapter);
setListViewHeightBasedOnChildren(apsListView);
if (MainApp.getSpecificPluginsVisibleInList(PluginBase.APS).size() == 0)
apsLabel.setVisibility(View.GONE);
- constraintsDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(ConstraintsInterface.class, PluginBase.BGSOURCE), PluginBase.CONSTRAINTS);
+ sensivityDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(SensitivityInterface.class, PluginBase.SENSITIVITY), PluginBase.SENSITIVITY);
+ sensitivityListView.setAdapter(sensivityDataAdapter);
+ setListViewHeightBasedOnChildren(sensitivityListView);
+ constraintsDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(ConstraintsInterface.class, PluginBase.CONSTRAINTS), PluginBase.CONSTRAINTS);
constraintsListView.setAdapter(constraintsDataAdapter);
setListViewHeightBasedOnChildren(constraintsListView);
if (MainApp.getSpecificPluginsVisibleInList(PluginBase.CONSTRAINTS).size() == 0)
@@ -232,7 +233,7 @@ public class ConfigBuilderFragment extends Fragment {
plugin.setFragmentVisible(type, cb.isChecked());
onEnabledCategoryChanged(plugin, type);
configBuilderPlugin.storeSettings();
- MainApp.bus().post(new EventRefreshGui(true));
+ MainApp.bus().post(new EventRefreshGui());
MainApp.bus().post(new EventConfigBuilderChange());
getPlugin().logPluginStatus();
Answers.getInstance().logCustom(new CustomEvent("ConfigurationChange"));
@@ -245,7 +246,7 @@ public class ConfigBuilderFragment extends Fragment {
PluginBase plugin = (PluginBase) cb.getTag();
plugin.setFragmentVisible(type, cb.isChecked());
configBuilderPlugin.storeSettings();
- MainApp.bus().post(new EventRefreshGui(true));
+ MainApp.bus().post(new EventRefreshGui());
getPlugin().logPluginStatus();
}
});
@@ -275,7 +276,7 @@ public class ConfigBuilderFragment extends Fragment {
}
// Hide enabled control and force enabled plugin if there is only one plugin available
- if (type == PluginBase.INSULIN || type == PluginBase.PUMP || type == PluginBase.TREATMENT || type == PluginBase.TEMPBASAL || type == PluginBase.PROFILE)
+ if (type == PluginBase.INSULIN || type == PluginBase.PUMP || type == PluginBase.TREATMENT || type == PluginBase.PROFILE || type == PluginBase.SENSITIVITY)
if (pluginList.size() < 2) {
holder.checkboxEnabled.setEnabled(false);
plugin.setFragmentEnabled(type, true);
@@ -324,6 +325,9 @@ public class ConfigBuilderFragment extends Fragment {
case PluginBase.INSULIN:
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(InsulinInterface.class);
break;
+ case PluginBase.SENSITIVITY:
+ pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class);
+ break;
case PluginBase.APS:
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class);
break;
@@ -333,7 +337,6 @@ public class ConfigBuilderFragment extends Fragment {
case PluginBase.BGSOURCE:
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class);
break;
- case PluginBase.TEMPBASAL:
case PluginBase.TREATMENT:
case PluginBase.PUMP:
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(PumpInterface.class);
@@ -355,6 +358,8 @@ public class ConfigBuilderFragment extends Fragment {
MainApp.getSpecificPlugin(VirtualPumpPlugin.class).setFragmentEnabled(type, true);
else if (type == PluginBase.INSULIN)
MainApp.getSpecificPlugin(InsulinFastactingPlugin.class).setFragmentEnabled(type, true);
+ else if (type == PluginBase.SENSITIVITY)
+ MainApp.getSpecificPlugin(SensitivityOref0Plugin.class).setFragmentEnabled(type, true);
else if (type == PluginBase.PROFILE)
MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentEnabled(type, true);
else
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java
index eaea986d19..6bc0e1e807 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java
@@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.ConfigBuilder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
-import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
@@ -16,17 +15,24 @@ 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.Services.Intents;
+import info.nightscout.androidaps.data.DetailedBolusInfo;
+import info.nightscout.androidaps.data.IobTotal;
+import info.nightscout.androidaps.data.MealData;
+import info.nightscout.androidaps.data.Intervals;
+import info.nightscout.androidaps.data.Profile;
+import info.nightscout.androidaps.data.ProfileIntervals;
import info.nightscout.androidaps.data.PumpEnactResult;
-import info.nightscout.androidaps.db.TempBasal;
+import info.nightscout.androidaps.db.ExtendedBolus;
+import info.nightscout.androidaps.db.ProfileSwitch;
+import info.nightscout.androidaps.db.TempTarget;
+import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventBolusRequested;
-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;
@@ -35,55 +41,48 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
-import info.nightscout.androidaps.interfaces.TempBasalsInterface;
+import info.nightscout.androidaps.interfaces.SensitivityInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
-import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgError;
import info.nightscout.androidaps.plugins.Loop.APSResult;
-import info.nightscout.androidaps.plugins.Loop.DeviceStatus;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
-import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA;
-import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResultMA;
import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressHelperActivity;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogressIfRunning;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.DbLogger;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
-import info.nightscout.utils.BatteryLevel;
-import info.nightscout.utils.DateUtil;
+import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
+import info.nightscout.utils.NSUpload;
import info.nightscout.utils.SP;
/**
* Created by mike on 05.08.2016.
*/
-public class ConfigBuilderPlugin implements PluginBase, PumpInterface, ConstraintsInterface {
+public class ConfigBuilderPlugin implements PluginBase, PumpInterface, ConstraintsInterface, TreatmentsInterface {
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 InsulinInterface activeInsulin;
+ private static BgSourceInterface activeBgSource;
+ private static PumpInterface activePump;
+ private static ProfileInterface activeProfile;
+ private static TreatmentsInterface activeTreatments;
+ private static APSInterface activeAPS;
+ private static LoopPlugin activeLoop;
+ private static InsulinInterface activeInsulin;
+ private static SensitivityInterface activeSensitivity;
static public String nightscoutVersionName = "";
static public Integer nightscoutVersionCode = 0;
static public String nsClientVersionName = "";
static public Integer nsClientVersionCode = 0;
- static ArrayList pluginList;
+ private static ArrayList pluginList;
- PowerManager.WakeLock mWakeLock;
+ private PowerManager.WakeLock mWakeLock;
public ConfigBuilderPlugin() {
MainApp.bus().register(this);
PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "ConfigBuilderPlugin");
- ;
}
@Override
@@ -114,12 +113,12 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
@Override
public boolean isEnabled(int type) {
- return type == GENERAL && true;
+ return type == GENERAL;
}
@Override
public boolean isVisibleInTabs(int type) {
- return type == GENERAL && true;
+ return type == GENERAL;
}
@Override
@@ -197,19 +196,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
return activeBgSource;
}
- @Nullable
- public static ProfileInterface getActiveProfile() {
+ public static ProfileInterface getActiveProfileInterface() {
return activeProfile;
}
- public static TreatmentsInterface getActiveTreatments() {
- return activeTreatments;
- }
-
- public static TempBasalsInterface getActiveTempBasals() {
- return activeTempBasals;
- }
-
public static InsulinInterface getActiveInsulin() {
return activeInsulin;
}
@@ -222,12 +212,20 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
return activeLoop;
}
- public void logPluginStatus() {
+ public static PumpInterface getActivePump() {
+ return activePump;
+ }
+
+ public static SensitivityInterface getActiveSensitivity() {
+ return activeSensitivity;
+ }
+
+ void logPluginStatus() {
for (PluginBase p : pluginList) {
log.debug(p.getName() + ":" +
(p.isEnabled(1) ? " GENERAL" : "") +
(p.isEnabled(2) ? " TREATMENT" : "") +
- (p.isEnabled(3) ? " TEMPBASAL" : "") +
+ (p.isEnabled(3) ? " SENSITIVITY" : "") +
(p.isEnabled(4) ? " PROFILE" : "") +
(p.isEnabled(5) ? " APS" : "") +
(p.isEnabled(6) ? " PUMP" : "") +
@@ -266,7 +264,18 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
}
- // PluginBase.PROFILE
+ // PluginBase.SENSITIVITY
+ pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class);
+ activeSensitivity = (SensitivityInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.SENSITIVITY);
+ if (Config.logConfigBuilder)
+ log.debug("Selected sensitivity interface: " + ((PluginBase) activeSensitivity).getName());
+ for (PluginBase p : pluginsInCategory) {
+ if (!p.getName().equals(((PluginBase) activeSensitivity).getName())) {
+ p.setFragmentVisible(PluginBase.SENSITIVITY, false);
+ }
+ }
+
+ // PluginBase.PROFILE
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class);
activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PROFILE);
if (Config.logConfigBuilder)
@@ -291,7 +300,9 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
// PluginBase.PUMP
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.PUMP);
activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PUMP);
- if (Config.logConfigBuilder && activePump != null)
+ if (activePump == null)
+ activePump = VirtualPumpPlugin.getInstance(); // for NSClient build
+ if (Config.logConfigBuilder)
log.debug("Selected pump interface: " + ((PluginBase) activePump).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activePump).getName())) {
@@ -312,17 +323,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
}
- // PluginBase.TEMPBASAL
- pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TEMPBASAL);
- activeTempBasals = (TempBasalsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TEMPBASAL);
- if (Config.logConfigBuilder && activeTempBasals != null)
- 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);
@@ -379,23 +379,9 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
@Override
- public boolean isTempBasalInProgress() {
- if (activePump != null)
- return activePump.isTempBasalInProgress();
- else return false;
- }
-
- @Override
- public boolean isExtendedBoluslInProgress() {
- if (activePump != null)
- return activePump.isExtendedBoluslInProgress();
- else return false;
- }
-
- @Override
- public int setNewBasalProfile(NSProfile profile) {
+ public int setNewBasalProfile(Profile profile) {
// Compare with pump limits
- NSProfile.BasalValue[] basalValues = profile.getBasalValues();
+ Profile.BasalValue[] basalValues = profile.getBasalValues();
for (int index = 0; index < basalValues.length; index++) {
if (basalValues[index].value < getPumpDescription().basalMinimumRate) {
@@ -417,7 +403,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
@Override
- public boolean isThisProfileSet(NSProfile profile) {
+ public boolean isThisProfileSet(Profile profile) {
if (activePump != null)
return activePump.isThisProfileSet(profile);
else return true;
@@ -445,166 +431,37 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
@Override
- public double getTempBasalAbsoluteRate() {
- if (activePump != null)
- return activePump.getTempBasalAbsoluteRate();
- else
- return 0d;
- }
-
- @Override
- public double getTempBasalRemainingMinutes() {
- if (activePump != null)
- return activePump.getTempBasalRemainingMinutes();
- else
- return 0d;
- }
-
- @Override
- public TempBasal getTempBasal(Date time) {
- if (activePump != null)
- return activePump.getTempBasal(time);
- else
- return null;
- }
-
- @Override
- public TempBasal getTempBasal() {
- if (activePump != null)
- return activePump.getTempBasal();
- else
- return null;
- }
-
- @Override
- public TempBasal getExtendedBolus() {
- if (activePump != null)
- return activePump.getExtendedBolus();
- else
- return null;
- }
-
- public PumpEnactResult deliverTreatmentFromBolusWizard(InsulinInterface insulinType, Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) {
+ public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
mWakeLock.acquire();
PumpEnactResult result;
- if (activePump != null) {
- insulin = applyBolusConstraints(insulin);
- carbs = applyCarbsConstraints(carbs);
+ detailedBolusInfo.insulin = applyBolusConstraints(detailedBolusInfo.insulin);
+ detailedBolusInfo.carbs = applyCarbsConstraints((int) detailedBolusInfo.carbs);
- BolusProgressDialog bolusProgressDialog = null;
- if (context != null) {
- bolusProgressDialog = new BolusProgressDialog();
- bolusProgressDialog.setInsulin(insulin);
- bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress");
- }
-
- MainApp.bus().post(new EventBolusRequested(insulin));
-
- result = activePump.deliverTreatment(insulinType, insulin, carbs, context);
-
- BolusProgressDialog.bolusEnded = true;
-
- MainApp.bus().post(new EventDismissBolusprogressIfRunning(result));
-
- if (result.success) {
- Treatment t = new Treatment(insulinType);
- t.insulin = result.bolusDelivered;
- if (carbTime == 0)
- t.carbs = (double) result.carbsDelivered; // with different carbTime record will come back from nightscout
- t.created_at = new Date();
- t.mealBolus = result.carbsDelivered > 0;
- MainApp.getDbHelper().create(t);
- t.setTimeIndex(t.getTimeIndex());
- t.carbs = (double) result.carbsDelivered;
- uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc);
- }
+ BolusProgressDialog bolusProgressDialog = null;
+ if (detailedBolusInfo.context != null) {
+ bolusProgressDialog = new BolusProgressDialog();
+ bolusProgressDialog.setInsulin(detailedBolusInfo.insulin);
+ bolusProgressDialog.show(((AppCompatActivity) detailedBolusInfo.context).getSupportFragmentManager(), "BolusProgress");
} else {
- if (Config.logCongigBuilderActions)
- log.debug("Creating treatment: " + insulin + " carbs: " + carbs);
- Treatment t = new Treatment(insulinType);
- t.insulin = insulin;
- t.carbs = (double) carbs;
- t.created_at = new Date();
- t.mealBolus = t.carbs > 0;
- MainApp.getDbHelper().create(t);
- t.setTimeIndex(t.getTimeIndex());
- t.sendToNSClient();
- result = new PumpEnactResult();
- result.success = true;
- result.bolusDelivered = insulin;
- result.carbsDelivered = carbs;
+ Intent i = new Intent();
+ i.putExtra("insulin", detailedBolusInfo.insulin);
+ i.setClass(MainApp.instance(), BolusProgressHelperActivity.class);
+ i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ MainApp.instance().startActivity(i);
}
+
+
+ MainApp.bus().post(new EventBolusRequested(detailedBolusInfo.insulin));
+
+ result = activePump.deliverTreatment(detailedBolusInfo);
+
+ BolusProgressDialog.bolusEnded = true;
+ MainApp.bus().post(new EventDismissBolusprogressIfRunning(result));
+
mWakeLock.release();
return result;
}
- @Override
- public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) {
- return deliverTreatment(insulinType, insulin, carbs, context, true);
- }
-
- public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context, boolean createTreatment) {
- mWakeLock.acquire();
- PumpEnactResult result;
- if (activePump != null) {
- insulin = applyBolusConstraints(insulin);
- carbs = applyCarbsConstraints(carbs);
-
- BolusProgressDialog bolusProgressDialog = null;
- if (context != null) {
- bolusProgressDialog = new BolusProgressDialog();
- bolusProgressDialog.setInsulin(insulin);
- bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress");
- } else {
- Intent i = new Intent();
- i.putExtra("insulin", insulin.doubleValue());
- i.setClass(MainApp.instance(), BolusProgressHelperActivity.class);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- MainApp.instance().startActivity(i);
- }
-
- MainApp.bus().post(new EventBolusRequested(insulin));
-
- result = activePump.deliverTreatment(insulinType, insulin, carbs, context);
-
- BolusProgressDialog.bolusEnded = true;
-
- MainApp.bus().post(new EventDismissBolusprogressIfRunning(result));
-
- if (Config.logCongigBuilderActions)
- log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered);
-
- if (result.success && createTreatment) {
- Treatment t = new Treatment(insulinType);
- t.insulin = result.bolusDelivered;
- t.carbs = (double) result.carbsDelivered;
- t.created_at = new Date();
- t.mealBolus = t.carbs > 0;
- MainApp.getDbHelper().create(t);
- t.setTimeIndex(t.getTimeIndex());
- t.sendToNSClient();
- }
- } else {
- if (Config.logCongigBuilderActions)
- log.debug("Creating treatment: " + insulin + " carbs: " + carbs);
- Treatment t = new Treatment(insulinType);
- t.insulin = insulin;
- t.carbs = (double) carbs;
- t.created_at = new Date();
- t.mealBolus = t.carbs > 0;
- MainApp.getDbHelper().create(t);
- t.setTimeIndex(t.getTimeIndex());
- t.sendToNSClient();
- result = new PumpEnactResult();
- result.success = true;
- result.bolusDelivered = insulin;
- result.carbsDelivered = carbs;
- }
- mWakeLock.release();
- return result;
- }
-
-
@Override
public void stopBolusDelivering() {
activePump.stopBolusDelivering();
@@ -618,22 +475,18 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
* @return
*/
@Override
- public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
+ public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean force) {
Double rateAfterConstraints = applyBasalConstraints(absoluteRate);
- PumpEnactResult result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes);
+ PumpEnactResult result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes, force);
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;
}
+ public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
+ return setTempBasalAbsolute(absoluteRate, durationInMinutes, false);
+ }
+
/**
* apply constraints, set temp based on percent and expecting result in percent
*
@@ -647,10 +500,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
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;
}
@@ -660,22 +509,14 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
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();
+ public PumpEnactResult cancelTempBasal(boolean force) {
+ PumpEnactResult result = activePump.cancelTempBasal(force);
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;
}
@@ -693,6 +534,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
* @param request
* @return
*/
+
public PumpEnactResult applyAPSRequest(APSResult request) {
request.rate = applyBasalConstraints(request.rate);
PumpEnactResult result;
@@ -721,7 +563,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
if (isTempBasalInProgress()) {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: cancelTempBasal()");
- result = cancelTempBasal();
+ result = cancelTempBasal(false);
} else {
result = new PumpEnactResult();
result.absolute = request.rate;
@@ -732,10 +574,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Basal set correctly");
}
- } else if (isTempBasalInProgress() && Math.abs(request.rate - getTempBasalAbsoluteRate()) < 0.05) {
+ } else if (isTempBasalInProgress() && Math.abs(request.rate - getTempBasalAbsoluteRateHistory()) < 0.05) {
result = new PumpEnactResult();
- result.absolute = getTempBasalAbsoluteRate();
- result.duration = activePump.getTempBasal().getPlannedRemainingMinutes();
+ result.absolute = getTempBasalAbsoluteRateHistory();
+ result.duration = getTempBasalFromHistory(System.currentTimeMillis()).getPlannedRemainingMinutes();
result.enacted = false;
result.comment = "Temp basal set correctly";
result.success = true;
@@ -788,6 +630,11 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
}
+ @Override
+ public boolean isFakingTempsByExtendedBoluses() {
+ return activePump.isFakingTempsByExtendedBoluses();
+ }
+
/**
* Constraints interface
**/
@@ -904,294 +751,246 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
return maxIobAfterConstrain;
}
- 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));
- data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + absolute + "u/h " + durationInMinutes + " min"); // ECOR
- 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(), ConfigBuilderPlugin.class);
- } catch (JSONException e) {
- e.printStackTrace();
- }
+ // ****** Treatments interface *****
+ @Override
+ public void updateTotalIOBTreatments() {
+ activeTreatments.updateTotalIOBTreatments();
}
- public static void uploadOpenAPSOffline(double durationInMinutes) {
- try {
- Context context = MainApp.instance().getApplicationContext();
- JSONObject data = new JSONObject();
- data.put("eventType", "OpenAPS Offline");
- data.put("duration", durationInMinutes);
- 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(), ConfigBuilderPlugin.class);
- } catch (JSONException e) {
- e.printStackTrace();
- }
+ @Override
+ public void updateTotalIOBTempBasals() {
+ activeTreatments.updateTotalIOBTempBasals();
}
- 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);
+ @Override
+ public IobTotal getLastCalculationTreatments() {
+ return activeTreatments.getLastCalculationTreatments();
+ }
+
+ @Override
+ public IobTotal getCalculationToTimeTreatments(long time) {
+ return activeTreatments.getCalculationToTimeTreatments(time);
+ }
+
+ @Override
+ public IobTotal getLastCalculationTempBasals() {
+ return activeTreatments.getLastCalculationTempBasals();
+ }
+
+ @Override
+ public IobTotal getCalculationToTimeTempBasals(long time) {
+ return activeTreatments.getCalculationToTimeTempBasals(time);
+ }
+
+ @Override
+ public MealData getMealData() {
+ return activeTreatments.getMealData();
+ }
+
+ @Override
+ public List getTreatmentsFromHistory() {
+ return activeTreatments.getTreatmentsFromHistory();
+ }
+
+ @Override
+ public List getTreatments5MinBackFromHistory(long time) {
+ return activeTreatments.getTreatments5MinBackFromHistory(time);
+ }
+
+ @Override
+ public boolean isInHistoryRealTempBasalInProgress() {
+ return activeTreatments.isInHistoryRealTempBasalInProgress();
+ }
+
+ @Override
+ @Nullable
+ public TemporaryBasal getRealTempBasalFromHistory(long time) {
+ return activeTreatments.getRealTempBasalFromHistory(time);
+ }
+
+ @Override
+ public boolean isTempBasalInProgress() {
+ return activeTreatments.isTempBasalInProgress();
+ }
+
+ @Override
+ @Nullable
+ public TemporaryBasal getTempBasalFromHistory(long time) {
+ return activeTreatments.getTempBasalFromHistory(time);
+ }
+
+ @Override
+ public double getTempBasalAbsoluteRateHistory() {
+ return activeTreatments.getTempBasalAbsoluteRateHistory();
+ }
+
+ @Override
+ public double getTempBasalRemainingMinutesFromHistory() {
+ return activeTreatments.getTempBasalRemainingMinutesFromHistory();
+ }
+
+ @Override
+ public Intervals getTemporaryBasalsFromHistory() {
+ return activeTreatments.getTemporaryBasalsFromHistory();
+ }
+
+ @Override
+ public boolean addToHistoryTempBasal(TemporaryBasal tempBasal) {
+ boolean newRecordCreated = activeTreatments.addToHistoryTempBasal(tempBasal);
+ if (newRecordCreated) {
+ if (tempBasal.durationInMinutes == 0)
+ NSUpload.uploadTempBasalEnd(tempBasal.date, false, tempBasal.pumpId);
+ else if (tempBasal.isAbsolute)
+ NSUpload.uploadTempBasalStartAbsolute(tempBasal, null);
+ else
+ NSUpload.uploadTempBasalStartPercent(tempBasal);
+ }
+ return newRecordCreated;
+ }
+
+ @Override
+ public boolean isInHistoryExtendedBoluslInProgress() {
+ return activeTreatments.isInHistoryExtendedBoluslInProgress();
+ }
+
+ @Override
+ @Nullable
+ public ExtendedBolus getExtendedBolusFromHistory(long time) {
+ return activeTreatments.getExtendedBolusFromHistory(time);
+ }
+
+ @Override
+ public boolean addToHistoryExtendedBolus(ExtendedBolus extendedBolus) {
+ boolean newRecordCreated = activeTreatments.addToHistoryExtendedBolus(extendedBolus);
+ if (newRecordCreated) {
+ if (extendedBolus.durationInMinutes == 0) {
+ if (activePump.isFakingTempsByExtendedBoluses())
+ NSUpload.uploadTempBasalEnd(extendedBolus.date, true, extendedBolus.pumpId);
+ else
+ NSUpload.uploadExtendedBolusEnd(extendedBolus.date, extendedBolus.pumpId);
+ } else if (activePump.isFakingTempsByExtendedBoluses())
+ NSUpload.uploadTempBasalStartAbsolute(new TemporaryBasal(extendedBolus), extendedBolus.insulin);
+ else
+ NSUpload.uploadExtendedBolus(extendedBolus);
+ }
+ return newRecordCreated;
+ }
+
+ @Override
+ public Intervals getExtendedBolusesFromHistory() {
+ return activeTreatments.getExtendedBolusesFromHistory();
+ }
+
+ @Override
+ // return true if new record is created
+ public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) {
+ boolean newRecordCreated = activeTreatments.addToHistoryTreatment(detailedBolusInfo);
+ if (newRecordCreated && detailedBolusInfo.isValid)
+ NSUpload.uploadBolusWizardRecord(detailedBolusInfo);
+ return newRecordCreated;
+ }
+
+ @Override
+ @Nullable
+ public TempTarget getTempTargetFromHistory(long time) {
+ return activeTreatments.getTempTargetFromHistory(time);
+ }
+
+ @Override
+ public Intervals getTempTargetsFromHistory() {
+ return activeTreatments.getTempTargetsFromHistory();
+ }
+
+ @Override
+ @Nullable
+ public ProfileSwitch getProfileSwitchFromHistory(long time) {
+ return activeTreatments.getProfileSwitchFromHistory(time);
+ }
+
+ @Override
+ public ProfileIntervals getProfileSwitchesFromHistory() {
+ return activeTreatments.getProfileSwitchesFromHistory();
+ }
+
+ @Override
+ public void addToHistoryProfileSwitch(ProfileSwitch profileSwitch) {
+ activeTreatments.addToHistoryProfileSwitch(profileSwitch);
+ NSUpload.uploadProfileSwitch(profileSwitch);
+ }
+
+ @Override
+ public long oldestDataAvailable() {
+ return activeTreatments.oldestDataAvailable();
+ }
+
+ public String getProfileName() {
+ return getProfileName(System.currentTimeMillis());
+ }
+
+ public String getProfileName(long time) {
+ ProfileSwitch profileSwitch = getProfileSwitchFromHistory(time);
+ if (profileSwitch != null) {
+ if (profileSwitch.profileJson != null) {
+ return profileSwitch.profileName;
} 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));
- data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + percent + "% " + durationInMinutes + " min"); // ECOR
- 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(), ConfigBuilderPlugin.class);
+ Profile profile = activeProfile.getProfile().getSpecificProfile(profileSwitch.profileName);
+ if (profile != null)
+ return profileSwitch.profileName;
}
- } catch (JSONException e) {
- e.printStackTrace();
}
+ // Unable to determine profile, failover to default
+ String defaultProfile = activeProfile.getProfile().getDefaultProfileName();
+ if (defaultProfile != null)
+ return defaultProfile;
+ // If default from plugin fails .... create empty
+ return "Default";
}
- 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));
- data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalendnote)); // ECOR
- 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(), ConfigBuilderPlugin.class);
- } catch (JSONException e) {
- e.printStackTrace();
- }
+ public Profile getProfile() {
+ return getProfile(System.currentTimeMillis());
}
- 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(), ConfigBuilderPlugin.class);
- } catch (JSONException e) {
- e.printStackTrace();
- }
+ public String getProfileUnits() {
+ return activeProfile.getUnits();
}
- public void uploadDeviceStatus() {
- DeviceStatus deviceStatus = new DeviceStatus();
- try {
- LoopPlugin.LastRun lastRun = LoopPlugin.lastRun;
- if (lastRun != null && lastRun.lastAPSRun.getTime() > new Date().getTime() - 300 * 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 DetermineBasalResultMA) {
- DetermineBasalResultMA result = (DetermineBasalResultMA) lastRun.request;
- deviceStatus.iob = result.iob.json();
- deviceStatus.iob.put("time", DateUtil.toISOString(lastRun.lastAPSRun));
- }
-
- if (lastRun.request instanceof DetermineBasalResultAMA) {
- DetermineBasalResultAMA result = (DetermineBasalResultAMA) 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);
- }
- } else {
- log.debug("OpenAPS data too old to upload");
- }
- if (activePump != null) {
- deviceStatus.device = "openaps://" + deviceID();
- JSONObject pumpstatus = getJSONStatus();
- if (pumpstatus != null) {
- deviceStatus.pump = pumpstatus;
+ public Profile getProfile(long time) {
+ if (activeTreatments == null)
+ return null; //app not initialized
+ //log.debug("Profile for: " + new Date(time).toLocaleString() + " : " + getProfileName(time));
+ boolean ignoreProfileSwitchEvents = SP.getBoolean(R.string.key_do_not_track_profile_switch, false);
+ if (!ignoreProfileSwitchEvents) {
+ ProfileSwitch profileSwitch = getProfileSwitchFromHistory(time);
+ if (profileSwitch != null) {
+ if (profileSwitch.profileJson != null) {
+ return profileSwitch.getProfileObject();
+ } else if (activeProfile.getProfile() != null) {
+ Profile profile = activeProfile.getProfile().getSpecificProfile(profileSwitch.profileName);
+ if (profile != null)
+ return profile;
}
}
-
- int batteryLevel = BatteryLevel.getBatteryLevel();
- deviceStatus.uploaderBattery = batteryLevel;
-
- deviceStatus.created_at = DateUtil.toISOString(new Date());
- deviceStatus.sendToNSClient();
+ // Unable to determine profile, failover to default
+ if (activeProfile.getProfile() == null)
+ return null; //app not initialized
+ }
+ Profile defaultProfile = activeProfile.getProfile().getDefaultProfile();
+ if (defaultProfile != null)
+ return defaultProfile;
+ // If default from plugin fails .... create empty
+ try {
+ Notification noisf = new Notification(Notification.ISF_MISSING, MainApp.sResources.getString(R.string.isfmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(noisf));
+ Notification noic = new Notification(Notification.IC_MISSING, MainApp.sResources.getString(R.string.icmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(noic));
+ Notification nobasal = new Notification(Notification.BASAL_MISSING, MainApp.sResources.getString(R.string.basalmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(nobasal));
+ Notification notarget = new Notification(Notification.TARGET_MISSING, MainApp.sResources.getString(R.string.targetmissing), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(notarget));
+ return new Profile(new JSONObject("{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"20\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"20\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"0.1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"6\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"8\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}}"));
} catch (JSONException e) {
e.printStackTrace();
}
+ return null;
}
-
- 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(), ConfigBuilderPlugin.class);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- public static void removeCareportalEntryFromNS(String _id) {
- try {
- Context context = MainApp.instance().getApplicationContext();
- Bundle bundle = new Bundle();
- bundle.putString("action", "dbRemove");
- bundle.putString("collection", "treatments");
- bundle.putString("_id", _id);
- Intent intent = new Intent(Intents.ACTION_DATABASE);
- intent.putExtras(bundle);
- intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- DbLogger.dbRemove(intent, _id, ConfigBuilderPlugin.class);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- public void uploadError(String error) {
- Context context = MainApp.instance().getApplicationContext();
- Bundle bundle = new Bundle();
- bundle.putString("action", "dbAdd");
- bundle.putString("collection", "treatments");
- JSONObject data = new JSONObject();
- try {
- data.put("eventType", "Announcement");
- data.put("created_at", DateUtil.toISOString(new Date()));
- data.put("notes", error);
- data.put("isAnnouncement", true);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- 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(), MsgError.class);
- }
-
- public void uploadAppStart() {
- if (SP.getBoolean(R.string.key_ns_logappstartedevent, true)) {
- Context context = MainApp.instance().getApplicationContext();
- Bundle bundle = new Bundle();
- bundle.putString("action", "dbAdd");
- bundle.putString("collection", "treatments");
- JSONObject data = new JSONObject();
- try {
- data.put("eventType", "Note");
- data.put("created_at", DateUtil.toISOString(new Date()));
- data.put("notes", MainApp.sResources.getString(R.string.androidaps_start));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- 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(), ConfigBuilderPlugin.class);
- }
- }
-
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java
new file mode 100644
index 0000000000..d6352cdb18
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java
@@ -0,0 +1,51 @@
+package info.nightscout.androidaps.plugins.ConfigBuilder;
+
+import android.support.annotation.Nullable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import info.nightscout.androidaps.data.DetailedBolusInfo;
+
+/**
+ * Created by mike on 08.08.2017.
+ */
+
+public class DetailedBolusInfoStorage {
+ private static Logger log = LoggerFactory.getLogger(DetailedBolusInfoStorage.class);
+ private static List store = new ArrayList<>();
+
+ public static void add(DetailedBolusInfo detailedBolusInfo) {
+ log.debug("Bolus info stored: " + new Date(detailedBolusInfo.date).toLocaleString());
+ store.add(detailedBolusInfo);
+ }
+
+ @Nullable
+ public static DetailedBolusInfo findDetailedBolusInfo(long bolustime) {
+ DetailedBolusInfo found = null;
+ for (int i = 0; i < store.size(); i++) {
+ long infoTime = store.get(i).date;
+ log.debug("Existing info: " + new Date(infoTime).toLocaleString());
+ if (bolustime > infoTime - 60 * 1000 && bolustime < infoTime + 60 * 1000) {
+ found = store.get(i);
+ break;
+ }
+ }
+ return found;
+ }
+
+ public static void remove(long bolustime) {
+ for (int i = 0; i < store.size(); i++) {
+ long infoTime = store.get(i).date;
+ if (bolustime > infoTime - 60 * 1000 && bolustime < infoTime + 60 * 1000) {
+ log.debug("Removing info: " + new Date(infoTime).toLocaleString());
+ store.remove(i);
+ break;
+ }
+ }
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesFragment.java
index 3334267057..906bf243ff 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesFragment.java
@@ -91,7 +91,7 @@ public class ObjectivesFragment extends Fragment {
}
});
- Long now = new Date().getTime();
+ Long now = System.currentTimeMillis();
if (position > 0 && objectives.get(position - 1).accomplished.getTime() == 0) {
// Phase 0: previous not completed
holder.startedLayout.setVisibility(View.GONE);
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java
index b6f4e57acd..e04efb70cb 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java
@@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
+import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
@@ -67,8 +68,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
@Override
public boolean isVisibleInTabs(int type) {
- LoopPlugin loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class);
- return type == CONSTRAINTS && fragmentVisible && loopPlugin != null && loopPlugin.isVisibleInTabs(LOOP);
+ return type == CONSTRAINTS && fragmentVisible && !BuildConfig.NSCLIENTOLNY;
}
@Override
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyFragment.java
deleted file mode 100644
index 07583e7622..0000000000
--- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyFragment.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package info.nightscout.androidaps.plugins.ConstraintsSafety;
-
-
-import android.support.v4.app.Fragment;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class SafetyFragment extends Fragment {
- private static Logger log = LoggerFactory.getLogger(SafetyFragment.class);
-
- private static SafetyPlugin safetyPlugin = new SafetyPlugin();
-
- public static SafetyPlugin getPlugin() {
- return safetyPlugin;
- }
-}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java
index cdf7f554ee..6880d1a21e 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java
@@ -3,6 +3,8 @@ package info.nightscout.androidaps.plugins.ConstraintsSafety;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.Objects;
+
import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
@@ -10,7 +12,7 @@ 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.NSClientInternal.data.NSProfile;
+import info.nightscout.androidaps.data.Profile;
import info.nightscout.utils.HardLimits;
import info.nightscout.utils.Round;
import info.nightscout.utils.SP;
@@ -21,9 +23,17 @@ import info.nightscout.utils.SP;
public class SafetyPlugin implements PluginBase, ConstraintsInterface {
private static Logger log = LoggerFactory.getLogger(SafetyPlugin.class);
+ static SafetyPlugin plugin = null;
+
+ public static SafetyPlugin getPlugin() {
+ if (plugin == null)
+ plugin = new SafetyPlugin();
+ return plugin;
+ }
+
@Override
public String getFragmentClass() {
- return SafetyFragment.class.getName();
+ return null;
}
@Override
@@ -105,7 +115,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface {
Double origAbsoluteRate = absoluteRate;
Double maxBasal = SP.getDouble("openapsma_max_basal", 1d);
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
+ Profile profile = MainApp.getConfigBuilder().getProfile();
if (profile == null) return absoluteRate;
if (absoluteRate < 0) absoluteRate = 0d;
@@ -118,8 +128,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface {
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 (absoluteRate > maxBasalMult * profile.getBasal()) {
+ absoluteRate = Math.floor(maxBasalMult * profile.getBasal() * 100) / 100;
if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit)
log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h");
}
@@ -136,9 +146,9 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface {
Integer origPercentRate = percentRate;
Double maxBasal = SP.getDouble("openapsma_max_basal", 1d);
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
+ Profile profile = MainApp.getConfigBuilder().getProfile();
if (profile == null) return percentRate;
- Double currentBasal = profile.getBasal(profile.secondsFromMidnight());
+ Double currentBasal = profile.getBasal();
Double absoluteRate = currentBasal * ((double) percentRate / 100);
@@ -153,17 +163,17 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface {
Double origRate = absoluteRate;
if (absoluteRate > maxBasal) {
absoluteRate = maxBasal;
- if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
+ if (Config.logConstraintsChanges && !Objects.equals(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)
+ if (absoluteRate > maxBasalMult * profile.getBasal()) {
+ absoluteRate = Math.floor(maxBasalMult * profile.getBasal() * 100) / 100;
+ if (Config.logConstraintsChanges && !Objects.equals(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)
+ if (Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit))
log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h");
}
@@ -172,7 +182,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface {
percentRateAfterConst = Round.ceilTo((double) percentRateAfterConst, 10d).intValue();
else percentRateAfterConst = Round.floorTo((double) percentRateAfterConst, 10d).intValue();
- if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit)
+ if (Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit))
log.debug("Recalculated percent rate " + percentRate + "% to " + percentRateAfterConst + "%");
return percentRateAfterConst;
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java
index abc768afdc..3eda06fb37 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java
@@ -9,20 +9,17 @@ import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.util.ArrayList;
-import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.interfaces.InsulinInterface;
-import info.nightscout.androidaps.plugins.Overview.graphExtensions.TimeAsXAxisLabelFormatter;
/**
* Created by mike on 21.04.2017.
*/
public class ActivityGraph extends GraphView {
-
Context context;
public ActivityGraph(Context context) {
@@ -36,28 +33,29 @@ public class ActivityGraph extends GraphView {
}
public void show(InsulinInterface insulin) {
+ removeAllSeries();
+ mSecondScale = null;
double dia = insulin.getDia();
int hours = (int) Math.floor(dia + 1);
- Treatment t = new Treatment(insulin);
- t.created_at = new Date(0);
- t.timeIndex = 0;
+ Treatment t = new Treatment();
+ t.date = 0;
t.insulin = 1d;
LineGraphSeries activitySeries = null;
LineGraphSeries iobSeries = null;
- List activityArray = new ArrayList();
- List iobArray = new ArrayList();
+ List activityArray = new ArrayList<>();
+ List iobArray = new ArrayList<>();
for (long time = 0; time <= hours * 60 * 60 * 1000; time += 5 * 60 * 1000L) {
- Iob iob = insulin.iobCalc(t, time, dia);
+ Iob iob = t.iobCalc(time, dia);
activityArray.add(new DataPoint(time / 60 / 1000, iob.activityContrib));
iobArray.add(new DataPoint(time / 60 / 1000, iob.iobContrib));
}
DataPoint[] activityDataPoints = new DataPoint[activityArray.size()];
activityDataPoints = activityArray.toArray(activityDataPoints);
- addSeries(activitySeries = new LineGraphSeries(activityDataPoints));
+ addSeries(activitySeries = new LineGraphSeries<>(activityDataPoints));
activitySeries.setThickness(8);
getViewport().setXAxisBoundsManual(true);
@@ -65,14 +63,16 @@ public class ActivityGraph extends GraphView {
getViewport().setMaxX(hours * 60);
getGridLabelRenderer().setNumHorizontalLabels(hours + 1);
getGridLabelRenderer().setHorizontalAxisTitle("[min]");
+ getGridLabelRenderer().setVerticalLabelsColor(activitySeries.getColor());
DataPoint[] iobDataPoints = new DataPoint[iobArray.size()];
iobDataPoints = iobArray.toArray(iobDataPoints);
- getSecondScale().addSeries(iobSeries = new LineGraphSeries(iobDataPoints));
+ getSecondScale().addSeries(iobSeries = new LineGraphSeries<>(iobDataPoints));
iobSeries.setDrawBackground(true);
iobSeries.setColor(Color.MAGENTA);
iobSeries.setBackgroundColor(Color.argb(70, 255, 0, 255));
getSecondScale().setMinY(0);
getSecondScale().setMaxY(1);
+ getGridLabelRenderer().setVerticalLabelsSecondScaleColor(Color.MAGENTA);
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingFragment.java
index a99e016621..f62d063193 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingFragment.java
@@ -35,12 +35,22 @@ public class InsulinFastactingFragment extends Fragment {
insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
- insulinName.setText(insulinFastactingPlugin.getFriendlyName());
- insulinComment.setText(insulinFastactingPlugin.getComment());
- insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinFastactingPlugin.getDia()).toString() + "h");
- insulinGraph.show(insulinFastactingPlugin);
+ updateGUI();
return view;
}
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateGUI();
+ }
+
+ private void updateGUI() {
+ insulinName.setText(insulinFastactingPlugin.getFriendlyName());
+ insulinComment.setText(insulinFastactingPlugin.getComment());
+ insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + Double.toString(insulinFastactingPlugin.getDia()) + "h");
+ insulinGraph.show(insulinFastactingPlugin);
+ }
+
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingPlugin.java
index 9773c20f55..e055e3d3ed 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/InsulinFastactingPlugin.java
@@ -1,7 +1,5 @@
package info.nightscout.androidaps.plugins.InsulinFastacting;
-import java.util.Date;
-
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
@@ -9,7 +7,6 @@ import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
-import info.nightscout.androidaps.interfaces.ProfileInterface;
/**
* Created by mike on 17.04.2017.
@@ -93,32 +90,29 @@ public class InsulinFastactingPlugin implements PluginBase, InsulinInterface {
@Override
public double getDia() {
- ProfileInterface profileInterface = MainApp.getConfigBuilder().getActiveProfile();
- if (profileInterface.getProfile() != null)
- return profileInterface.getProfile().getDia();
- return Constants.defaultDIA;
+ return MainApp.getConfigBuilder().getProfile() != null ? MainApp.getConfigBuilder().getProfile().getDia() : Constants.defaultDIA;
}
@Override
- public Iob iobCalc(Treatment treatment, long time, Double dia) {
+ public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) {
Iob result = new Iob();
- Double scaleFactor = 3.0 / dia;
- Double peak = 75d;
- Double end = 180d;
+ double scaleFactor = 3.0 / dia;
+ double peak = 75d;
+ double end = 180d;
if (treatment.insulin != 0d) {
- Long bolusTime = treatment.created_at.getTime();
- Double minAgo = scaleFactor * (time - bolusTime) / 1000d / 60d;
+ long bolusTime = treatment.date;
+ double minAgo = scaleFactor * (time - bolusTime) / 1000d / 60d;
if (minAgo < peak) {
- Double x1 = minAgo / 5d + 1;
+ double x1 = minAgo / 5d + 1;
result.iobContrib = treatment.insulin * (1 - 0.001852 * x1 * x1 + 0.001852 * x1);
// units: BG (mg/dL) = (BG/U) * U insulin * scalar
result.activityContrib = treatment.insulin * (2 / dia / 60 / peak) * minAgo;
} else if (minAgo < end) {
- Double x2 = (minAgo - 75) / 5;
+ double x2 = (minAgo - 75) / 5;
result.iobContrib = treatment.insulin * (0.001323 * x2 * x2 - 0.054233 * x2 + 0.55556);
result.activityContrib = treatment.insulin * (2 / dia / 60 - (minAgo - peak) * 2 / dia / 60 / (60 * 3 - peak));
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedFragment.java
index acc915b001..1747f5f4ab 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedFragment.java
@@ -36,12 +36,22 @@ public class InsulinFastactingProlongedFragment extends Fragment {
insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
- insulinName.setText(insulinFastactingProlongedPlugin.getFriendlyName());
- insulinComment.setText(insulinFastactingProlongedPlugin.getComment());
- insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinFastactingProlongedPlugin.getDia()).toString() + "h");
- insulinGraph.show(insulinFastactingProlongedPlugin);
+ updateGUI();
return view;
}
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateGUI();
+ }
+
+ private void updateGUI() {
+ insulinName.setText(insulinFastactingProlongedPlugin.getFriendlyName());
+ insulinComment.setText(insulinFastactingProlongedPlugin.getComment());
+ insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + Double.toString(insulinFastactingProlongedPlugin.getDia()) + "h");
+ insulinGraph.show(insulinFastactingProlongedPlugin);
+ }
+
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedPlugin.java
index 58d644043c..3c5df052df 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastactingProlonged/InsulinFastactingProlongedPlugin.java
@@ -1,7 +1,5 @@
package info.nightscout.androidaps.plugins.InsulinFastactingProlonged;
-import java.util.Date;
-
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
@@ -9,7 +7,6 @@ import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
-import info.nightscout.androidaps.interfaces.ProfileInterface;
/**
* Created by mike on 17.04.2017.
@@ -93,37 +90,34 @@ public class InsulinFastactingProlongedPlugin implements PluginBase, InsulinInte
@Override
public double getDia() {
- ProfileInterface profileInterface = MainApp.getConfigBuilder().getActiveProfile();
- if (profileInterface.getProfile() != null)
- return profileInterface.getProfile().getDia();
- return Constants.defaultDIA;
+ return MainApp.getConfigBuilder().getProfile() != null ? MainApp.getConfigBuilder().getProfile().getDia() : Constants.defaultDIA;
}
@Override
- public Iob iobCalc(Treatment treatment, long time, Double dia) {
+ public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) {
Iob result = new Iob();
//Double scaleFactor = 3.0 / dia;
- Double peak = 75d * dia / 6.0;
- Double tail = 180d * dia / 6.0;
- Double end = 360d * dia / 6.0;
- Double Total = 2 * peak + (tail - peak) * 5 / 2 + (end - tail) / 2;
+ double peak = 75d * dia / 6.0;
+ double tail = 180d * dia / 6.0;
+ double end = 360d * dia / 6.0;
+ double Total = 2 * peak + (tail - peak) * 5 / 2 + (end - tail) / 2;
if (treatment.insulin != 0d) {
- Long bolusTime = treatment.created_at.getTime();
- Double minAgo = (time - bolusTime) / 1000d / 60d;
+ long bolusTime = treatment.date;
+ double minAgo = (time - bolusTime) / 1000d / 60d;
if (minAgo < peak) {
- Double x1 = 6 / dia * minAgo / 5d + 1;
+ double x1 = 6 / dia * minAgo / 5d + 1;
result.iobContrib = treatment.insulin * (1 - 0.0012595 * x1 * x1 + 0.0012595 * x1);
// units: BG (mg/dL) = (BG/U) * U insulin * scalar
result.activityContrib = treatment.insulin * ((2 * peak / Total) * 2 / peak / peak * minAgo);
} else if (minAgo < tail) {
- Double x2 = (6 / dia * (minAgo - peak)) / 5;
+ double x2 = (6 / dia * (minAgo - peak)) / 5;
result.iobContrib = treatment.insulin * (0.00074 * x2 * x2 - 0.0403 * x2 + 0.69772);
result.activityContrib = treatment.insulin * (-((2 * peak / Total) * 2 / peak * 3 / 4) / (tail - peak) * (minAgo - peak) + (2 * peak / Total) * 2 / peak);
} else if (minAgo < end) {
- Double x3 = (6 / dia * (minAgo - tail)) / 5;
+ double x3 = (6 / dia * (minAgo - tail)) / 5;
result.iobContrib = treatment.insulin * (0.0001323 * x3 * x3 - 0.0097 * x3 + 0.17776);
result.activityContrib = treatment.insulin * (-((2 * peak / Total) * 2 / peak * 1 / 4) / (end - tail) * (minAgo - tail) + (2 * peak / Total) * 2 / peak / 4);
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java
new file mode 100644
index 0000000000..10dfd5dce5
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java
@@ -0,0 +1,108 @@
+package info.nightscout.androidaps.plugins.InsulinOrefCurves;
+
+import info.nightscout.androidaps.Constants;
+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.InsulinInterface;
+import info.nightscout.androidaps.interfaces.PluginBase;
+import info.nightscout.androidaps.plugins.Overview.Notification;
+import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
+
+/**
+ * Created by adrian on 13.08.2017.
+ */
+
+public abstract class InsulinOrefBasePlugin implements PluginBase, InsulinInterface {
+
+ public static double MIN_DIA = 5;
+
+ long lastWarned = 0;
+
+ @Override
+ public int getType() {
+ return INSULIN;
+ }
+
+ @Override
+ public String getNameShort() {
+ return MainApp.sResources.getString(R.string.insulin_shortname);
+ }
+
+ @Override
+ public boolean canBeHidden(int type) {
+ return true;
+ }
+
+ @Override
+ public boolean hasFragment() {
+ return true;
+ }
+
+ @Override
+ public boolean showInList(int type) {
+ return true;
+ }
+
+ @Override
+ public double getDia() {
+ double dia = getUserDefinedDia();
+ if(dia >= MIN_DIA){
+ return dia;
+ } else {
+ if((System.currentTimeMillis() - lastWarned) > 60*1000) {
+ lastWarned = System.currentTimeMillis();
+ Notification notification = new Notification(Notification.SHORT_DIA, String.format(MainApp.sResources.getString(R.string.dia_too_short), dia, MIN_DIA), Notification.URGENT);
+ MainApp.bus().post(new EventNewNotification(notification));
+ }
+ return MIN_DIA;
+ }
+ }
+
+ public double getUserDefinedDia() {
+ return MainApp.getConfigBuilder().getProfile() != null ? MainApp.getConfigBuilder().getProfile().getDia() : Constants.defaultDIA;
+ }
+
+ @Override
+ public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) {
+ Iob result = new Iob();
+
+ int peak = getPeak();
+
+
+ if (treatment.insulin != 0d) {
+
+ long bolusTime = treatment.date;
+ double t = (time - bolusTime) / 1000d / 60d;
+
+ double td = getDia()*60; //getDIA() always > 5
+ double tp = peak;
+
+ // force the IOB to 0 if over DIA hours have passed
+ if (t < td) {
+ double tau = tp * (1 - tp / td) / (1 - 2 * tp / td);
+ double a = 2 * tau / td;
+ double S = 1 / (1 - a + (1 + a) * Math.exp(-td / tau));
+ result.activityContrib = treatment.insulin * (S / Math.pow(tau, 2)) * t * (1 - t / td) * Math.exp(-t / tau);
+ result.iobContrib = treatment.insulin * (1 - S * (1 - a) * ((Math.pow(t, 2) / (tau * td * (1 - a)) - t / tau - 1) * Math.exp(-t / tau) + 1));
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public String getComment() {
+ String comment = commentStandardText();
+ double userDia = getUserDefinedDia();
+ if(userDia < MIN_DIA){
+ comment += "\n" + String.format(MainApp.sResources.getString(R.string.dia_too_short), userDia, MIN_DIA);
+ }
+ return comment;
+ }
+
+ abstract int getPeak();
+
+ abstract String commentStandardText();
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakFragment.java
new file mode 100644
index 0000000000..f7120369b3
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakFragment.java
@@ -0,0 +1,58 @@
+package info.nightscout.androidaps.plugins.InsulinOrefCurves;
+
+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.plugins.InsulinFastacting.ActivityGraph;
+
+/**
+ * Created by adrian on 14/08/17.
+ */
+
+public class InsulinOrefFreePeakFragment extends Fragment {
+
+ static InsulinOrefFreePeakPlugin insulinPlugin = new InsulinOrefFreePeakPlugin();
+
+ static public InsulinOrefFreePeakPlugin getPlugin() {
+ return insulinPlugin;
+ }
+
+ TextView insulinName;
+ TextView insulinComment;
+ TextView insulinDia;
+ ActivityGraph insulinGraph;
+
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View view = inflater.inflate(R.layout.insulin_fragment, container, false);
+
+ insulinName = (TextView) view.findViewById(R.id.insulin_name);
+ insulinComment = (TextView) view.findViewById(R.id.insulin_comment);
+ insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
+ insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
+
+ updateGUI();
+
+ return view;
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateGUI();
+ }
+
+ private void updateGUI() {
+ insulinName.setText(insulinPlugin.getFriendlyName());
+ insulinComment.setText(insulinPlugin.getComment());
+ insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + Double.toString(insulinPlugin.getDia()) + "h");
+ insulinGraph.show(insulinPlugin);
+ }
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakPlugin.java
new file mode 100644
index 0000000000..747eabda5b
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakPlugin.java
@@ -0,0 +1,67 @@
+package info.nightscout.androidaps.plugins.InsulinOrefCurves;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.utils.SP;
+
+/**
+ * Created by adrian on 14/08/17.
+ */
+
+public class InsulinOrefFreePeakPlugin extends InsulinOrefBasePlugin {
+
+ private static boolean fragmentEnabled = false;
+ private static boolean fragmentVisible = false;
+
+ public static final int DEFAULT_PEAK = 75;
+
+ @Override
+ public int getId() {
+ return OREF_FREE_PEAK;
+ }
+
+ @Override
+ public String getName() {
+ return MainApp.sResources.getString(R.string.free_peak_oref);
+ }
+
+ @Override
+ public String getFragmentClass() {
+ return InsulinOrefFreePeakFragment.class.getName();
+ }
+
+ @Override
+ public String getFriendlyName() {
+ return MainApp.sResources.getString(R.string.free_peak_oref);
+ }
+
+ @Override
+ public String commentStandardText() {
+ return MainApp.sResources.getString(R.string.insulin_peak_time) + ": " + getPeak();
+ }
+
+ @Override
+ public boolean isEnabled(int type) {
+ return type == INSULIN && fragmentEnabled;
+ }
+
+ @Override
+ public boolean isVisibleInTabs(int type) {
+ return type == INSULIN && fragmentVisible;
+ }
+
+ @Override
+ public void setFragmentEnabled(int type, boolean fragmentEnabled) {
+ if (type == INSULIN) this.fragmentEnabled = fragmentEnabled;
+ }
+
+ @Override
+ public void setFragmentVisible(int type, boolean fragmentVisible) {
+ if (type == INSULIN) this.fragmentVisible = fragmentVisible;
+ }
+
+ @Override
+ int getPeak() {
+ return SP.getInt(R.string.key_insulin_oref_peak, DEFAULT_PEAK);
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingFragment.java
new file mode 100644
index 0000000000..e30c087d21
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingFragment.java
@@ -0,0 +1,57 @@
+package info.nightscout.androidaps.plugins.InsulinOrefCurves;
+
+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.plugins.InsulinFastacting.ActivityGraph;
+
+/**
+ * Created by adrian on 14/08/17.
+ */
+
+public class InsulinOrefRapidActingFragment extends Fragment {
+
+ static InsulinOrefRapidActingPlugin insulinPlugin = new InsulinOrefRapidActingPlugin();
+
+ static public InsulinOrefRapidActingPlugin getPlugin() {
+ return insulinPlugin;
+ }
+
+ TextView insulinName;
+ TextView insulinComment;
+ TextView insulinDia;
+ ActivityGraph insulinGraph;
+
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View view = inflater.inflate(R.layout.insulin_fragment, container, false);
+
+ insulinName = (TextView) view.findViewById(R.id.insulin_name);
+ insulinComment = (TextView) view.findViewById(R.id.insulin_comment);
+ insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
+ insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
+
+ updateGUI();
+
+ return view;
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateGUI();
+ }
+
+ private void updateGUI() {
+ insulinName.setText(insulinPlugin.getFriendlyName());
+ insulinComment.setText(insulinPlugin.getComment());
+ insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + Double.toString(insulinPlugin.getDia()) + "h");
+ insulinGraph.show(insulinPlugin);
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingPlugin.java
new file mode 100644
index 0000000000..6f6973bc58
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingPlugin.java
@@ -0,0 +1,66 @@
+package info.nightscout.androidaps.plugins.InsulinOrefCurves;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+
+/**
+ * Created by adrian on 14/08/17.
+ */
+
+public class InsulinOrefRapidActingPlugin extends InsulinOrefBasePlugin {
+
+ private static boolean fragmentEnabled = false;
+ private static boolean fragmentVisible = false;
+
+ public static final int PEAK = 75;
+
+ @Override
+ public int getId() {
+ return OREF_RAPID_ACTING;
+ }
+
+ @Override
+ public String getName() {
+ return MainApp.sResources.getString(R.string.rapid_acting_oref);
+ }
+
+ @Override
+ public String getFragmentClass() {
+ return InsulinOrefRapidActingFragment.class.getName();
+ }
+
+ @Override
+ public String getFriendlyName() {
+ return MainApp.sResources.getString(R.string.rapid_acting_oref);
+ }
+
+ @Override
+ public String commentStandardText() {
+ return MainApp.sResources.getString(R.string.fastactinginsulincomment);
+ }
+
+ @Override
+ public boolean isEnabled(int type) {
+ return type == INSULIN && fragmentEnabled;
+ }
+
+ @Override
+ public boolean isVisibleInTabs(int type) {
+ return type == INSULIN && fragmentVisible;
+ }
+
+ @Override
+ public void setFragmentEnabled(int type, boolean fragmentEnabled) {
+ if (type == INSULIN) this.fragmentEnabled = fragmentEnabled;
+ }
+
+ @Override
+ public void setFragmentVisible(int type, boolean fragmentVisible) {
+ if (type == INSULIN) this.fragmentVisible = fragmentVisible;
+ }
+
+ @Override
+ int getPeak() {
+ return PEAK;
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingFragment.java
new file mode 100644
index 0000000000..ea05c7f2e7
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingFragment.java
@@ -0,0 +1,58 @@
+package info.nightscout.androidaps.plugins.InsulinOrefCurves;
+
+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.plugins.InsulinFastacting.ActivityGraph;
+
+/**
+ * Created by adrian on 14/08/17.
+ */
+
+public class InsulinOrefUltraRapidActingFragment extends Fragment {
+
+ static InsulinOrefUltraRapidActingPlugin insulinPlugin = new InsulinOrefUltraRapidActingPlugin();
+
+ static public InsulinOrefUltraRapidActingPlugin getPlugin() {
+ return insulinPlugin;
+ }
+
+ TextView insulinName;
+ TextView insulinComment;
+ TextView insulinDia;
+ ActivityGraph insulinGraph;
+
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View view = inflater.inflate(R.layout.insulin_fragment, container, false);
+
+ insulinName = (TextView) view.findViewById(R.id.insulin_name);
+ insulinComment = (TextView) view.findViewById(R.id.insulin_comment);
+ insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
+ insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
+
+ updateGUI();
+
+ return view;
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateGUI();
+ }
+
+ private void updateGUI() {
+ insulinName.setText(insulinPlugin.getFriendlyName());
+ insulinComment.setText(insulinPlugin.getComment());
+ insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + Double.toString(insulinPlugin.getDia()) + "h");
+ insulinGraph.show(insulinPlugin);
+ }
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingPlugin.java
new file mode 100644
index 0000000000..975707d38b
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingPlugin.java
@@ -0,0 +1,66 @@
+package info.nightscout.androidaps.plugins.InsulinOrefCurves;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+
+/**
+ * Created by adrian on 14/08/17.
+ */
+
+public class InsulinOrefUltraRapidActingPlugin extends InsulinOrefBasePlugin {
+
+ private static boolean fragmentEnabled = false;
+ private static boolean fragmentVisible = false;
+
+ public static final int PEAK = 55;
+
+ @Override
+ public int getId() {
+ return OREF_ULTRA_RAPID_ACTING;
+ }
+
+ @Override
+ public String getName() {
+ return MainApp.sResources.getString(R.string.ultrarapid_oref);
+ }
+
+ @Override
+ public String getFragmentClass() {
+ return InsulinOrefUltraRapidActingFragment.class.getName();
+ }
+
+ @Override
+ public String getFriendlyName() {
+ return MainApp.sResources.getString(R.string.ultrarapid_oref);
+ }
+
+ @Override
+ public String commentStandardText() {
+ return MainApp.sResources.getString(R.string.ultrafastactinginsulincomment);
+ }
+
+ @Override
+ public boolean isEnabled(int type) {
+ return type == INSULIN && fragmentEnabled;
+ }
+
+ @Override
+ public boolean isVisibleInTabs(int type) {
+ return type == INSULIN && fragmentVisible;
+ }
+
+ @Override
+ public void setFragmentEnabled(int type, boolean fragmentEnabled) {
+ if (type == INSULIN) this.fragmentEnabled = fragmentEnabled;
+ }
+
+ @Override
+ public void setFragmentVisible(int type, boolean fragmentVisible) {
+ if (type == INSULIN) this.fragmentVisible = fragmentVisible;
+ }
+
+ @Override
+ int getPeak() {
+ return PEAK;
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java
index 32ed9f8ed5..9939bd44d1 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java
@@ -1,24 +1,95 @@
package info.nightscout.androidaps.plugins.IobCobCalculator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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.Profile;
+import info.nightscout.androidaps.db.Treatment;
+import info.nightscout.androidaps.interfaces.PluginBase;
+import info.nightscout.androidaps.plugins.SensitivityAAPS.SensitivityAAPSPlugin;
+import info.nightscout.utils.SP;
/**
* Created by mike on 25.04.2017.
*/
public class AutosensData {
- long time = 0L;
+ private static Logger log = LoggerFactory.getLogger(AutosensData.class);
+
+ static class CarbsInPast {
+ long time = 0L;
+ double carbs = 0d;
+ double min5minCarbImpact = 0d;
+ double remaining = 0d;
+
+ public CarbsInPast(Treatment t) {
+ time = t.date;
+ carbs = t.carbs;
+ remaining = t.carbs;
+ if (MainApp.getSpecificPlugin(SensitivityAAPSPlugin.class) != null && MainApp.getSpecificPlugin(SensitivityAAPSPlugin.class).isEnabled(PluginBase.SENSITIVITY)) {
+ double maxAbsorptionHours = SP.getDouble(R.string.key_absorption_maxtime, 4d);
+ Profile profile = MainApp.getConfigBuilder().getProfile(t.date);
+ double sens = Profile.toMgdl(profile.getIsf(t.date), profile.getUnits());
+ double ic = profile.getIc(t.date);
+ min5minCarbImpact = t.carbs / (maxAbsorptionHours * 60 / 5) * sens / ic;
+ log.debug("Min 5m carbs impact for " + carbs + "g @" + new Date(t.date).toLocaleString() + " for " + maxAbsorptionHours + "h calculated to " + min5minCarbImpact + " ISF: " + sens + " IC: " + ic);
+ } else {
+ min5minCarbImpact = SP.getDouble("openapsama_min_5m_carbimpact", 3.0);
+ }
+ }
+ }
+
+ public long time = 0L;
public String pastSensitivity = "";
public double deviation = 0d;
- boolean calculateWithDeviation = false;
+ boolean nonCarbsDeviation = false;
+ public boolean nonEqualDeviation = false;
+ List activeCarbsList = new ArrayList<>();
double absorbed = 0d;
- double carbsFromBolus = 0d;
+ public double carbsFromBolus = 0d;
public double cob = 0;
public double bgi = 0d;
public double delta = 0d;
+ public double autosensRatio = 1d;
+
public String log(long time) {
- return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Delta=" + delta + " Bgi=" + bgi + " Deviation=" + deviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob;
+ return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Delta=" + delta + " Bgi=" + bgi + " Deviation=" + deviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob + " autosensRatio=" + autosensRatio;
+ }
+
+ public int minOld() {
+ return (int) ((System.currentTimeMillis() - time) / 1000 / 60);
+ }
+
+ // remove carbs older than 4h
+ public void removeOldCarbs(long toTime) {
+ for (int i = 0; i < activeCarbsList.size(); i++) {
+ CarbsInPast c = activeCarbsList.get(i);
+ if (c.time + 4 * 60 * 60 * 1000L < toTime) {
+ activeCarbsList.remove(i--);
+ if (c.remaining > 0)
+ cob -= c.remaining;
+ log.debug("Removing carbs at "+ new Date(toTime).toLocaleString() + " + after 4h :" + new Date(c.time).toLocaleString());
+ }
+ }
+ }
+
+ public void substractAbosorbedCarbs() {
+ double ac = absorbed;
+ for (int i = 0; i < activeCarbsList.size() && ac > 0; i++) {
+ CarbsInPast c = activeCarbsList.get(i);
+ if (c.remaining > 0) {
+ double sub = Math.min(ac, c.remaining);
+ c.remaining -= sub;
+ ac -= sub;
+ }
+ }
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java
deleted file mode 100644
index db1db95998..0000000000
--- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package info.nightscout.androidaps.plugins.IobCobCalculator;
-
-import android.support.v4.app.Fragment;
-
-/**
- * Created by adrian on 17/11/16.
- */
-
-public class IobCobCalculatorFragment extends Fragment {
-
- private static IobCobCalculatorPlugin iobCobCalculatorPlugin = new IobCobCalculatorPlugin();
-
- public static IobCobCalculatorPlugin getPlugin() {
- return iobCobCalculatorPlugin;
- }
-}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java
index 85a5c886bd..445eda93cd 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java
@@ -12,27 +12,27 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Date;
import java.util.List;
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.IobTotal;
+import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.BgReading;
+import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.db.Treatment;
+import info.nightscout.androidaps.events.EventConfigBuilderChange;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventNewBasalProfile;
+import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.PluginBase;
-import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
+import info.nightscout.androidaps.plugins.IobCobCalculator.events.BasalData;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
-import info.nightscout.utils.Round;
-import info.nightscout.utils.SP;
-import info.nightscout.utils.SafeParse;
/**
* Created by mike on 24.04.2017.
@@ -43,6 +43,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
private static LongSparseArray iobTable = new LongSparseArray<>(); // oldest at index 0
private static LongSparseArray autosensDataTable = new LongSparseArray<>(); // oldest at index 0
+ private static LongSparseArray basalDataTable = new LongSparseArray<>(); // oldest at index 0
private static volatile List bgReadings = null; // newest at index 0
private static volatile List bucketed_data = null;
@@ -52,7 +53,23 @@ public class IobCobCalculatorPlugin implements PluginBase {
private static Handler sHandler = null;
private static HandlerThread sHandlerThread = null;
- private static Object dataLock = new Object();
+ private static final Object dataLock = new Object();
+
+ private static IobCobCalculatorPlugin plugin = null;
+
+ public static IobCobCalculatorPlugin getPlugin() {
+ if (plugin == null)
+ plugin = new IobCobCalculatorPlugin();
+ return plugin;
+ }
+
+ public static LongSparseArray getAutosensDataTable() {
+ return autosensDataTable;
+ }
+
+ public static List getBucketedData() {
+ return bucketed_data;
+ }
@Override
public int getType() {
@@ -61,7 +78,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
@Override
public String getFragmentClass() {
- return IobCobCalculatorFragment.class.getName();
+ return null;
}
@Override
@@ -109,7 +126,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
}
- public IobCobCalculatorPlugin() {
+ IobCobCalculatorPlugin() {
MainApp.bus().register(this);
if (sHandlerThread == null) {
sHandlerThread = new HandlerThread(IobCobCalculatorPlugin.class.getSimpleName());
@@ -140,7 +157,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
private static int indexNewerThan(long time) {
for (int index = 0; index < bucketed_data.size(); index++) {
- if (bucketed_data.get(index).timeIndex < time)
+ if (bucketed_data.get(index).date < time)
return index - 1;
}
return -1;
@@ -156,14 +173,103 @@ public class IobCobCalculatorPlugin implements PluginBase {
private void loadBgData() {
//log.debug("Locking loadBgData");
synchronized (dataLock) {
- onNewProfile(new EventNewBasalProfile(null, "IobCobCalculator init"));
- bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime((long) (new Date().getTime() - 60 * 60 * 1000L * (24 + dia)), false);
+ onNewProfile(null);
+ bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime((long) (System.currentTimeMillis() - 60 * 60 * 1000L * (24 + dia)), false);
log.debug("BG data loaded. Size: " + bgReadings.size());
}
//log.debug("Releasing loadBgData");
}
- public void createBucketedData() {
+ private boolean isAbout5minData() {
+ synchronized (dataLock) {
+ if (bgReadings == null || bgReadings.size() < 3) {
+ return true;
+ }
+ long totalDiff = 0;
+ for (int i = 1; i < bgReadings.size(); ++i) {
+ long bgTime = bgReadings.get(i).date;
+ long lastbgTime = bgReadings.get(i - 1).date;
+ long diff = lastbgTime - bgTime;
+ totalDiff += diff;
+ if (diff > 30 * 1000 && diff < 270 * 1000) { // 0:30 - 4:30
+ log.debug("Interval detection: values: " + bgReadings.size() + " diff: " + (diff / 1000) + "sec is5minData: " + false);
+ return false;
+ }
+ }
+ double intervals = totalDiff / (5 * 60 * 1000d);
+ double variability = Math.abs(intervals - Math.round(intervals));
+ boolean is5mindata = variability < 0.02;
+ log.debug("Interval detection: values: " + bgReadings.size() + " variability: " + variability + " is5minData: " + is5mindata);
+ return is5mindata;
+ }
+ }
+
+ private void createBucketedData() {
+ if (isAbout5minData())
+ createBucketedData5min();
+ else
+ createBucketedDataRecalculated();
+ }
+
+ @Nullable
+ private BgReading findNewer(long time) {
+ BgReading lastFound = bgReadings.get(0);
+ if (lastFound.date < time) return null;
+ for (int i = 1; i < bgReadings.size(); ++i) {
+ if (bgReadings.get(i).date > time) continue;
+ lastFound = bgReadings.get(i);
+ if (bgReadings.get(i).date < time) break;
+ }
+ return lastFound;
+ }
+
+ @Nullable
+ private BgReading findOlder(long time) {
+ BgReading lastFound = bgReadings.get(bgReadings.size() - 1);
+ if (lastFound.date > time) return null;
+ for (int i = bgReadings.size() - 2; i >=0 ; --i) {
+ if (bgReadings.get(i).date < time) continue;
+ lastFound = bgReadings.get(i);
+ if (bgReadings.get(i).date > time) break;
+ }
+ return lastFound;
+ }
+
+ private void createBucketedDataRecalculated() {
+ synchronized (dataLock) {
+ if (bgReadings == null || bgReadings.size() < 3) {
+ bucketed_data = null;
+ return;
+ }
+
+ bucketed_data = new ArrayList<>();
+ long currentTime = bgReadings.get(0).date + 5 * 60 * 1000 - bgReadings.get(0).date % (5 * 60 * 1000) - 5 * 60 * 1000L;
+ //log.debug("First reading: " + new Date(currentTime).toLocaleString());
+
+ while (true) {
+ // test if current value is older than current time
+ BgReading newer = findNewer(currentTime);
+ BgReading older = findOlder(currentTime);
+ if (newer == null || older == null)
+ break;
+
+ double bgDelta = newer.value - older.value;
+ long timeDiffToNew = newer.date - currentTime;
+
+ double currentBg = newer.value - (double) timeDiffToNew / (newer.date - older.date) * bgDelta;
+ BgReading newBgreading = new BgReading();
+ newBgreading.date = currentTime;
+ newBgreading.value = Math.round(currentBg);
+ bucketed_data.add(newBgreading);
+ //log.debug("BG: " + newBgreading.value + " (" + new Date(newBgreading.date).toLocaleString() + ") Prev: " + older.value + " (" + new Date(older.date).toLocaleString() + ") Newer: " + newer.value + " (" + new Date(newer.date).toLocaleString() + ")");
+ currentTime -= 5 * 60 * 1000L;
+
+ }
+ }
+ }
+
+
+ public void createBucketedData5min() {
//log.debug("Locking createBucketedData");
synchronized (dataLock) {
if (bgReadings == null || bgReadings.size() < 3) {
@@ -175,8 +281,8 @@ public class IobCobCalculatorPlugin implements PluginBase {
bucketed_data.add(bgReadings.get(0));
int j = 0;
for (int i = 1; i < bgReadings.size(); ++i) {
- long bgTime = bgReadings.get(i).getTimeIndex();
- long lastbgTime = bgReadings.get(i - 1).getTimeIndex();
+ long bgTime = bgReadings.get(i).date;
+ long lastbgTime = bgReadings.get(i - 1).date;
//log.error("Processing " + i + ": " + new Date(bgTime).toString() + " " + bgReadings.get(i).value + " Previous: " + new Date(lastbgTime).toString() + " " + bgReadings.get(i - 1).value);
if (bgReadings.get(i).value < 39 || bgReadings.get(i - 1).value < 39) {
continue;
@@ -193,14 +299,14 @@ public class IobCobCalculatorPlugin implements PluginBase {
nextbgTime = lastbgTime - 5 * 60 * 1000;
j++;
BgReading newBgreading = new BgReading();
- newBgreading.timeIndex = nextbgTime;
+ newBgreading.date = nextbgTime;
double gapDelta = bgReadings.get(i).value - lastbg;
//console.error(gapDelta, lastbg, elapsed_minutes);
double nextbg = lastbg + (5d / elapsed_minutes * gapDelta);
newBgreading.value = Math.round(nextbg);
//console.error("Interpolated", bucketed_data[j]);
bucketed_data.add(newBgreading);
- //log.error("******************************************************************************************************* Adding:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value);
+ //log.error("******************************************************************************************************* Adding:" + new Date(newBgreading.date).toString() + " " + newBgreading.value);
elapsed_minutes = elapsed_minutes - 5;
lastbg = nextbg;
@@ -209,16 +315,16 @@ public class IobCobCalculatorPlugin implements PluginBase {
j++;
BgReading newBgreading = new BgReading();
newBgreading.value = bgReadings.get(i).value;
- newBgreading.timeIndex = bgTime;
+ newBgreading.date = bgTime;
bucketed_data.add(newBgreading);
- //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value);
+ //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.date).toString() + " " + newBgreading.value);
} else if (Math.abs(elapsed_minutes) > 2) {
j++;
BgReading newBgreading = new BgReading();
newBgreading.value = bgReadings.get(i).value;
- newBgreading.timeIndex = bgTime;
+ newBgreading.date = bgTime;
bucketed_data.add(newBgreading);
- //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value);
+ //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.date).toString() + " " + newBgreading.value);
} else {
bucketed_data.get(j).value = (bucketed_data.get(j).value + bgReadings.get(i).value) / 2;
//log.error("***** Average");
@@ -229,35 +335,30 @@ public class IobCobCalculatorPlugin implements PluginBase {
//log.debug("Releasing createBucketedData");
}
- public void calculateSensitivityData() {
+ private void calculateSensitivityData() {
+ if (MainApp.getConfigBuilder() == null)
+ return; // app still initializing
+ if (MainApp.getConfigBuilder().getProfile() == null)
+ return; // app still initializing
//log.debug("Locking calculateSensitivityData");
+ long oldestTimeWithData = oldestDataAvailable();
+
synchronized (dataLock) {
- NSProfile profile = ConfigBuilderPlugin.getActiveProfile() != null ? ConfigBuilderPlugin.getActiveProfile().getProfile() : null;
- if (profile == null || profile.getIsf(NSProfile.secondsFromMidnight()) == null || profile.getIc(NSProfile.secondsFromMidnight()) == null) {
- log.debug("calculateSensitivityData: No profile available");
- return;
- }
-
- if (ConfigBuilderPlugin.getActiveTreatments() == null) {
- log.debug("calculateSensitivityData: No treatments plugin");
- return;
- }
-
- TreatmentsInterface treatmentsInterface = ConfigBuilderPlugin.getActiveTreatments();
if (bucketed_data == null || bucketed_data.size() < 3) {
log.debug("calculateSensitivityData: No bucketed data available");
return;
}
- long prevDataTime = roundUpTime(bucketed_data.get(bucketed_data.size() - 3).timeIndex);
+ long prevDataTime = roundUpTime(bucketed_data.get(bucketed_data.size() - 3).date);
log.debug("Prev data time: " + new Date(prevDataTime).toLocaleString());
AutosensData previous = autosensDataTable.get(prevDataTime);
// start from oldest to be able sub cob
for (int i = bucketed_data.size() - 4; i >= 0; i--) {
// check if data already exists
- long bgTime = bucketed_data.get(i).timeIndex;
+ long bgTime = bucketed_data.get(i).date;
bgTime = roundUpTime(bgTime);
+ Profile profile = MainApp.getConfigBuilder().getProfile(bgTime);
AutosensData existing;
if ((existing = autosensDataTable.get(bgTime)) != null) {
@@ -265,11 +366,14 @@ public class IobCobCalculatorPlugin implements PluginBase {
continue;
}
- int secondsFromMidnight = NSProfile.secondsFromMidnight(bgTime);
- double sens = NSProfile.toMgdl(profile.getIsf(secondsFromMidnight), profile.getUnits());
+ double sens = Profile.toMgdl(profile.getIsf(bgTime), profile.getUnits());
AutosensData autosensData = new AutosensData();
autosensData.time = bgTime;
+ if (previous != null)
+ autosensData.activeCarbsList = new ArrayList<>(previous.activeCarbsList);
+ else
+ autosensData.activeCarbsList = new ArrayList<>();
//console.error(bgTime , bucketed_data[i].glucose);
double bg;
@@ -282,25 +386,36 @@ public class IobCobCalculatorPlugin implements PluginBase {
}
delta = (bg - bucketed_data.get(i + 1).value);
- IobTotal iob = calulateFromTreatmentsAndTemps(bgTime);
+ IobTotal iob = calculateFromTreatmentsAndTemps(bgTime);
double bgi = -iob.activity * sens * 5;
double deviation = delta - bgi;
- List recentTreatments = treatmentsInterface.getTreatments5MinBack(bgTime);
+ List recentTreatments = MainApp.getConfigBuilder().getTreatments5MinBackFromHistory(bgTime);
for (int ir = 0; ir < recentTreatments.size(); ir++) {
autosensData.carbsFromBolus += recentTreatments.get(ir).carbs;
+ autosensData.activeCarbsList.add(new AutosensData.CarbsInPast(recentTreatments.get(ir)));
}
+
// if we are absorbing carbs
if (previous != null && previous.cob > 0) {
+ // calculate sum of min carb impact from all active treatments
+ double totalMinCarbsImpact = 0d;
+ for (int ii = 0; ii < autosensData.activeCarbsList.size(); ++ii) {
+ AutosensData.CarbsInPast c = autosensData.activeCarbsList.get(ii);
+ totalMinCarbsImpact += c.min5minCarbImpact;
+ }
+
// figure out how many carbs that represents
- // but always assume at least 3mg/dL/5m (default) absorption
- double ci = Math.max(deviation, SP.getDouble("openapsama_min_5m_carbimpact", 3.0));
- autosensData.absorbed = ci * profile.getIc(secondsFromMidnight) / sens;
+ // but always assume at least 3mg/dL/5m (default) absorption per active treatment
+ double ci = Math.max(deviation, totalMinCarbsImpact);
+ autosensData.absorbed = ci * profile.getIc(bgTime) / sens;
// and add that to the running total carbsAbsorbed
autosensData.cob = Math.max(previous.cob - autosensData.absorbed, 0d);
+ autosensData.substractAbosorbedCarbs();
}
+ autosensData.removeOldCarbs(bgTime);
autosensData.cob += autosensData.carbsFromBolus;
autosensData.deviation = deviation;
autosensData.bgi = bgi;
@@ -310,12 +425,15 @@ public class IobCobCalculatorPlugin implements PluginBase {
if (autosensData.cob <= 0) {
if (Math.abs(deviation) < Constants.DEVIATION_TO_BE_EQUAL) {
autosensData.pastSensitivity += "=";
+ autosensData.nonEqualDeviation = true;
} else if (deviation > 0) {
autosensData.pastSensitivity += "+";
+ autosensData.nonEqualDeviation = true;
} else {
autosensData.pastSensitivity += "-";
+ autosensData.nonEqualDeviation = true;
}
- autosensData.calculateWithDeviation = true;
+ autosensData.nonCarbsDeviation = true;
} else {
autosensData.pastSensitivity += "C";
}
@@ -323,69 +441,112 @@ public class IobCobCalculatorPlugin implements PluginBase {
previous = autosensData;
autosensDataTable.put(bgTime, autosensData);
- log.debug(autosensData.log(bgTime));
+ autosensData.autosensRatio = detectSensitivity(oldestTimeWithData, bgTime).ratio;
+ if (Config.logAutosensData)
+ log.debug(autosensData.log(bgTime));
}
}
MainApp.bus().post(new EventAutosensCalculationFinished());
//log.debug("Releasing calculateSensitivityData");
}
- public static IobTotal calulateFromTreatmentsAndTemps(long time) {
- long now = new Date().getTime();
+ public static long oldestDataAvailable() {
+ long now = System.currentTimeMillis();
+
+ long oldestDataAvailable = MainApp.getConfigBuilder().oldestDataAvailable();
+ long getBGDataFrom = Math.max(oldestDataAvailable, (long) (now - 60 * 60 * 1000L * (24 + MainApp.getConfigBuilder().getProfile().getDia())));
+ log.debug("Limiting data to oldest available temps: " + new Date(oldestDataAvailable).toString());
+ return getBGDataFrom;
+ }
+
+ public static IobTotal calculateFromTreatmentsAndTempsSynchronized(long time) {
+ synchronized (dataLock) {
+ return calculateFromTreatmentsAndTemps(time);
+ }
+ }
+
+ public static IobTotal calculateFromTreatmentsAndTemps(long time) {
+ long now = System.currentTimeMillis();
time = roundUpTime(time);
if (time < now && iobTable.get(time) != null) {
- //log.debug(">>> Cache hit");
+ //og.debug(">>> calculateFromTreatmentsAndTemps Cache hit " + new Date(time).toLocaleString());
return iobTable.get(time);
} else {
- //log.debug(">>> Cache miss " + new Date(time).toLocaleString());
+ //log.debug(">>> calculateFromTreatmentsAndTemps Cache miss " + new Date(time).toLocaleString());
}
- IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getCalculationToTime(time).round();
- IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getCalculationToTime(time).round();
-/*
- if (basalIob.basaliob > 0) {
- log.debug(new Date(time).toLocaleString() + " basaliob: " + basalIob.basaliob );
- }
-*/
+ IobTotal bolusIob = MainApp.getConfigBuilder().getCalculationToTimeTreatments(time).round();
+ IobTotal basalIob = MainApp.getConfigBuilder().getCalculationToTimeTempBasals(time).round();
+
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
- if (time < new Date().getTime()) {
+ if (time < System.currentTimeMillis()) {
iobTable.put(time, iobTotal);
}
return iobTotal;
}
+ @Nullable
private static Long findPreviousTimeFromBucketedData(long time) {
if (bucketed_data == null)
return null;
for (int index = 0; index < bucketed_data.size(); index++) {
- if (bucketed_data.get(index).timeIndex < time)
- return bucketed_data.get(index).timeIndex;
+ if (bucketed_data.get(index).date < time)
+ return bucketed_data.get(index).date;
}
return null;
}
- public static AutosensData getAutosensData(long time) {
- long now = new Date().getTime();
- if (time > now)
- return null;
- Long previous = findPreviousTimeFromBucketedData(time);
- if (previous == null)
- return null;
- time = roundUpTime(previous);
- AutosensData data = autosensDataTable.get(time);
- if (data != null) {
- //log.debug(">>> Cache hit " + data.log(time));
- return data;
+ public static BasalData getBasalData(long time) {
+ long now = System.currentTimeMillis();
+ time = roundUpTime(time);
+ BasalData retval = basalDataTable.get(time);
+ if (retval == null) {
+ retval = new BasalData();
+ TemporaryBasal tb = MainApp.getConfigBuilder().getTempBasalFromHistory(time);
+ retval.basal = MainApp.getConfigBuilder().getProfile(time).getBasal(time);
+ if (tb != null) {
+ retval.isTempBasalRunning = true;
+ retval.tempBasalAbsolute = tb.tempBasalConvertedToAbsolute(time);
+ } else {
+ retval.isTempBasalRunning = false;
+ retval.tempBasalAbsolute = retval.basal;
+ }
+ if (time < now) {
+ basalDataTable.append(time, retval);
+ }
+ //log.debug(">>> getBasalData Cache miss " + new Date(time).toLocaleString());
} else {
- //log.debug(">>> Cache miss " + new Date(time).toLocaleString());
- return null;
+ //log.debug(">>> getBasalData Cache hit " + new Date(time).toLocaleString());
+ }
+ return retval;
+ }
+
+ @Nullable
+ public static AutosensData getAutosensData(long time) {
+ synchronized (dataLock) {
+ long now = System.currentTimeMillis();
+ if (time > now)
+ return null;
+ Long previous = findPreviousTimeFromBucketedData(time);
+ if (previous == null)
+ return null;
+ time = roundUpTime(previous);
+ AutosensData data = autosensDataTable.get(time);
+ if (data != null) {
+ //log.debug(">>> getAutosensData Cache hit " + data.log(time));
+ return data;
+ } else {
+ //log.debug(">>> getAutosensData Cache miss " + new Date(time).toLocaleString());
+ return null;
+ }
}
}
+ @Nullable
public static AutosensData getLastAutosensData() {
if (autosensDataTable.size() < 1)
return null;
AutosensData data = autosensDataTable.valueAt(autosensDataTable.size() - 1);
- if (data.time < new Date().getTime() - 5 * 60 * 1000) {
+ if (data.time < System.currentTimeMillis() - 5 * 60 * 1000) {
return null;
} else {
return data;
@@ -393,129 +554,31 @@ public class IobCobCalculatorPlugin implements PluginBase {
}
public static IobTotal[] calculateIobArrayInDia() {
- NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
+ Profile profile = MainApp.getConfigBuilder().getProfile();
// predict IOB out to DIA plus 30m
- long time = new Date().getTime();
+ long time = System.currentTimeMillis();
+ time = roundUpTime(time);
int len = (int) ((profile.getDia() * 60 + 30) / 5);
IobTotal[] array = new IobTotal[len];
int pos = 0;
for (int i = 0; i < len; i++) {
long t = time + i * 5 * 60000;
- IobTotal iob = calulateFromTreatmentsAndTemps(t);
+ IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t);
array[pos] = iob;
pos++;
}
return array;
}
- public static AutosensResult detectSensitivity(long fromTime) {
- //log.debug("Locking detectSensitivity");
+ public static AutosensResult detectSensitivityWithLock(long fromTime, long toTime) {
synchronized (dataLock) {
- if (autosensDataTable == null || autosensDataTable.size() < 4) {
- log.debug("No autosens data available");
- return new AutosensResult();
- }
-
- AutosensData current = getLastAutosensData();
- if (current == null) {
- log.debug("No current autosens data available");
- return new AutosensResult();
- }
-
-
- List deviationsArray = new ArrayList<>();
- String pastSensitivity = "";
- int index = 0;
- while (index < autosensDataTable.size()) {
- AutosensData autosensData = autosensDataTable.valueAt(index);
-
- if (autosensData.time < fromTime) {
- index++;
- continue;
- }
-
- if (autosensData.calculateWithDeviation)
- deviationsArray.add(autosensData.deviation);
-
- pastSensitivity += autosensData.pastSensitivity;
- int secondsFromMidnight = NSProfile.secondsFromMidnight(autosensData.time);
- if (secondsFromMidnight % 3600 < 2.5 * 60 || secondsFromMidnight % 3600 > 57.5 * 60) {
- pastSensitivity += "(" + Math.round(secondsFromMidnight / 3600d) + ")";
- }
- index++;
- }
-
- Double[] deviations = new Double[deviationsArray.size()];
- deviations = deviationsArray.toArray(deviations);
-
- if (ConfigBuilderPlugin.getActiveProfile() == null || ConfigBuilderPlugin.getActiveProfile().getProfile() == null) {
- log.debug("No profile available");
- return new AutosensResult();
- }
-
- NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
-
- Double sens = profile.getIsf(NSProfile.secondsFromMidnight());
-
- if (sens == null || profile.getMaxDailyBasal() == 0) {
- log.debug("No profile available");
- return new AutosensResult();
- }
-
- double ratio = 1;
- String ratioLimit = "";
- String sensResult = "";
-
- log.debug("Records: " + index + " " + pastSensitivity);
- Arrays.sort(deviations);
-
- for (double i = 0.9; i > 0.1; i = i - 0.02) {
- if (percentile(deviations, (i + 0.02)) >= 0 && percentile(deviations, i) < 0) {
- log.debug(Math.round(100 * i) + "% of non-meal deviations negative (target 45%-50%)");
- }
- }
- double pSensitive = percentile(deviations, 0.50);
- double pResistant = percentile(deviations, 0.45);
-
- double basalOff = 0;
-
- if (pSensitive < 0) { // sensitive
- basalOff = pSensitive * (60 / 5) / NSProfile.toMgdl(sens, profile.getUnits());
- sensResult = "Excess insulin sensitivity detected";
- } else if (pResistant > 0) { // resistant
- basalOff = pResistant * (60 / 5) / NSProfile.toMgdl(sens, profile.getUnits());
- sensResult = "Excess insulin resistance detected";
- } else {
- sensResult = "Sensitivity normal";
- }
- log.debug(sensResult);
- ratio = 1 + (basalOff / profile.getMaxDailyBasal());
-
- double rawRatio = ratio;
- ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_min", "0.7")));
- ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_max", "1.2")));
-
- if (ratio != rawRatio) {
- ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio;
- log.debug(ratioLimit);
- }
-
- double newisf = Math.round(NSProfile.toMgdl(sens, profile.getUnits()) / ratio);
- if (ratio != 1) {
- log.debug("ISF adjusted from " + NSProfile.toMgdl(sens, profile.getUnits()) + " to " + newisf);
- }
-
- AutosensResult output = new AutosensResult();
- output.ratio = Round.roundTo(ratio, 0.01);
- output.carbsAbsorbed = Round.roundTo(current.cob, 0.01);
- output.pastSensitivity = pastSensitivity;
- output.ratioLimit = ratioLimit;
- output.sensResult = sensResult;
- return output;
+ return detectSensitivity(fromTime, toTime);
}
- //log.debug("Releasing detectSensitivity");
}
+ private static AutosensResult detectSensitivity(long fromTime, long toTime) {
+ return ConfigBuilderPlugin.getActiveSensitivity().detectSensitivity(fromTime, toTime);
+ }
public static JSONArray convertToJSONArray(IobTotal[] iobArray) {
JSONArray array = new JSONArray();
@@ -539,17 +602,52 @@ public class IobCobCalculatorPlugin implements PluginBase {
@Subscribe
public void onNewProfile(EventNewBasalProfile ev) {
- if (MainApp.getConfigBuilder().getActiveProfile() == null)
- return;
- NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
- if (profile != null) {
- dia = profile.getDia();
- }
- if (ev.newNSProfile == null) { // on init no need of reset
+ if (MainApp.getConfigBuilder() == null)
+ return; // app still initializing
+ Profile profile = MainApp.getConfigBuilder().getProfile();
+ if (profile == null)
+ return; // app still initializing
+ dia = profile.getDia();
+ if (ev == null) { // on init no need of reset
return;
}
synchronized (dataLock) {
- log.debug("Invalidating cached data because of new profile from " + ev.from + ". IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records");
+ log.debug("Invalidating cached data because of new profile. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records");
+ iobTable = new LongSparseArray<>();
+ autosensDataTable = new LongSparseArray<>();
+ }
+ sHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ calculateSensitivityData();
+ }
+ });
+ }
+
+ @Subscribe
+ public void onStatusEvent(EventPreferenceChange ev) {
+ if (ev.isChanged(R.string.key_openapsama_autosens_period) ||
+ ev.isChanged(R.string.key_age) ||
+ ev.isChanged(R.string.key_absorption_maxtime)
+ ) {
+ synchronized (dataLock) {
+ log.debug("Invalidating cached data because of preference change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records");
+ iobTable = new LongSparseArray<>();
+ autosensDataTable = new LongSparseArray<>();
+ }
+ sHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ calculateSensitivityData();
+ }
+ });
+ }
+ }
+
+ @Subscribe
+ public void onStatusEvent(EventConfigBuilderChange ev) {
+ synchronized (dataLock) {
+ log.debug("Invalidating cached data because of configuration change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records");
iobTable = new LongSparseArray<>();
autosensDataTable = new LongSparseArray<>();
}
@@ -570,7 +668,9 @@ public class IobCobCalculatorPlugin implements PluginBase {
log.debug("Invalidating cached data to: " + new Date(time).toLocaleString());
for (int index = iobTable.size() - 1; index >= 0; index--) {
if (iobTable.keyAt(index) > time) {
- log.debug("Removing from iobTable: " + new Date(iobTable.keyAt(index)).toLocaleString());
+ if (Config.logAutosensData)
+ if (Config.logAutosensData)
+ log.debug("Removing from iobTable: " + new Date(iobTable.keyAt(index)).toLocaleString());
iobTable.removeAt(index);
} else {
break;
@@ -578,12 +678,22 @@ public class IobCobCalculatorPlugin implements PluginBase {
}
for (int index = autosensDataTable.size() - 1; index >= 0; index--) {
if (autosensDataTable.keyAt(index) > time) {
- log.debug("Removing from autosensDataTable: " + new Date(autosensDataTable.keyAt(index)).toLocaleString());
+ if (Config.logAutosensData)
+ log.debug("Removing from autosensDataTable: " + new Date(autosensDataTable.keyAt(index)).toLocaleString());
autosensDataTable.removeAt(index);
} else {
break;
}
}
+ for (int index = basalDataTable.size() - 1; index >= 0; index--) {
+ if (basalDataTable.keyAt(index) > time) {
+ if (Config.logAutosensData)
+ log.debug("Removing from basalDataTable: " + new Date(basalDataTable.keyAt(index)).toLocaleString());
+ basalDataTable.removeAt(index);
+ } else {
+ break;
+ }
+ }
}
sHandler.post(new Runnable() {
@Override
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/events/BasalData.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/events/BasalData.java
new file mode 100644
index 0000000000..4e1dcc1bf6
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/events/BasalData.java
@@ -0,0 +1,11 @@
+package info.nightscout.androidaps.plugins.IobCobCalculator.events;
+
+/**
+ * Created by mike on 10.06.2017.
+ */
+
+public class BasalData {
+ public double basal;
+ public double tempBasalAbsolute;
+ public boolean isTempBasalRunning;
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java
index 0d72464b73..3080717773 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java
@@ -402,16 +402,4 @@ public class DeviceStatus {
return record;
}
- public void sendToNSClient() {
- Context context = MainApp.instance().getApplicationContext();
- Bundle bundle = new Bundle();
- bundle.putString("action", "dbAdd");
- bundle.putString("collection", "devicestatus");
- bundle.putString("data", mongoRecord().toString());
- Intent intent = new Intent(Intents.ACTION_DATABASE);
- intent.putExtras(bundle);
- intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- DbLogger.dbAdd(intent, mongoRecord().toString(), DeviceStatus.class);
- }
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java
index de295d8a6e..2fb143517b 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java
@@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.Loop;
import android.app.Activity;
import android.os.Bundle;
-import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -19,10 +18,11 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui;
-public class LoopFragment extends Fragment implements View.OnClickListener {
+public class LoopFragment extends SubscriberFragment implements View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(LoopFragment.class);
private static LoopPlugin loopPlugin;
@@ -61,18 +61,6 @@ public class LoopFragment extends Fragment implements View.OnClickListener {
return view;
}
- @Override
- public void onPause() {
- super.onPause();
- MainApp.bus().unregister(this);
- }
-
- @Override
- public void onResume() {
- super.onResume();
- MainApp.bus().register(this);
- }
-
@Override
public void onClick(View view) {
switch (view.getId()) {
@@ -110,7 +98,8 @@ public class LoopFragment extends Fragment implements View.OnClickListener {
}
- void updateGUI() {
+ @Override
+ protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java
index f6d319dd7f..6e459a03b2 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java
@@ -10,8 +10,11 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.support.v7.app.NotificationCompat;
+import com.crashlytics.android.answers.Answers;
+import com.crashlytics.android.answers.CustomEvent;
import com.squareup.otto.Subscribe;
+import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,7 +35,10 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui;
import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification;
+import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin;
+import info.nightscout.utils.NSUpload;
import info.nightscout.utils.SP;
+import info.nightscout.utils.SafeParse;
/**
* Created by mike on 05.08.2016.
@@ -163,7 +169,7 @@ public class LoopPlugin implements PluginBase {
if (loopSuspendedTill == 0)
return 0;
- long now = new Date().getTime();
+ long now = System.currentTimeMillis();
long msecDiff = loopSuspendedTill - now;
if (loopSuspendedTill <= now) { // time exceeded
@@ -178,7 +184,7 @@ public class LoopPlugin implements PluginBase {
if (loopSuspendedTill == 0)
return false;
- long now = new Date().getTime();
+ long now = System.currentTimeMillis();
if (loopSuspendedTill <= now) { // time exceeded
suspendTo(0L);
@@ -192,7 +198,7 @@ public class LoopPlugin implements PluginBase {
if (loopSuspendedTill == 0)
return false;
- long now = new Date().getTime();
+ long now = System.currentTimeMillis();
if (loopSuspendedTill <= now) { // time exceeded
suspendTo(0L);
@@ -205,7 +211,7 @@ public class LoopPlugin implements PluginBase {
public void invoke(String initiator, boolean allowNotification) {
try {
if (Config.logFunctionCalls)
- log.debug("invoke");
+ log.debug("invoke from " + initiator);
ConstraintsInterface constraintsInterface = MainApp.getConfigBuilder();
if (!constraintsInterface.isLoopEnabled()) {
log.debug(MainApp.sResources.getString(R.string.loopdisabled));
@@ -230,6 +236,12 @@ public class LoopPlugin implements PluginBase {
return;
}
+ if (configBuilder.getProfile() == null) {
+ log.debug(MainApp.sResources.getString(R.string.noprofileselected));
+ MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.sResources.getString(R.string.noprofileselected)));
+ return;
+ }
+
// Check if pump info is loaded
if (configBuilder.getBaseBasalRate() < 0.01d) return;
@@ -256,7 +268,7 @@ public class LoopPlugin implements PluginBase {
lastRun.source = ((PluginBase) usedAPS).getName();
lastRun.setByPump = null;
- if (constraintsInterface.isClosedModeEnabled()) {
+ if (constraintsInterface.isClosedModeEnabled()) {
if (result.changeRequested) {
final PumpEnactResult waiting = new PumpEnactResult();
final PumpEnactResult previousResult = lastRun.setByPump;
@@ -267,6 +279,7 @@ public class LoopPlugin implements PluginBase {
@Override
public void run() {
final PumpEnactResult applyResult = configBuilder.applyAPSRequest(resultAfterConstraints);
+ Answers.getInstance().logCustom(new CustomEvent("APSRequest"));
if (applyResult.enacted || applyResult.success) {
lastRun.setByPump = applyResult;
lastRun.lastEnact = lastRun.lastAPSRun;
@@ -316,7 +329,7 @@ public class LoopPlugin implements PluginBase {
}
MainApp.bus().post(new EventLoopUpdateGui());
- MainApp.getConfigBuilder().uploadDeviceStatus();
+ NSUpload.uploadDeviceStatus();
} finally {
if (Config.logFunctionCalls)
log.debug("invoke end");
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java
index c3e8c777c6..164e9902df 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java
@@ -28,12 +28,13 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventPreferenceChange;
+import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientRestart;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI;
import info.nightscout.utils.SP;
-public class NSClientInternalFragment extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
+public class NSClientInternalFragment extends SubscriberFragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
private static Logger log = LoggerFactory.getLogger(NSClientInternalFragment.class);
static NSClientInternalPlugin nsClientInternalPlugin;
@@ -58,8 +59,6 @@ public class NSClientInternalFragment extends Fragment implements View.OnClickLi
private CheckBox autoscrollCheckbox;
private CheckBox pausedCheckbox;
- String status = "";
-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@@ -119,7 +118,7 @@ public class NSClientInternalFragment extends Fragment implements View.OnClickLi
builder.setMessage("Clear queue? All data in queue will be lost!");
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
- getPlugin().queue().clearQueue();
+ UploadQueue.clearQueue();
updateGUI();
Answers.getInstance().logCustom(new CustomEvent("NSClientClearQueue"));
}
@@ -152,36 +151,25 @@ public class NSClientInternalFragment extends Fragment implements View.OnClickLi
}
}
- @Override
- public void onPause() {
- super.onPause();
- MainApp.bus().unregister(this);
- }
-
- @Override
- public void onResume() {
- super.onResume();
- MainApp.bus().register(this);
- updateGUI();
- }
-
@Subscribe
public void onStatusEvent(final EventNSClientUpdateGUI ev) {
updateGUI();
}
- private void updateGUI() {
+ @Override
+ protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
- logTextView.setText(getPlugin().textLog);
+ NSClientInternalPlugin.updateLog();
+ logTextView.setText(NSClientInternalPlugin.textLog);
if (getPlugin().autoscroll) {
logScrollview.fullScroll(ScrollView.FOCUS_DOWN);
}
urlTextView.setText(getPlugin().url());
- Spanned queuetext = Html.fromHtml(MainApp.sResources.getString(R.string.queue) + " " + getPlugin().queue().size() + "");
+ Spanned queuetext = Html.fromHtml(MainApp.sResources.getString(R.string.queue) + " " + UploadQueue.size() + "");
queueTextView.setText(queuetext);
statusTextView.setText(getPlugin().status);
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java
index 07c34cda8c..b33d1cb2c3 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java
@@ -9,17 +9,16 @@ import android.os.HandlerThread;
import android.os.IBinder;
import android.text.Html;
import android.text.Spanned;
-import android.text.TextUtils;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
+import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
@@ -35,29 +34,28 @@ import info.nightscout.utils.ToastUtils;
public class NSClientInternalPlugin implements PluginBase {
private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class);
- boolean fragmentEnabled = true;
- boolean fragmentVisible = true;
+ private boolean fragmentEnabled = true;
+ private boolean fragmentVisible = true;
static public Handler handler;
- static private HandlerThread handlerThread;
- public List listLog = new ArrayList();
- public Spanned textLog = Html.fromHtml("");
+ private static List listLog = new ArrayList<>();
+ static Spanned textLog = Html.fromHtml("");
public boolean paused = false;
- public boolean autoscroll = true;
+ boolean autoscroll = true;
public String status = "";
public NSClientService nsClientService = null;
- public NSClientInternalPlugin() {
+ NSClientInternalPlugin() {
MainApp.bus().register(this);
paused = SP.getBoolean(R.string.key_nsclientinternal_paused, false);
autoscroll = SP.getBoolean(R.string.key_nsclientinternal_autoscroll, true);
if (handler == null) {
- handlerThread = new HandlerThread(NSClientInternalPlugin.class.getSimpleName() + "Handler");
+ HandlerThread handlerThread = new HandlerThread(NSClientInternalPlugin.class.getSimpleName() + "Handler");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}
@@ -115,7 +113,7 @@ public class NSClientInternalPlugin implements PluginBase {
@Override
public boolean showInList(int type) {
- return true;
+ return !Config.NSCLIENT;
}
@Override
@@ -128,7 +126,7 @@ public class NSClientInternalPlugin implements PluginBase {
if (type == GENERAL) this.fragmentVisible = fragmentVisible;
}
- ServiceConnection mConnection = new ServiceConnection() {
+ private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
log.debug("Service is disconnected");
@@ -161,45 +159,44 @@ public class NSClientInternalPlugin implements PluginBase {
MainApp.bus().post(new EventNSClientUpdateGUI());
}
- public void clearLog() {
+ synchronized void clearLog() {
handler.post(new Runnable() {
@Override
public void run() {
- listLog = new ArrayList();
- updateLog();
+ listLog = new ArrayList<>();
+ MainApp.bus().post(new EventNSClientUpdateGUI());
}
});
}
- private void addToLog(final EventNSClientNewLog ev) {
+ private synchronized void addToLog(final EventNSClientNewLog ev) {
handler.post(new Runnable() {
@Override
public void run() {
- SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
listLog.add(ev);
// remove the first line if log is too large
if (listLog.size() >= Constants.MAX_LOG_LINES) {
listLog.remove(0);
}
- updateLog();
+ MainApp.bus().post(new EventNSClientUpdateGUI());
}
});
}
- private void updateLog() {
+ static synchronized void updateLog() {
try {
- Spanned newTextLog = Html.fromHtml("");
- for (EventNSClientNewLog log : listLog) {
- newTextLog = (Spanned) TextUtils.concat(newTextLog, log.toHtml());
+ StringBuilder newTextLog = new StringBuilder();
+ List temporaryList = new ArrayList<>(listLog);
+ for (EventNSClientNewLog log : temporaryList) {
+ newTextLog.append(log.toPreparedHtml());
}
- textLog = newTextLog;
- MainApp.bus().post(new EventNSClientUpdateGUI());
+ textLog = Html.fromHtml(newTextLog.toString());
} catch (OutOfMemoryError e) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "Out of memory!\nStop using this phone !!!", R.raw.error);
}
}
- public void resend(String reason) {
+ void resend(String reason) {
if (nsClientService != null)
nsClientService.resend(reason);
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java
index d2696dce11..76a2855478 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java
@@ -50,7 +50,7 @@ public class UploadQueue {
public void run() {
log.debug("QUEUE adding: " + dbr.data);
MainApp.getDbHelper().create(dbr);
- NSClientInternalPlugin plugin = (NSClientInternalPlugin) MainApp.getSpecificPlugin(NSClientInternalPlugin.class);
+ NSClientInternalPlugin plugin = MainApp.getSpecificPlugin(NSClientInternalPlugin.class);
if (plugin != null) {
plugin.resend("newdata");
}
@@ -113,7 +113,7 @@ public class UploadQueue {
String result = "";
CloseableIterator iterator = null;
try {
- iterator = MainApp.getDbHelper().getDaoDbRequest().closeableIterator();
+ iterator = MainApp.getDbHelper().getDbRequestInterator();
try {
while (iterator.hasNext()) {
DbRequest dbr = iterator.next();
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAckAlarm.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAckAlarm.java
new file mode 100644
index 0000000000..216fa3d19a
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAckAlarm.java
@@ -0,0 +1,50 @@
+package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.androidaps.plugins.NSClientInternal.data.NSAlarm;
+import info.nightscout.utils.SP;
+
+/**
+ * Created by mike on 11.06.2017.
+ */
+
+public class BroadcastAckAlarm {
+ private static Logger log = LoggerFactory.getLogger(BroadcastAckAlarm.class);
+
+ public static void handleClearAlarm(NSAlarm originalAlarm, Context context, long silenceTimeInMsec) {
+
+ Bundle bundle = new Bundle();
+ bundle.putInt("level", originalAlarm.getLevel());
+ bundle.putString("group", originalAlarm.getGroup());
+ bundle.putLong("silenceTime", silenceTimeInMsec);
+ Intent intent = new Intent(Intents.ACTION_ACK_ALARM);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
+
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putInt("level", originalAlarm.getLevel());
+ bundle.putString("group", originalAlarm.getGroup());
+ bundle.putLong("silenceTime", silenceTimeInMsec);
+ intent = new Intent(Intents.ACTION_ACK_ALARM);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
+ }
+
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAlarm.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAlarm.java
new file mode 100644
index 0000000000..0fee6600bd
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAlarm.java
@@ -0,0 +1,43 @@
+package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
+
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
+
+/**
+ * Created by mike on 26.06.2016.
+ */
+public class BroadcastAlarm {
+ private static Logger log = LoggerFactory.getLogger(BroadcastAlarm.class);
+
+ public static void handleAlarm(JSONObject alarm, Context context) {
+ Bundle bundle = new Bundle();
+ bundle.putString("data", alarm.toString());
+ Intent intent = new Intent(Intents.ACTION_ALARM);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
+
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("data", alarm.toString());
+ intent = new Intent(Intents.ACTION_ALARM);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAnnouncement.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAnnouncement.java
new file mode 100644
index 0000000000..9b133c2551
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastAnnouncement.java
@@ -0,0 +1,44 @@
+package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
+
+/**
+ * Created by mike on 26.06.2016.
+ */
+public class BroadcastAnnouncement {
+ private static Logger log = LoggerFactory.getLogger(BroadcastAnnouncement.class);
+
+ public static void handleAnnouncement(JSONObject announcement, Context context) {
+ Bundle bundle = new Bundle();
+ bundle.putString("data", announcement.toString());
+ Intent intent = new Intent(Intents.ACTION_ANNOUNCEMENT);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
+
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("data", announcement.toString());
+ intent = new Intent(Intents.ACTION_ANNOUNCEMENT);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastCals.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastCals.java
index 58a24e825c..7580a00088 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastCals.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastCals.java
@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
import org.json.JSONArray;
import org.slf4j.Logger;
@@ -11,7 +12,10 @@ import org.slf4j.LoggerFactory;
import java.util.List;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
/**
* Created by mike on 26.06.2016.
@@ -19,16 +23,24 @@ import info.nightscout.androidaps.Services.Intents;
public class BroadcastCals {
private static Logger log = LoggerFactory.getLogger(BroadcastCals.class);
- public void handleNewCal(JSONArray cals, Context context, boolean isDelta) {
+ public static void handleNewCal(JSONArray cals, Context context, boolean isDelta) {
+
Bundle bundle = new Bundle();
bundle.putString("cals", cals.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_CAL);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("CAL " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("cals", cals.toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_CAL);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastClearAlarm.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastClearAlarm.java
new file mode 100644
index 0000000000..23406837a7
--- /dev/null
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastClearAlarm.java
@@ -0,0 +1,43 @@
+package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
+
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
+import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
+
+/**
+ * Created by mike on 26.06.2016.
+ */
+public class BroadcastClearAlarm {
+ private static Logger log = LoggerFactory.getLogger(BroadcastClearAlarm.class);
+
+ public static void handleClearAlarm(JSONObject clearalarm, Context context) {
+ Bundle bundle = new Bundle();
+ bundle.putString("data", clearalarm.toString());
+ Intent intent = new Intent(Intents.ACTION_CLEAR_ALARM);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
+
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("data", clearalarm.toString());
+ intent = new Intent(Intents.ACTION_CLEAR_ALARM);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
+ }
+}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastDeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastDeviceStatus.java
index 64fd72b36a..a912fc5d3c 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastDeviceStatus.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastDeviceStatus.java
@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -12,34 +13,58 @@ import org.slf4j.LoggerFactory;
import java.util.List;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
public class BroadcastDeviceStatus {
private static Logger log = LoggerFactory.getLogger(BroadcastDeviceStatus.class);
- public void handleNewDeviceStatus(JSONObject status, Context context, boolean isDelta) {
+ public static void handleNewDeviceStatus(JSONObject status, Context context, boolean isDelta) {
Bundle bundle = new Bundle();
bundle.putString("devicestatus", status.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_DEVICESTATUS);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("DEVICESTATUS " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("devicestatus", status.toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_DEVICESTATUS);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
- public void handleNewDeviceStatus(JSONArray statuses, Context context, boolean isDelta) {
- Bundle bundle = new Bundle();
- bundle.putString("devicestatuses", statuses.toString());
- bundle.putBoolean("delta", isDelta);
- Intent intent = new Intent(Intents.ACTION_NEW_DEVICESTATUS);
- intent.putExtras(bundle);
- intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ public static void handleNewDeviceStatus(JSONArray statuses, Context context, boolean isDelta) {
- log.debug("DEVICESTATUS " + x.size() + " receivers");
+ List splitted = BroadcastTreatment.splitArray(statuses);
+ for (JSONArray part: splitted) {
+ Bundle bundle = new Bundle();
+ bundle.putString("devicestatuses", part.toString());
+ bundle.putBoolean("delta", isDelta);
+ Intent intent = new Intent(Intents.ACTION_NEW_DEVICESTATUS);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
+ }
+
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ splitted = BroadcastTreatment.splitArray(statuses);
+ for (JSONArray part : splitted) {
+ Bundle bundle = new Bundle();
+ bundle.putString("devicestatuses", part.toString());
+ bundle.putBoolean("delta", isDelta);
+ Intent intent = new Intent(Intents.ACTION_NEW_DEVICESTATUS);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
+ }
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastMbgs.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastMbgs.java
index 33669e1852..a715747791 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastMbgs.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastMbgs.java
@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
import org.json.JSONArray;
import org.slf4j.Logger;
@@ -11,7 +12,10 @@ import org.slf4j.LoggerFactory;
import java.util.List;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
/**
* Created by mike on 26.06.2016.
@@ -19,16 +23,24 @@ import info.nightscout.androidaps.Services.Intents;
public class BroadcastMbgs {
private static Logger log = LoggerFactory.getLogger(BroadcastMbgs.class);
- public void handleNewMbg(JSONArray mbgs, Context context, boolean isDelta) {
+ public static void handleNewMbg(JSONArray mbgs, Context context, boolean isDelta) {
+
Bundle bundle = new Bundle();
bundle.putString("mbgs", mbgs.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_MBG);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("MBG " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("mbgs", mbgs.toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_MBG);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastProfile.java
index a1d44d0340..3802b09657 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastProfile.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastProfile.java
@@ -4,14 +4,18 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
+import info.nightscout.androidaps.data.ProfileStore;
+import info.nightscout.utils.SP;
/**
@@ -20,18 +24,25 @@ import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
public class BroadcastProfile {
private static Logger log = LoggerFactory.getLogger(BroadcastProfile.class);
- public void handleNewTreatment(NSProfile profile, Context context, boolean isDelta) {
+ public static void handleNewTreatment(ProfileStore profile, Context context, boolean isDelta) {
+
Bundle bundle = new Bundle();
bundle.putString("profile", profile.getData().toString());
- bundle.putString("activeprofile", profile.getActiveProfile());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_PROFILE);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("PROFILE " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("profile", profile.getData().toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_PROFILE);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastQueueStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastQueueStatus.java
index ed2b01a27c..abc49be18a 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastQueueStatus.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastQueueStatus.java
@@ -5,13 +5,18 @@ import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
/**
* Created by mike on 28.02.2016.
*/
public class BroadcastQueueStatus {
- public void handleNewStatus(int size, Context context) {
+ public static void handleNewStatus(int size, Context context) {
+
+ if(!SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) return;
+
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"sendQueue");
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastSgvs.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastSgvs.java
index 81a2621a77..d4193f3cb7 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastSgvs.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastSgvs.java
@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -12,7 +13,10 @@ import org.slf4j.LoggerFactory;
import java.util.List;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
+import info.nightscout.utils.SP;
/**
* Created by mike on 22.02.2016.
@@ -20,30 +24,45 @@ import info.nightscout.androidaps.Services.Intents;
public class BroadcastSgvs {
private static Logger log = LoggerFactory.getLogger(BroadcastSgvs.class);
- public void handleNewSgv(JSONObject sgv, Context context, boolean isDelta) {
+ public static void handleNewSgv(JSONObject sgv, Context context, boolean isDelta) {
+
Bundle bundle = new Bundle();
bundle.putString("sgv", sgv.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_SGV);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("SGV " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("sgv", sgv.toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_SGV);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
- public void handleNewSgv(JSONArray sgvs, Context context, boolean isDelta) {
+ public static void handleNewSgv(JSONArray sgvs, Context context, boolean isDelta) {
Bundle bundle = new Bundle();
bundle.putString("sgvs", sgvs.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_SGV);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("SGV " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("sgvs", sgvs.toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_SGV);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java
index b99950c89b..7c6862cb14 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java
@@ -5,6 +5,7 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -12,9 +13,11 @@ import org.slf4j.LoggerFactory;
import java.util.List;
import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
-import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus;
+import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus;
import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService;
+import info.nightscout.utils.SP;
/**
* Created by mike on 24.02.2016.
@@ -22,14 +25,15 @@ import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientServ
public class BroadcastStatus {
private static Logger log = LoggerFactory.getLogger(BroadcastStatus.class);
- public void handleNewStatus(NSStatus status, Context context, boolean isDelta) {
+ public static void handleNewStatus(NSSettingsStatus status, Context context, boolean isDelta) {
+
Bundle bundle = new Bundle();
try {
bundle.putString("nsclientversionname", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionName);
bundle.putInt("nsclientversioncode", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
- };
+ }
bundle.putString("nightscoutversionname", NSClientService.nightscoutVersionName);
bundle.putInt("nightscoutversioncode", NSClientService.nightscoutVersionCode);
bundle.putString("status", status.getData().toString());
@@ -37,9 +41,24 @@ public class BroadcastStatus {
Intent intent = new Intent(Intents.ACTION_NEW_STATUS);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("STATUS: " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ try {
+ bundle.putString("nsclientversionname", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionName);
+ bundle.putInt("nsclientversioncode", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode);
+ } catch (PackageManager.NameNotFoundException e) {
+ e.printStackTrace();
+ }
+ bundle.putString("nightscoutversionname", NSClientService.nightscoutVersionName);
+ bundle.putInt("nightscoutversioncode", NSClientService.nightscoutVersionCode);
+ bundle.putString("status", status.getData().toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_STATUS);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
}
diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java
index 9d1abe43e3..0d998a2bcd 100644
--- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java
+++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java
@@ -4,6 +4,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
+import android.os.TransactionTooLargeException;
+import android.support.v4.content.LocalBroadcastManager;
import org.json.JSONArray;
import org.json.JSONException;
@@ -11,10 +13,15 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.ArrayList;
import java.util.List;
+import info.nightscout.androidaps.MainApp;
+import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment;
+import info.nightscout.utils.SP;
+import info.nightscout.utils.ToastUtils;
/**
* Created by mike on 20.02.2016.
@@ -22,86 +29,173 @@ import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment;
public class BroadcastTreatment {
private static Logger log = LoggerFactory.getLogger(BroadcastTreatment.class);
- public void handleNewTreatment(NSTreatment treatment, Context context, boolean isDelta) {
+ public static void handleNewTreatment(NSTreatment treatment, Context context, boolean isDelta) {
+
Bundle bundle = new Bundle();
bundle.putString("treatment", treatment.getData().toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
- log.debug("TREAT_ADD " + treatment.getEventType() + " " + x.size() + " receivers");
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
+ bundle = new Bundle();
+ bundle.putString("treatment", treatment.getData().toString());
+ bundle.putBoolean("delta", isDelta);
+ intent = new Intent(Intents.ACTION_NEW_TREATMENT);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
}
- public void handleNewTreatment(JSONArray treatments, Context context, boolean isDelta) {
- Bundle bundle = new Bundle();
- bundle.putString("treatments", treatments.toString());
- bundle.putBoolean("delta", isDelta);
- Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT);
- intent.putExtras(bundle);
- intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List x = context.getPackageManager().queryBroadcastReceivers(intent, 0);
+ public static void handleNewTreatment(JSONArray treatments, Context context, boolean isDelta) {
- log.debug("TREAT_ADD " + treatments.length() + " " + x.size() + " receivers");
+ List splitted = splitArray(treatments);
+ for (JSONArray part: splitted) {
+ Bundle bundle = new Bundle();
+ bundle.putString("treatments", part.toString());
+ bundle.putBoolean("delta", isDelta);
+ Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
+ }
+
+ if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)){
+ splitted = splitArray(treatments);
+ for (JSONArray part: splitted) {
+ Bundle bundle = new Bundle();
+ bundle.putString("treatments", part.toString());
+ bundle.putBoolean("delta", isDelta);
+ Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT);
+ intent.putExtras(bundle);
+ intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
+ context.sendBroadcast(intent);
+ }
+ }
}
public void handleChangedTreatment(JSONObject treatment, Context context, boolean isDelta) {
+
Bundle bundle = new Bundle();
bundle.putString("treatment", treatment.toString());
bundle.putBoolean("delta", isDelta);
Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
- context.sendBroadcast(intent);
- List