From baac836d648a5107001377205822a6a94a1750b6 Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Sat, 9 Feb 2019 22:14:10 +0100 Subject: [PATCH] Don't read past history events when pump is newly paired. --- .../PumpInsightLocal/LocalInsightPlugin.java | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java index 53fb720132..5fc3ac3f0b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java @@ -1057,11 +1057,7 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con startMessage.setDirection(HistoryReadingDirection.BACKWARD); startMessage.setOffset(0xFFFFFFFF); connectionService.requestMessage(startMessage).await(); - for (int i = 0; i < 50; i++) { - List newEvents = connectionService.requestMessage(new ReadHistoryEventsMessage()).await().getHistoryEvents(); - if (newEvents.size() == 0) break; - historyEvents.addAll(newEvents); - } + historyEvents = connectionService.requestMessage(new ReadHistoryEventsMessage()).await().getHistoryEvents(); } else { StartReadingHistoryMessage startMessage = new StartReadingHistoryMessage(); startMessage.setDirection(HistoryReadingDirection.FORWARD); @@ -1073,7 +1069,15 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con historyEvents.addAll(newEvents); } } - processHistoryEvents(pumpSerial, historyEvents); + Collections.sort(historyEvents); + Collections.reverse(historyEvents); + if (historyOffset != null) processHistoryEvents(pumpSerial, historyEvents); + if (historyEvents.size() > 0) { + historyOffset = new InsightHistoryOffset(); + historyOffset.pumpSerial = pumpSerial; + historyOffset.offset = historyEvents.get(0).getEventPosition(); + MainApp.getDbHelper().createOrUpdate(historyOffset); + } } catch (AppLayerErrorException e) { log.info("Exception while reading history: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); } catch (InsightException e) { @@ -1097,8 +1101,6 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con } private void processHistoryEvents(String serial, List historyEvents) { - Collections.sort(historyEvents); - Collections.reverse(historyEvents); List temporaryBasals = new ArrayList<>(); List pumpStartedEvents = new ArrayList<>(); for (HistoryEvent historyEvent : historyEvents) @@ -1121,12 +1123,6 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con Collections.sort(temporaryBasals, (o1, o2) -> (int) (o1.date - o2.date)); for (TemporaryBasal temporaryBasal : temporaryBasals) TreatmentsPlugin.getPlugin().addToHistoryTempBasal(temporaryBasal); - if (historyEvents.size() > 0) { - InsightHistoryOffset historyOffset = new InsightHistoryOffset(); - historyOffset.pumpSerial = serial; - historyOffset.offset = historyEvents.get(0).getEventPosition(); - MainApp.getDbHelper().createOrUpdate(historyOffset); - } } private boolean processHistoryEvent(String serial, List temporaryBasals, List pumpStartedEvents, HistoryEvent event) {