From 80a463a56d4462ca7ce5905011f25b8229fcb4df Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 11 Dec 2016 10:52:50 +0100 Subject: [PATCH 1/8] parse allowed phone numbers --- .../SmsCommunicatorPlugin.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 365027c012..50ea260884 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -16,12 +16,14 @@ import java.text.Normalizer; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.StringTokenizer; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; @@ -41,6 +43,8 @@ public class SmsCommunicatorPlugin implements PluginBase { final long CONFIRM_TIMEOUT = 5 * 60 * 1000L; + List allowedNumbers = new ArrayList(); + public class Sms { String phoneNumber; String text; @@ -81,6 +85,7 @@ public class SmsCommunicatorPlugin implements PluginBase { public SmsCommunicatorPlugin() { MainApp.bus().register(this); + processSettings(null); } @Override @@ -123,6 +128,29 @@ public class SmsCommunicatorPlugin implements PluginBase { SmsCommunicatorPlugin.fragmentVisible = fragmentVisible; } + @Subscribe + public void processSettings(final EventPreferenceChange ev) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + String settings = sharedPreferences.getString("smscommunicator_allowednumbers", ""); + + String pattern = ";"; + + String[] substrings = settings.split(pattern); + for (String number: substrings) + { + String cleaned = number.replaceAll("\\s+",""); + allowedNumbers.add(cleaned); + log.debug("Found allowed number: " + cleaned); + } + } + + boolean isAllowedNumber(String number) { + for (String num: allowedNumbers) { + if (num.equals(number)) return true; + } + return false; + } + @Subscribe public void onStatusEvent(final EventNewSMS ev) { @@ -135,14 +163,13 @@ public class SmsCommunicatorPlugin implements PluginBase { } private void processSms(Sms receivedSms) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + if (!isEnabled(PluginBase.GENERAL)) { log.debug("Ignoring SMS. Plugin disabled."); return; } - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - String allowedNumbers = sharedPreferences.getString("smscommunicator_allowednumbers", ""); - - if (!allowedNumbers.contains(receivedSms.phoneNumber)) { + if (!isAllowedNumber(receivedSms.phoneNumber)) { log.debug("Ignoring SMS from: " + receivedSms.phoneNumber + ". Sender not allowed"); return; } From 02b3da97e05e3d2c12fdfdf94758fcde6557a200 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 25 Dec 2016 11:29:13 +0100 Subject: [PATCH 2/8] send important confirmations to all phone numbers --- .../SmsCommunicatorPlugin.java | 80 +++++++++++-------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 50ea260884..11dfa750a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -71,6 +71,14 @@ public class SmsCommunicatorPlugin implements PluginBase { sent = true; } + public Sms(String phoneNumber, String text, Date date, String confirmCode) { + this.phoneNumber = phoneNumber; + this.text = text; + this.date = date; + this.confirmCode = confirmCode; + sent = true; + } + public String toString() { return "SMS from " + phoneNumber + ": " + text; } @@ -183,7 +191,6 @@ public class SmsCommunicatorPlugin implements PluginBase { Double amount = 0d; Double tempBasal = 0d; String passCode = ""; - Sms newSms = null; if (splited.length > 0) { switch (splited[0].toUpperCase()) { @@ -194,7 +201,7 @@ public class SmsCommunicatorPlugin implements PluginBase { if (loopPlugin != null && loopPlugin.isEnabled(PluginBase.LOOP)) { loopPlugin.setFragmentEnabled(PluginBase.LOOP, false); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeendisabled); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } receivedSms.processed = true; break; @@ -203,7 +210,7 @@ public class SmsCommunicatorPlugin implements PluginBase { if (loopPlugin != null && !loopPlugin.isEnabled(PluginBase.LOOP)) { loopPlugin.setFragmentEnabled(PluginBase.LOOP, true); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeenenabled); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } receivedSms.processed = true; break; @@ -215,7 +222,7 @@ public class SmsCommunicatorPlugin implements PluginBase { } else { reply = MainApp.sResources.getString(R.string.smscommunicator_loopisdisabled); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } receivedSms.processed = true; break; @@ -229,7 +236,7 @@ public class SmsCommunicatorPlugin implements PluginBase { MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); reply = "TERATMENTS REFRESH " + q.size() + " receivers"; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); receivedSms.processed = true; break; } @@ -241,7 +248,7 @@ public class SmsCommunicatorPlugin implements PluginBase { MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); reply = "NSCLIENT RESTART " + q.size() + " receivers"; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); receivedSms.processed = true; break; } @@ -250,7 +257,7 @@ public class SmsCommunicatorPlugin implements PluginBase { DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); if (danaRPlugin != null && danaRPlugin.isEnabled(PluginBase.PUMP)) reply = danaRPlugin.shortStatus(); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); receivedSms.processed = true; break; case "BASAL": @@ -261,13 +268,11 @@ public class SmsCommunicatorPlugin implements PluginBase { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalstopreplywithcode), passCode); receivedSms.processed = true; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); - newSms.confirmCode = passCode; resetWaitingMessages(); - cancelTempBasalWaitingForConfirmation = newSms; + sendSMS(cancelTempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); } else { reply = MainApp.sResources.getString(R.string.remotebasalnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } } else { tempBasal = SafeParse.stringToDouble(splited[1]); @@ -276,14 +281,12 @@ public class SmsCommunicatorPlugin implements PluginBase { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); receivedSms.processed = true; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); - newSms.tempBasal = tempBasal; - newSms.confirmCode = passCode; resetWaitingMessages(); - tempBasalWaitingForConfirmation = newSms; - } else { + sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); + tempBasalWaitingForConfirmation.tempBasal = tempBasal; + } else { reply = MainApp.sResources.getString(R.string.remotebasalnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } } } @@ -291,7 +294,7 @@ public class SmsCommunicatorPlugin implements PluginBase { case "BOLUS": if (new Date().getTime() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) { reply = MainApp.sResources.getString(R.string.remotebolusnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } else if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); @@ -300,14 +303,12 @@ public class SmsCommunicatorPlugin implements PluginBase { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode); receivedSms.processed = true; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); - newSms.bolusRequested = amount; - newSms.confirmCode = passCode; resetWaitingMessages(); - bolusWaitingForConfirmation = newSms; + sendSMS(bolusWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); + bolusWaitingForConfirmation.bolusRequested = amount; } else { reply = MainApp.sResources.getString(R.string.remotebolusnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } } break; @@ -323,11 +324,12 @@ public class SmsCommunicatorPlugin implements PluginBase { reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), result.bolusDelivered); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); lastRemoteBolusTime = new Date(); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); } else { reply = MainApp.sResources.getString(R.string.bolusfailed); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); } } else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed && tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - tempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { @@ -339,11 +341,12 @@ public class SmsCommunicatorPlugin implements PluginBase { if (result.success) { reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); } else { reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalfailed); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); } } else if (cancelTempBasalWaitingForConfirmation != null && !cancelTempBasalWaitingForConfirmation.processed && cancelTempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - cancelTempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { @@ -355,30 +358,39 @@ public class SmsCommunicatorPlugin implements PluginBase { if (result.success) { reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalcanceled)); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); } else { reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalcancelfailed); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); } } else { - newSms = new Sms(receivedSms.phoneNumber, MainApp.sResources.getString(R.string.smscommunicator_unknowncommand), new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, MainApp.sResources.getString(R.string.smscommunicator_unknowncommand), new Date())); } resetWaitingMessages(); break; } } - if (newSms != null) { - SmsManager smsManager = SmsManager.getDefault(); - newSms.text = stripAccents(newSms.text); - if (newSms.text.length() > 140) newSms.text = newSms.text.substring(0, 139); - smsManager.sendTextMessage(newSms.phoneNumber, null, newSms.text, null, null); - messages.add(newSms); - } MainApp.bus().post(new EventSmsCommunicatorUpdateGui()); } + public void sendSMSToAllNumbers(Sms sms) { + for (int i = 0; i < allowedNumbers.size(); i++) { + sms.phoneNumber = allowedNumbers.get(i); + sendSMS(sms); + } + } + + public void sendSMS(Sms sms) { + SmsManager smsManager = SmsManager.getDefault(); + sms.text = stripAccents(sms.text); + if (sms.text.length() > 140) sms.text = sms.text.substring(0, 139); + smsManager.sendTextMessage(sms.phoneNumber, null, sms.text, null, null); + messages.add(sms); + } + private String generatePasscode() { int startChar1 = 'A'; // on iphone 1st char is uppercase :) String passCode = Character.toString((char) (startChar1 + Math.random() * ('z' - 'a' + 1))); From 381de0a5f5c3f227aac61ea5b4ba45825f8379b1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 25 Dec 2016 11:51:46 +0100 Subject: [PATCH 3/8] function to send notification --- .../plugins/SmsCommunicator/SmsCommunicatorPlugin.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 11dfa750a4..83fe6a8d12 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -376,6 +376,13 @@ public class SmsCommunicatorPlugin implements PluginBase { MainApp.bus().post(new EventSmsCommunicatorUpdateGui()); } + public void sendNotificationToAllNumbers(String text) { + for (int i = 0; i < allowedNumbers.size(); i++) { + Sms sms = new Sms(allowedNumbers.get(i), text, new Date()); + sendSMS(sms); + } + } + public void sendSMSToAllNumbers(Sms sms) { for (int i = 0; i < allowedNumbers.size(); i++) { sms.phoneNumber = allowedNumbers.get(i); From 61720a74ec383dcbba44e5e8caffc197ddf4c29c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 25 Dec 2016 13:34:48 +0100 Subject: [PATCH 4/8] do not return error if not initialized --- .../info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java | 2 +- .../androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java index 888c320c6e..e80c0110db 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java @@ -243,7 +243,7 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf @Override public boolean isThisProfileSet(NSProfile profile) { if (!isInitialized()) - return false; + return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS DanaRPump pump = getDanaRPump(); int basalValues = pump.basal48Enable ? 48 : 24; int basalIncrement = pump.basal48Enable ? 30 * 60 : 60 * 60; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java index 391b0fd4d5..42dd2e59f9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java @@ -243,7 +243,7 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints @Override public boolean isThisProfileSet(NSProfile profile) { if (!isInitialized()) - return false; + return true; // TODO: not sure what's better. so far TRUE to prevent too many SMS DanaRKoreanPump pump = getDanaRPump(); int basalValues = pump.basal48Enable ? 48 : 24; int basalIncrement = pump.basal48Enable ? 30 * 60 : 60 * 60; From 46be73d187b3aed2ac7093a61819e164d8ed5a25 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 25 Dec 2016 14:26:41 +0100 Subject: [PATCH 5/8] send sms notification when profile received from NS --- .../androidaps/Services/DataService.java | 21 ++++++++++++++----- .../androidaps/interfaces/PumpInterface.java | 5 ++++- .../ConfigBuilder/ConfigBuilderPlugin.java | 8 ++++--- .../androidaps/plugins/DanaR/DanaRPlugin.java | 8 ++++--- .../DanaRKorean/DanaRKoreanPlugin.java | 8 ++++--- .../VirtualPump/VirtualPumpPlugin.java | 3 ++- 6 files changed, 37 insertions(+), 16 deletions(-) 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 4c7f237feb..a2cadd544c 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -33,12 +33,14 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.events.EventTreatmentChange; +import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.History.DanaRNSHistorySync; import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin; import info.nightscout.androidaps.plugins.Objectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; +import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorPlugin; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS; import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; @@ -91,7 +93,7 @@ public class DataService extends IntentService { // Objectives 0 ObjectivesPlugin.bgIsAvailableInNS = true; ObjectivesPlugin.saveProgress(); - } else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action)){ + } else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action)) { // always handle Profile if NSProfile is enabled without looking at nsUploadOnly handleNewDataFromNSClient(intent); } else if (!nsUploadOnly && @@ -250,8 +252,15 @@ public class DataService extends IntentService { PumpInterface pump = MainApp.getConfigBuilder(); if (pump != null) { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - if (SP.getBoolean("syncprofiletopump", false)) - pump.setNewBasalProfile(nsProfile); + 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"); } @@ -414,7 +423,8 @@ public class DataService extends IntentService { if (trJson.has("eventType")) { treatment.mealBolus = true; if (trJson.get("eventType").equals("Correction Bolus")) treatment.mealBolus = false; - if (trJson.get("eventType").equals("Bolus Wizard") && treatment.carbs <= 0) treatment.mealBolus = false; + if (trJson.get("eventType").equals("Bolus Wizard") && treatment.carbs <= 0) + treatment.mealBolus = false; } treatment.setTimeIndex(treatment.getTimeIndex()); try { @@ -464,7 +474,8 @@ public class DataService extends IntentService { if (trJson.has("eventType")) { treatment.mealBolus = true; if (trJson.get("eventType").equals("Correction Bolus")) treatment.mealBolus = false; - if (trJson.get("eventType").equals("Bolus Wizard") && treatment.carbs <= 0) treatment.mealBolus = false; + if (trJson.get("eventType").equals("Bolus Wizard") && treatment.carbs <= 0) + treatment.mealBolus = false; } treatment.setTimeIndex(treatment.getTimeIndex()); try { 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 107d7ad372..cde760fc28 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -22,7 +22,10 @@ public interface PumpInterface { boolean isExtendedBoluslInProgress(); // Upload to pump new basal profile - void setNewBasalProfile(NSProfile profile); + int SUCCESS = 0; + int FAILED = 1; + int NOT_NEEDED = 2; + int setNewBasalProfile(NSProfile profile); boolean isThisProfileSet(NSProfile profile); double getBaseBasalRate(); // base basal rate, not temp basal 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 0cfdd6b8b2..ccbec0581f 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 @@ -49,6 +49,7 @@ import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorPlugin; import info.nightscout.client.data.DbLogger; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; @@ -334,7 +335,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } @Override - public void setNewBasalProfile(NSProfile profile) { + public int setNewBasalProfile(NSProfile profile) { // Compare with pump limits NSProfile.BasalValue[] basalValues = profile.getBasalValues(); @@ -342,7 +343,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain if (basalValues[index].value < getPumpDescription().basalMinimumRate) { Notification notification = new Notification(Notification.BASAL_VALUE_BELOW_MINIMUM, MainApp.sResources.getString(R.string.basalvaluebelowminimum), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); - return; + return FAILED; } } @@ -350,8 +351,9 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain if (isThisProfileSet(profile)) { log.debug("Correct profile already set"); + return NOT_NEEDED; } else { - activePump.setNewBasalProfile(profile); + return activePump.setNewBasalProfile(profile); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java index e80c0110db..021372f0b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java @@ -218,25 +218,27 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public void setNewBasalProfile(NSProfile profile) { + public int setNewBasalProfile(NSProfile profile) { if (sExecutionService == null) { log.error("setNewBasalProfile sExecutionService is null"); - return; + return FAILED; } if (!isInitialized()) { log.error("setNewBasalProfile not initialized"); Notification notification = new Notification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED, MainApp.sResources.getString(R.string.pumpNotInitializedProfileNotSet), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); - return; + return FAILED; } else { MainApp.bus().post(new EventDismissNotification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED)); } if (!sExecutionService.updateBasalsInPump(profile)) { Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, MainApp.sResources.getString(R.string.failedupdatebasalprofile), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); + return FAILED; } else { MainApp.bus().post(new EventDismissNotification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED)); MainApp.bus().post(new EventDismissNotification(Notification.FAILED_UDPATE_PROFILE)); + return SUCCESS; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java index 42dd2e59f9..e4a3d06959 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java @@ -218,25 +218,27 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints } @Override - public void setNewBasalProfile(NSProfile profile) { + public int setNewBasalProfile(NSProfile profile) { if (sExecutionService == null) { log.error("setNewBasalProfile sExecutionService is null"); - return; + return FAILED; } if (!isInitialized()) { log.error("setNewBasalProfile not initialized"); Notification notification = new Notification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED, MainApp.sResources.getString(R.string.pumpNotInitializedProfileNotSet), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); - return; + return FAILED; } else { MainApp.bus().post(new EventDismissNotification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED)); } if (!sExecutionService.updateBasalsInPump(profile)) { Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, MainApp.sResources.getString(R.string.failedupdatebasalprofile), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); + return FAILED; } else { MainApp.bus().post(new EventDismissNotification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED)); MainApp.bus().post(new EventDismissNotification(Notification.FAILED_UDPATE_PROFILE)); + return SUCCESS; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java index df94d1168a..743b7b576a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java @@ -124,8 +124,9 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { } @Override - public void setNewBasalProfile(NSProfile profile) { + public int setNewBasalProfile(NSProfile profile) { // Do nothing here. we are using MainApp.getConfigBuilder().getActiveProfile().getProfile(); + return SUCCESS; } @Override From 463a913cd106f30995c12a0d9667cf390af97f5a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 25 Dec 2016 15:08:40 +0100 Subject: [PATCH 6/8] add debug to profile comparation --- .../info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java index 021372f0b7..99f318c5a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java @@ -252,8 +252,10 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf for (int h = 0; h < basalValues; h++) { Double pumpValue = pump.pumpProfiles[pump.activeProfile][h]; Double profileValue = profile.getBasal(h * basalIncrement); - if (!pumpValue.equals(profileValue)) + if (!pumpValue.equals(profileValue)) { + log.debug("Diff found. Hour: " + h + " Pump: " + pumpValue + " Profile: " + profileValue); return false; + } } return true; } From a109bb985346912ffb5d35f759deaf0ba12d9fed Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 25 Dec 2016 23:18:46 +0100 Subject: [PATCH 7/8] handle switching profiles in correct order --- .../info/nightscout/androidaps/Services/DataService.java | 8 ++------ .../plugins/Careportal/Dialogs/NewNSTreatmentDialog.java | 4 +++- 2 files changed, 5 insertions(+), 7 deletions(-) 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 a2cadd544c..6b5bdfc9c2 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -245,10 +245,8 @@ public class DataService extends IntentService { String activeProfile = bundles.getString("activeprofile"); String profile = bundles.getString("profile"); NSProfile nsProfile = new NSProfile(new JSONObject(profile), activeProfile); - if (MainApp.getConfigBuilder() == null) { - log.error("Config builder not ready on receive profile"); - return; - } + MainApp.bus().post(new EventNewBasalProfile(nsProfile)); + PumpInterface pump = MainApp.getConfigBuilder(); if (pump != null) { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); @@ -260,13 +258,11 @@ public class DataService extends IntentService { } } } - } else { log.error("No active pump selected"); } if (Config.logIncommingData) log.debug("Received profile: " + activeProfile + " " + profile); - MainApp.bus().post(new EventNewBasalProfile(nsProfile)); } catch (JSONException e) { e.printStackTrace(); } 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 1011caf40a..43b383f33a 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 @@ -541,7 +541,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick builder.setMessage(confirmText); builder.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - ConfigBuilderPlugin.uploadCareportalEntryToNS(data); if (options.executeProfileSwitch) { if (data.has("profile")) { sHandler.post(new Runnable() { @@ -559,12 +558,15 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } else { log.error("No active pump selected"); } + ConfigBuilderPlugin.uploadCareportalEntryToNS(data); } catch (JSONException e) { e.printStackTrace(); } } }); } + } else { + ConfigBuilderPlugin.uploadCareportalEntryToNS(data); } } }); From cd41bff2bb9bc6b22018ca82371079cf4e7712fa Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 27 Dec 2016 12:17:26 +0100 Subject: [PATCH 8/8] SMS command BG --- .../SmsCommunicatorPlugin.java | 56 +++++++++++++++++-- app/src/main/res/values/strings.xml | 2 + wear/wear.iml | 20 +++---- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 2bd37249b2..f878b5676f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -23,13 +23,19 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; +import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; /** @@ -144,16 +150,15 @@ public class SmsCommunicatorPlugin implements PluginBase { String pattern = ";"; String[] substrings = settings.split(pattern); - for (String number: substrings) - { - String cleaned = number.replaceAll("\\s+",""); + for (String number : substrings) { + String cleaned = number.replaceAll("\\s+", ""); allowedNumbers.add(cleaned); log.debug("Found allowed number: " + cleaned); } } boolean isAllowedNumber(String number) { - for (String num: allowedNumbers) { + for (String num : allowedNumbers) { if (num.equals(number)) return true; } return false; @@ -194,6 +199,39 @@ public class SmsCommunicatorPlugin implements PluginBase { if (splited.length > 0) { switch (splited[0].toUpperCase()) { + case "BG": + BgReading actualBG = MainApp.getDbHelper().actualBg(); + BgReading lastBG = MainApp.getDbHelper().lastBg(); + + NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + String units = profile.getUnits(); + + Long agoMsec = new Date().getTime() - lastBG.timeIndex; + int agoMin = (int) (agoMsec / 60d / 1000d); + + if (actualBG != null) { + reply = MainApp.sResources.getString(R.string.actualbg) + " " + actualBG.valueToUnitsToString(units) + ", "; + } else if (lastBG != null) { + reply = MainApp.sResources.getString(R.string.lastbg) + " " + lastBG.valueToUnitsToString(units) + " " + agoMin + MainApp.sResources.getString(R.string.minago) + ", "; + } + DatabaseHelper.GlucoseStatus glucoseStatus = MainApp.getDbHelper().getGlucoseStatusData(); + if (glucoseStatus != null) + reply += MainApp.sResources.getString(R.string.delta) + ": " + NSProfile.toUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units) + " " + units + ", "; + + MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); + IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); + if (bolusIob == null) bolusIob = new IobTotal(); + MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); + IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); + if (basalIob == null) basalIob = new IobTotal(); + + reply += MainApp.sResources.getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" + + MainApp.sResources.getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + + MainApp.sResources.getString(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)"; + + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); + receivedSms.processed = true; + break; case "LOOP": switch (splited[1].toUpperCase()) { case "STOP": @@ -255,9 +293,15 @@ public class SmsCommunicatorPlugin implements PluginBase { break; case "DANAR": DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); - if (danaRPlugin != null && danaRPlugin.isEnabled(PluginBase.PUMP)) + if (danaRPlugin != null && danaRPlugin.isEnabled(PluginBase.PUMP)) { reply = danaRPlugin.shortStatus(); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); + } + DanaRKoreanPlugin danaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class); + if (danaRKoreanPlugin != null && danaRKoreanPlugin.isEnabled(PluginBase.PUMP)) { + reply = danaRKoreanPlugin.shortStatus(); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); + } receivedSms.processed = true; break; case "BASAL": @@ -284,7 +328,7 @@ public class SmsCommunicatorPlugin implements PluginBase { resetWaitingMessages(); sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); tempBasalWaitingForConfirmation.tempBasal = tempBasal; - } else { + } else { reply = MainApp.sResources.getString(R.string.remotebasalnotallowed); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3010875970..02bd48a9f1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -384,4 +384,6 @@ Enable extended boluses on pump Change mode from U/d to U/h on pump Basal value below minimum. Profile not set! + BG: + Last BG: diff --git a/wear/wear.iml b/wear/wear.iml index d38c08321d..a09c181fa1 100644 --- a/wear/wear.iml +++ b/wear/wear.iml @@ -66,14 +66,6 @@ - - - - - - - - @@ -82,6 +74,14 @@ + + + + + + + + @@ -123,8 +123,6 @@ - - @@ -133,13 +131,11 @@ - -