NSCv3: improve comm

This commit is contained in:
Milos Kozak 2023-01-02 22:38:17 +01:00
parent ddba45622c
commit 7ad13882ef
5 changed files with 79 additions and 53 deletions

View file

@ -105,7 +105,6 @@ class NSAndroidClientImpl(
}
}
// TODO: parameters like count?
override suspend fun getSgvs(): List<NSSgvV3> = callWrapper(dispatcher) {
val response = api.getSgvs()
@ -143,27 +142,36 @@ class NSAndroidClientImpl(
val remoteEntry = nsSgvV3.toRemoteEntry()
remoteEntry.app = "AAPS"
val response = api.createEntry(remoteEntry)
if (response.isSuccessful) {
if (response.code() == 200) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = null,
isDeduplication = true,
deduplicatedIdentifier = null,
lastModified = null
)
} else if (response.code() == 201) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = response.body()?.result?.identifier ?: throw UnknownResponseNightscoutException(),
isDeduplication = response.body()?.result?.isDeduplication ?: false,
deduplicatedIdentifier = response.body()?.result?.deduplicatedIdentifier,
lastModified = response.body()?.result?.lastModified
)
} else throw UnsuccessfullNightscoutException()
} else {
throw UnsuccessfullNightscoutException()
}
val errorResponse = response.errorBody()?.string()
if (response.code() == 200) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = null,
isDeduplication = true
)
} else if (response.code() == 201) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = response.body()?.result?.identifier
?: throw UnknownResponseNightscoutException(),
isDeduplication = response.body()?.result?.isDeduplication ?: false,
deduplicatedIdentifier = response.body()?.result?.deduplicatedIdentifier,
lastModified = response.body()?.result?.lastModified
)
} else if (response.code() == 400 && errorResponse?.contains("Bad or missing utcOffset field") == true && nsSgvV3.utcOffset != 0L) {
// Record can be originally uploaded without utcOffset
// 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
// not possible to upload
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = null,
errorResponse = errorResponse
)
} else throw UnknownResponseNightscoutException()
}
override suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
@ -225,28 +233,36 @@ class NSAndroidClientImpl(
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException()
remoteTreatment.app = "AAPS"
val response = api.createTreatment(remoteTreatment)
if (response.isSuccessful) {
if (response.code() == 200) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = null,
isDeduplication = true,
deduplicatedIdentifier = null,
lastModified = null
)
} else if (response.code() == 201) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = response.body()?.result?.identifier
?: throw UnknownResponseNightscoutException(),
isDeduplication = response.body()?.result?.isDeduplication ?: false,
deduplicatedIdentifier = response.body()?.result?.deduplicatedIdentifier,
lastModified = response.body()?.result?.lastModified
)
} else throw UnknownResponseNightscoutException()
} else {
throw UnsuccessfullNightscoutException()
}
val errorResponse = response.errorBody()?.string()
if (response.code() == 200) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = null,
isDeduplication = true
)
} else if (response.code() == 201) {
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = response.body()?.result?.identifier
?: throw UnknownResponseNightscoutException(),
isDeduplication = response.body()?.result?.isDeduplication ?: false,
deduplicatedIdentifier = response.body()?.result?.deduplicatedIdentifier,
lastModified = response.body()?.result?.lastModified
)
} else if (response.code() == 400 && errorResponse?.contains("Bad or missing utcOffset field") == true && nsTreatment.utcOffset != 0L) {
// Record can be originally uploaded without utcOffset
// 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
// not possible to upload
return@callWrapper CreateUpdateResponse(
response = response.code(),
identifier = null,
errorResponse = errorResponse
)
} else throw UnknownResponseNightscoutException()
}
override suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {

View file

@ -5,5 +5,6 @@ class CreateUpdateResponse(
val identifier: String?,
val isDeduplication: Boolean? = false,
val deduplicatedIdentifier: String? = null,
val lastModified: Long? = null
val lastModified: Long? = null,
val errorResponse: String? = null
)

View file

@ -153,6 +153,7 @@ class NSClientPlugin @Inject constructor(
if (activePlugin.activeBgSource is DoingOwnUploadSource) {
preferenceFragment.findPreference<SwitchPreference>(rh.gs(info.nightscout.core.utils.R.string.key_do_ns_upload))?.isVisible = false
}
preferenceFragment.findPreference<SwitchPreference>(rh.gs(R.string.key_ns_client_token))?.isVisible = false
}
override val hasWritePermission: Boolean get() = nsClientService?.hasWriteAuth ?: false

View file

@ -70,6 +70,10 @@ import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
@ -115,6 +119,7 @@ class NSClientV3Plugin @Inject constructor(
}
private val disposable = CompositeDisposable()
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private lateinit var runLoop: Runnable
private val handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
private val listLog: MutableList<EventNSClientNewLog> = ArrayList()
@ -141,7 +146,6 @@ class NSClientV3Plugin @Inject constructor(
internal var firstLoadContinueTimestamp = LastModified(LastModified.Collections()) // timestamp of last fetched data for every collection during initial load
override fun onStart() {
// context.bindService(Intent(context, NSClientService::class.java), mConnection, Context.BIND_AUTO_CREATE)
super.onStart()
lastLoadedSrvModified = Json.decodeFromString(
@ -221,6 +225,7 @@ class NSClientV3Plugin @Inject constructor(
preferenceFragment.findPreference<SwitchPreference>(rh.gs(info.nightscout.core.utils.R.string.key_ns_create_announcements_from_carbs_req))?.isVisible = false
}
preferenceFragment.findPreference<SwitchPreference>(rh.gs(R.string.key_ns_receive_tbr_eb))?.isVisible = config.isEngineeringMode()
preferenceFragment.findPreference<SwitchPreference>(rh.gs(info.nightscout.core.utils.R.string.key_nsclientinternal_api_secret))?.isVisible = false
}
override val hasWritePermission: Boolean get() = nsAndroidClient?.lastStatus?.apiPermissions?.isFull() ?: false
@ -335,7 +340,7 @@ class NSClientV3Plugin @Inject constructor(
is DataSyncSelector.PairGlucoseValue -> dataPair.value.toNSSvgV3()
else -> null
}?.let { data ->
runBlocking {
scope.launch {
try {
val id = if (dataPair.value is TraceableDBEntry) (dataPair.value as TraceableDBEntry).interfaceIDs.nightscoutId else ""
rxBus.send(
@ -354,10 +359,11 @@ class NSClientV3Plugin @Inject constructor(
when (result.response) {
200 -> rxBus.send(EventNSClientNewLog("UPDATED", "OK ${dataPair.value.javaClass.simpleName}"))
201 -> rxBus.send(EventNSClientNewLog("ADDED", "OK ${dataPair.value.javaClass.simpleName} ${result.identifier}"))
400 -> rxBus.send(EventNSClientNewLog("FAIL", "${dataPair.value.javaClass.simpleName} ${result.errorResponse}"))
else -> {
rxBus.send(EventNSClientNewLog("ERROR", "${dataPair.value.javaClass.simpleName} "))
return@runBlocking
return@launch
}
}
when (dataPair) {
@ -387,7 +393,7 @@ class NSClientV3Plugin @Inject constructor(
is DataSyncSelector.PairFood -> dataPair.value.toNSFood()
else -> null
}?.let { data ->
runBlocking {
scope.launch {
try {
val id = if (dataPair.value is TraceableDBEntry) (dataPair.value as TraceableDBEntry).interfaceIDs.nightscoutId else ""
rxBus.send(
@ -406,10 +412,11 @@ class NSClientV3Plugin @Inject constructor(
when (result.response) {
200 -> rxBus.send(EventNSClientNewLog("UPDATED", "OK ${dataPair.value.javaClass.simpleName}"))
201 -> rxBus.send(EventNSClientNewLog("ADDED", "OK ${dataPair.value.javaClass.simpleName} ${result.identifier}"))
400 -> rxBus.send(EventNSClientNewLog("FAIL", "${dataPair.value.javaClass.simpleName} ${result.errorResponse}"))
else -> {
rxBus.send(EventNSClientNewLog("ERROR", "${dataPair.value.javaClass.simpleName} "))
return@runBlocking
return@launch
}
}
when (dataPair) {
@ -465,7 +472,7 @@ class NSClientV3Plugin @Inject constructor(
is DataSyncSelector.PairOfflineEvent -> dataPair.value.toNSOfflineEvent()
else -> null
}?.let { data ->
runBlocking {
scope.launch {
try {
val id = if (dataPair.value is TraceableDBEntry) (dataPair.value as TraceableDBEntry).interfaceIDs.nightscoutId else ""
rxBus.send(
@ -484,10 +491,11 @@ class NSClientV3Plugin @Inject constructor(
when (result.response) {
200 -> rxBus.send(EventNSClientNewLog("UPDATED", "OK ${dataPair.value.javaClass.simpleName}"))
201 -> rxBus.send(EventNSClientNewLog("ADDED", "OK ${dataPair.value.javaClass.simpleName} ${result.identifier}"))
400 -> rxBus.send(EventNSClientNewLog("FAIL", "${dataPair.value.javaClass.simpleName} ${result.errorResponse}"))
else -> {
rxBus.send(EventNSClientNewLog("ERROR", "${dataPair.value.javaClass.simpleName} "))
return@runBlocking
return@launch
}
}
when (dataPair) {

View file

@ -18,7 +18,7 @@ abstract class DiaconnHistoryDatabase : RoomDatabase() {
companion object {
const val VERSION = 1
const val VERSION = 2
fun build(context: Context) =
Room.databaseBuilder(