diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java index f26bdb6fad..468626fe26 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java @@ -265,7 +265,9 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public boolean loadHistory(byte type) { connectIfNotConnected("loadHistory"); - return danaRSService.loadHistory(type); + danaRSService.loadHistory(type); + disconnect("LoadHistory"); + return true; } // Constraints interface @@ -393,10 +395,12 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, if (!danaRSService.updateBasalsInPump(profile)) { Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, MainApp.sResources.getString(R.string.failedupdatebasalprofile), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); + disconnect("SetNewBasalProfile"); return FAILED; } else { MainApp.bus().post(new EventDismissNotification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED)); MainApp.bus().post(new EventDismissNotification(Notification.FAILED_UDPATE_PROFILE)); + disconnect("SetNewBasalProfile"); return SUCCESS; } } @@ -431,6 +435,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, log.debug("Refreshing data from pump"); if (!isConnected() && !isConnecting()) { connect(reason); + disconnect("RefreshDataFromPump"); } else log.debug("Already connecting ..."); } @@ -445,7 +450,6 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { - DetailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); int speed = 12; switch (preferencesSpeed) { @@ -460,8 +464,8 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, break; } // v2 stores end time for bolus, we need to adjust time - // default delivery speed is 12 U/min - detailedBolusInfo.date += detailedBolusInfo.insulin / speed * 60d * 1000; + // default delivery speed is 12 sec/U + detailedBolusInfo.date += detailedBolusInfo.insulin * speed * 1000; // clean carbs to prevent counting them as twice because they will picked up as another record // I don't think it's necessary to copy DetailedBolusInfo right now for carbs records double carbs = detailedBolusInfo.carbs; @@ -469,6 +473,8 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, int carbTime = detailedBolusInfo.carbTime; detailedBolusInfo.carbTime = 0; + DetailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history + Treatment t = new Treatment(); boolean connectionOK = false; connectIfNotConnected("bolus"); @@ -481,7 +487,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); if (Config.logPumpActions) log.debug("deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered); - // remove carbs because it's get from history seprately + disconnect("DeliverTreatment"); return result; } else { PumpEnactResult result = new PumpEnactResult(); @@ -624,6 +630,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, result.isPercent = true; if (Config.logPumpActions) log.debug("setTempBasalPercent: OK"); + disconnect("setTempBasalPercent"); return result; } result.enacted = false; @@ -647,6 +654,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, result.isPercent = true; if (Config.logPumpActions) log.debug("setHighTempBasalPercent: OK"); + disconnect("setHighTempBasalPercent"); return result; } result.enacted = false; @@ -690,6 +698,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, result.isPercent = false; if (Config.logPumpActions) log.debug("setExtendedBolus: OK"); + disconnect("setExtendedBolus"); return result; } result.enacted = false; @@ -708,6 +717,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, danaRSService.tempBasalStop(); result.enacted = true; result.isTempCancel = true; + disconnect("cancelTempBasal"); } if (!pump.isTempBasalInProgress) { result.success = true; @@ -734,6 +744,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, danaRSService.extendedBolusStop(); result.enacted = true; result.isTempCancel = true; + disconnect("extendedBolusStop"); } if (!pump.isExtendedInProgress) { result.success = true; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Basal_Rate.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Basal_Rate.java index 6f56464c51..a64da24bf3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Basal_Rate.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Basal_Rate.java @@ -4,13 +4,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; + import com.cozmo.danar.util.BleCommandUtil; public class DanaRS_Packet_Basal_Set_Basal_Rate extends DanaRS_Packet { private static Logger log = LoggerFactory.getLogger(DanaRS_Packet_Basal_Set_Basal_Rate.class); private double[] profileBasalRate; - public int error; public DanaRS_Packet_Basal_Set_Basal_Rate() { super(); @@ -38,11 +38,12 @@ public class DanaRS_Packet_Basal_Set_Basal_Rate extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal.java index 70a8b02826..538750a45b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal.java @@ -10,8 +10,6 @@ import com.cozmo.danar.util.BleCommandUtil; public class DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal extends DanaRS_Packet { private static Logger log = LoggerFactory.getLogger(DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal.class); - public int error; - public DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal() { super(); opCode = BleCommandUtil.DANAR_PACKET__OPCODE_BASAL__CANCEL_TEMPORARY_BASAL; @@ -22,9 +20,12 @@ public class DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal extends DanaRS_Packe @Override public void handleMessage(byte[] data) { - error = byteArrayToInt(getBytes(data, DATA_START, 1)); + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result " + error); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Basal_Rate.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Basal_Rate.java index 82a63f0ca7..66d0e7b4bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Basal_Rate.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Basal_Rate.java @@ -11,7 +11,6 @@ public class DanaRS_Packet_Basal_Set_Profile_Basal_Rate extends DanaRS_Packet { private int profileNumber; // 0 - 4 private double[] profileBasalRate; - public int error; public DanaRS_Packet_Basal_Set_Profile_Basal_Rate() { super(); @@ -41,11 +40,12 @@ public class DanaRS_Packet_Basal_Set_Profile_Basal_Rate extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Number.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Number.java index fe47f89dfc..69f7eae8dd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Number.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Profile_Number.java @@ -10,7 +10,6 @@ import com.cozmo.danar.util.BleCommandUtil; public class DanaRS_Packet_Basal_Set_Profile_Number extends DanaRS_Packet { private static Logger log = LoggerFactory.getLogger(DanaRS_Packet_Basal_Set_Profile_Number.class); private int profileNumber; - public int error; public DanaRS_Packet_Basal_Set_Profile_Number() { super(); @@ -33,11 +32,12 @@ public class DanaRS_Packet_Basal_Set_Profile_Number extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_Off.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_Off.java index 1b9444c2d3..4c78038162 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_Off.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_Off.java @@ -8,7 +8,6 @@ import com.cozmo.danar.util.BleCommandUtil; public class DanaRS_Packet_Basal_Set_Suspend_Off extends DanaRS_Packet { private static Logger log = LoggerFactory.getLogger(DanaRS_Packet_Basal_Set_Suspend_Off.class); - public int error; public DanaRS_Packet_Basal_Set_Suspend_Off() { super(); @@ -20,11 +19,12 @@ public class DanaRS_Packet_Basal_Set_Suspend_Off extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_On.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_On.java index 01e90e026c..75a2a5fada 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_On.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Suspend_On.java @@ -8,7 +8,6 @@ import com.cozmo.danar.util.BleCommandUtil; public class DanaRS_Packet_Basal_Set_Suspend_On extends DanaRS_Packet { private static Logger log = LoggerFactory.getLogger(DanaRS_Packet_Basal_Set_Suspend_On.class); - public int error; public DanaRS_Packet_Basal_Set_Suspend_On() { super(); @@ -20,11 +19,12 @@ public class DanaRS_Packet_Basal_Set_Suspend_On extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Temporary_Basal.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Temporary_Basal.java index a81d85f016..d2f68ff864 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Temporary_Basal.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Basal_Set_Temporary_Basal.java @@ -11,7 +11,6 @@ public class DanaRS_Packet_Basal_Set_Temporary_Basal extends DanaRS_Packet { private int temporaryBasalRatio; private int temporaryBasalDuration; - public int error; public DanaRS_Packet_Basal_Set_Temporary_Basal() { super(); @@ -37,11 +36,12 @@ public class DanaRS_Packet_Basal_Set_Temporary_Basal extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Bolus_Option.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Bolus_Option.java index e20baeaee6..103bac3ecf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Bolus_Option.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Bolus_Option.java @@ -111,12 +111,12 @@ public class DanaRS_Packet_Bolus_Set_Bolus_Option extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_CIR_CF_Array.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_CIR_CF_Array.java index cf6a459d83..062ae00712 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_CIR_CF_Array.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_CIR_CF_Array.java @@ -83,12 +83,12 @@ public class DanaRS_Packet_Bolus_Set_CIR_CF_Array extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Dual_Bolus.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Dual_Bolus.java index 31c80a702a..e769bea195 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Dual_Bolus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Dual_Bolus.java @@ -44,12 +44,12 @@ public class DanaRS_Packet_Bolus_Set_Dual_Bolus extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus.java index d5f61c0946..887a9d852a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus.java @@ -40,12 +40,12 @@ public class DanaRS_Packet_Bolus_Set_Extended_Bolus extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus_Cancel.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus_Cancel.java index 10aa5f4979..132de649cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus_Cancel.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Extended_Bolus_Cancel.java @@ -19,12 +19,12 @@ public class DanaRS_Packet_Bolus_Set_Extended_Bolus_Cancel extends DanaRS_Packet @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Initial_Bolus.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Initial_Bolus.java index a475e7675d..a174d39b53 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Initial_Bolus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Initial_Bolus.java @@ -44,12 +44,12 @@ public class DanaRS_Packet_Bolus_Set_Initial_Bolus extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java index 1263aeb629..4c5cc23ef3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java @@ -48,14 +48,12 @@ public class DanaRS_Packet_Bolus_Set_Step_Bolus_Start extends DanaRS_Packet { } @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - - failed = status != 0x00; - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java index 61ddd05c9b..5383fca413 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java @@ -35,13 +35,14 @@ public class DanaRS_Packet_Bolus_Set_Step_Bolus_Stop extends DanaRS_Packet { @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); stopped = true; if (!forced) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_History_Upload_Mode.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_History_Upload_Mode.java index 7d9ec07ff1..878cfd64dd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_History_Upload_Mode.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_History_Upload_Mode.java @@ -31,12 +31,12 @@ public class DanaRS_Packet_General_Set_History_Upload_Mode extends DanaRS_Packet @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_User_Time_Change_Flag_Clear.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_User_Time_Change_Flag_Clear.java index 9cc6b7b2e0..3917f31050 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_User_Time_Change_Flag_Clear.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Set_User_Time_Change_Flag_Clear.java @@ -17,12 +17,12 @@ public class DanaRS_Packet_General_Set_User_Time_Change_Flag_Clear extends DanaR @Override public void handleMessage(byte[] data) { - int dataIndex = DATA_START; - int dataSize = 1; - int status = byteArrayToInt(getBytes(data, dataIndex, dataSize)); - + int result = intFromBuff(data, 0, 1); if (Config.logDanaMessageDetail) { - log.debug("Result: " + status); + if (result == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + result); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.java index e93e18796d..007623afca 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.java @@ -45,7 +45,10 @@ public class DanaRS_Packet_Option_Set_Pump_Time extends DanaRS_Packet { int dataSize = 1; error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (error == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + error); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_User_Option.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_User_Option.java index 49c6d35708..e5d9374f32 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_User_Option.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Option_Set_User_Option.java @@ -47,7 +47,10 @@ public class DanaRS_Packet_Option_Set_User_Option extends DanaRS_Packet { int dataSize = 1; error = byteArrayToInt(getBytes(data, dataIndex, dataSize)); if (Config.logDanaMessageDetail) { - log.debug("Result: " + error); + if (error == 0) + log.debug("Result OK"); + else + log.error("Result Error: " + error); } } 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 dde50a93e1..42f077887d 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 @@ -165,19 +165,6 @@ public class BLEComm { MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING)); isConnecting = true; - // Following should be removed later because we close Gatt on disconnect and this should never happen - if ((mBluetoothDeviceAddress != null) && (address.equals(mBluetoothDeviceAddress)) && (mBluetoothGatt != null)) { - log.debug("Trying to use an existing mBluetoothGatt for connection."); - sHandler.post(updateProgress); - if (mBluetoothGatt.connect()) { - setCharacteristicNotification(getUARTReadBTGattChar(), true); - return true; - } - sHandler.removeCallbacks(updateProgress); - return false; - } - // end - BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { log.debug("Device not found. Unable to connect."); @@ -201,12 +188,19 @@ public class BLEComm { public void disconnect(String from) { log.debug("disconnect from: " + from); + + // cancel previous scheduled disconnection to prevent closing upcomming connection + if (scheduledDisconnection != null) + scheduledDisconnection.cancel(false); + scheduledDisconnection = null; + if ((mBluetoothAdapter == null) || (mBluetoothGatt == null)) { return; } setCharacteristicNotification(getUARTReadBTGattChar(), false); mBluetoothGatt.disconnect(); isConnected = false; + SystemClock.sleep(2000); } public void close() { @@ -214,6 +208,7 @@ public class BLEComm { if (mBluetoothGatt == null) { return; } + mBluetoothGatt.close(); mBluetoothGatt = null; } @@ -687,18 +682,19 @@ public class BLEComm { } public void scheduleDisconnection() { + class DisconnectRunnable implements Runnable { public void run() { disconnect("scheduleDisconnection"); scheduledDisconnection = null; } } - // prepare task for execution in 5 sec + // prepare task for execution in 30 sec // cancel waiting task to prevent sending multiple disconnections if (scheduledDisconnection != null) scheduledDisconnection.cancel(false); Runnable task = new DisconnectRunnable(); - final int sec = 5; + final int sec = 30; scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS); log.debug("Disconnection scheduled"); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java index 6b39788342..e3baf10870 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java @@ -26,6 +26,7 @@ import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.PumpDanaR.events.EventDanaRNewStatus; @@ -188,16 +189,16 @@ public class DanaRSService extends Service { while (!msg.done && bleComm.isConnected()) { SystemClock.sleep(100); } - SystemClock.sleep(200); lastHistoryFetched = DanaRS_Packet_APS_History_Events.lastEventTimeLoaded; return true; } - public boolean bolus(double insulin, int carbs, long carbtime, Treatment t) { + public boolean bolus(final double insulin, int carbs, long carbtime, Treatment t) { + MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.startingbolus))); bolusingTreatment = t; - int speed = SP.getInt(R.string.key_danars_bolusspeed, 0); - DanaRS_Packet_Bolus_Set_Step_Bolus_Start start = new DanaRS_Packet_Bolus_Set_Step_Bolus_Start(insulin, speed); + final int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); + DanaRS_Packet_Bolus_Set_Step_Bolus_Start start = new DanaRS_Packet_Bolus_Set_Step_Bolus_Start(insulin, preferencesSpeed); DanaRS_Packet_Bolus_Set_Step_Bolus_Stop stop = new DanaRS_Packet_Bolus_Set_Step_Bolus_Stop(insulin, t); // initialize static variables DanaRS_Packet_Notify_Delivery_Complete complete = new DanaRS_Packet_Notify_Delivery_Complete(insulin, t); // initialize static variables @@ -210,28 +211,57 @@ public class DanaRSService extends Service { bleComm.sendMessage(msgSetHistoryEntry_v2); lastHistoryFetched = carbtime - 60000; } - if (insulin > 0) { - DanaRS_Packet_Notify_Delivery_Rate_Display progress = new DanaRS_Packet_Notify_Delivery_Rate_Display(insulin, t); // initialize static variables + final long bolusStart = System.currentTimeMillis(); + if (insulin > 0) { if (!stop.stopped) { bleComm.sendMessage(start); } else { t.insulin = 0d; return false; } + DanaRS_Packet_Notify_Delivery_Rate_Display progress = new DanaRS_Packet_Notify_Delivery_Rate_Display(insulin, t); // initialize static variables + while (!stop.stopped && !start.failed && !complete.done) { SystemClock.sleep(100); - if ((System.currentTimeMillis() - progress.lastReceive) > 5 * 1000L) { // if i didn't receive status for more than 5 sec expecting broken comm + if ((System.currentTimeMillis() - progress.lastReceive) > 15 * 1000L) { // if i didn't receive status for more than 20 sec expecting broken comm stop.stopped = true; stop.forced = true; log.debug("Communication stopped"); } } } - SystemClock.sleep(3000); + + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + bolusingEvent.t = t; + bolusingEvent.percent = 99; + bolusingTreatment = null; - DanaRSPlugin.connectIfNotConnected("ReadHistoryAfterBolus"); + int speed = 12; + switch (preferencesSpeed) { + case 0: + speed = 12; + break; + case 1: + speed = 30; + break; + case 2: + speed = 60; + break; + } + long bolusDurationInMSec = (long) (insulin * speed * 1000); + long expectedEnd = bolusStart + bolusDurationInMSec + 2000; + while (System.currentTimeMillis() < expectedEnd) { + long waitTime = expectedEnd - System.currentTimeMillis(); + bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.waitingforestimatedbolusend), waitTime / 1000); + MainApp.bus().post(bolusingEvent); + SystemClock.sleep(1000); + } + if (!(isConnected())) + DanaRSPlugin.getPlugin().connect("loadEvents"); loadEvents(); + bolusingEvent.percent = 100; + MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.disconnecting))); return true; }