From 2610edae55ecfeef395ec373027b71f4f83cd419 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 12 May 2017 00:38:26 +0200 Subject: [PATCH] DanaRv2 Modify 6 --- .../PumpDanaR/comm/MessageOriginalNames.java | 7 ++-- .../PumpDanaRv2/comm/MessageHashTable.java | 3 +- .../PumpDanaRv2/comm/MsgStatusAPS.java | 30 +++++++++++++++++ .../comm/MsgStatusAPSTempBasal.java | 32 +++++++++++++++---- .../services/DanaRv2ExecutionService.java | 13 ++++++-- 5 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPS.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MessageOriginalNames.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MessageOriginalNames.java index 3526479947..535bf33bd1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MessageOriginalNames.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MessageOriginalNames.java @@ -155,9 +155,10 @@ public class MessageOriginalNames { messageNames.put(0xE002, "CMD_PUMPSET_APSTEMP"); messageNames.put(0xE003, "CMD_HISTORY_APSTEMP"); - messageNames.put(0xE001, "CMD_PUMP_APSTEMP_VALUE"); - messageNames.put(0xE002, "CMD_PUMPSET_APSTEMP"); - messageNames.put(0xE003, "CMD_HISTORY_APSTEMP"); + messageNames.put(0xE001, "CMD_PUMP_APS"); + messageNames.put(0xE002, "CMD_PUMPSET_APS"); + messageNames.put(0xE003, "CMD_HISTORY_APS"); + messageNames.put(0xE004, "CMD_PUMPSET_APSTEMP"); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MessageHashTable.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MessageHashTable.java index 270b108c71..39c0cd25c3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MessageHashTable.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MessageHashTable.java @@ -117,9 +117,10 @@ public class MessageHashTable { put(new MsgHistoryNewDone()); // 0x42F1 CMD_HISTORY_NEW_DONE put(new MsgHistoryNew()); // 0x42F2 CMD_HISTORY_NEW put(new MsgCheckValue()); // 0xF0F1 CMD_PUMP_CHECK_VALUE - put(new MsgStatusAPSTempBasal()); // 0xE001 CMD_PUMP_APSTEMP_VALUE + put(new MsgStatusAPS()); // 0xE001 CMD_PUMP_APS put(new MsgSetAPSTempBasalStart()); // 0xE002 CMD_PUMPSET_APSTEMP put(new MsgHistoryEvents()); // 0xE003 CMD_HISTORY_EVENTS + put(new MsgStatusAPSTempBasal()); // 0xE004 CMD_PUMP_APSTEMP } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPS.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPS.java new file mode 100644 index 0000000000..2d26a455f6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPS.java @@ -0,0 +1,30 @@ +package info.nightscout.androidaps.plugins.PumpDanaRv2.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; +import info.nightscout.androidaps.plugins.PumpDanaR.comm.MessageBase; + +public class MsgStatusAPS extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgStatusAPS.class); + + public MsgStatusAPS() { + SetCommand(0xE001); + } + + public void handleMessage(byte[] bytes) { + double iob = intFromBuff(bytes, 0, 2) / 100d; + double deliveredSoFar = intFromBuff(bytes, 2, 2) / 100d; + + DanaRPump pump = DanaRPump.getInstance(); + pump.iob = iob; + + if (Config.logDanaMessageDetail) { + log.debug("Delivered so far: " + deliveredSoFar); + log.debug("Current pump IOB: " + iob); + } + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPSTempBasal.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPSTempBasal.java index ec9b191661..f76738ce39 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPSTempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgStatusAPSTempBasal.java @@ -1,8 +1,12 @@ package info.nightscout.androidaps.plugins.PumpDanaRv2.comm; +import android.support.annotation.NonNull; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Date; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.comm.MessageBase; @@ -11,20 +15,36 @@ public class MsgStatusAPSTempBasal extends MessageBase { private static Logger log = LoggerFactory.getLogger(MsgStatusAPSTempBasal.class); public MsgStatusAPSTempBasal() { - SetCommand(0xE001); + SetCommand(0xE004); } public void handleMessage(byte[] bytes) { - double iob = intFromBuff(bytes, 0, 2) / 100d; - double deliveredSoFar = intFromBuff(bytes, 2, 2) / 100d; + boolean isTempBasalInProgress = intFromBuff(bytes, 0, 1) == 1; + int tempBasalPercent = intFromBuff(bytes, 1, 2); + int tempBasalTotalSec = intFromBuff(bytes, 3, 1) * 60; + int tempBasalRunningSeconds = intFromBuff(bytes, 4, 3); + int tempBasalRemainingMin = (tempBasalTotalSec - tempBasalRunningSeconds) / 60; + Date tempBasalStart = isTempBasalInProgress ? getDateFromTempBasalSecAgo(tempBasalRunningSeconds) : new Date(0); DanaRPump pump = DanaRPump.getInstance(); - pump.iob = iob; + pump.isTempBasalInProgress = isTempBasalInProgress; + pump.tempBasalPercent = tempBasalPercent; + pump.tempBasalRemainingMin = tempBasalRemainingMin; + pump.tempBasalTotalSec = tempBasalTotalSec; + pump.tempBasalStart = tempBasalStart; if (Config.logDanaMessageDetail) { - log.debug("Delivered so far: " + deliveredSoFar); - log.debug("Current pump IOB: " + iob); + log.debug("Is temp basal running: " + isTempBasalInProgress); + log.debug("Current temp basal percent: " + tempBasalPercent); + log.debug("Current temp basal remaining min: " + tempBasalRemainingMin); + log.debug("Current temp basal total sec: " + tempBasalTotalSec); + log.debug("Current temp basal start: " + tempBasalStart); } } + @NonNull + private Date getDateFromTempBasalSecAgo(int tempBasalAgoSecs) { + return new Date((long) (Math.ceil(new Date().getTime() / 1000d) - tempBasalAgoSecs) * 1000); + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java index 43297bc486..db9cfd5ec7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java @@ -82,6 +82,7 @@ import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin; import info.nightscout.androidaps.plugins.PumpDanaRv2.SerialIOThread; import info.nightscout.androidaps.plugins.PumpDanaRv2.comm.MsgHistoryEvents; import info.nightscout.androidaps.plugins.PumpDanaRv2.comm.MsgSetAPSTempBasalStart; +import info.nightscout.androidaps.plugins.PumpDanaRv2.comm.MsgStatusAPS; import info.nightscout.androidaps.plugins.PumpDanaRv2.comm.MsgStatusAPSTempBasal; import info.nightscout.utils.SP; import info.nightscout.utils.ToastUtils; @@ -284,6 +285,7 @@ public class DanaRv2ExecutionService extends Service { MsgStatus statusMsg = new MsgStatus(); MsgStatusBasic statusBasicMsg = new MsgStatusBasic(); MsgStatusTempBasal tempStatusMsg = new MsgStatusTempBasal(); + MsgStatusAPSTempBasal tempAPSStatusMsg = new MsgStatusAPSTempBasal(); MsgStatusBolusExtended exStatusMsg = new MsgStatusBolusExtended(); MsgCheckValue checkValue = new MsgCheckValue(); @@ -295,6 +297,7 @@ public class DanaRv2ExecutionService extends Service { } mSerialIOThread.sendMessage(tempStatusMsg); // do this before statusBasic because here is temp duration + mSerialIOThread.sendMessage(tempAPSStatusMsg); mSerialIOThread.sendMessage(exStatusMsg); mSerialIOThread.sendMessage(statusMsg); mSerialIOThread.sendMessage(statusBasicMsg); @@ -309,13 +312,17 @@ public class DanaRv2ExecutionService extends Service { // Load of status of current basal rate failed, give one more try mSerialIOThread.sendMessage(tempStatusMsg); } + if (!tempAPSStatusMsg.received) { + // Load of status of current basal rate failed, give one more try + mSerialIOThread.sendMessage(tempAPSStatusMsg); + } if (!exStatusMsg.received) { // Load of status of current extended bolus failed, give one more try mSerialIOThread.sendMessage(exStatusMsg); } // Check we have really current status of pump - if (!statusMsg.received || !statusBasicMsg.received || !tempStatusMsg.received || !exStatusMsg.received) { + if (!statusMsg.received || !statusBasicMsg.received || !tempStatusMsg.received || !tempAPSStatusMsg.received || !exStatusMsg.received) { waitMsec(10 * 1000); log.debug("getPumpStatus failed"); return false; @@ -362,6 +369,7 @@ public class DanaRv2ExecutionService extends Service { MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStart(percent, durationInHours)); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); + mSerialIOThread.sendMessage(new MsgStatusAPSTempBasal()); MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); return true; } @@ -371,8 +379,9 @@ public class DanaRv2ExecutionService extends Service { if (!isConnected()) return false; MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetAPSTempBasalStart(percent)); - mSerialIOThread.sendMessage(new MsgStatusAPSTempBasal()); + mSerialIOThread.sendMessage(new MsgStatusAPS()); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); + mSerialIOThread.sendMessage(new MsgStatusAPSTempBasal()); MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); return true; }