|
|
|
@ -7,7 +7,7 @@ import app.aaps.core.nssdk.exceptions.InvalidAccessTokenException
|
|
|
|
|
import app.aaps.core.nssdk.exceptions.InvalidFormatNightscoutException
|
|
|
|
|
import app.aaps.core.nssdk.exceptions.InvalidParameterNightscoutException
|
|
|
|
|
import app.aaps.core.nssdk.exceptions.UnknownResponseNightscoutException
|
|
|
|
|
import app.aaps.core.nssdk.exceptions.UnsuccessfullNightscoutException
|
|
|
|
|
import app.aaps.core.nssdk.exceptions.UnsuccessfulNightscoutException
|
|
|
|
|
import app.aaps.core.nssdk.interfaces.NSAndroidClient
|
|
|
|
|
import app.aaps.core.nssdk.localmodel.Status
|
|
|
|
|
import app.aaps.core.nssdk.localmodel.devicestatus.NSDeviceStatus
|
|
|
|
@ -108,11 +108,11 @@ class NSAndroidClientImpl(
|
|
|
|
|
|
|
|
|
|
val response = api.lastModified()
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper response.body()?.result ?: throw UnsuccessfullNightscoutException()
|
|
|
|
|
return@callWrapper response.body()?.result ?: throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getSgvs(): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
|
|
|
@ -127,7 +127,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getSgvsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
|
|
|
@ -144,7 +144,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getSgvsNewerThan(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
|
|
|
@ -159,7 +159,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun createSgv(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
@ -197,7 +197,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
errorResponse = errorResponse ?: response.message()
|
|
|
|
|
)
|
|
|
|
|
} else
|
|
|
|
|
throw UnsuccessfullNightscoutException(errorResponse ?: response.message())
|
|
|
|
|
throw UnsuccessfulNightscoutException(errorResponse ?: response.message())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
@ -206,19 +206,11 @@ class NSAndroidClientImpl(
|
|
|
|
|
nsSgvV3.utcOffset = null
|
|
|
|
|
nsSgvV3.date = null
|
|
|
|
|
val remoteEntry = nsSgvV3.toRemoteEntry()
|
|
|
|
|
val identifier = remoteEntry.identifier ?: throw InvalidFormatNightscoutException()
|
|
|
|
|
val identifier = remoteEntry.identifier ?: throw InvalidFormatNightscoutException("Invalid format")
|
|
|
|
|
val response =
|
|
|
|
|
if (nsSgvV3.isValid) api.updateEntry(remoteEntry, identifier)
|
|
|
|
|
else api.deleteEntry(identifier)
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
|
identifier = null,
|
|
|
|
|
isDeduplication = false,
|
|
|
|
|
deduplicatedIdentifier = null,
|
|
|
|
|
lastModified = null
|
|
|
|
|
)
|
|
|
|
|
} else if (response.code() == 404) { // not found
|
|
|
|
|
if (response.isSuccessful || response.code() == 404) { // OK or not found
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
|
identifier = null,
|
|
|
|
@ -233,7 +225,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
errorResponse = response.errorBody()?.string() ?: response.message()
|
|
|
|
|
)
|
|
|
|
|
} else
|
|
|
|
|
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Int): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
|
|
|
@ -248,7 +240,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getTreatmentsModifiedSince(from: Long, limit: Int): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
|
|
|
@ -264,7 +256,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getDeviceStatusModifiedSince(from: Long): List<NSDeviceStatus> = callWrapper(dispatcher) {
|
|
|
|
@ -275,7 +267,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun createDeviceStatus(nsDeviceStatus: NSDeviceStatus): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
@ -291,7 +283,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
deduplicatedIdentifier = response.body()?.deduplicatedIdentifier,
|
|
|
|
|
lastModified = response.body()?.lastModified
|
|
|
|
|
)
|
|
|
|
|
} else throw UnknownResponseNightscoutException()
|
|
|
|
|
} else throw UnknownResponseNightscoutException("Unsuccessful")
|
|
|
|
|
} else return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
|
identifier = null,
|
|
|
|
@ -301,7 +293,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
|
|
|
|
|
override suspend fun createTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
|
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException()
|
|
|
|
|
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException("Invalid format")
|
|
|
|
|
remoteTreatment.app = "AAPS"
|
|
|
|
|
val response = api.createTreatment(remoteTreatment)
|
|
|
|
|
val errorResponse = response.errorBody()?.string()
|
|
|
|
@ -333,7 +325,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
errorResponse = errorResponse ?: response.message()
|
|
|
|
|
)
|
|
|
|
|
} else
|
|
|
|
|
throw UnsuccessfullNightscoutException(errorResponse ?: response.message())
|
|
|
|
|
throw UnsuccessfulNightscoutException(errorResponse ?: response.message())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
@ -341,20 +333,12 @@ class NSAndroidClientImpl(
|
|
|
|
|
// following cannot be updated
|
|
|
|
|
nsTreatment.utcOffset = null
|
|
|
|
|
nsTreatment.date = null
|
|
|
|
|
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException()
|
|
|
|
|
val identifier = remoteTreatment.identifier ?: throw InvalidFormatNightscoutException()
|
|
|
|
|
val remoteTreatment = nsTreatment.toRemoteTreatment() ?: throw InvalidFormatNightscoutException("Invalid format")
|
|
|
|
|
val identifier = remoteTreatment.identifier ?: throw InvalidFormatNightscoutException("Invalid format")
|
|
|
|
|
val response =
|
|
|
|
|
if (nsTreatment.isValid) api.updateTreatment(remoteTreatment, identifier)
|
|
|
|
|
else api.deleteTreatment(identifier)
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
|
identifier = null,
|
|
|
|
|
isDeduplication = false,
|
|
|
|
|
deduplicatedIdentifier = null,
|
|
|
|
|
lastModified = null
|
|
|
|
|
)
|
|
|
|
|
} else if (response.code() == 404) { // not found
|
|
|
|
|
if (response.isSuccessful || response.code() == 404) { // OK or not found
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
|
identifier = null,
|
|
|
|
@ -369,7 +353,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
errorResponse = response.errorBody()?.string() ?: response.message()
|
|
|
|
|
)
|
|
|
|
|
} else
|
|
|
|
|
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getFoods(limit: Int): NSAndroidClient.ReadResponse<List<NSFood>> = callWrapper(dispatcher) {
|
|
|
|
@ -384,7 +368,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -414,7 +398,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
deduplicatedIdentifier = response.body()?.deduplicatedIdentifier,
|
|
|
|
|
lastModified = response.body()?.lastModified
|
|
|
|
|
)
|
|
|
|
|
} else throw UnsuccessfullNightscoutException()
|
|
|
|
|
} else throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
} else if (response.code() in 400..499) {
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
@ -422,25 +406,17 @@ class NSAndroidClientImpl(
|
|
|
|
|
errorResponse = response.errorBody()?.string() ?: response.message()
|
|
|
|
|
)
|
|
|
|
|
} else
|
|
|
|
|
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun updateFood(nsFood: NSFood): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
|
val remoteFood = nsFood.toRemoteFood()
|
|
|
|
|
val identifier = nsFood.identifier ?: throw InvalidFormatNightscoutException()
|
|
|
|
|
val identifier = nsFood.identifier ?: throw InvalidFormatNightscoutException("Invalid format")
|
|
|
|
|
val response =
|
|
|
|
|
if (nsFood.isValid) api.updateFood(remoteFood, identifier)
|
|
|
|
|
else api.deleteFood(identifier)
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
|
identifier = null,
|
|
|
|
|
isDeduplication = false,
|
|
|
|
|
deduplicatedIdentifier = null,
|
|
|
|
|
lastModified = null
|
|
|
|
|
)
|
|
|
|
|
} else if (response.code() == 404) { // not found
|
|
|
|
|
if (response.isSuccessful || response.code() == 404) { // OK or not found
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
|
identifier = null,
|
|
|
|
@ -455,7 +431,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
errorResponse = response.errorBody()?.string() ?: response.message()
|
|
|
|
|
)
|
|
|
|
|
} else
|
|
|
|
|
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun createProfileStore(remoteProfileStore: JSONObject): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
@ -470,7 +446,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
deduplicatedIdentifier = response.body()?.deduplicatedIdentifier,
|
|
|
|
|
lastModified = response.body()?.lastModified
|
|
|
|
|
)
|
|
|
|
|
} else throw UnsuccessfullNightscoutException()
|
|
|
|
|
} else throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
} else if (response.code() in 400..499) {
|
|
|
|
|
return@callWrapper CreateUpdateResponse(
|
|
|
|
|
response = response.code(),
|
|
|
|
@ -478,7 +454,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
errorResponse = response.errorBody()?.string() ?: response.message()
|
|
|
|
|
)
|
|
|
|
|
} else
|
|
|
|
|
throw UnsuccessfullNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
throw UnsuccessfulNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getLastProfileStore(): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
|
|
|
@ -491,7 +467,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getProfileModifiedSince(from: Long): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
|
|
|
@ -504,7 +480,7 @@ class NSAndroidClientImpl(
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
throw UnsuccessfulNightscoutException("Unsuccessful")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private suspend fun <T> callWrapper(dispatcher: CoroutineDispatcher, block: suspend () -> T): T =
|
|
|
|
|