From 5022a7771172b0bddb2f72a36ea3760fc80e465c Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Sun, 8 Apr 2018 14:31:24 +0200 Subject: [PATCH] Automatic careportal events --- .../history/HistoryIntentAdapter.java | 50 ++++++++++++++++--- .../PumpInsight/history/HistoryReceiver.java | 13 ++++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java index 23f5e5ac3c..7ca901f81d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java @@ -1,22 +1,24 @@ package info.nightscout.androidaps.plugins.PumpInsight.history; import android.content.Intent; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.TDD; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; +import org.json.JSONException; +import org.json.JSONObject; +import sugar.free.sightparser.handling.HistoryBroadcast; import java.util.Date; -import android.database.DatabaseUtils; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.db.DatabaseHelper; -import info.nightscout.androidaps.db.TDD; -import sugar.free.sightparser.handling.HistoryBroadcast; - import static info.nightscout.androidaps.plugins.PumpInsight.history.PumpIdCache.updatePumpSerialNumber; /** * Created by jamorham on 27/01/2018. - * + *

* Parse inbound logbook intents - * */ class HistoryIntentAdapter { @@ -121,4 +123,36 @@ class HistoryIntentAdapter { TDD tdd = new TDD(date.getTime(), bolus, basal, bolus + basal); MainApp.getDbHelper().createOrUpdateTDD(tdd); } + + void processCannulaFilledIntent(Intent intent) { + Date date = getDateExtra(intent, HistoryBroadcast.EXTRA_EVENT_TIME); + uploadCareportalEvent(date, CareportalEvent.SITECHANGE); + } + + void processCartridgeInsertedIntent(Intent intent) { + Date date = getDateExtra(intent, HistoryBroadcast.EXTRA_EVENT_TIME); + uploadCareportalEvent(date, CareportalEvent.INSULINCHANGE); + } + + void processBatteryInsertedIntent(Intent intent) { + Date date = getDateExtra(intent, HistoryBroadcast.EXTRA_EVENT_TIME); + uploadCareportalEvent(date, CareportalEvent.PUMPBATTERYCHANGE); + } + + private void uploadCareportalEvent(Date date, String event) { + if (SP.getBoolean("insight_automatic_careportal_events", false)) { + CareportalEvent careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(event); + if (careportalEvent == null || careportalEvent.date == date.getTime()) return; + try { + JSONObject data = new JSONObject(); + String enteredBy = SP.getString("careportal_enteredby", ""); + if (!enteredBy.equals("")) data.put("enteredBy", enteredBy); + data.put("created_at", DateUtil.toISOString(date)); + data.put("eventType", event); + NSUpload.uploadCareportalEntryToNS(data); + } catch (JSONException e) { + e.printStackTrace(); + } + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryReceiver.java index 0523eddc6e..b1eb736f40 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryReceiver.java @@ -43,6 +43,9 @@ public class HistoryReceiver { filter.addAction(ACTION_SYNC_STARTED); filter.addAction(ACTION_STILL_SYNCING); filter.addAction(ACTION_SYNC_FINISHED); + filter.addAction(ACTION_CANNULA_FILLED); + filter.addAction(ACTION_CARTRIDGE_INSERTED); + filter.addAction(ACTION_BATTERY_INSERTED); MainApp.instance().registerReceiver(historyReceiver, filter); } @@ -74,7 +77,6 @@ public class HistoryReceiver { } switch (action) { - case ACTION_SYNC_STARTED: status = SYNCING; break; @@ -93,6 +95,15 @@ public class HistoryReceiver { case ACTION_DAILY_TOTAL: intentAdapter.processDailyTotalIntent(intent); break; + case ACTION_CANNULA_FILLED: + intentAdapter.processCannulaFilledIntent(intent); + break; + case ACTION_CARTRIDGE_INSERTED: + intentAdapter.processCartridgeInsertedIntent(intent); + break; + case ACTION_BATTERY_INSERTED: + intentAdapter.processBatteryInsertedIntent(intent); + break; } } };