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 89eb892f05..831e5bcc0d 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -397,7 +397,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { QueryBuilder queryBuilder = daoBgReadings.queryBuilder(); queryBuilder.orderBy("date", false); queryBuilder.limit(1L); - queryBuilder.where().gt("value", 38).and().eq("isValid", true); + queryBuilder.where().ge("value", 39).and().eq("isValid", true); PreparedQuery preparedQuery = queryBuilder.prepare(); bgList = daoBgReadings.query(preparedQuery); @@ -435,7 +435,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { QueryBuilder queryBuilder = daoBgreadings.queryBuilder(); queryBuilder.orderBy("date", ascending); Where where = queryBuilder.where(); - where.ge("date", mills).and().gt("value", 38).and().eq("isValid", true); + where.ge("date", mills).and().ge("value", 39).and().eq("isValid", true); PreparedQuery preparedQuery = queryBuilder.prepare(); bgReadings = daoBgreadings.query(preparedQuery); return bgReadings; @@ -452,7 +452,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { QueryBuilder queryBuilder = daoBgreadings.queryBuilder(); queryBuilder.orderBy("date", ascending); Where where = queryBuilder.where(); - where.between("date", start, end).and().gt("value", 38).and().eq("isValid", true); + where.between("date", start, end).and().ge("value", 39).and().eq("isValid", true); PreparedQuery preparedQuery = queryBuilder.prepare(); bgReadings = daoBgreadings.query(preparedQuery); return bgReadings; 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 2a169dc4f3..cb495059b8 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 @@ -270,7 +270,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick if (profile == null) { editBg.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false, bgTextWatcher); editTemptarget.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); - } else if (profile.getUnits().equals(Constants.MMOL)) { + } else if (units.equals(Constants.MMOL)) { editBg.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false, bgTextWatcher); editTemptarget.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); } else { @@ -279,7 +279,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } sensorRadioButton.setOnCheckedChangeListener((buttonView, isChecked) -> { - Double bg1 = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile.getUnits()); + Double bg1 = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, units); if (savedInstanceState != null && savedInstanceState.getDouble("editBg") != bg1) { editBg.setValue(savedInstanceState.getDouble("editBg")); } else { @@ -459,7 +459,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick if ((data.size() > 0) && (data.get(0).date > millis - 7 * 60 * 1000L) && (data.get(0).date < millis + 7 * 60 * 1000L)) { - editBg.setValue(Profile.fromMgdlToUnits(data.get(0).value, profile != null ? profile.getUnits() : Constants.MGDL)); + editBg.setValue(Profile.fromMgdlToUnits(data.get(0).value, units)); } } @@ -736,8 +736,8 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick .reason(reason) .source(Source.USER); if (tempTarget.durationInMinutes != 0) { - tempTarget.low(Profile.toMgdl(targetBottom, profile.getUnits())) - .high(Profile.toMgdl(targetTop, profile.getUnits())); + tempTarget.low(Profile.toMgdl(targetBottom, units)) + .high(Profile.toMgdl(targetTop, units)); } else { tempTarget.low(0).high(0); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java index 5135c6b87b..3678617c96 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java @@ -307,6 +307,11 @@ public class APSResult { PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); Profile profile = ProfileFunctions.getInstance().getProfile(); + if (profile == null) { + log.error("FALSE: No Profile"); + return false; + } + if (usePercent) { if (activeTemp == null && percent == 100) { if (L.isEnabled(L.APS)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java index fcd65bbb1e..48bb311385 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java @@ -144,9 +144,9 @@ public class BLEComm { if (L.isEnabled(L.PUMPBTCOMM)) log.debug("Trying to create a new connection from: " + from); + mBluetoothDeviceName = device.getName(); mBluetoothGatt = device.connectGatt(service.getApplicationContext(), false, mGattCallback); setCharacteristicNotification(getUARTReadBTGattChar(), true); - mBluetoothDeviceName = device.getName(); return true; } @@ -663,7 +663,7 @@ public class BLEComm { private void SendPumpCheck() { // 1st message sent to pump after connect String devicename = getConnectDeviceName(); - if(devicename == null || devicename == ""){ + if(devicename == null || devicename.equals("")){ Notification n = new Notification(Notification.DEVICENOTPAIRED, MainApp.gs(R.string.pairfirst), Notification.URGENT); MainApp.bus().post(new EventNewNotification(n)); return; diff --git a/app/src/main/java/info/nightscout/utils/JsonHelper.java b/app/src/main/java/info/nightscout/utils/JsonHelper.java index 503d0bf395..e082ed99f7 100644 --- a/app/src/main/java/info/nightscout/utils/JsonHelper.java +++ b/app/src/main/java/info/nightscout/utils/JsonHelper.java @@ -22,7 +22,7 @@ public class JsonHelper { public static Object safeGetObject(JSONObject json, String fieldName, Object defaultValue) { Object result = defaultValue; - if (json.has(fieldName)) { + if (json != null && json.has(fieldName)) { try { result = json.get(fieldName); } catch (JSONException ignored) { @@ -36,7 +36,7 @@ public class JsonHelper { public static String safeGetString(JSONObject json, String fieldName) { String result = null; - if (json.has(fieldName)) { + if (json != null && json.has(fieldName)) { try { result = json.getString(fieldName); } catch (JSONException ignored) { @@ -49,7 +49,7 @@ public class JsonHelper { public static String safeGetString(JSONObject json, String fieldName, String defaultValue) { String result = defaultValue; - if (json.has(fieldName)) { + if (json != null && json.has(fieldName)) { try { result = json.getString(fieldName); } catch (JSONException ignored) { @@ -62,7 +62,7 @@ public class JsonHelper { public static double safeGetDouble(JSONObject json, String fieldName) { double result = 0d; - if (json.has(fieldName)) { + if (json != null && json.has(fieldName)) { try { result = json.getDouble(fieldName); } catch (JSONException ignored) { @@ -75,7 +75,7 @@ public class JsonHelper { public static int safeGetInt(JSONObject json, String fieldName) { int result = 0; - if (json.has(fieldName)) { + if (json != null && json.has(fieldName)) { try { result = json.getInt(fieldName); } catch (JSONException ignored) { @@ -88,7 +88,7 @@ public class JsonHelper { public static long safeGetLong(JSONObject json, String fieldName) { long result = 0; - if (json.has(fieldName)) { + if (json != null && json.has(fieldName)) { try { result = json.getLong(fieldName); } catch (JSONException e) { @@ -101,7 +101,7 @@ public class JsonHelper { public static boolean safeGetBoolean(JSONObject json, String fieldName) { boolean result = false; - if (json.has(fieldName)) { + if (json != null && json.has(fieldName)) { try { result = json.getBoolean(fieldName); } catch (JSONException e) { diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index afac79c1ea..e989930348 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -1054,4 +1054,5 @@ Context | Edit Context Нарушение ограничения инсулина Минимальный запрос на изменения [%] Алгоритм Ипж выдаст всплывающее окно с запросом на новые изменения, только если изменение больше, чем это значение. Значение по умолчанию — 20% + Выполните сопряжение помпы с телефоном! diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 2278019f53..915351c430 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -1,6 +1,6 @@ - Max izin verilen bolus [U] - Max izin verilen karbonhidrat [g] + Max izin verilen bolus [U] + Max izin verilen karbonhidrat [g] diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index ee87420641..f4ffc33af5 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -24,6 +24,7 @@ import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; +import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentService; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; @@ -176,8 +177,10 @@ public class AAPSMocker { public static DanaRPlugin mockDanaRPlugin() { PowerMockito.mockStatic(DanaRPlugin.class); DanaRPlugin danaRPlugin = mock(DanaRPlugin.class); + DanaRv2Plugin danaRv2Plugin = mock(DanaRv2Plugin.class); DanaRKoreanPlugin danaRKoreanPlugin = mock(DanaRKoreanPlugin.class); when(MainApp.getSpecificPlugin(DanaRPlugin.class)).thenReturn(danaRPlugin); + when(MainApp.getSpecificPlugin(DanaRv2Plugin.class)).thenReturn(danaRv2Plugin); when(MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).thenReturn(danaRKoreanPlugin); return danaRPlugin; } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpdanaRv2/DanaRv2PluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpdanaRv2/DanaRv2PluginTest.java new file mode 100644 index 0000000000..140b0165bd --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpdanaRv2/DanaRv2PluginTest.java @@ -0,0 +1,82 @@ +package info.nightscout.androidaps.plugins.PumpdanaRv2; + +import android.content.Context; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; +import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin; +import info.nightscout.utils.SP; +import info.nightscout.utils.ToastUtils; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +/** + * Created by Rumen on 01.08.2018 + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class, SP.class}) +public class DanaRv2PluginTest { + DanaRv2Plugin danaRv2Plugin; + + @Test + public void getPlugin() { + danaRv2Plugin.getPlugin(); + } + + @Test + public void basalRateShouldBeLimited() throws Exception { + danaRv2Plugin.setPluginEnabled(PluginType.PUMP, true); + danaRv2Plugin.setPluginEnabled(PluginType.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); + danaRv2Plugin.applyBasalConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals(0.8d, c.value()); + Assert.assertEquals("DanaRv2: Limiting basal rate to 0.80 U/h because of pump limit", c.getReasons()); + Assert.assertEquals("DanaRv2: Limiting basal rate to 0.80 U/h because of pump limit", c.getMostLimitedReasons()); + } + + @Test + public void percentBasalRateShouldBeLimited() throws Exception { + danaRv2Plugin.setPluginEnabled(PluginType.PUMP, true); + danaRv2Plugin.setPluginEnabled(PluginType.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + Constraint c = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); + danaRv2Plugin.applyBasalPercentConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals((Integer) 200, c.value()); + Assert.assertEquals("DanaRv2: Limiting percent rate to 200% because of pump limit", c.getReasons()); + Assert.assertEquals("DanaRv2: Limiting percent rate to 200% because of pump limit", c.getMostLimitedReasons()); + } + + @Before + public void prepareMocks() throws Exception { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + AAPSMocker.mockApplicationContext(); + AAPSMocker.mockSP(); + AAPSMocker.mockCommandQueue(); + +// when(SP.getString(R.string.key_danars_address, "")).thenReturn(""); + + danaRv2Plugin = DanaRv2Plugin.getPlugin(); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpdanaRv2/comm/MessageHashTable_v2Test.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpdanaRv2/comm/MessageHashTable_v2Test.java new file mode 100644 index 0000000000..63e3af0fa4 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpdanaRv2/comm/MessageHashTable_v2Test.java @@ -0,0 +1,60 @@ +package info.nightscout.androidaps.plugins.PumpdanaRv2.comm; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.PumpDanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.PumpDanaRv2.comm.MessageHashTable_v2; +import info.nightscout.androidaps.plugins.PumpDanaRv2.comm.MsgStatusAPS_v2; +import info.nightscout.utils.SP; + +import static org.junit.Assert.*; +/** + * Created by Rumen Georgiev on 30.10.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, SP.class, L.class}) +public class MessageHashTable_v2Test { + @Test + public void runTest() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockApplicationContext(); + AAPSMocker.mockSP(); + AAPSMocker.mockL(); + AAPSMocker.mockBus(); + + MessageHashTable_v2 packet = new MessageHashTable_v2(); + + MessageBase forTesting = new MsgStatusAPS_v2(); + MessageBase testPacket = MessageHashTable_v2.findMessage(forTesting.getCommand()); + assertEquals(0xE001, testPacket.getCommand()); + // try putting another command + MessageBase testMessage = new MessageBase(); + testMessage.SetCommand(0xE005); + packet.put(testMessage); + assertEquals(0xE005, packet.findMessage(0xE005).getCommand()); + } + + byte[] createArray(int length, byte fillWith){ + byte[] ret = new byte[length]; + for(int i = 0; i