diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 74704b2234..95b5f7bde1 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -32,6 +32,9 @@ import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesFragme 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; @@ -114,6 +117,9 @@ public class MainApp extends Application { 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()); diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index 71c1445298..f8f30dbe6c 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -15,6 +15,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.PumpCombo.ComboPlugin; +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; @@ -146,6 +147,11 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre if (virtualPumpPlugin != null && virtualPumpPlugin.isEnabled(PluginBase.PUMP)) { addPreferencesFromResource(R.xml.pref_virtualpump); } + InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin = (InsulinOrefFreePeakPlugin) MainApp.getSpecificPlugin(InsulinOrefFreePeakPlugin.class); + if(insulinOrefFreePeakPlugin.isEnabled(PluginBase.INSULIN)){ + addPreferencesFromResource(R.xml.pref_insulinoreffreepeak); + } + NSClientInternalPlugin nsClientInternalPlugin = (NSClientInternalPlugin) MainApp.getSpecificPlugin(NSClientInternalPlugin.class); if (nsClientInternalPlugin != null && nsClientInternalPlugin.isEnabled(PluginBase.GENERAL)) { addPreferencesFromResource(R.xml.pref_nsclientinternal); 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 3b04c30d94..39eb023cb3 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -37,6 +37,7 @@ import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.receivers.DataReceiver; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; +import info.nightscout.utils.BundleLogger; import info.nightscout.utils.SP; @@ -56,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; diff --git a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java index 00a04df4c4..aa54ac100f 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java +++ b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java @@ -22,12 +22,12 @@ public class DetailedBolusInfo { 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 boolean addToTreatments = true; 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/OverlappingIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java index 608fa8d328..070426cca0 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java +++ b/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java @@ -15,7 +15,7 @@ public class OverlappingIntervals extends Intervals { boolean needToCut = false; long cutTime = 0; - for (int index = rawData.size()-1; index > 0; index--) { //begin with newest + for (int index = rawData.size()-1; index >= 0; index--) { //begin with newest Interval cur = rawData.valueAt(index); if (cur.isEndingEvent()){ needToCut = true; @@ -31,7 +31,7 @@ public class OverlappingIntervals extends Intervals { @Nullable public synchronized T getValueByInterval(long time) { - for (int index = rawData.size()-1; index > 0; index--) { //begin with newest + for (int index = rawData.size()-1; index >= 0; index--) { //begin with newest T cur = rawData.valueAt(index); if (cur.match(time)){ return cur; diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index dc49d95455..ac7cc0edd0 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -212,7 +212,8 @@ public class Profile { retValue += format.format(o2.getDouble("value")); } retValue += " " + units; - retValue += "\n"; + if (index + 1 < array.length()) + retValue += "\n"; } catch (JSONException e) { e.printStackTrace(); } @@ -391,4 +392,10 @@ public class Profile { if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(valueInMgdl); else return DecimalFormatter.to1Decimal(valueInMmol); } + + // targets are stored in mg/dl + public static String toTargetRangeString(double low, double high, String units) { + if (low == high) return toUnitsString(low, Profile.fromMgdlToUnits(low, Constants.MMOL), units); + else return toUnitsString(low, Profile.fromMgdlToUnits(low, Constants.MMOL), units) + " - " + toUnitsString(high, Profile.fromMgdlToUnits(high, Constants.MMOL), units); + } } 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 9734456cc9..2b4cc87f20 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -598,6 +598,16 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { scheduleTreatmentChange(); } + 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) { 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 722b534c7d..aebd52ac30 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java @@ -181,6 +181,8 @@ public class Treatment implements DataPointWithLabelInterface { // ----------------- DataPointInterface end -------------------- public Iob iobCalc(long time, double dia) { + if (!isValid) + return new Iob(); InsulinInterface insulinInterface = MainApp.getInsulinIterfaceById(insulinInterfaceID); if (insulinInterface == null) insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); 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 20c563dede..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,6 +12,10 @@ 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(); 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 c6b278955f..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 @@ -215,7 +215,7 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL } break; case R.id.actions_canceltempbasal: - if (MainApp.getConfigBuilder().isInHistoryRealTempBasalInProgress()) { + if (MainApp.getConfigBuilder().isTempBasalInProgress()) { sHandler.post(new Runnable() { @Override public void run() { 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 834206ca34..8a4ec0be38 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 @@ -164,8 +164,8 @@ public class FillDialog extends DialogFragment implements OnClickListener { DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); detailedBolusInfo.insulin = finalInsulinAfterConstraints; detailedBolusInfo.context = context; - detailedBolusInfo.addToTreatments = false; detailedBolusInfo.source = Source.NONE; + 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); 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 65b84ed79d..e09f143498 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 @@ -887,10 +887,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override // return true if new record is created public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) { - if (!detailedBolusInfo.addToTreatments) - return false; boolean newRecordCreated = activeTreatments.addToHistoryTreatment(detailedBolusInfo); - if (newRecordCreated) + if (newRecordCreated && detailedBolusInfo.isValid) NSUpload.uploadBolusWizardRecord(detailedBolusInfo); return newRecordCreated; } 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 index c717d62edb..e99c5a8f22 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java @@ -28,7 +28,7 @@ public class DetailedBolusInfoStorage { public static DetailedBolusInfo findDetailedBolusInfo(long bolustime) { DetailedBolusInfo found = null; for (int i = 0; i < store.size(); i++) { - long infoTime = store.get(0).date; + 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); 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 a2181a3a15..5458337782 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 @@ -21,7 +21,6 @@ import info.nightscout.androidaps.interfaces.InsulinInterface; */ public class ActivityGraph extends GraphView { - Context context; public ActivityGraph(Context context) { @@ -35,6 +34,8 @@ public class ActivityGraph extends GraphView { } public void show(InsulinInterface insulin) { + removeAllSeries(); + mSecondScale = null; double dia = insulin.getDia(); int hours = (int) Math.floor(dia + 1); 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..5db351fea5 --- /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) + " " + new Double(insulinPlugin.getDia()).toString() + "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..ef11435805 --- /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) + " " + new Double(insulinPlugin.getDia()).toString() + "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..f09ba4e01d --- /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) + " " + new Double(insulinPlugin.getDia()).toString() + "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/OpenAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java index 6e1070eac3..66696ac9b8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java @@ -208,7 +208,7 @@ public class DetermineBasalAdapterAMAJS { mProfile = new V8Object(mV8rt); mProfile.add("max_iob", maxIob); - mProfile.add("dia", profile.getDia()); + mProfile.add("dia", Math.min(profile.getDia(), 3d)); mProfile.add("type", "current"); mProfile.add("max_daily_basal", profile.getMaxDailyBasal()); mProfile.add("max_basal", maxBasal); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java index 7df122fc92..dc4d2ee70d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java @@ -231,7 +231,7 @@ public class DetermineBasalAdapterMAJS { String units = profile.getUnits(); mProfile.add("max_iob", maxIob); - mProfile.add("dia", profile.getDia()); + mProfile.add("dia", Math.min(profile.getDia(), 3d)); mProfile.add("type", "current"); mProfile.add("max_daily_basal", profile.getMaxDailyBasal()); mProfile.add("max_basal", maxBasal); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java index 9f35599b36..13b85e08fc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java @@ -47,6 +47,9 @@ public class Notification { public static final int NSANNOUNCEMENT = 18; public static final int NSALARM = 19; public static final int NSURGENTALARM = 20; + public static final int SHORT_DIA = 21; + public static final int TOAST_ALARM = 22; + public int id; public Date date; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index ff6218786e..342f6bd20f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -952,19 +952,11 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, tempTargetView.setTextColor(Color.BLACK); tempTargetView.setBackgroundColor(MainApp.sResources.getColor(R.color.tempTargetBackground)); tempTargetView.setVisibility(View.VISIBLE); - if (tempTarget.low == tempTarget.high) - tempTargetView.setText(Profile.toUnitsString(tempTarget.low, Profile.fromMgdlToUnits(tempTarget.low, units), units)); - else - tempTargetView.setText(Profile.toUnitsString(tempTarget.low, Profile.fromMgdlToUnits(tempTarget.low, units), units) + " - " + Profile.toUnitsString(tempTarget.high, Profile.fromMgdlToUnits(tempTarget.high, units), units)); + tempTargetView.setText(Profile.toTargetRangeString(tempTarget.low, tempTarget.high, units)); } else { tempTargetView.setTextColor(Color.WHITE); tempTargetView.setBackgroundColor(MainApp.sResources.getColor(R.color.tempTargetDisabledBackground)); - double low = MainApp.getConfigBuilder().getProfile().getTargetLow(); - double high = MainApp.getConfigBuilder().getProfile().getTargetHigh(); - if (low == high) - tempTargetView.setText("" + low); - else - tempTargetView.setText(low + " - " + high); + tempTargetView.setText(Profile.toTargetRangeString(profile.getTargetLow(), profile.getTargetHigh(), units)); tempTargetView.setVisibility(View.VISIBLE); } if (Config.NSCLIENT && tempTarget == null) { @@ -1605,7 +1597,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, List treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory(); for (int tx = 0; tx < treatments.size(); tx++) { - DataPointWithLabelInterface t = treatments.get(tx); + Treatment t = treatments.get(tx); + if (!t.isValid) + continue; if (t.getX() < fromTime || t.getX() > endTime) continue; t.setY(getNearestBg((long) t.getX(), bgReadingsArray)); filteredTreatments.add(t); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java index c42f1d0c64..c7d7333aba 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java @@ -500,9 +500,13 @@ public class CircadianPercentageProfileFragment extends SubscriberFragment { activity.runOnUiThread(new Runnable() { @Override public void run() { - if (!MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended() || !MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + if (!MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended()) { profileswitchButton.setVisibility(View.GONE); + } else if (!MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + profileswitchButton.setText(MainApp.instance().getText(R.string.activate_profile)); + profileswitchButton.setVisibility(View.VISIBLE); } else { + profileswitchButton.setText(MainApp.instance().getText(R.string.send_to_pump)); profileswitchButton.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java index 7161a1d9de..e491a41b1b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java @@ -148,9 +148,13 @@ public class LocalProfileFragment extends SubscriberFragment { activity.runOnUiThread(new Runnable() { @Override public void run() { - if (!MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended() || !MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + if (!MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended()) { profileswitchButton.setVisibility(View.GONE); + } else if (!MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + profileswitchButton.setText(MainApp.instance().getText(R.string.activate_profile)); + profileswitchButton.setVisibility(View.VISIBLE); } else { + profileswitchButton.setText(MainApp.instance().getText(R.string.send_to_pump)); profileswitchButton.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfileFragment.java index c8b5b2c5db..007c3dfbdf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfileFragment.java @@ -24,14 +24,14 @@ public class NSProfileFragment extends SubscriberFragment { return nsProfilePlugin; } - private static TextView noProfile; - private static TextView units; - private static TextView dia; - private static TextView activeProfile; - private static TextView ic; - private static TextView isf; - private static TextView basal; - private static TextView target; + private TextView noProfile; + private TextView units; + private TextView dia; + private TextView activeProfile; + private TextView ic; + private TextView isf; + private TextView basal; + private TextView target; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java index a4e3a8eecf..5cf36409b6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java @@ -156,9 +156,13 @@ public class SimpleProfileFragment extends SubscriberFragment { activity.runOnUiThread(new Runnable() { @Override public void run() { - if (!MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended() || !MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + if (!MainApp.getConfigBuilder().isInitialized() || MainApp.getConfigBuilder().isSuspended()) { profileswitchButton.setVisibility(View.GONE); + } else if (!MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + profileswitchButton.setText(MainApp.instance().getText(R.string.activate_profile)); + profileswitchButton.setVisibility(View.VISIBLE); } else { + profileswitchButton.setText(MainApp.instance().getText(R.string.send_to_pump)); profileswitchButton.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/Dialogs/ProfileViewDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/Dialogs/ProfileViewDialog.java index 4015b8856b..a20934fdb7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/Dialogs/ProfileViewDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/Dialogs/ProfileViewDialog.java @@ -33,16 +33,16 @@ import info.nightscout.utils.DecimalFormatter; public class ProfileViewDialog extends DialogFragment { private static Logger log = LoggerFactory.getLogger(ProfileViewDialog.class); - private static TextView noProfile; - private static TextView units; - private static TextView dia; - private static TextView activeProfile; - private static TextView ic; - private static TextView isf; - private static TextView basal; - private static TextView target; + private TextView noProfile; + private TextView units; + private TextView dia; + private TextView activeProfile; + private TextView ic; + private TextView isf; + private TextView basal; + private TextView target; - private static Button refreshButton; + private Button refreshButton; Handler mHandler; static HandlerThread mHandlerThread; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetTime.java index aa15a02709..3910d52c97 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetTime.java @@ -13,9 +13,11 @@ import info.nightscout.androidaps.Config; public class MsgSetTime extends MessageBase { private static Logger log = LoggerFactory.getLogger(MsgSetTime.class); + private static Date time; public MsgSetTime(Date time) { SetCommand(0x330a); + this.time = time; AddParamDateTime(time); } @@ -23,6 +25,6 @@ public class MsgSetTime extends MessageBase { int result = intFromBuff(bytes, 0, 1); if (Config.logDanaMessageDetail) - log.debug("Result: " + result); + log.debug("Result of setting time: " + time + " is " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSettingPumpTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSettingPumpTime.java index 23f0e4560f..3da1451899 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSettingPumpTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSettingPumpTime.java @@ -27,7 +27,7 @@ public class MsgSettingPumpTime extends MessageBase { ); if (Config.logDanaMessageDetail) - log.debug("Pump time: " + time); + log.debug("Pump time: " + time + " Phone time: " + new Date()); DanaRPump.getInstance().pumpTime = time; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java index e4e32e39c3..a1d8ecff87 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java @@ -326,11 +326,18 @@ public class DanaRExecutionService extends Service { //0x3201 mSerialIOThread.sendMessage(new MsgSettingMaxValues()); mSerialIOThread.sendMessage(new MsgSettingGlucose()); - mSerialIOThread.sendMessage(new MsgSettingPumpTime()); mSerialIOThread.sendMessage(new MsgSettingActiveProfile()); mSerialIOThread.sendMessage(new MsgSettingProfileRatios()); mSerialIOThread.sendMessage(new MsgSettingProfileRatiosAll()); - mSerialIOThread.sendMessage(new MsgSetTime(new Date())); + mSerialIOThread.sendMessage(new MsgSettingPumpTime()); + long timeDiff = (danaRPump.pumpTime.getTime() - System.currentTimeMillis()) / 1000L; + log.debug("Pump time difference: " + timeDiff + " seconds"); + if (Math.abs(timeDiff) > 10) { + mSerialIOThread.sendMessage(new MsgSetTime(new Date())); + mSerialIOThread.sendMessage(new MsgSettingPumpTime()); + timeDiff = (danaRPump.pumpTime.getTime() - System.currentTimeMillis()) / 1000L; + log.debug("Pump time difference: " + timeDiff + " seconds"); + } danaRPump.lastSettingsRead = now; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/services/DanaRKoreanExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/services/DanaRKoreanExecutionService.java index d803c17bda..f7ad5976b5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/services/DanaRKoreanExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/services/DanaRKoreanExecutionService.java @@ -322,9 +322,15 @@ public class DanaRKoreanExecutionService extends Service { //0x3201 mSerialIOThread.sendMessage(new MsgSettingMaxValues()); mSerialIOThread.sendMessage(new MsgSettingGlucose()); - mSerialIOThread.sendMessage(new MsgSettingPumpTime()); mSerialIOThread.sendMessage(new MsgSettingProfileRatios()); - mSerialIOThread.sendMessage(new MsgSetTime(new Date())); + long timeDiff = (danaRPump.pumpTime.getTime() - System.currentTimeMillis()) / 1000L; + log.debug("Pump time difference: " + timeDiff + " seconds"); + if (Math.abs(timeDiff) > 10) { + mSerialIOThread.sendMessage(new MsgSetTime(new Date())); + mSerialIOThread.sendMessage(new MsgSettingPumpTime()); + timeDiff = (danaRPump.pumpTime.getTime() - System.currentTimeMillis()) / 1000L; + log.debug("Pump time difference: " + timeDiff + " seconds"); + } danaRPump.lastSettingsRead = now; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java index 5c1b20a9ea..416a96c269 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java @@ -99,14 +99,14 @@ public class MsgHistoryEvents_v2 extends MessageBase { MainApp.getConfigBuilder().addToHistoryExtendedBolus(extendedBolus); break; case DanaRPump.BOLUS: - log.debug("EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); detailedBolusInfo.insulin = param1 / 100d; - MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + boolean newRecord = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + log.debug((newRecord ? "**NEW** " : "") + "EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); break; case DanaRPump.DUALBOLUS: - log.debug("EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); detailedBolusInfo.insulin = param1 / 100d; - MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + newRecord = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + log.debug((newRecord ? "**NEW** " : "") + "EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); break; case DanaRPump.DUALEXTENDEDSTART: log.debug("EVENT DUALEXTENDEDSTART (" + recordCode + ") " + datetime.toLocaleString() + " Amount: " + (param1 / 100d) + "U Duration: " + param2 + "min"); @@ -134,9 +134,9 @@ public class MsgHistoryEvents_v2 extends MessageBase { log.debug("EVENT PROFILECHANGE (" + recordCode + ") " + datetime.toLocaleString() + " No: " + param1 + " CurrentRate: " + (param2 / 100d) + "U/h"); break; case DanaRPump.CARBS: - log.debug("EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g"); detailedBolusInfo.carbs = param1; - MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + newRecord = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + log.debug((newRecord ? "**NEW** " : "") + "EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g"); break; default: log.debug("Event: " + recordCode + " " + datetime.toLocaleString() + " Param1: " + param1 + " Param2: " + param2); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java index 6527eeebaf..98d869e09c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java @@ -297,11 +297,18 @@ public class DanaRv2ExecutionService extends Service { //0x3201 mSerialIOThread.sendMessage(new MsgSettingMaxValues()); mSerialIOThread.sendMessage(new MsgSettingGlucose()); - mSerialIOThread.sendMessage(new MsgSettingPumpTime()); mSerialIOThread.sendMessage(new MsgSettingActiveProfile()); mSerialIOThread.sendMessage(new MsgSettingProfileRatios()); mSerialIOThread.sendMessage(new MsgSettingProfileRatiosAll()); - mSerialIOThread.sendMessage(new MsgSetTime(new Date())); + mSerialIOThread.sendMessage(new MsgSettingPumpTime()); + long timeDiff = (danaRPump.pumpTime.getTime() - System.currentTimeMillis()) / 1000L; + log.debug("Pump time difference: " + timeDiff + " seconds"); + if (Math.abs(timeDiff) > 10) { + mSerialIOThread.sendMessage(new MsgSetTime(new Date())); + mSerialIOThread.sendMessage(new MsgSettingPumpTime()); + timeDiff = (danaRPump.pumpTime.getTime() - System.currentTimeMillis()) / 1000L; + log.debug("Pump time difference: " + timeDiff + " seconds"); + } danaRPump.lastSettingsRead = now; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 2153fba0df..3050d431bc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -179,6 +179,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { for (Integer pos = 0; pos < treatments.size(); pos++) { Treatment t = treatments.get(pos); + if (!t.isValid) continue; if (t.date > time) continue; Iob tIOB = t.iobCalc(time, dia); total.iob += tIOB.iobContrib; @@ -222,6 +223,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { long dia_ago = now - (new Double(1.5d * profile.getDia() * 60 * 60 * 1000l)).longValue(); for (Treatment treatment : treatments) { + if (!treatment.isValid) + continue; long t = treatment.date; if (t > dia_ago && t <= now) { if (treatment.carbs >= 1) { @@ -250,6 +253,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { List in5minback = new ArrayList<>(); for (Integer pos = 0; pos < treatments.size(); pos++) { Treatment t = treatments.get(pos); + if (!t.isValid) + continue; if (t.date <= time && t.date > time - 5 * 60 * 1000 && t.carbs > 0) in5minback.add(t); } @@ -411,6 +416,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { treatment.source = detailedBolusInfo.source; treatment.pumpId = detailedBolusInfo.pumpId; treatment.insulin = detailedBolusInfo.insulin; + treatment.isValid = detailedBolusInfo.isValid; + treatment.isSMB = detailedBolusInfo.isSMB; if (detailedBolusInfo.carbTime == 0) treatment.carbs = detailedBolusInfo.carbs; treatment.source = detailedBolusInfo.source; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/ProfileViewerDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/ProfileViewerDialog.java new file mode 100644 index 0000000000..b4461eabb7 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/ProfileViewerDialog.java @@ -0,0 +1,108 @@ +package info.nightscout.androidaps.plugins.Treatments.fragments; + +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.db.ProfileSwitch; +import info.nightscout.androidaps.plugins.PumpDanaR.Dialogs.ProfileViewDialog; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.DecimalFormatter; + +/** + * Created by adrian on 17/08/17. + */ + +public class ProfileViewerDialog extends DialogFragment { + + private long time; + + private static Logger log = LoggerFactory.getLogger(ProfileViewDialog.class); + + private TextView noProfile; + private TextView units; + private TextView dia; + private TextView activeProfile; + private TextView ic; + private TextView isf; + private TextView basal; + private TextView target; + private View dateDelimiter; + private LinearLayout dateLayout; + private TextView dateTextView; + private Button refreshButton; + + static ProfileViewerDialog newInstance(long time) { + ProfileViewerDialog dialog = new ProfileViewerDialog(); + + Bundle args = new Bundle(); + args.putLong("time", time); + dialog.setArguments(args); + + return dialog; + } + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + time = getArguments().getLong("time"); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View layout = inflater.inflate(R.layout.nsprofileviewer_fragment, container, false); + + noProfile = (TextView) layout.findViewById(R.id.profileview_noprofile); + units = (TextView) layout.findViewById(R.id.profileview_units); + dia = (TextView) layout.findViewById(R.id.profileview_dia); + activeProfile = (TextView) layout.findViewById(R.id.profileview_activeprofile); + ic = (TextView) layout.findViewById(R.id.profileview_ic); + isf = (TextView) layout.findViewById(R.id.profileview_isf); + basal = (TextView) layout.findViewById(R.id.profileview_basal); + target = (TextView) layout.findViewById(R.id.profileview_target); + refreshButton = (Button) layout.findViewById(R.id.profileview_reload); + refreshButton.setVisibility(View.GONE); + dateDelimiter = layout.findViewById(R.id.profileview_datedelimiter); + dateDelimiter.setVisibility(View.VISIBLE); + dateLayout = (LinearLayout) layout.findViewById(R.id.profileview_datelayout); + dateLayout.setVisibility(View.VISIBLE); + dateTextView = (TextView) layout.findViewById(R.id.profileview_date); + + setContent(); + return layout; + } + + private void setContent() { + Profile profile = null; + ProfileSwitch profileSwitch = MainApp.getConfigBuilder().getProfileSwitchFromHistory(time); + if(profileSwitch!=null && profileSwitch.profileJson != null){ + profile = profileSwitch.getProfileObject(); + } + if (profile != null) { + noProfile.setVisibility(View.GONE); + units.setText(profile.getUnits()); + dia.setText(DecimalFormatter.to2Decimal(profile.getDia()) + " h"); + activeProfile.setText(profileSwitch.profileName); + dateTextView.setText(DateUtil.dateAndTimeString(profileSwitch.date)); + ic.setText(profile.getIcList()); + isf.setText(profile.getIsfList()); + basal.setText(profile.getBasalList()); + target.setText(profile.getTargetList()); + } else { + noProfile.setVisibility(View.VISIBLE); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java index 0e7d94cd88..b47cd3a6bf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java @@ -85,6 +85,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. holder.mealOrCorrection.setText(t.mealBolus ? MainApp.sResources.getString(R.string.mealbolus) : MainApp.sResources.getString(R.string.correctionbous)); holder.ph.setVisibility(t.source == Source.PUMP ? View.VISIBLE : View.GONE); holder.ns.setVisibility(t._id != null ? View.VISIBLE : View.GONE); + holder.invalid.setVisibility(t.isValid ? View.GONE : View.VISIBLE); if (iob.iobContrib != 0) holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); else @@ -113,6 +114,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. TextView remove; TextView ph; TextView ns; + TextView invalid; TreatmentsViewHolder(View itemView) { super(itemView); @@ -125,6 +127,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. mealOrCorrection = (TextView) itemView.findViewById(R.id.treatments_mealorcorrection); ph = (TextView) itemView.findViewById(R.id.pump_sign); ns = (TextView) itemView.findViewById(R.id.ns_sign); + invalid = (TextView) itemView.findViewById(R.id.invalid_sign); remove = (TextView) itemView.findViewById(R.id.treatments_remove); remove.setOnClickListener(this); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); @@ -141,10 +144,15 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { final String _id = treatment._id; - if (_id != null && !_id.equals("")) { - NSUpload.removeCareportalEntryFromNS(_id); + if (treatment.source == Source.PUMP) { + treatment.isValid = false; + MainApp.getDbHelper().update(treatment); + } else { + if (_id != null && !_id.equals("")) { + NSUpload.removeCareportalEntryFromNS(_id); + } + MainApp.getDbHelper().delete(treatment); } - MainApp.getDbHelper().delete(treatment); updateGUI(); Answers.getInstance().logCustom(new CustomEvent("RemoveTreatment")); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsProfileSwitchFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsProfileSwitchFragment.java index e5c1134787..fc9d836b8b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsProfileSwitchFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsProfileSwitchFragment.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.graphics.Paint; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; @@ -84,6 +85,9 @@ public class TreatmentsProfileSwitchFragment extends SubscriberFragment implemen else holder.date.setTextColor(holder.duration.getCurrentTextColor()); holder.remove.setTag(profileSwitch); + holder.name.setTag(profileSwitch); + holder.date.setTag(profileSwitch); + } @Override @@ -116,6 +120,9 @@ public class TreatmentsProfileSwitchFragment extends SubscriberFragment implemen remove = (TextView) itemView.findViewById(R.id.profileswitch_remove); remove.setOnClickListener(this); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + name.setOnClickListener(this); + date.setOnClickListener(this); + } @Override @@ -123,9 +130,11 @@ public class TreatmentsProfileSwitchFragment extends SubscriberFragment implemen final ProfileSwitch profileSwitch = (ProfileSwitch) v.getTag(); switch (v.getId()) { case R.id.profileswitch_remove: - OKDialog.show(getActivity(), MainApp.sResources.getString(R.string.confirmation), MainApp.sResources.getString(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(profileSwitch.date), new Runnable() { - @Override - public void run() { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); + builder.setMessage(MainApp.sResources.getString(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(profileSwitch.date)); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { final String _id = profileSwitch._id; if (_id != null && !_id.equals("")) { NSUpload.removeCareportalEntryFromNS(_id); @@ -133,6 +142,15 @@ public class TreatmentsProfileSwitchFragment extends SubscriberFragment implemen MainApp.getDbHelper().delete(profileSwitch); } }); + builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null); + builder.show(); + break; + case R.id.profileswitch_date: + case R.id.profileswitch_name: + long time = ((ProfileSwitch)v.getTag()).date; + ProfileViewerDialog pvd = ProfileViewerDialog.newInstance(time); + FragmentManager manager = getFragmentManager(); + pvd.show(manager, "ProfileViewDialog"); break; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java index 008c82e612..9e3836e118 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java @@ -141,7 +141,6 @@ public class TreatmentsTempTargetFragment extends SubscriberFragment implements @Override public void onClick(View v) { final TempTarget tempTarget = (TempTarget) v.getTag(); - final Context finalContext = context; switch (v.getId()) { case R.id.temptargetrange_remove: AlertDialog.Builder builder = new AlertDialog.Builder(context); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 2db941e1c5..a4bbd34f1c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -472,7 +472,7 @@ public class ActionStringHandler { //Check for Temp-Target: TempTarget tempTarget = MainApp.getConfigBuilder().getTempTargetFromHistory(System.currentTimeMillis()); if (tempTarget != null) { - ret += "Temp Target: " + Profile.toUnitsString(tempTarget.low, Profile.fromMgdlToUnits(tempTarget.low, profile.getUnits()), profile.getUnits()) + " - " + Profile.toUnitsString(tempTarget.high, Profile.fromMgdlToUnits(tempTarget.high, profile.getUnits()), profile.getUnits()); + ret += "Temp Target: " + Profile.toTargetRangeString(tempTarget.low, tempTarget.low, profile.getUnits()); ret += "\nuntil: " + DateUtil.timeString(tempTarget.originalEnd()); ret += "\n\n"; } @@ -615,7 +615,7 @@ public class ActionStringHandler { public void run() { DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); detailedBolusInfo.insulin = amount; - detailedBolusInfo.addToTreatments = false; + detailedBolusInfo.isValid = false; detailedBolusInfo.source = Source.USER; PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(detailedBolusInfo); if (!result.success) { diff --git a/app/src/main/java/info/nightscout/utils/BundleLogger.java b/app/src/main/java/info/nightscout/utils/BundleLogger.java new file mode 100644 index 0000000000..ba6ffff869 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/BundleLogger.java @@ -0,0 +1,21 @@ +package info.nightscout.utils; + +import android.os.Bundle; + +/** + * Created by mike on 14.08.2017. + */ + +public class BundleLogger { + public static String log(Bundle bundle) { + if (bundle == null) { + return null; + } + String string = "Bundle{"; + for (String key : bundle.keySet()) { + string += " " + key + " => " + bundle.get(key) + ";"; + } + string += " }Bundle"; + return string; + } +} diff --git a/app/src/main/java/info/nightscout/utils/DateUtil.java b/app/src/main/java/info/nightscout/utils/DateUtil.java index b058450512..bf2c1ba1fd 100644 --- a/app/src/main/java/info/nightscout/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/utils/DateUtil.java @@ -85,12 +85,16 @@ public class DateUtil { } public static int toSeconds(String hh_colon_mm) { - Pattern p = Pattern.compile("(\\d+):(\\d+)"); + Pattern p = Pattern.compile("(\\d+):(\\d+)( a.m.| p.m.|)"); Matcher m = p.matcher(hh_colon_mm); int retval = 0; if (m.find()) { retval = SafeParse.stringToInt(m.group(1)) * 60 * 60 + SafeParse.stringToInt(m.group(2)) * 60; + if (m.group(3).equals(" .a.m") && m.group(1).equals("12")) + retval -= 12 * 60 * 60; + if (m.group(3).equals(" p.m.") && !m.group(1).equals("12")) + retval += 12 * 60 * 60; } return retval; } diff --git a/app/src/main/java/info/nightscout/utils/ToastUtils.java b/app/src/main/java/info/nightscout/utils/ToastUtils.java index dba4fb4202..4e1e4ac4ed 100644 --- a/app/src/main/java/info/nightscout/utils/ToastUtils.java +++ b/app/src/main/java/info/nightscout/utils/ToastUtils.java @@ -6,6 +6,10 @@ import android.os.Handler; import android.os.Looper; import android.widget.Toast; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.Overview.Notification; +import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; + public class ToastUtils { public static void showToastInUiThread(final Context ctx, @@ -25,6 +29,13 @@ public class ToastUtils { showToastInUiThread(ctx, string); playSound(ctx, soundID); + new Thread(new Runnable() { + @Override + public void run() { + Notification notification = new Notification(Notification.TOAST_ALARM, string, Notification.URGENT); + MainApp.bus().post(new EventNewNotification(notification)); + } + }).start(); } private static void playSound(final Context ctx, final int soundID) { diff --git a/app/src/main/res/layout/nsprofileviewer_fragment.xml b/app/src/main/res/layout/nsprofileviewer_fragment.xml index e2c48fb79f..f0e385e86b 100644 --- a/app/src/main/res/layout/nsprofileviewer_fragment.xml +++ b/app/src/main/res/layout/nsprofileviewer_fragment.xml @@ -35,7 +35,7 @@ android:layout_weight="2" android:gravity="end" android:paddingRight="5dp" - android:text="@string/nsprofileview_activeprofile_label" + android:text="@string/careportal_newnstreatment_profile_label" android:textSize="14sp" /> + + + + + + + + + + + + + android:orientation="horizontal" + android:visibility="gone">