From 97524c47a2db15e5d452a9a540723d63ad3320e3 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 23 Jan 2023 11:26:37 +0100 Subject: [PATCH 1/2] NSCv3: change error response processing --- .../nightscout/sdk/NSAndroidClientImpl.kt | 103 ++++++++++++------ .../UnsuccessfullNightscoutException.kt | 7 +- 2 files changed, 74 insertions(+), 36 deletions(-) diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt index 13c4ecb91f..792b726b10 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/NSAndroidClientImpl.kt @@ -173,18 +173,22 @@ class NSAndroidClientImpl( // because utcOffset is mandatory and cannot be change, try 0 nsSgvV3.utcOffset = 0 return@callWrapper createSvg(nsSgvV3) - } else if (response.code() == 400 && errorResponse?.contains("Field utcOffset cannot be modified by the client") == true) { - // there is different utcOffset than in AAPS and zero + } else if (response.code() == 400 && errorResponse?.contains("cannot be modified by the client") == true) { + // there is different field to field in AAPS // not possible to upload return@callWrapper CreateUpdateResponse( response = response.code(), identifier = null, errorResponse = errorResponse ) - } else if (response.code() in 400..499) - throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message()) - else - throw UnsuccessfullNightscoutException() + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = errorResponse ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(errorResponse ?: response.message()) } override suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) { @@ -213,10 +217,14 @@ class NSAndroidClientImpl( deduplicatedIdentifier = null, lastModified = null ) - } else if (response.code() in 400..499) - throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message()) - else - throw UnsuccessfullNightscoutException() + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = response.errorBody()?.string() ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { @@ -280,10 +288,14 @@ class NSAndroidClientImpl( lastModified = response.body()?.result?.lastModified ) } else throw UnknownResponseNightscoutException() - } else if (response.code() in 400..499) - throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message()) - else - throw UnsuccessfullNightscoutException() + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = response.errorBody()?.string() ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } override suspend fun createTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) { @@ -311,18 +323,22 @@ class NSAndroidClientImpl( // because utcOffset is mandatory and cannot be change, try 0 nsTreatment.utcOffset = 0 return@callWrapper createTreatment(nsTreatment) - } else if (response.code() == 400 && errorResponse?.contains("Field utcOffset cannot be modified by the client") == true) { - // there is different utcOffset than in AAPS and zero + } else if (response.code() == 400 && errorResponse?.contains("cannot be modified by the client") == true) { + // there is different field to field in AAPS // not possible to upload return@callWrapper CreateUpdateResponse( response = response.code(), identifier = null, errorResponse = errorResponse ) - } else if (response.code() in 400..499) - throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message()) - else - throw UnsuccessfullNightscoutException() + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = errorResponse ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(errorResponse ?: response.message()) } override suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) { @@ -351,10 +367,14 @@ class NSAndroidClientImpl( deduplicatedIdentifier = null, lastModified = null ) - } else if (response.code() in 400..499) - throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message()) - else - throw UnsuccessfullNightscoutException() + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = response.errorBody()?.string() ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } override suspend fun getFoods(limit: Long): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { @@ -404,9 +424,14 @@ class NSAndroidClientImpl( lastModified = response.body()?.result?.lastModified ) } else throw UnsuccessfullNightscoutException() - } else { - throw UnsuccessfullNightscoutException() - } + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = response.errorBody()?.string() ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } override suspend fun updateFood(nsFood: NSFood): CreateUpdateResponse = callWrapper(dispatcher) { @@ -432,10 +457,14 @@ class NSAndroidClientImpl( deduplicatedIdentifier = null, lastModified = null ) - } else if (response.code() in 400..499) - throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message()) - else - throw UnsuccessfullNightscoutException() + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = response.errorBody()?.string() ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } override suspend fun createProfileStore(remoteProfileStore: JSONObject): CreateUpdateResponse = callWrapper(dispatcher) { @@ -459,10 +488,14 @@ class NSAndroidClientImpl( lastModified = response.body()?.result?.lastModified ) } else throw UnsuccessfullNightscoutException() - } else if (response.code() in 400..499) - throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message()) - else - throw UnsuccessfullNightscoutException() + } else if (response.code() in 400..499) { + return@callWrapper CreateUpdateResponse( + response = response.code(), + identifier = null, + errorResponse = response.errorBody()?.string() ?: response.message() + ) + } else + throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message()) } override suspend fun getLastProfileStore(): NSAndroidClient.ReadResponse> = callWrapper(dispatcher) { diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/exceptions/UnsuccessfullNightscoutException.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/exceptions/UnsuccessfullNightscoutException.kt index 7cea408407..4ed4be646e 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/exceptions/UnsuccessfullNightscoutException.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/exceptions/UnsuccessfullNightscoutException.kt @@ -1,3 +1,8 @@ package info.nightscout.sdk.exceptions -class UnsuccessfullNightscoutException : NightscoutException() +class UnsuccessfullNightscoutException : NightscoutException { + constructor() : super() + constructor(message: String) : super(message) + constructor(message: String, cause: Throwable) : super(message, cause) + constructor(cause: Throwable?) : super(cause) +} From 6cd32e84fe006bcdfe89a9d1c93aa6e3472f5033 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 23 Jan 2023 13:56:19 +0100 Subject: [PATCH 2/2] BolusWizard: use 100% if no BG available --- .../java/info/nightscout/ui/dialogs/WizardDialog.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ui/src/main/java/info/nightscout/ui/dialogs/WizardDialog.kt b/ui/src/main/java/info/nightscout/ui/dialogs/WizardDialog.kt index fdf6839038..ddc4f91a64 100644 --- a/ui/src/main/java/info/nightscout/ui/dialogs/WizardDialog.kt +++ b/ui/src/main/java/info/nightscout/ui/dialogs/WizardDialog.kt @@ -50,6 +50,7 @@ import info.nightscout.shared.extensions.toVisibility import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.utils.DateUtil +import info.nightscout.shared.utils.T import info.nightscout.ui.R import info.nightscout.ui.databinding.DialogWizardBinding import io.reactivex.rxjava3.disposables.CompositeDisposable @@ -171,8 +172,18 @@ class WizardDialog : DaggerDialogFragment() { ?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher ) + // If there is no BG using % lower that 100% leads to high BGs + // because loop doesn't add missing insulin + var percentage = sp.getInt(info.nightscout.core.utils.R.string.key_boluswizard_percentage, 100).toDouble() + repository.getLastGlucoseValueWrapped().blockingGet().let { + // if last value is older than 6 min or there is no bg + if (it is ValueWrapper.Existing) + if (it.value.timestamp < dateUtil.now() - T.mins(6).msecs()) + percentage = 100.0 + } + if (usePercentage) { - calculatedPercentage = sp.getInt(info.nightscout.core.utils.R.string.key_boluswizard_percentage, 100).toDouble() + calculatedPercentage = percentage binding.correctionInput.setParams(calculatedPercentage, 10.0, 200.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) binding.correctionInput.value = calculatedPercentage binding.correctionUnit.text = "%"