From a19a5a442bdec8fd00a3d063676da8fac758d075 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 1 Jun 2017 20:52:38 +0200 Subject: [PATCH] some v2 driver tweaking --- .../androidaps/db/DatabaseHelper.java | 44 +++++++++++-------- .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 26 +++++------ .../PumpDanaRv2/comm/MsgHistoryEvents_v2.java | 9 ++++ .../services/DanaRv2ExecutionService.java | 10 +++++ 4 files changed, 56 insertions(+), 33 deletions(-) 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 370d613909..b9e45d4145 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -59,7 +59,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final int DATABASE_VERSION = 7; - private static Long latestTreatmentChange = null; + private static Long earliestDataChange = null; private static final ScheduledExecutorService bgWorker = Executors.newSingleThreadScheduledExecutor(); private static ScheduledFuture scheduledBgPost = null; @@ -184,7 +184,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class); TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); - updateLatestTreatmentChange(0); + updateEarliestDataChange(0); } catch (SQLException e) { e.printStackTrace(); } @@ -209,7 +209,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { try { TableUtils.dropTable(connectionSource, Treatment.class, true); TableUtils.createTableIfNotExists(connectionSource, Treatment.class); - updateLatestTreatmentChange(0); + updateEarliestDataChange(0); } catch (SQLException e) { e.printStackTrace(); } @@ -230,7 +230,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { try { TableUtils.dropTable(connectionSource, TemporaryBasal.class, true); TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class); - updateLatestTreatmentChange(0); + updateEarliestDataChange(0); } catch (SQLException e) { e.printStackTrace(); } @@ -241,7 +241,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { try { TableUtils.dropTable(connectionSource, ExtendedBolus.class, true); TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); - updateLatestTreatmentChange(0); + updateEarliestDataChange(0); } catch (SQLException e) { e.printStackTrace(); } @@ -465,7 +465,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { boolean historyChange = changeAffectingIobCob(treatment); status = getDaoTreatments().createOrUpdate(treatment); if (historyChange) - updateLatestTreatmentChange(treatment.date); + updateEarliestDataChange(treatment.date); } catch (SQLException e) { e.printStackTrace(); } @@ -476,7 +476,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void delete(Treatment treatment) { try { getDaoTreatments().delete(treatment); - updateLatestTreatmentChange(treatment.date); + updateEarliestDataChange(treatment.date); } catch (SQLException e) { e.printStackTrace(); } @@ -540,13 +540,13 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return null; } - void updateLatestTreatmentChange(long newDate) { - if (latestTreatmentChange == null) { - latestTreatmentChange = newDate; + void updateEarliestDataChange(long newDate) { + if (earliestDataChange == null) { + earliestDataChange = newDate; return; } - if (newDate < latestTreatmentChange) { - latestTreatmentChange = newDate; + if (newDate < earliestDataChange) { + earliestDataChange = newDate; } } @@ -556,9 +556,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { log.debug("Firing EventTreatmentChange"); MainApp.bus().post(new EventReloadTreatmentData()); MainApp.bus().post(new EventTreatmentChange()); - if (latestTreatmentChange != null) - MainApp.bus().post(new EventNewHistoryData(latestTreatmentChange)); - latestTreatmentChange = null; + if (earliestDataChange != null) + MainApp.bus().post(new EventNewHistoryData(earliestDataChange)); + earliestDataChange = null; scheduledTratmentPost = null; } } @@ -830,7 +830,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { tempBasal.date = tempBasal.date - tempBasal.date % 1000; try { getDaoTemporaryBasal().createOrUpdate(tempBasal); - updateLatestTreatmentChange(tempBasal.date); + updateEarliestDataChange(tempBasal.date); } catch (SQLException e) { e.printStackTrace(); } @@ -840,7 +840,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void delete(TemporaryBasal tempBasal) { try { getDaoTemporaryBasal().delete(tempBasal); - updateLatestTreatmentChange(tempBasal.date); + updateEarliestDataChange(tempBasal.date); } catch (SQLException e) { e.printStackTrace(); } @@ -893,6 +893,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { log.debug("Firing EventTempBasalChange"); MainApp.bus().post(new EventReloadTempBasalData()); MainApp.bus().post(new EventTempBasalChange()); + if (earliestDataChange != null) + MainApp.bus().post(new EventNewHistoryData(earliestDataChange)); + earliestDataChange = null; scheduledTemBasalsPost = null; } } @@ -1051,7 +1054,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { extendedBolus.date = extendedBolus.date - extendedBolus.date % 1000; try { getDaoExtendedBolus().createOrUpdate(extendedBolus); - updateLatestTreatmentChange(extendedBolus.date); + updateEarliestDataChange(extendedBolus.date); } catch (SQLException e) { e.printStackTrace(); } @@ -1061,7 +1064,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void delete(ExtendedBolus extendedBolus) { try { getDaoExtendedBolus().delete(extendedBolus); - updateLatestTreatmentChange(extendedBolus.date); + updateEarliestDataChange(extendedBolus.date); } catch (SQLException e) { e.printStackTrace(); } @@ -1189,6 +1192,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { log.debug("Firing EventExtendedBolusChange"); MainApp.bus().post(new EventReloadTreatmentData()); MainApp.bus().post(new EventExtendedBolusChange()); + if (earliestDataChange != null) + MainApp.bus().post(new EventNewHistoryData(earliestDataChange)); + earliestDataChange = null; scheduledExtendedBolusPost = null; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index 9b17a3e192..cee70f606d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -287,7 +287,8 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, ConstraintsInte if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { Treatment t = new Treatment(detailedBolusInfo.insulinInterface); boolean connectionOK = false; - if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, new Date().getTime() + detailedBolusInfo.carbTime * 60 * 1000, t); + if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) + connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, new Date().getTime() + detailedBolusInfo.carbTime * 60 * 1000, t); PumpEnactResult result = new PumpEnactResult(); result.success = connectionOK; result.bolusDelivered = t.insulin; @@ -370,22 +371,19 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, ConstraintsInte if (Config.logPumpActions) log.debug("setTempBasalAbsolute: Correct temp basal already set (doLowTemp || doHighTemp)"); return result; - } else { - if (Config.logPumpActions) - log.debug("setTempBasalAbsolute: Stopping temp basal (doLowTemp || doHighTemp)"); - result = cancelTempBasal(); - // Check for proper result - if (!result.success) { - log.error("setTempBasalAbsolute: Failed to stop previous temp basal (doLowTemp || doHighTemp)"); - return result; - } - } - } + } } // Convert duration from minutes to hours if (Config.logPumpActions) log.debug("setTempBasalAbsolute: Setting temp basal " + percentRate + "% for " + durationInMinutes + " mins (doLowTemp || doHighTemp)"); // use special APS temp basal call ... 100+/15min .... 100-/30min - setHighTempBasalPercent(percentRate); + result = setHighTempBasalPercent(percentRate); + if (!result.success) { + log.error("setTempBasalAbsolute: Failed to set hightemp basal"); + return result; + } + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: hightemp basal set ok"); + return result; } // We should never end here log.error("setTempBasalAbsolute: Internal error"); @@ -437,7 +435,7 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, ConstraintsInte } result.enacted = false; result.success = false; - result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly); + result.comment = MainApp.instance().getString(R.string.tempbasaldeliveryerror); log.error("setTempBasalPercent: Failed to set temp basal"); return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java index 2dfc7cb64d..750efa1178 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java @@ -54,21 +54,30 @@ public class MsgHistoryEvents_v2 extends MessageBase { TemporaryBasal temporaryBasal = MainApp.getDbHelper().findTempBasalByTime(datetime.getTime()); if (temporaryBasal != null) { + log.debug("EVENT (" + recordCode + ") " + datetime.toLocaleString() + " Param1: " + param1 + " Param2: " + param2); log.debug("Existing temporaryBasal found. Skipping ..."); + if (datetime.getTime() > lastEventTimeLoaded) + lastEventTimeLoaded = datetime.getTime(); return; } temporaryBasal = new TemporaryBasal(); ExtendedBolus extendedBolus = MainApp.getDbHelper().findExtendedBolusByTime(datetime.getTime()); if (extendedBolus != null) { + log.debug("EVENT (" + recordCode + ") " + datetime.toLocaleString() + " Param1: " + param1 + " Param2: " + param2); log.debug("Existing extendedBolus found. Skipping ..."); + if (datetime.getTime() > lastEventTimeLoaded) + lastEventTimeLoaded = datetime.getTime(); return; } extendedBolus = new ExtendedBolus(); Treatment treatment = MainApp.getDbHelper().findTreatmentByTime(datetime.getTime()); if (treatment != null) { + log.debug("EVENT (" + recordCode + ") " + datetime.toLocaleString() + " Param1: " + param1 + " Param2: " + param2); log.debug("Existing treatment found. Skipping ..."); + if (datetime.getTime() > lastEventTimeLoaded) + lastEventTimeLoaded = datetime.getTime(); return; } DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); 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 2d1f8d9739..5435280758 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 @@ -328,6 +328,11 @@ public class DanaRv2ExecutionService extends Service { public boolean tempBasal(int percent, int durationInHours) { connect("tempBasal"); if (!isConnected()) return false; + if (danaRPump.isTempBasalInProgress) { + MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.stoppingtempbasal))); + mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); + waitMsec(500); + } MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStart(percent, durationInHours)); mSerialIOThread.sendMessage(new MsgStatusTempBasal_v2()); @@ -339,6 +344,11 @@ public class DanaRv2ExecutionService extends Service { public boolean highTempBasal(int percent) { connect("highTempBasal"); if (!isConnected()) return false; + if (danaRPump.isTempBasalInProgress) { + MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.stoppingtempbasal))); + mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); + waitMsec(500); + } MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetAPSTempBasalStart_v2(percent)); mSerialIOThread.sendMessage(new MsgStatusTempBasal_v2());