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 93fef87342..75e830e654 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -346,7 +346,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { try { getDaoBgReadings().update(bgReading); } catch (SQLException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } @@ -1201,6 +1201,15 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { scheduleCareportalEventChange(); } + public CareportalEvent getCareportalEventFromTimestamp(long timestamp) { + try { + return getDaoCareportalEvents().queryForId(timestamp); + } catch (SQLException e) { + log.error("Unhandled exception", e); + } + return null; + } + @Nullable public CareportalEvent getLastCareportalEvent(String event) { try { 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..1ddc4fd4b7 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,25 @@ package info.nightscout.androidaps.plugins.PumpInsight.history; import android.content.Intent; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +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 +124,80 @@ 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)) { + if (MainApp.getDbHelper().getCareportalEventFromTimestamp(date.getTime()) != null) 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(); + } + } + } + + void processOccurenceOfAlertIntent(Intent intent) { + if (SP.getBoolean("insight_automatic_careportal_events", false)) { + Date date = getDateExtra(intent, HistoryBroadcast.EXTRA_EVENT_TIME); + String alertType = intent.getStringExtra(HistoryBroadcast.EXTRA_ALERT_TYPE); + if (MainApp.getDbHelper().getCareportalEventFromTimestamp(date.getTime()) != null) 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", CareportalEvent.NOTE); + data.put("notes", MainApp.instance().getString(getAlertText(alertType))); + NSUpload.uploadCareportalEntryToNS(data); + } catch (JSONException e) { + e.printStackTrace(); + } + } + } + + private int getAlertText(String type) { + if (type.equals("Error6MechanicalError")) return R.string.alert_e6; + if (type.equals("Error7ElectronicError")) return R.string.alert_e7; + if (type.equals("Error10RewindError")) return R.string.alert_e10; + if (type.equals("Error13LanguageError")) return R.string.alert_e13; + if (type.equals("Maintenance20CartridgeNotInserted")) return R.string.alert_m20; + if (type.equals("Maintenance21CartridgeEmpty")) return R.string.alert_m21; + if (type.equals("Maintenance22BatteryEmpty")) return R.string.alert_m22; + if (type.equals("Maintenance23AutomaticOff")) return R.string.alert_m23; + if (type.equals("Maintenance24Occlusion")) return R.string.alert_m24; + if (type.equals("Maintenance25LoantimeOver")) return R.string.alert_m25; + if (type.equals("Maintenance26CartridgeChangeNotCompleted")) return R.string.alert_m26; + if (type.equals("Maintenance27DataDownloadFailed")) return R.string.alert_m27; + if (type.equals("Maintenance28PauseModeTimeout")) return R.string.alert_m28; + if (type.equals("Maintenance29BatteryTypeNotSet")) return R.string.alert_m29; + if (type.equals("Maintenance30CartridgeTypeNotSet")) return R.string.alert_m30; + if (type.equals("Warning31CartridgeLow")) return R.string.alert_w31; + if (type.equals("Warning32BatteryLow")) return R.string.alert_w32; + if (type.equals("Warning33InvalidDateTime")) return R.string.alert_w33; + if (type.equals("Warning34EndOfWarranty")) return R.string.alert_w34; + if (type.equals("Warning36TBRCancelled")) return R.string.alert_w36; + if (type.equals("Warning38BolusCancelled")) return R.string.alert_w38; + if (type.equals("Warning39LoantimeWarning")) return R.string.alert_w39; + return 0; + } } 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..0e254e3341 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,10 @@ 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); + filter.addAction(ACTION_OCCURENCE_OF_ALERT); MainApp.instance().registerReceiver(historyReceiver, filter); } @@ -74,7 +78,6 @@ public class HistoryReceiver { } switch (action) { - case ACTION_SYNC_STARTED: status = SYNCING; break; @@ -93,6 +96,18 @@ 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; + case ACTION_OCCURENCE_OF_ALERT: + intentAdapter.processOccurenceOfAlertIntent(intent); + break; } } }; diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index 96d24af4d4..9b9b940ef9 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -406,6 +406,10 @@ public class NSUpload { } public static void uploadError(String error) { + uploadError(error, new Date()); + } + + public static void uploadError(String error, Date date) { Context context = MainApp.instance().getApplicationContext(); Bundle bundle = new Bundle(); bundle.putString("action", "dbAdd"); @@ -413,7 +417,7 @@ public class NSUpload { JSONObject data = new JSONObject(); try { data.put("eventType", "Announcement"); - data.put("created_at", DateUtil.toISOString(new Date())); + data.put("created_at", DateUtil.toISOString(date)); data.put("enteredBy", SP.getString("careportal_enteredby", MainApp.gs(R.string.app_name))); data.put("notes", error); data.put("isAnnouncement", true); diff --git a/app/src/main/res/values-de/insight_alerts.xml b/app/src/main/res/values-de/insight_alerts.xml new file mode 100644 index 0000000000..886122994d --- /dev/null +++ b/app/src/main/res/values-de/insight_alerts.xml @@ -0,0 +1,27 @@ + + + Warnung W31: Ampulle fast leer + Warnung W32: Batterie fast leer + Warnung W33: Zeit/Datum ungültig + Warnung W34: Garantie abgelaufen + Warnung W36: TBR abgebrochen + Warnung W38: Bolus abgebrochen + Warnung W39: Leihdauer-Warnung + + Wartung M20: Keine Ampulle eingesetzt + Wartung M21: Ampulle leer + Wartung M22: Batterie leer + Wartung M23: Sicherheitsabschaltung + Wartung M24: Verstopfung + Wartung M25: Leihdauer abgelaufen + Wartung M26: Ampullenwechsel nicht abgeschlossen + Wartung M27: Datenübertragung fehlgeschlagen + Wartung M28: Zeitüberschreitung bei Pause + Wartung M29: Batterietyp nicht eingestellt + Wartung M30: Ampullentyp nicht eingestellt + + Fehler E6: Mechanikfehler + Fehler E7: Elektronikfehler + Fehler E10: Fehler beim Zurückfahren + Fehler E13: Fehler in der Sprachanzeige + \ No newline at end of file diff --git a/app/src/main/res/values/insight_alerts.xml b/app/src/main/res/values/insight_alerts.xml new file mode 100644 index 0000000000..d78c448898 --- /dev/null +++ b/app/src/main/res/values/insight_alerts.xml @@ -0,0 +1,27 @@ + + + Warning W31: Cartridge low + Warning W32: Battery low + Warning W33: Invalid time/date + Warning W34: End of warranty + Warning W36: TBR cancelled + Warning W38: Bolus canelled + Warning W39: Loantime warning + + Maintenance M20: Cartridge not inserted + Maintenance M21: Cartridge empty + Maintenance M22: Battery empty + Maintenance M23: Automatic off + Maintenance M24: Occlusion + Maintenance M25: Loantime over - end of operation + Maintenance M26: Cartridge change not completed + Maintenance M27: Data download failed + Maintenance M28: Pause mode timeout + Maintenance M29: Battery type not set + Maintenance M30: Cartridge type not set + + Error E6: Mechanical error + Error E7: Electronic error + Error E10: Rewind error + Error E13: Language error + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 27d352570d..8ac005b04e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -989,6 +989,8 @@ IobCobCalculator Waiting for time synchronization (%d sec) Disconnected (%d m) + Automatic careportal events + Automatically upload insulin, cannula and battery changes and pump alarms to Nightscout openapsmb_max_iob Maximum total IOB OpenAPS can\'t go over [U] This value is called Max IOB in OpenAPS context\nOpenAPS will not add more insulin if current IOB is greater than this value diff --git a/app/src/main/res/xml/pref_insightpump.xml b/app/src/main/res/xml/pref_insightpump.xml index 93d51f1318..aee9976391 100644 --- a/app/src/main/res/xml/pref_insightpump.xml +++ b/app/src/main/res/xml/pref_insightpump.xml @@ -19,4 +19,9 @@ android:key="insight_real_tbr_cancel" android:title="@string/insight_use_real_tbr_cancels" android:summary="@string/insight_actually_cancel_tbr_summary"/> - \ No newline at end of file + + \ No newline at end of file