From 7ba4c19298db1a48c5e4adcf82d24fdd502e91d1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 17 Jun 2022 12:47:40 +0200 Subject: [PATCH] sync BolusCalculatorResult in full mode --- .../nsclient/NSClientAddUpdateWorker.kt | 158 ++++++++++-------- 1 file changed, 92 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt index 5707cd439c..5abed9f9f2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt @@ -85,14 +85,16 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { - uel.log(Action.BOLUS, Sources.NSClient, + uel.log( + Action.BOLUS, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Insulin(it.amount) ) aapsLogger.debug(LTag.DATABASE, "Inserted bolus $it") } result.invalidated.forEach { - uel.log(Action.BOLUS_REMOVED, Sources.NSClient, + uel.log( + Action.BOLUS_REMOVED, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Insulin(it.amount) ) @@ -119,21 +121,24 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { - uel.log(Action.CARBS, Sources.NSClient, + uel.log( + Action.CARBS, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Gram(it.amount.toInt()) ) aapsLogger.debug(LTag.DATABASE, "Inserted carbs $it") } result.invalidated.forEach { - uel.log(Action.CARBS_REMOVED, Sources.NSClient, + uel.log( + Action.CARBS_REMOVED, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Gram(it.amount.toInt()) ) aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") } result.updated.forEach { - uel.log(Action.CARBS, Sources.NSClient, + uel.log( + Action.CARBS, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Gram(it.amount.toInt()) ) @@ -157,8 +162,8 @@ class NSClientAddUpdateWorker( virtualPumpPlugin.fakeDataDetected = true } when { - insulin > 0 || carbs > 0 -> Any() - eventType == TherapyEvent.Type.TEMPORARY_TARGET.text -> + insulin > 0 || carbs > 0 -> Any() + eventType == TherapyEvent.Type.TEMPORARY_TARGET.text -> if (sp.getBoolean(R.string.key_ns_receive_temp_target, false) || config.NSCLIENT) { temporaryTargetFromJson(json)?.let { temporaryTarget -> repository.runTransactionForResult(SyncNsTemporaryTargetTransaction(temporaryTarget)) @@ -169,7 +174,8 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { tt -> - uel.log(Action.TT, Sources.NSClient, + uel.log( + Action.TT, Sources.NSClient, ValueWithUnit.TherapyEventTTReason(tt.reason), ValueWithUnit.fromGlucoseUnit(tt.lowTarget, Constants.MGDL), ValueWithUnit.fromGlucoseUnit(tt.highTarget, Constants.MGDL).takeIf { tt.lowTarget != tt.highTarget }, @@ -178,7 +184,8 @@ class NSClientAddUpdateWorker( aapsLogger.debug(LTag.DATABASE, "Inserted TemporaryTarget $tt") } result.invalidated.forEach { tt -> - uel.log(Action.TT_REMOVED, Sources.NSClient, + uel.log( + Action.TT_REMOVED, Sources.NSClient, ValueWithUnit.TherapyEventTTReason(tt.reason), ValueWithUnit.Mgdl(tt.lowTarget), ValueWithUnit.Mgdl(tt.highTarget).takeIf { tt.lowTarget != tt.highTarget }, @@ -187,7 +194,8 @@ class NSClientAddUpdateWorker( aapsLogger.debug(LTag.DATABASE, "Invalidated TemporaryTarget $tt") } result.ended.forEach { tt -> - uel.log(Action.CANCEL_TT, Sources.NSClient, + uel.log( + Action.CANCEL_TT, Sources.NSClient, ValueWithUnit.TherapyEventTTReason(tt.reason), ValueWithUnit.Mgdl(tt.lowTarget), ValueWithUnit.Mgdl(tt.highTarget).takeIf { tt.lowTarget != tt.highTarget }, @@ -204,7 +212,7 @@ class NSClientAddUpdateWorker( } } ?: aapsLogger.error("Error parsing TT json $json") } - eventType == TherapyEvent.Type.NOTE.text && json.isEffectiveProfileSwitch() -> // replace this by new Type when available in NS + eventType == TherapyEvent.Type.NOTE.text && json.isEffectiveProfileSwitch() -> // replace this by new Type when available in NS if (sp.getBoolean(R.string.key_ns_receive_profile_switch, false) || config.NSCLIENT) { effectiveProfileSwitchFromJson(json, dateUtil)?.let { effectiveProfileSwitch -> repository.runTransactionForResult(SyncNsEffectiveProfileSwitchTransaction(effectiveProfileSwitch)) @@ -215,13 +223,17 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { - uel.log(Action.PROFILE_SWITCH, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp)) + uel.log( + Action.PROFILE_SWITCH, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp) + ) aapsLogger.debug(LTag.DATABASE, "Inserted EffectiveProfileSwitch $it") } result.invalidated.forEach { - uel.log(Action.PROFILE_SWITCH_REMOVED, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp)) + uel.log( + Action.PROFILE_SWITCH_REMOVED, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp) + ) aapsLogger.debug(LTag.DATABASE, "Invalidated EffectiveProfileSwitch $it") } result.updatedNsId.forEach { @@ -230,34 +242,34 @@ class NSClientAddUpdateWorker( } } ?: aapsLogger.error("Error parsing EffectiveProfileSwitch json $json") } - eventType == TherapyEvent.Type.BOLUS_WIZARD.text -> - if (config.NSCLIENT) { - bolusCalculatorResultFromJson(json)?.let { bolusCalculatorResult -> - repository.runTransactionForResult(SyncNsBolusCalculatorResultTransaction(bolusCalculatorResult)) - .doOnError { - aapsLogger.error(LTag.DATABASE, "Error while saving BolusCalculatorResult", it) - ret = Result.failure(workDataOf("Error" to it.toString())) + eventType == TherapyEvent.Type.BOLUS_WIZARD.text -> + bolusCalculatorResultFromJson(json)?.let { bolusCalculatorResult -> + repository.runTransactionForResult(SyncNsBolusCalculatorResultTransaction(bolusCalculatorResult)) + .doOnError { + aapsLogger.error(LTag.DATABASE, "Error while saving BolusCalculatorResult", it) + ret = Result.failure(workDataOf("Error" to it.toString())) + } + .blockingGet() + .also { result -> + result.inserted.forEach { + uel.log( + Action.BOLUS_CALCULATOR_RESULT, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp), + ) + aapsLogger.debug(LTag.DATABASE, "Inserted BolusCalculatorResult $it") } - .blockingGet() - .also { result -> - result.inserted.forEach { - uel.log(Action.BOLUS_CALCULATOR_RESULT, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp), - ) - aapsLogger.debug(LTag.DATABASE, "Inserted BolusCalculatorResult $it") - } - result.invalidated.forEach { - uel.log(Action.BOLUS_CALCULATOR_RESULT_REMOVED, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp), - ) - aapsLogger.debug(LTag.DATABASE, "Invalidated BolusCalculatorResult $it") - } - result.updatedNsId.forEach { - aapsLogger.debug(LTag.DATABASE, "Updated nsId BolusCalculatorResult $it") - } + result.invalidated.forEach { + uel.log( + Action.BOLUS_CALCULATOR_RESULT_REMOVED, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp), + ) + aapsLogger.debug(LTag.DATABASE, "Invalidated BolusCalculatorResult $it") } - } ?: aapsLogger.error("Error parsing BolusCalculatorResult json $json") - } + result.updatedNsId.forEach { + aapsLogger.debug(LTag.DATABASE, "Updated nsId BolusCalculatorResult $it") + } + } + } ?: aapsLogger.error("Error parsing BolusCalculatorResult json $json") eventType == TherapyEvent.Type.CANNULA_CHANGE.text || eventType == TherapyEvent.Type.INSULIN_CHANGE.text || eventType == TherapyEvent.Type.SENSOR_CHANGE.text || @@ -267,7 +279,7 @@ class NSClientAddUpdateWorker( eventType == TherapyEvent.Type.QUESTION.text || eventType == TherapyEvent.Type.EXERCISE.text || eventType == TherapyEvent.Type.NOTE.text || - eventType == TherapyEvent.Type.PUMP_BATTERY_CHANGE.text -> + eventType == TherapyEvent.Type.PUMP_BATTERY_CHANGE.text -> if (sp.getBoolean(R.string.key_ns_receive_therapy_events, false) || config.NSCLIENT) { therapyEventFromJson(json)?.let { therapyEvent -> repository.runTransactionForResult(SyncNsTherapyEventTransaction(therapyEvent)) @@ -285,18 +297,18 @@ class NSClientAddUpdateWorker( result.inserted.forEach { therapyEvent -> uel.log(action, Sources.NSClient, therapyEvent.note ?: "", - ValueWithUnit.Timestamp(therapyEvent.timestamp), - ValueWithUnit.TherapyEventType(therapyEvent.type), - ValueWithUnit.fromGlucoseUnit(therapyEvent.glucose ?:0.0,therapyEvent.glucoseUnit.toString).takeIf { therapyEvent.glucose != null } + ValueWithUnit.Timestamp(therapyEvent.timestamp), + ValueWithUnit.TherapyEventType(therapyEvent.type), + ValueWithUnit.fromGlucoseUnit(therapyEvent.glucose ?: 0.0, therapyEvent.glucoseUnit.toString).takeIf { therapyEvent.glucose != null } ) aapsLogger.debug(LTag.DATABASE, "Inserted TherapyEvent $therapyEvent") } result.invalidated.forEach { therapyEvent -> uel.log(Action.CAREPORTAL_REMOVED, Sources.NSClient, therapyEvent.note ?: "", - ValueWithUnit.Timestamp(therapyEvent.timestamp), - ValueWithUnit.TherapyEventType(therapyEvent.type), - ValueWithUnit.fromGlucoseUnit(therapyEvent.glucose ?:0.0, therapyEvent.glucoseUnit.toString).takeIf { therapyEvent.glucose != null } + ValueWithUnit.Timestamp(therapyEvent.timestamp), + ValueWithUnit.TherapyEventType(therapyEvent.type), + ValueWithUnit.fromGlucoseUnit(therapyEvent.glucose ?: 0.0, therapyEvent.glucoseUnit.toString).takeIf { therapyEvent.glucose != null } ) aapsLogger.debug(LTag.DATABASE, "Invalidated TherapyEvent $therapyEvent") } @@ -309,7 +321,7 @@ class NSClientAddUpdateWorker( } } ?: aapsLogger.error("Error parsing TherapyEvent json $json") } - eventType == TherapyEvent.Type.COMBO_BOLUS.text -> + eventType == TherapyEvent.Type.COMBO_BOLUS.text -> if (buildHelper.isEngineeringMode() && sp.getBoolean(R.string.key_ns_receive_tbr_eb, false) || config.NSCLIENT) { extendedBolusFromJson(json)?.let { extendedBolus -> repository.runTransactionForResult(SyncNsExtendedBolusTransaction(extendedBolus)) @@ -320,7 +332,8 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { - uel.log(Action.EXTENDED_BOLUS, Sources.NSClient, + uel.log( + Action.EXTENDED_BOLUS, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Insulin(it.amount), ValueWithUnit.UnitPerHour(it.rate), @@ -329,7 +342,8 @@ class NSClientAddUpdateWorker( aapsLogger.debug(LTag.DATABASE, "Inserted ExtendedBolus $it") } result.invalidated.forEach { - uel.log(Action.EXTENDED_BOLUS_REMOVED, Sources.NSClient, + uel.log( + Action.EXTENDED_BOLUS_REMOVED, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Insulin(it.amount), ValueWithUnit.UnitPerHour(it.rate), @@ -338,7 +352,8 @@ class NSClientAddUpdateWorker( aapsLogger.debug(LTag.DATABASE, "Invalidated ExtendedBolus $it") } result.ended.forEach { - uel.log(Action.CANCEL_EXTENDED_BOLUS, Sources.NSClient, + uel.log( + Action.CANCEL_EXTENDED_BOLUS, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), ValueWithUnit.Insulin(it.amount), ValueWithUnit.UnitPerHour(it.rate), @@ -355,7 +370,7 @@ class NSClientAddUpdateWorker( } } ?: aapsLogger.error("Error parsing ExtendedBolus json $json") } - eventType == TherapyEvent.Type.TEMPORARY_BASAL.text -> + eventType == TherapyEvent.Type.TEMPORARY_BASAL.text -> if (buildHelper.isEngineeringMode() && sp.getBoolean(R.string.key_ns_receive_tbr_eb, false) || config.NSCLIENT) { temporaryBasalFromJson(json)?.let { temporaryBasal -> repository.runTransactionForResult(SyncNsTemporaryBasalTransaction(temporaryBasal)) @@ -366,7 +381,8 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { - uel.log(Action.TEMP_BASAL, Sources.NSClient, + uel.log( + Action.TEMP_BASAL, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), if (it.isAbsolute) ValueWithUnit.UnitPerHour(it.rate) else ValueWithUnit.Percent(it.rate.toInt()), ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) @@ -374,7 +390,8 @@ class NSClientAddUpdateWorker( aapsLogger.debug(LTag.DATABASE, "Inserted TemporaryBasal $it") } result.invalidated.forEach { - uel.log(Action.TEMP_BASAL_REMOVED, Sources.NSClient, + uel.log( + Action.TEMP_BASAL_REMOVED, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), if (it.isAbsolute) ValueWithUnit.UnitPerHour(it.rate) else ValueWithUnit.Percent(it.rate.toInt()), ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) @@ -382,7 +399,8 @@ class NSClientAddUpdateWorker( aapsLogger.debug(LTag.DATABASE, "Invalidated TemporaryBasal $it") } result.ended.forEach { - uel.log(Action.CANCEL_TEMP_BASAL, Sources.NSClient, + uel.log( + Action.CANCEL_TEMP_BASAL, Sources.NSClient, ValueWithUnit.Timestamp(it.timestamp), if (it.isAbsolute) ValueWithUnit.UnitPerHour(it.rate) else ValueWithUnit.Percent(it.rate.toInt()), ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) @@ -398,7 +416,7 @@ class NSClientAddUpdateWorker( } } ?: aapsLogger.error("Error parsing TemporaryBasal json $json") } - eventType == TherapyEvent.Type.PROFILE_SWITCH.text -> + eventType == TherapyEvent.Type.PROFILE_SWITCH.text -> if (sp.getBoolean(R.string.key_ns_receive_profile_switch, false) || config.NSCLIENT) { profileSwitchFromJson(json, dateUtil, activePlugin)?.let { profileSwitch -> repository.runTransactionForResult(SyncNsProfileSwitchTransaction(profileSwitch)) @@ -409,13 +427,17 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { - uel.log(Action.PROFILE_SWITCH, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp)) + uel.log( + Action.PROFILE_SWITCH, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp) + ) aapsLogger.debug(LTag.DATABASE, "Inserted ProfileSwitch $it") } result.invalidated.forEach { - uel.log(Action.PROFILE_SWITCH_REMOVED, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp)) + uel.log( + Action.PROFILE_SWITCH_REMOVED, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp) + ) aapsLogger.debug(LTag.DATABASE, "Invalidated ProfileSwitch $it") } result.updatedNsId.forEach { @@ -424,7 +446,7 @@ class NSClientAddUpdateWorker( } } ?: aapsLogger.error("Error parsing ProfileSwitch json $json") } - eventType == TherapyEvent.Type.APS_OFFLINE.text -> + eventType == TherapyEvent.Type.APS_OFFLINE.text -> if (sp.getBoolean(R.string.key_ns_receive_offline_event, false) && buildHelper.isEngineeringMode() || config.NSCLIENT) { offlineEventFromJson(json)?.let { offlineEvent -> repository.runTransactionForResult(SyncNsOfflineEventTransaction(offlineEvent)) @@ -435,21 +457,24 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> result.inserted.forEach { oe -> - uel.log(Action.LOOP_CHANGE, Sources.NSClient, + uel.log( + Action.LOOP_CHANGE, Sources.NSClient, ValueWithUnit.OfflineEventReason(oe.reason), ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(oe.duration).toInt()) ) aapsLogger.debug(LTag.DATABASE, "Inserted OfflineEvent $oe") } result.invalidated.forEach { oe -> - uel.log(Action.LOOP_REMOVED, Sources.NSClient, + uel.log( + Action.LOOP_REMOVED, Sources.NSClient, ValueWithUnit.OfflineEventReason(oe.reason), ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(oe.duration).toInt()) ) aapsLogger.debug(LTag.DATABASE, "Invalidated OfflineEvent $oe") } result.ended.forEach { oe -> - uel.log(Action.LOOP_CHANGE, Sources.NSClient, + uel.log( + Action.LOOP_CHANGE, Sources.NSClient, ValueWithUnit.OfflineEventReason(oe.reason), ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(oe.duration).toInt()) ) @@ -472,7 +497,8 @@ class NSClientAddUpdateWorker( val enteredBy = JsonHelper.safeGetString(json, "enteredBy", "") val notes = JsonHelper.safeGetString(json, "notes", "") if (date > now - 15 * 60 * 1000L && notes.isNotEmpty() - && enteredBy != sp.getString("careportal_enteredby", "AndroidAPS")) { + && enteredBy != sp.getString("careportal_enteredby", "AndroidAPS") + ) { val defaultVal = config.NSCLIENT if (sp.getBoolean(R.string.key_ns_announcements, defaultVal)) { val announcement = Notification(Notification.NS_ANNOUNCEMENT, notes, Notification.ANNOUNCEMENT, 60)