NSCv3: improve error handling
This commit is contained in:
parent
4b0c86546c
commit
a3711f0c0c
10 changed files with 298 additions and 278 deletions
|
@ -3,7 +3,7 @@ package info.nightscout.rx.events
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
class EventNSClientNewLog(val action: String, val logText: String) : Event() {
|
class EventNSClientNewLog(val action: String, val logText: String?) : Event() {
|
||||||
var date = System.currentTimeMillis()
|
var date = System.currentTimeMillis()
|
||||||
|
|
||||||
private var timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
|
private var timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
|
||||||
|
|
|
@ -16,6 +16,7 @@ import info.nightscout.plugins.sync.nsShared.StoreDataForDbImpl
|
||||||
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
||||||
import info.nightscout.rx.bus.RxBus
|
import info.nightscout.rx.bus.RxBus
|
||||||
import info.nightscout.rx.events.EventNSClientNewLog
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
import info.nightscout.sdk.interfaces.NSAndroidClient
|
import info.nightscout.sdk.interfaces.NSAndroidClient
|
||||||
import info.nightscout.sdk.localmodel.entry.NSSgvV3
|
import info.nightscout.sdk.localmodel.entry.NSSgvV3
|
||||||
import info.nightscout.shared.sharedPreferences.SP
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
|
@ -49,6 +50,7 @@ class LoadBgWorker(
|
||||||
return Result.success(workDataOf("Result" to "Load not enabled"))
|
return Result.success(workDataOf("Result" to "Load not enabled"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
||||||
val isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.ENTRIES)
|
val isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.ENTRIES)
|
||||||
val lastLoaded =
|
val lastLoaded =
|
||||||
|
@ -65,7 +67,7 @@ class LoadBgWorker(
|
||||||
nsClientV3Plugin.scheduleIrregularExecution() // Idea is to run after 5 min after last BG
|
nsClientV3Plugin.scheduleIrregularExecution() // Idea is to run after 5 min after last BG
|
||||||
}
|
}
|
||||||
sgvs = response.values
|
sgvs = response.values
|
||||||
aapsLogger.debug("SGVS: $sgvs")
|
aapsLogger.debug(LTag.NSCLIENT, "SGVS: $sgvs")
|
||||||
if (sgvs.isNotEmpty()) {
|
if (sgvs.isNotEmpty()) {
|
||||||
val action = if (isFirstLoad) "RCV-FIRST" else "RCV"
|
val action = if (isFirstLoad) "RCV-FIRST" else "RCV"
|
||||||
rxBus.send(EventNSClientNewLog(action, "${sgvs.size} SVGs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
rxBus.send(EventNSClientNewLog(action, "${sgvs.size} SVGs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
||||||
|
@ -114,6 +116,12 @@ class LoadBgWorker(
|
||||||
.then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())
|
.then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())
|
||||||
.enqueue()
|
.enqueue()
|
||||||
}
|
}
|
||||||
|
} catch (error: Exception) {
|
||||||
|
aapsLogger.error("Error: ", error)
|
||||||
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
|
}
|
||||||
|
|
||||||
return Result.success()
|
return Result.success()
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -15,7 +15,6 @@ import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
import info.nightscout.shared.utils.DateUtil
|
import info.nightscout.shared.utils.DateUtil
|
||||||
import info.nightscout.shared.utils.T
|
import info.nightscout.shared.utils.T
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class LoadDeviceStatusWorker(
|
class LoadDeviceStatusWorker(
|
||||||
|
@ -32,9 +31,7 @@ class LoadDeviceStatusWorker(
|
||||||
|
|
||||||
override suspend fun doWorkAndLog(): Result {
|
override suspend fun doWorkAndLog(): Result {
|
||||||
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
||||||
var ret = Result.success()
|
|
||||||
|
|
||||||
runBlocking {
|
|
||||||
try {
|
try {
|
||||||
val from = dateUtil.now() - T.mins(7).msecs()
|
val from = dateUtil.now() - T.mins(7).msecs()
|
||||||
val deviceStatuses = nsAndroidClient.getDeviceStatusModifiedSince(from)
|
val deviceStatuses = nsAndroidClient.getDeviceStatusModifiedSince(from)
|
||||||
|
@ -54,9 +51,10 @@ class LoadDeviceStatusWorker(
|
||||||
)
|
)
|
||||||
} catch (error: Exception) {
|
} catch (error: Exception) {
|
||||||
aapsLogger.error("Error: ", error)
|
aapsLogger.error("Error: ", error)
|
||||||
ret = Result.failure(workDataOf("Error" to error.toString()))
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return ret
|
return Result.success()
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,10 +13,10 @@ import info.nightscout.plugins.sync.nsShared.StoreDataForDbImpl
|
||||||
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
||||||
import info.nightscout.rx.bus.RxBus
|
import info.nightscout.rx.bus.RxBus
|
||||||
import info.nightscout.rx.events.EventNSClientNewLog
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
import info.nightscout.sdk.localmodel.food.NSFood
|
import info.nightscout.sdk.localmodel.food.NSFood
|
||||||
import info.nightscout.shared.utils.DateUtil
|
import info.nightscout.shared.utils.DateUtil
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class LoadFoodsWorker(
|
class LoadFoodsWorker(
|
||||||
|
@ -36,10 +36,10 @@ class LoadFoodsWorker(
|
||||||
|
|
||||||
// Food database doesn't provide last record modification
|
// Food database doesn't provide last record modification
|
||||||
// Read full collection every 5th attempt
|
// Read full collection every 5th attempt
|
||||||
runBlocking {
|
try {
|
||||||
if (nsClientV3Plugin.lastLoadedSrvModified.collections.foods++ % 5 == 0L) {
|
if (nsClientV3Plugin.lastLoadedSrvModified.collections.foods++ % 5 == 0L) {
|
||||||
val foods: List<NSFood> = nsAndroidClient.getFoods(1000).values
|
val foods: List<NSFood> = nsAndroidClient.getFoods(1000).values
|
||||||
aapsLogger.debug("FOODS: $foods")
|
aapsLogger.debug(LTag.NSCLIENT, "FOODS: $foods")
|
||||||
rxBus.send(EventNSClientNewLog("RCV", "${foods.size} FOODs"))
|
rxBus.send(EventNSClientNewLog("RCV", "${foods.size} FOODs"))
|
||||||
// Schedule processing of fetched data
|
// Schedule processing of fetched data
|
||||||
WorkManager.getInstance(context)
|
WorkManager.getInstance(context)
|
||||||
|
@ -61,7 +61,12 @@ class LoadFoodsWorker(
|
||||||
OneTimeWorkRequest.Builder(LoadProfileStoreWorker::class.java).build()
|
OneTimeWorkRequest.Builder(LoadProfileStoreWorker::class.java).build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
} catch (error: Exception) {
|
||||||
|
aapsLogger.error("Error: ", error)
|
||||||
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result.success()
|
return Result.success()
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,8 +5,10 @@ import androidx.work.WorkerParameters
|
||||||
import androidx.work.workDataOf
|
import androidx.work.workDataOf
|
||||||
import info.nightscout.core.utils.worker.LoggingWorker
|
import info.nightscout.core.utils.worker.LoggingWorker
|
||||||
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
||||||
|
import info.nightscout.rx.bus.RxBus
|
||||||
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class LoadLastModificationWorker(
|
class LoadLastModificationWorker(
|
||||||
|
@ -14,21 +16,20 @@ class LoadLastModificationWorker(
|
||||||
) : LoggingWorker(context, params, Dispatchers.IO) {
|
) : LoggingWorker(context, params, Dispatchers.IO) {
|
||||||
|
|
||||||
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
|
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
|
||||||
|
@Inject lateinit var rxBus: RxBus
|
||||||
|
|
||||||
override suspend fun doWorkAndLog(): Result {
|
override suspend fun doWorkAndLog(): Result {
|
||||||
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
||||||
var ret = Result.success()
|
|
||||||
|
|
||||||
runBlocking {
|
|
||||||
try {
|
try {
|
||||||
val lm = nsAndroidClient.getLastModified()
|
val lm = nsAndroidClient.getLastModified()
|
||||||
nsClientV3Plugin.newestDataOnServer = lm
|
nsClientV3Plugin.newestDataOnServer = lm
|
||||||
aapsLogger.debug("LAST MODIFIED: ${nsClientV3Plugin.newestDataOnServer}")
|
aapsLogger.debug(LTag.NSCLIENT, "LAST MODIFIED: ${nsClientV3Plugin.newestDataOnServer}")
|
||||||
} catch (error: Exception) {
|
} catch (error: Exception) {
|
||||||
aapsLogger.error("Error: ", error)
|
aapsLogger.error(LTag.NSCLIENT, "Error: ", error)
|
||||||
ret = Result.failure(workDataOf("Error" to error.toString()))
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
}
|
}
|
||||||
}
|
return Result.success()
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,10 +13,10 @@ import info.nightscout.interfaces.workflow.WorkerClasses
|
||||||
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
||||||
import info.nightscout.rx.bus.RxBus
|
import info.nightscout.rx.bus.RxBus
|
||||||
import info.nightscout.rx.events.EventNSClientNewLog
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
import info.nightscout.sdk.interfaces.NSAndroidClient
|
import info.nightscout.sdk.interfaces.NSAndroidClient
|
||||||
import info.nightscout.shared.utils.DateUtil
|
import info.nightscout.shared.utils.DateUtil
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
@ -35,19 +35,17 @@ class LoadProfileStoreWorker(
|
||||||
|
|
||||||
override suspend fun doWorkAndLog(): Result {
|
override suspend fun doWorkAndLog(): Result {
|
||||||
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
||||||
var ret = Result.success()
|
|
||||||
|
|
||||||
val lastLoaded = max(nsClientV3Plugin.lastLoadedSrvModified.collections.profile, 0)
|
|
||||||
runBlocking {
|
|
||||||
if ((nsClientV3Plugin.newestDataOnServer?.collections?.profile ?: Long.MAX_VALUE) > lastLoaded)
|
|
||||||
try {
|
try {
|
||||||
|
val lastLoaded = max(nsClientV3Plugin.lastLoadedSrvModified.collections.profile, 0)
|
||||||
|
if ((nsClientV3Plugin.newestDataOnServer?.collections?.profile ?: Long.MAX_VALUE) > lastLoaded) {
|
||||||
val response: NSAndroidClient.ReadResponse<List<JSONObject>> = nsAndroidClient.getLastProfileStore()
|
val response: NSAndroidClient.ReadResponse<List<JSONObject>> = nsAndroidClient.getLastProfileStore()
|
||||||
val profiles = response.values
|
val profiles = response.values
|
||||||
if (profiles.size == 1) {
|
if (profiles.size == 1) {
|
||||||
val profile = profiles[0]
|
val profile = profiles[0]
|
||||||
JsonHelper.safeGetLongAllowNull(profile, "srvModified")?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.profile = it }
|
JsonHelper.safeGetLongAllowNull(profile, "srvModified")?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.profile = it }
|
||||||
nsClientV3Plugin.storeLastLoadedSrvModified()
|
nsClientV3Plugin.storeLastLoadedSrvModified()
|
||||||
aapsLogger.debug("PROFILE: $profile")
|
aapsLogger.debug(LTag.NSCLIENT, "PROFILE: $profile")
|
||||||
rxBus.send(EventNSClientNewLog("RCV", "1 PROFILE from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
rxBus.send(EventNSClientNewLog("RCV", "1 PROFILE from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
||||||
WorkManager.getInstance(context)
|
WorkManager.getInstance(context)
|
||||||
.beginUniqueWork(
|
.beginUniqueWork(
|
||||||
|
@ -67,11 +65,7 @@ class LoadProfileStoreWorker(
|
||||||
OneTimeWorkRequest.Builder(LoadDeviceStatusWorker::class.java).build()
|
OneTimeWorkRequest.Builder(LoadDeviceStatusWorker::class.java).build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (error: Exception) {
|
} else {
|
||||||
aapsLogger.error("Error: ", error)
|
|
||||||
ret = Result.failure(workDataOf("Error" to error.toString()))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
rxBus.send(EventNSClientNewLog("RCV END", "No PROFILE from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
rxBus.send(EventNSClientNewLog("RCV END", "No PROFILE from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
||||||
WorkManager.getInstance(context)
|
WorkManager.getInstance(context)
|
||||||
.enqueueUniqueWork(
|
.enqueueUniqueWork(
|
||||||
|
@ -80,7 +74,12 @@ class LoadProfileStoreWorker(
|
||||||
OneTimeWorkRequest.Builder(LoadDeviceStatusWorker::class.java).build()
|
OneTimeWorkRequest.Builder(LoadDeviceStatusWorker::class.java).build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
} catch (error: Exception) {
|
||||||
|
aapsLogger.error("Error: ", error)
|
||||||
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
}
|
}
|
||||||
return ret
|
|
||||||
|
return Result.success()
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,8 +5,10 @@ import androidx.work.WorkerParameters
|
||||||
import androidx.work.workDataOf
|
import androidx.work.workDataOf
|
||||||
import info.nightscout.core.utils.worker.LoggingWorker
|
import info.nightscout.core.utils.worker.LoggingWorker
|
||||||
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
||||||
|
import info.nightscout.rx.bus.RxBus
|
||||||
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class LoadStatusWorker(
|
class LoadStatusWorker(
|
||||||
|
@ -14,20 +16,19 @@ class LoadStatusWorker(
|
||||||
) : LoggingWorker(context, params, Dispatchers.IO) {
|
) : LoggingWorker(context, params, Dispatchers.IO) {
|
||||||
|
|
||||||
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
|
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
|
||||||
|
@Inject lateinit var rxBus: RxBus
|
||||||
|
|
||||||
override suspend fun doWorkAndLog(): Result {
|
override suspend fun doWorkAndLog(): Result {
|
||||||
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
||||||
var ret = Result.success()
|
|
||||||
|
|
||||||
runBlocking {
|
|
||||||
try {
|
try {
|
||||||
val status = nsAndroidClient.getStatus()
|
val status = nsAndroidClient.getStatus()
|
||||||
aapsLogger.debug("STATUS: $status")
|
aapsLogger.debug(LTag.NSCLIENT, "STATUS: $status")
|
||||||
} catch (error: Exception) {
|
} catch (error: Exception) {
|
||||||
aapsLogger.error("Error: ", error)
|
aapsLogger.error("Error: ", error)
|
||||||
ret = Result.failure(workDataOf("Error" to error.toString()))
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
}
|
}
|
||||||
}
|
return Result.success()
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -14,11 +14,11 @@ import info.nightscout.interfaces.sync.NsClient
|
||||||
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
|
||||||
import info.nightscout.rx.bus.RxBus
|
import info.nightscout.rx.bus.RxBus
|
||||||
import info.nightscout.rx.events.EventNSClientNewLog
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
import info.nightscout.sdk.interfaces.NSAndroidClient
|
import info.nightscout.sdk.interfaces.NSAndroidClient
|
||||||
import info.nightscout.sdk.localmodel.treatment.NSTreatment
|
import info.nightscout.sdk.localmodel.treatment.NSTreatment
|
||||||
import info.nightscout.shared.utils.DateUtil
|
import info.nightscout.shared.utils.DateUtil
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
|
@ -36,28 +36,25 @@ class LoadTreatmentsWorker(
|
||||||
|
|
||||||
override suspend fun doWorkAndLog(): Result {
|
override suspend fun doWorkAndLog(): Result {
|
||||||
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null"))
|
||||||
var ret = Result.success()
|
|
||||||
|
|
||||||
|
try {
|
||||||
val isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.TREATMENTS)
|
val isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.TREATMENTS)
|
||||||
val lastLoaded =
|
val lastLoaded =
|
||||||
if (isFirstLoad) max(nsClientV3Plugin.firstLoadContinueTimestamp.collections.treatments, dateUtil.now() - nsClientV3Plugin.maxAge)
|
if (isFirstLoad) max(nsClientV3Plugin.firstLoadContinueTimestamp.collections.treatments, dateUtil.now() - nsClientV3Plugin.maxAge)
|
||||||
else max(nsClientV3Plugin.lastLoadedSrvModified.collections.treatments, dateUtil.now() - nsClientV3Plugin.maxAge)
|
else max(nsClientV3Plugin.lastLoadedSrvModified.collections.treatments, dateUtil.now() - nsClientV3Plugin.maxAge)
|
||||||
runBlocking {
|
if ((nsClientV3Plugin.newestDataOnServer?.collections?.treatments ?: Long.MAX_VALUE) > lastLoaded) {
|
||||||
if ((nsClientV3Plugin.newestDataOnServer?.collections?.treatments ?: Long.MAX_VALUE) > lastLoaded)
|
|
||||||
try {
|
|
||||||
val treatments: List<NSTreatment>
|
val treatments: List<NSTreatment>
|
||||||
val response: NSAndroidClient.ReadResponse<List<NSTreatment>>?
|
val response: NSAndroidClient.ReadResponse<List<NSTreatment>>?
|
||||||
if (isFirstLoad) {
|
if (isFirstLoad) {
|
||||||
val lastLoadedIso = dateUtil.toISOString(lastLoaded)
|
val lastLoadedIso = dateUtil.toISOString(lastLoaded)
|
||||||
response = nsAndroidClient.getTreatmentsNewerThan(lastLoadedIso, 500)
|
response = nsAndroidClient.getTreatmentsNewerThan(lastLoadedIso, 500)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
response = nsAndroidClient.getTreatmentsModifiedSince(lastLoaded, 500)
|
response = nsAndroidClient.getTreatmentsModifiedSince(lastLoaded, 500)
|
||||||
response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.treatments = it }
|
response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.treatments = it }
|
||||||
nsClientV3Plugin.storeLastLoadedSrvModified()
|
nsClientV3Plugin.storeLastLoadedSrvModified()
|
||||||
}
|
}
|
||||||
treatments = response.values
|
treatments = response.values
|
||||||
aapsLogger.debug("TREATMENTS: $treatments")
|
aapsLogger.debug(LTag.NSCLIENT, "TREATMENTS: $treatments")
|
||||||
if (treatments.isNotEmpty()) {
|
if (treatments.isNotEmpty()) {
|
||||||
val action = if (isFirstLoad) "RCV-FIRST" else "RCV"
|
val action = if (isFirstLoad) "RCV-FIRST" else "RCV"
|
||||||
rxBus.send(EventNSClientNewLog(action, "${treatments.size} TRs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
rxBus.send(EventNSClientNewLog(action, "${treatments.size} TRs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
|
||||||
|
@ -89,11 +86,7 @@ class LoadTreatmentsWorker(
|
||||||
OneTimeWorkRequest.Builder(LoadFoodsWorker::class.java).build()
|
OneTimeWorkRequest.Builder(LoadFoodsWorker::class.java).build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (error: Exception) {
|
} else {
|
||||||
aapsLogger.error("Error: ", error)
|
|
||||||
ret = Result.failure(workDataOf("Error" to error.toString()))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// End first load
|
// End first load
|
||||||
if (isFirstLoad) {
|
if (isFirstLoad) {
|
||||||
nsClientV3Plugin.lastLoadedSrvModified.collections.treatments = lastLoaded
|
nsClientV3Plugin.lastLoadedSrvModified.collections.treatments = lastLoaded
|
||||||
|
@ -108,7 +101,12 @@ class LoadTreatmentsWorker(
|
||||||
OneTimeWorkRequest.Builder(LoadFoodsWorker::class.java).build()
|
OneTimeWorkRequest.Builder(LoadFoodsWorker::class.java).build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
} catch (error: Exception) {
|
||||||
|
aapsLogger.error("Error: ", error)
|
||||||
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
}
|
}
|
||||||
return ret
|
|
||||||
|
return Result.success()
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -12,6 +12,8 @@ import info.nightscout.database.impl.AppRepository
|
||||||
import info.nightscout.interfaces.nsclient.StoreDataForDb
|
import info.nightscout.interfaces.nsclient.StoreDataForDb
|
||||||
import info.nightscout.interfaces.utils.JsonHelper
|
import info.nightscout.interfaces.utils.JsonHelper
|
||||||
import info.nightscout.plugins.sync.nsclientV3.extensions.toFood
|
import info.nightscout.plugins.sync.nsclientV3.extensions.toFood
|
||||||
|
import info.nightscout.rx.bus.RxBus
|
||||||
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
import info.nightscout.rx.logging.LTag
|
import info.nightscout.rx.logging.LTag
|
||||||
import info.nightscout.sdk.localmodel.food.NSFood
|
import info.nightscout.sdk.localmodel.food.NSFood
|
||||||
import info.nightscout.shared.sharedPreferences.SP
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
|
@ -30,15 +32,15 @@ class ProcessFoodWorker(
|
||||||
@Inject lateinit var sp: SP
|
@Inject lateinit var sp: SP
|
||||||
@Inject lateinit var dataWorkerStorage: DataWorkerStorage
|
@Inject lateinit var dataWorkerStorage: DataWorkerStorage
|
||||||
@Inject lateinit var storeDataForDb: StoreDataForDb
|
@Inject lateinit var storeDataForDb: StoreDataForDb
|
||||||
|
@Inject lateinit var rxBus: RxBus
|
||||||
|
|
||||||
override suspend fun doWorkAndLog(): Result {
|
override suspend fun doWorkAndLog(): Result {
|
||||||
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1))
|
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1))
|
||||||
?: return Result.failure(workDataOf("Error" to "missing input data"))
|
?: return Result.failure(workDataOf("Error" to "missing input data"))
|
||||||
aapsLogger.debug(LTag.DATABASE, "Received Food Data: $data")
|
aapsLogger.debug(LTag.DATABASE, "Received Food Data: $data")
|
||||||
|
|
||||||
val ret = Result.success()
|
try {
|
||||||
val foods = mutableListOf<Food>()
|
val foods = mutableListOf<Food>()
|
||||||
|
|
||||||
if (data is JSONArray) {
|
if (data is JSONArray) {
|
||||||
for (index in 0 until data.length()) {
|
for (index in 0 until data.length()) {
|
||||||
val jsonFood: JSONObject = data.getJSONObject(index)
|
val jsonFood: JSONObject = data.getJSONObject(index)
|
||||||
|
@ -68,6 +70,12 @@ class ProcessFoodWorker(
|
||||||
foods += (data[i] as NSFood).toFood()
|
foods += (data[i] as NSFood).toFood()
|
||||||
}
|
}
|
||||||
storeDataForDb.foods.addAll(foods)
|
storeDataForDb.foods.addAll(foods)
|
||||||
return ret
|
} catch (error: Exception) {
|
||||||
|
aapsLogger.error("Error: ", error)
|
||||||
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.success()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,6 @@ import info.nightscout.core.utils.worker.LoggingWorker
|
||||||
import info.nightscout.database.impl.AppRepository
|
import info.nightscout.database.impl.AppRepository
|
||||||
import info.nightscout.interfaces.Config
|
import info.nightscout.interfaces.Config
|
||||||
import info.nightscout.interfaces.Constants
|
import info.nightscout.interfaces.Constants
|
||||||
import info.nightscout.interfaces.XDripBroadcast
|
|
||||||
import info.nightscout.interfaces.logging.UserEntryLogger
|
|
||||||
import info.nightscout.interfaces.nsclient.StoreDataForDb
|
import info.nightscout.interfaces.nsclient.StoreDataForDb
|
||||||
import info.nightscout.interfaces.plugin.ActivePlugin
|
import info.nightscout.interfaces.plugin.ActivePlugin
|
||||||
import info.nightscout.plugins.sync.R
|
import info.nightscout.plugins.sync.R
|
||||||
|
@ -24,6 +22,7 @@ import info.nightscout.plugins.sync.nsclientV3.extensions.toTemporaryBasal
|
||||||
import info.nightscout.plugins.sync.nsclientV3.extensions.toTemporaryTarget
|
import info.nightscout.plugins.sync.nsclientV3.extensions.toTemporaryTarget
|
||||||
import info.nightscout.plugins.sync.nsclientV3.extensions.toTherapyEvent
|
import info.nightscout.plugins.sync.nsclientV3.extensions.toTherapyEvent
|
||||||
import info.nightscout.rx.bus.RxBus
|
import info.nightscout.rx.bus.RxBus
|
||||||
|
import info.nightscout.rx.events.EventNSClientNewLog
|
||||||
import info.nightscout.rx.logging.LTag
|
import info.nightscout.rx.logging.LTag
|
||||||
import info.nightscout.sdk.interfaces.NSAndroidClient
|
import info.nightscout.sdk.interfaces.NSAndroidClient
|
||||||
import info.nightscout.sdk.localmodel.treatment.NSBolus
|
import info.nightscout.sdk.localmodel.treatment.NSBolus
|
||||||
|
@ -54,8 +53,6 @@ class ProcessTreatmentsWorker(
|
||||||
@Inject lateinit var repository: AppRepository
|
@Inject lateinit var repository: AppRepository
|
||||||
@Inject lateinit var activePlugin: ActivePlugin
|
@Inject lateinit var activePlugin: ActivePlugin
|
||||||
@Inject lateinit var rxBus: RxBus
|
@Inject lateinit var rxBus: RxBus
|
||||||
@Inject lateinit var uel: UserEntryLogger
|
|
||||||
@Inject lateinit var xDripBroadcast: XDripBroadcast
|
|
||||||
@Inject lateinit var storeDataForDb: StoreDataForDb
|
@Inject lateinit var storeDataForDb: StoreDataForDb
|
||||||
|
|
||||||
override suspend fun doWorkAndLog(): Result {
|
override suspend fun doWorkAndLog(): Result {
|
||||||
|
@ -63,8 +60,8 @@ class ProcessTreatmentsWorker(
|
||||||
val treatments = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as NSAndroidClient.ReadResponse<List<NSTreatment>>?
|
val treatments = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as NSAndroidClient.ReadResponse<List<NSTreatment>>?
|
||||||
?: return Result.failure(workDataOf("Error" to "missing input data"))
|
?: return Result.failure(workDataOf("Error" to "missing input data"))
|
||||||
|
|
||||||
|
try {
|
||||||
var latestDateInReceivedData: Long = 0
|
var latestDateInReceivedData: Long = 0
|
||||||
val ret = Result.success()
|
|
||||||
for (treatment in treatments.values) {
|
for (treatment in treatments.values) {
|
||||||
aapsLogger.debug(LTag.DATABASE, "Received NS treatment: $treatment")
|
aapsLogger.debug(LTag.DATABASE, "Received NS treatment: $treatment")
|
||||||
val date = treatment.date ?: continue
|
val date = treatment.date ?: continue
|
||||||
|
@ -140,6 +137,11 @@ class ProcessTreatmentsWorker(
|
||||||
}
|
}
|
||||||
activePlugin.activeNsClient?.updateLatestTreatmentReceivedIfNewer(latestDateInReceivedData)
|
activePlugin.activeNsClient?.updateLatestTreatmentReceivedIfNewer(latestDateInReceivedData)
|
||||||
// xDripBroadcast.sendTreatments(treatments)
|
// xDripBroadcast.sendTreatments(treatments)
|
||||||
return ret
|
} catch (error: Exception) {
|
||||||
|
aapsLogger.error("Error: ", error)
|
||||||
|
rxBus.send(EventNSClientNewLog("ERROR", error.localizedMessage))
|
||||||
|
return Result.failure(workDataOf("Error" to error.localizedMessage))
|
||||||
|
}
|
||||||
|
return Result.success()
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue