diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java index c87432a9ab..6b9b353979 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.db.ProfileSwitch; +import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.events.EventProfileSwitchChange; import info.nightscout.androidaps.interfaces.ProfileInterface; @@ -139,4 +140,44 @@ public class ProfileFunctions { return null; } + public static ProfileSwitch prepareProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift, long date) { + ProfileSwitch profileSwitch = new ProfileSwitch(); + profileSwitch.date = date; + profileSwitch.source = Source.USER; + profileSwitch.profileName = profileName; + profileSwitch.profileJson = profileStore.getSpecificProfile(profileName).getData().toString(); + profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName(); + profileSwitch.durationInMinutes = duration; + profileSwitch.isCPP = percentage != 100 || timeshift != 0; + profileSwitch.timeshift = timeshift; + profileSwitch.percentage = percentage; + return profileSwitch; + } + + public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift) { + ProfileSwitch profileSwitch = prepareProfileSwitch(profileStore, profileName, duration, percentage, timeshift, System.currentTimeMillis()); + TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch); + FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch")); + } + + public static void doProfileSwitch(final int duration, final int percentage, final int timeshift) { + ProfileSwitch profileSwitch = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(System.currentTimeMillis()); + if (profileSwitch != null) { + profileSwitch = new ProfileSwitch(); + profileSwitch.date = System.currentTimeMillis(); + profileSwitch.source = Source.USER; + profileSwitch.profileName = getInstance().getProfileName(System.currentTimeMillis(), false); + profileSwitch.profileJson = getInstance().getProfile().getData().toString(); + profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName(); + profileSwitch.durationInMinutes = duration; + profileSwitch.isCPP = percentage != 100 || timeshift != 0; + profileSwitch.timeshift = timeshift; + profileSwitch.percentage = percentage; + TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch); + FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch")); + } else { + log.error("No profile switch existing"); + } + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java index 1e38a0131a..e4eccd18d9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java @@ -722,7 +722,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick public void createNSTreatment(JSONObject data) { if (options.executeProfileSwitch) { if (data.has("profile")) { - doProfileSwitch(profileStore, JsonHelper.safeGetString(data, "profile"), JsonHelper.safeGetInt(data, "duration"), JsonHelper.safeGetInt(data, "percentage"), JsonHelper.safeGetInt(data, "timeshift")); + ProfileFunctions.doProfileSwitch(profileStore, JsonHelper.safeGetString(data, "profile"), JsonHelper.safeGetInt(data, "duration"), JsonHelper.safeGetInt(data, "percentage"), JsonHelper.safeGetInt(data, "timeshift")); } } else if (options.executeTempTarget) { final int duration = JsonHelper.safeGetInt(data, "duration"); @@ -746,7 +746,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } } else { if (JsonHelper.safeGetString(data, "eventType").equals(CareportalEvent.PROFILESWITCH)) { - ProfileSwitch profileSwitch = prepareProfileSwitch( + ProfileSwitch profileSwitch = ProfileFunctions.prepareProfileSwitch( profileStore, JsonHelper.safeGetString(data, "profile"), JsonHelper.safeGetInt(data, "duration"), @@ -762,46 +762,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } } - public static ProfileSwitch prepareProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift, long date) { - ProfileSwitch profileSwitch = new ProfileSwitch(); - profileSwitch.date = date; - profileSwitch.source = Source.USER; - profileSwitch.profileName = profileName; - profileSwitch.profileJson = profileStore.getSpecificProfile(profileName).getData().toString(); - profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName(); - profileSwitch.durationInMinutes = duration; - profileSwitch.isCPP = percentage != 100 || timeshift != 0; - profileSwitch.timeshift = timeshift; - profileSwitch.percentage = percentage; - return profileSwitch; - } - - public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift) { - ProfileSwitch profileSwitch = prepareProfileSwitch(profileStore, profileName, duration, percentage, timeshift, System.currentTimeMillis()); - TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch); - FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch")); - } - - public static void doProfileSwitch(final int duration, final int percentage, final int timeshift) { - ProfileSwitch profileSwitch = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(System.currentTimeMillis()); - if (profileSwitch != null) { - profileSwitch = new ProfileSwitch(); - profileSwitch.date = System.currentTimeMillis(); - profileSwitch.source = Source.USER; - profileSwitch.profileName = ProfileFunctions.getInstance().getProfileName(System.currentTimeMillis(), false); - profileSwitch.profileJson = ProfileFunctions.getInstance().getProfile().getData().toString(); - profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName(); - profileSwitch.durationInMinutes = duration; - profileSwitch.isCPP = percentage != 100 || timeshift != 0; - profileSwitch.timeshift = timeshift; - profileSwitch.percentage = percentage; - TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch); - FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch")); - } else { - log.error("No profile switch existing"); - } - } - @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putString("notesEdit", notesEdit.getText().toString()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.java index d6ea031fa7..6b5d5b8747 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.java @@ -4,6 +4,7 @@ abstract class SmsAction implements Runnable { Double aDouble; Integer anInteger; Integer secondInteger; + String aString; SmsAction() {} @@ -16,6 +17,11 @@ abstract class SmsAction implements Runnable { this.secondInteger = secondInteger; } + SmsAction(String aString, Integer secondInteger) { + this.aString = aString; + this.secondInteger = secondInteger; + } + SmsAction(Integer anInteger) { this.anInteger = anInteger; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java index 9c4063c38d..d58e2aaacf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java @@ -25,6 +25,7 @@ import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.Source; @@ -34,6 +35,7 @@ import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; @@ -184,6 +186,14 @@ public class SmsCommunicatorPlugin extends PluginBase { case "PUMP": processPUMP(splitted, receivedSms); break; + case "PROFILE": + if (!remoteCommandsAllowed) + sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); + else if (splitted.length == 2 || splitted.length == 3) + processPROFILE(splitted, receivedSms); + else + sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); + break; case "BASAL": if (!remoteCommandsAllowed) sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); @@ -409,6 +419,72 @@ public class SmsCommunicatorPlugin extends PluginBase { receivedSms.processed = true; } + private void processPROFILE(String[] splitted, Sms receivedSms) { + // load profiles + ProfileInterface anInterface = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface(); + if (anInterface == null) { + sendSMS(new Sms(receivedSms.phoneNumber, R.string.notconfigured)); + receivedSms.processed = true; + return; + } + ProfileStore store = anInterface.getProfile(); + if (store == null) { + sendSMS(new Sms(receivedSms.phoneNumber, R.string.notconfigured)); + receivedSms.processed = true; + return; + } + final ArrayList list = store.getProfileList(); + + if (splitted[1].toUpperCase().equals("STATUS")) { + sendSMS(new Sms(receivedSms.phoneNumber, ProfileFunctions.getInstance().getProfileName())); + } else if (splitted[1].toUpperCase().equals("LIST")) { + if (list.isEmpty()) + sendSMS(new Sms(receivedSms.phoneNumber, R.string.invalidprofile)); + else { + String reply = ""; + for (int i = 0; i < list.size(); i++) { + if (i > 0) + reply += "\n"; + reply += (i + 1) + ". "; + reply += list.get(i); + } + sendSMS(new Sms(receivedSms.phoneNumber, reply)); + } + } else { + + int pindex = SafeParse.stringToInt(splitted[1]); + int percentage = 100; + if (splitted.length > 2) + percentage = SafeParse.stringToInt(splitted[2]); + + if (pindex > list.size()) + sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); + else if (percentage == 0) + sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); + else if (pindex == 0) + sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); + else { + final Profile profile = store.getSpecificProfile((String) list.get(pindex - 1)); + if (profile == null) + sendSMS(new Sms(receivedSms.phoneNumber, R.string.noprofile)); + else { + String passCode = generatePasscode(); + String reply = String.format(MainApp.gs(R.string.smscommunicator_profilereplywithcode), list.get(pindex - 1), percentage, passCode); + receivedSms.processed = true; + int finalPercentage = percentage; + messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction((String) list.get(pindex - 1), finalPercentage) { + @Override + public void run() { + ProfileFunctions.doProfileSwitch(store, (String) list.get(pindex - 1), 0, finalPercentage, 0); + sendSMS(new Sms(receivedSms.phoneNumber, R.string.profileswitchcreated)); + } + }); + } + } + } + receivedSms.processed = true; + } + private void processBASAL(String[] splitted, Sms receivedSms) { if (splitted[1].toUpperCase().equals("CANCEL") || splitted[1].toUpperCase().equals("STOP")) { String passCode = generatePasscode(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java index aef742b49b..b92410d67f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java @@ -693,7 +693,7 @@ public class ActionStringHandler { //send profile to pumpe new NewNSTreatmentDialog(); //init - NewNSTreatmentDialog.doProfileSwitch(0, percentage, timeshift); + ProfileFunctions.doProfileSwitch(0, percentage, timeshift); } private static void generateTempTarget(int duration, double low, double high) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.java index 913f6a1d12..66b895839d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.java @@ -22,7 +22,6 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.ProfileStore; -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.common.SubscriberFragment; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI; @@ -157,7 +156,7 @@ public class NSProfileFragment extends SubscriberFragment { Profile profile = store.getSpecificProfile(name); if (profile != null) { OKDialog.showConfirmation(getActivity(), MainApp.gs(R.string.activate_profile) + ": " + name + " ?", () -> - NewNSTreatmentDialog.doProfileSwitch(store, name, 0, 100, 0) + ProfileFunctions.doProfileSwitch(store, name, 0, 100, 0) ); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7576c277be..0ddc68865a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -368,6 +368,7 @@ Remote command is not allowed Remote bolus not available. Try again later. To start basal %1$.2fU/h reply with code %2$s + To switch profile to %1$s %2$d%% reply with code %3$s To start extended bolus %1$.2fU for %2$d min reply with code %3$s To start basal %1$d%% reply with code %2$s To suspend loop for %1$d minutes reply with code %2$s @@ -1322,6 +1323,8 @@ Free at least %1$d MB from internal storage! Loop disabled! Wrong format Wrong code. Command cancelled. + Not configured + Profile switch created %1$d day %1$d days