Use coroutines for Workers

This commit is contained in:
Milos Kozak 2023-01-10 12:15:04 +01:00
parent b726f149b4
commit 5306e62f91
50 changed files with 348 additions and 198 deletions

View file

@ -14,7 +14,6 @@ import info.nightscout.androidaps.BuildConfig
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.configuration.maintenance.MaintenancePlugin import info.nightscout.configuration.maintenance.MaintenancePlugin
import info.nightscout.core.profile.ProfileSealed import info.nightscout.core.profile.ProfileSealed
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.core.utils.worker.LoggingWorker 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
@ -35,6 +34,7 @@ import info.nightscout.shared.sharedPreferences.SP
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 info.nightscout.ui.widget.Widget import info.nightscout.ui.widget.Widget
import kotlinx.coroutines.Dispatchers
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
@ -42,7 +42,7 @@ import kotlin.math.abs
class KeepAliveWorker( class KeepAliveWorker(
private val context: Context, private val context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var localAlertUtils: LocalAlertUtils @Inject lateinit var localAlertUtils: LocalAlertUtils
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@ -74,7 +74,7 @@ class KeepAliveWorker(
private const val KA_10 = "KeepAlive_10" private const val KA_10 = "KeepAlive_10"
} }
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
aapsLogger.debug(LTag.CORE, "KeepAlive received from: " + inputData.getString("schedule")) aapsLogger.debug(LTag.CORE, "KeepAlive received from: " + inputData.getString("schedule"))
// 15 min interval is WorkManager minimum so schedule another instances to have 5 min interval // 15 min interval is WorkManager minimum so schedule another instances to have 5 min interval

View file

@ -6,11 +6,13 @@ import androidx.work.Data
import androidx.work.ExistingWorkPolicy import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager import androidx.work.WorkManager
import info.nightscout.androidaps.annotations.OpenForTesting
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@OpenForTesting
@Singleton @Singleton
class DataWorkerStorage @Inject constructor( class DataWorkerStorage @Inject constructor(
private val context: Context private val context: Context

View file

@ -1,16 +1,18 @@
package info.nightscout.core.utils.worker package info.nightscout.core.utils.worker
import android.content.Context import android.content.Context
import androidx.work.Worker import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import androidx.work.workDataOf import androidx.work.workDataOf
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import javax.inject.Inject import javax.inject.Inject
abstract class LoggingWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { abstract class LoggingWorker(context: Context, workerParams: WorkerParameters, val dispatcher: CoroutineDispatcher) : CoroutineWorker(context, workerParams) {
@Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var fabricPrivacy: FabricPrivacy
@ -19,10 +21,12 @@ abstract class LoggingWorker(context: Context, workerParams: WorkerParameters) :
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this) (context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
} }
override fun doWork(): Result = override suspend fun doWork(): Result =
try { try {
doWorkAndLog().also { withContext(dispatcher) {
aapsLogger.debug(LTag.WORKER, "Worker result ${it::class.java.simpleName.uppercase()} for ${this::class.java}") doWorkAndLog().also {
aapsLogger.debug(LTag.WORKER, "Worker result ${it::class.java.simpleName.uppercase()} for ${this::class.java}")
}
} }
} catch (e: Exception) { } catch (e: Exception) {
fabricPrivacy.logException(e) fabricPrivacy.logException(e)
@ -30,5 +34,5 @@ abstract class LoggingWorker(context: Context, workerParams: WorkerParameters) :
Result.failure(workDataOf("Error" to e.localizedMessage)) Result.failure(workDataOf("Error" to e.localizedMessage))
} }
abstract fun doWorkAndLog(): Result abstract suspend fun doWorkAndLog(): Result
} }

View file

@ -59,6 +59,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
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 java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.IOException import java.io.IOException
@ -381,7 +382,7 @@ class ImportExportPrefsImpl @Inject constructor(
class CsvExportWorker( class CsvExportWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var rh: ResourceHelper @Inject lateinit var rh: ResourceHelper
@ -391,7 +392,7 @@ class ImportExportPrefsImpl @Inject constructor(
@Inject lateinit var storage: Storage @Inject lateinit var storage: Storage
@Inject lateinit var persistenceLayer: PersistenceLayer @Inject lateinit var persistenceLayer: PersistenceLayer
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val entries = persistenceLayer.getUserEntryFilteredDataFromTime(MidnightTime.calc() - T.days(90).msecs()).blockingGet() val entries = persistenceLayer.getUserEntryFilteredDataFromTime(MidnightTime.calc() - T.days(90).msecs()).blockingGet()
prefFileList.ensureExportDirExists() prefFileList.ensureExportDirExists()
val newFile = prefFileList.newExportCsvFile() val newFile = prefFileList.newExportCsvFile()

View file

@ -71,6 +71,7 @@ import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign import io.reactivex.rxjava3.kotlin.plusAssign
import kotlinx.coroutines.Dispatchers
import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.StringUtils
import org.joda.time.DateTime import org.joda.time.DateTime
import java.text.Normalizer import java.text.Normalizer
@ -197,12 +198,12 @@ class SmsCommunicatorPlugin @Inject constructor(
class SmsCommunicatorWorker( class SmsCommunicatorWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var smsCommunicatorPlugin: SmsCommunicatorPlugin @Inject lateinit var smsCommunicatorPlugin: SmsCommunicatorPlugin
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val bundle = dataWorkerStorage.pickupBundle(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) val bundle = dataWorkerStorage.pickupBundle(inputData.getLong(DataWorkerStorage.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))
val format = bundle.getString("format") val format = bundle.getString("format")

View file

@ -40,6 +40,7 @@ import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.Dispatchers
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
@ -432,7 +433,7 @@ class ProfilePlugin @Inject constructor(
class NSProfileWorker( class NSProfileWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -444,7 +445,7 @@ class ProfilePlugin @Inject constructor(
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
@Inject lateinit var instantiator: Instantiator @Inject lateinit var instantiator: Instantiator
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val profileJson = dataWorkerStorage.pickupJSONObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) val profileJson = dataWorkerStorage.pickupJSONObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))
xDripBroadcast.sendProfile(profileJson) xDripBroadcast.sendProfile(profileJson)

View file

@ -20,6 +20,7 @@ import info.nightscout.interfaces.source.BgSource
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -54,13 +55,13 @@ class AidexPlugin @Inject constructor(
class AidexWorker( class AidexWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var aidexPlugin: AidexPlugin @Inject lateinit var aidexPlugin: AidexPlugin
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!aidexPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled")) if (!aidexPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled"))

View file

@ -36,6 +36,7 @@ import info.nightscout.shared.sharedPreferences.SP
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 info.nightscout.source.activities.RequestDexcomPermissionActivity import info.nightscout.source.activities.RequestDexcomPermissionActivity
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.math.abs import kotlin.math.abs
@ -83,7 +84,7 @@ class DexcomPlugin @Inject constructor(
class DexcomWorker( class DexcomWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var dexcomPlugin: DexcomPlugin @Inject lateinit var dexcomPlugin: DexcomPlugin
@ -94,7 +95,7 @@ class DexcomPlugin @Inject constructor(
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@Inject lateinit var uel: UserEntryLogger @Inject lateinit var uel: UserEntryLogger
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!dexcomPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled")) if (!dexcomPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled"))

View file

@ -22,6 +22,7 @@ import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.Dispatchers
import java.util.Arrays import java.util.Arrays
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -53,7 +54,7 @@ class EversensePlugin @Inject constructor(
class EversenseWorker( class EversenseWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var eversensePlugin: EversensePlugin @Inject lateinit var eversensePlugin: EversensePlugin
@ -62,7 +63,7 @@ class EversensePlugin @Inject constructor(
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!eversensePlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled")) if (!eversensePlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled"))

View file

@ -19,6 +19,7 @@ import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -44,14 +45,14 @@ class GlimpPlugin @Inject constructor(
class GlimpWorker( class GlimpWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var glimpPlugin: GlimpPlugin @Inject lateinit var glimpPlugin: GlimpPlugin
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!glimpPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled")) if (!glimpPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled"))

View file

@ -20,6 +20,7 @@ import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.Dispatchers
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import javax.inject.Inject import javax.inject.Inject
@ -45,7 +46,7 @@ class MM640gPlugin @Inject constructor(
class MM640gWorker( class MM640gWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var mM640gPlugin: MM640gPlugin @Inject lateinit var mM640gPlugin: MM640gPlugin
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@ -54,7 +55,7 @@ class MM640gPlugin @Inject constructor(
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!mM640gPlugin.isEnabled()) return Result.success() if (!mM640gPlugin.isEnabled()) return Result.success()

View file

@ -30,6 +30,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
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 org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import java.security.InvalidParameterException import java.security.InvalidParameterException
@ -85,8 +86,7 @@ class NSClientSourcePlugin @Inject constructor(
class NSClientSourceWorker( class NSClientSourceWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : ) : LoggingWorker(context, params, Dispatchers.IO) {
LoggingWorker(context, params) {
@Inject lateinit var nsClientSourcePlugin: NSClientSourcePlugin @Inject lateinit var nsClientSourcePlugin: NSClientSourcePlugin
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@ -127,7 +127,7 @@ class NSClientSourcePlugin @Inject constructor(
} }
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
val sgvs = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) val sgvs = 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"))

View file

@ -20,6 +20,7 @@ import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import kotlinx.coroutines.Dispatchers
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import javax.inject.Inject import javax.inject.Inject
@ -46,14 +47,14 @@ class PoctechPlugin @Inject constructor(
class PoctechWorker( class PoctechWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var poctechPlugin: PoctechPlugin @Inject lateinit var poctechPlugin: PoctechPlugin
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!poctechPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled")) if (!poctechPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled"))

View file

@ -18,6 +18,7 @@ import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -43,7 +44,7 @@ class TomatoPlugin @Inject constructor(
class TomatoWorker( class TomatoWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var tomatoPlugin: TomatoPlugin @Inject lateinit var tomatoPlugin: TomatoPlugin
@ -52,7 +53,7 @@ class TomatoPlugin @Inject constructor(
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!tomatoPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled")) if (!tomatoPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled"))

View file

@ -20,6 +20,7 @@ import info.nightscout.interfaces.source.XDrip
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -60,13 +61,13 @@ class XdripPlugin @Inject constructor(
class XdripWorker( class XdripWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var xdripPlugin: XdripPlugin @Inject lateinit var xdripPlugin: XdripPlugin
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
if (!xdripPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled")) if (!xdripPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Plugin not enabled"))

View file

@ -61,6 +61,7 @@ import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.Dispatchers
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -123,11 +124,11 @@ class StoreDataForDbImpl @Inject constructor(
class StoreBgWorker( class StoreBgWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
storeDataForDb.storeGlucoseValuesToDb() storeDataForDb.storeGlucoseValuesToDb()
return Result.success() return Result.success()
} }
@ -136,11 +137,11 @@ class StoreDataForDbImpl @Inject constructor(
class StoreFoodWorker( class StoreFoodWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
storeDataForDb.storeFoodsToDb() storeDataForDb.storeFoodsToDb()
return Result.success() return Result.success()
} }

View file

@ -28,12 +28,13 @@ import info.nightscout.rx.AapsSchedulers
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.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class NSClientAddAckWorker( class NSClientAddAckWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@ -43,7 +44,7 @@ class NSClientAddAckWorker(
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val ack = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as NSAddAck? val ack = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as NSAddAck?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -29,12 +29,13 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class NSClientAddUpdateWorker( class NSClientAddUpdateWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var config: Config @Inject lateinit var config: Config
@ -46,7 +47,7 @@ class NSClientAddUpdateWorker(
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val treatments = dataWorkerStorage.pickupJSONArray(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) val treatments = dataWorkerStorage.pickupJSONArray(inputData.getLong(DataWorkerStorage.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -10,19 +10,20 @@ import info.nightscout.interfaces.nsclient.StoreDataForDb
import info.nightscout.plugins.sync.nsclient.data.NSMbg import info.nightscout.plugins.sync.nsclient.data.NSMbg
import info.nightscout.plugins.sync.nsclient.extensions.therapyEventFromNsMbg import info.nightscout.plugins.sync.nsclient.extensions.therapyEventFromNsMbg
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class NSClientMbgWorker( class NSClientMbgWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var config: Config @Inject lateinit var config: Config
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val ret = Result.success() val ret = Result.success()
val acceptNSData = sp.getBoolean(info.nightscout.core.utils.R.string.key_ns_receive_therapy_events, false) || config.NSCLIENT val acceptNSData = sp.getBoolean(info.nightscout.core.utils.R.string.key_ns_receive_therapy_events, false) || config.NSCLIENT

View file

@ -23,12 +23,13 @@ import info.nightscout.plugins.sync.nsclient.acks.NSUpdateAck
import info.nightscout.rx.AapsSchedulers import info.nightscout.rx.AapsSchedulers
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 kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class NSClientUpdateRemoveAckWorker( class NSClientUpdateRemoveAckWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@ -36,7 +37,7 @@ class NSClientUpdateRemoveAckWorker(
@Inject lateinit var dataSyncSelector: DataSyncSelector @Inject lateinit var dataSyncSelector: DataSyncSelector
@Inject lateinit var aapsSchedulers: AapsSchedulers @Inject lateinit var aapsSchedulers: AapsSchedulers
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
var ret = Result.success() var ret = Result.success()
val ack = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as NSUpdateAck? val ack = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as NSUpdateAck?

View file

@ -14,6 +14,7 @@ import androidx.work.WorkManager
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.core.validators.ValidatingEditTextPreference import info.nightscout.core.validators.ValidatingEditTextPreference
import info.nightscout.database.ValueWrapper import info.nightscout.database.ValueWrapper
@ -85,6 +86,7 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@OpenForTesting
@Singleton @Singleton
class NSClientV3Plugin @Inject constructor( class NSClientV3Plugin @Inject constructor(
injector: HasAndroidInjector, injector: HasAndroidInjector,
@ -128,13 +130,13 @@ class NSClientV3Plugin @Inject constructor(
override val status override val status
get() = get() =
when { when {
sp.getBoolean(R.string.key_ns_client_paused, false) -> rh.gs(info.nightscout.core.ui.R.string.paused) sp.getBoolean(R.string.key_ns_client_paused, false) -> rh.gs(info.nightscout.core.ui.R.string.paused)
isAllowed.not() -> blockingReason isAllowed.not() -> blockingReason
nsAndroidClient?.lastStatus == null -> rh.gs(R.string.not_connected) nsAndroidClient?.lastStatus == null -> rh.gs(R.string.not_connected)
workIsRunning(arrayOf(JOB_NAME)) -> rh.gs(R.string.working) workIsRunning(arrayOf(JOB_NAME)) -> rh.gs(R.string.working)
nsAndroidClient?.lastStatus?.apiPermissions?.isFull() == true -> rh.gs(info.nightscout.shared.R.string.connected) nsAndroidClient?.lastStatus?.apiPermissions?.isFull() == true -> rh.gs(info.nightscout.shared.R.string.connected)
nsAndroidClient?.lastStatus?.apiPermissions?.isRead() == true -> rh.gs(R.string.read_only) nsAndroidClient?.lastStatus?.apiPermissions?.isRead() == true -> rh.gs(R.string.read_only)
else -> rh.gs(info.nightscout.core.ui.R.string.unknown) else -> rh.gs(info.nightscout.core.ui.R.string.unknown)
} }
internal var nsAndroidClient: NSAndroidClient? = null internal var nsAndroidClient: NSAndroidClient? = null

View file

@ -9,18 +9,19 @@ import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGUI import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGUI
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 kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
@OpenForTesting @OpenForTesting
class DataSyncWorker( class DataSyncWorker(
context: Context, params: WorkerParameters context: Context, params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var dataSyncSelector: DataSyncSelector @Inject lateinit var dataSyncSelector: DataSyncSelector
@Inject lateinit var activePlugin: ActivePlugin @Inject lateinit var activePlugin: ActivePlugin
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
if (activePlugin.activeNsClient?.hasWritePermission == true) { if (activePlugin.activeNsClient?.hasWritePermission == true) {
rxBus.send(EventNSClientNewLog("UPL", "Start")) rxBus.send(EventNSClientNewLog("UPL", "Start"))
dataSyncSelector.doUpload() dataSyncSelector.doUpload()

View file

@ -19,13 +19,13 @@ 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
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.max import kotlin.math.max
class LoadBgWorker( class LoadBgWorker(
context: Context, params: WorkerParameters context: Context, params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -35,66 +35,55 @@ class LoadBgWorker(
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin @Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
@Inject lateinit var workerClasses: WorkerClasses @Inject lateinit var workerClasses: WorkerClasses
companion object { override suspend fun doWorkAndLog(): Result {
val JOB_NAME: String = this::class.java.simpleName
}
override 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 isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.ENTRIES) val isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.ENTRIES)
val lastLoaded = val lastLoaded =
if (isFirstLoad) max(nsClientV3Plugin.firstLoadContinueTimestamp.collections.entries, dateUtil.now() - nsClientV3Plugin.maxAge) if (isFirstLoad) max(nsClientV3Plugin.firstLoadContinueTimestamp.collections.entries, dateUtil.now() - nsClientV3Plugin.maxAge)
else max(nsClientV3Plugin.lastLoadedSrvModified.collections.entries, dateUtil.now() - nsClientV3Plugin.maxAge) else max(nsClientV3Plugin.lastLoadedSrvModified.collections.entries, dateUtil.now() - nsClientV3Plugin.maxAge)
runBlocking { if ((nsClientV3Plugin.newestDataOnServer?.collections?.entries ?: Long.MAX_VALUE) > lastLoaded) {
if ((nsClientV3Plugin.newestDataOnServer?.collections?.entries ?: Long.MAX_VALUE) > lastLoaded) val sgvs: List<NSSgvV3>
try { val response: NSAndroidClient.ReadResponse<List<NSSgvV3>>?
val sgvs: List<NSSgvV3> if (isFirstLoad) response = nsAndroidClient.getSgvsNewerThan(lastLoaded, 500)
val response: NSAndroidClient.ReadResponse<List<NSSgvV3>>? else {
if (isFirstLoad) response = nsAndroidClient.getSgvsNewerThan(lastLoaded, 500) response = nsAndroidClient.getSgvsModifiedSince(lastLoaded, 500)
else { response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.entries = it }
response = nsAndroidClient.getSgvsModifiedSince(lastLoaded, 500) nsClientV3Plugin.storeLastLoadedSrvModified()
response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.entries = it } nsClientV3Plugin.scheduleIrregularExecution() // Idea is to run after 5 min after last BG
nsClientV3Plugin.storeLastLoadedSrvModified() }
nsClientV3Plugin.scheduleIrregularExecution() // Idea is to run after 5 min after last BG sgvs = response.values
} aapsLogger.debug("SGVS: $sgvs")
sgvs = response.values if (sgvs.isNotEmpty()) {
aapsLogger.debug("SGVS: $sgvs") val action = if (isFirstLoad) "RCV-FIRST" else "RCV"
if (sgvs.isNotEmpty()) { rxBus.send(EventNSClientNewLog(action, "${sgvs.size} SVGs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
val action = if (isFirstLoad) "RCV-FIRST" else "RCV" // Objective0
rxBus.send(EventNSClientNewLog(action, "${sgvs.size} SVGs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}")) sp.putBoolean(info.nightscout.core.utils.R.string.key_objectives_bg_is_available_in_ns, true)
// Objective0 // Schedule processing of fetched data and continue of loading
sp.putBoolean(info.nightscout.core.utils.R.string.key_objectives_bg_is_available_in_ns, true) WorkManager.getInstance(context).beginUniqueWork(
// Schedule processing of fetched data and continue of loading NSClientV3Plugin.JOB_NAME,
WorkManager.getInstance(context).beginUniqueWork( ExistingWorkPolicy.APPEND_OR_REPLACE,
JOB_NAME, OneTimeWorkRequest.Builder(workerClasses.nsClientSourceWorker).setInputData(dataWorkerStorage.storeInputData(sgvs)).build()
ExistingWorkPolicy.APPEND_OR_REPLACE, )
OneTimeWorkRequest.Builder(workerClasses.nsClientSourceWorker).setInputData(dataWorkerStorage.storeInputData(sgvs)).build() // response 304 == Not modified (happens when date > srvModified => bad time on phone or server during upload
) .then(response.code != 304, OneTimeWorkRequest.Builder(LoadBgWorker::class.java).build())
// response 304 == Not modified (happens when date > srvModified => bad time on phone or server during upload .then(response.code == 304, OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())
.then(response.code != 304, OneTimeWorkRequest.Builder(LoadBgWorker::class.java).build()) .enqueue()
.then(response.code == 304, OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) } else {
.enqueue() // End first load
} else { if (isFirstLoad) {
// End first load nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded
if (isFirstLoad) { nsClientV3Plugin.storeLastLoadedSrvModified()
nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded }
nsClientV3Plugin.storeLastLoadedSrvModified() rxBus.send(EventNSClientNewLog("RCV END", "No SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
} WorkManager.getInstance(context)
rxBus.send(EventNSClientNewLog("RCV END", "No SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}")) .beginUniqueWork(
WorkManager.getInstance(context) NSClientV3Plugin.JOB_NAME,
.beginUniqueWork( ExistingWorkPolicy.APPEND_OR_REPLACE,
NSClientV3Plugin.JOB_NAME, OneTimeWorkRequest.Builder(StoreDataForDbImpl.StoreBgWorker::class.java).build()
ExistingWorkPolicy.APPEND_OR_REPLACE, )
OneTimeWorkRequest.Builder(StoreDataForDbImpl.StoreBgWorker::class.java).build() .then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())
) .enqueue()
.then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) }
.enqueue()
}
} catch (error: Exception) {
aapsLogger.error("Error: ", error)
ret = Result.failure(workDataOf("Error" to error.toString()))
} }
else { else {
// End first load // End first load
@ -112,7 +101,6 @@ class LoadBgWorker(
.then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) .then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())
.enqueue() .enqueue()
} }
} return Result.success()
return ret
} }
} }

View file

@ -14,13 +14,14 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventNSClientNewLog 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.runBlocking import kotlinx.coroutines.runBlocking
import javax.inject.Inject import javax.inject.Inject
class LoadDeviceStatusWorker( class LoadDeviceStatusWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -29,7 +30,7 @@ class LoadDeviceStatusWorker(
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@Inject lateinit var nsDeviceStatusHandler: NSDeviceStatusHandler @Inject lateinit var nsDeviceStatusHandler: NSDeviceStatusHandler
override 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() var ret = Result.success()

View file

@ -15,13 +15,14 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventNSClientNewLog import info.nightscout.rx.events.EventNSClientNewLog
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.runBlocking import kotlinx.coroutines.runBlocking
import javax.inject.Inject import javax.inject.Inject
class LoadFoodsWorker( class LoadFoodsWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -30,7 +31,7 @@ class LoadFoodsWorker(
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override 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"))
// Food database doesn't provide last record modification // Food database doesn't provide last record modification

View file

@ -5,16 +5,17 @@ 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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import javax.inject.Inject import javax.inject.Inject
class LoadLastModificationWorker( class LoadLastModificationWorker(
context: Context, params: WorkerParameters context: Context, params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin @Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
override 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() var ret = Result.success()

View file

@ -15,6 +15,7 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventNSClientNewLog import info.nightscout.rx.events.EventNSClientNewLog
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.runBlocking import kotlinx.coroutines.runBlocking
import org.json.JSONObject import org.json.JSONObject
import javax.inject.Inject import javax.inject.Inject
@ -23,7 +24,7 @@ import kotlin.math.max
class LoadProfileStoreWorker( class LoadProfileStoreWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -32,7 +33,7 @@ class LoadProfileStoreWorker(
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@Inject lateinit var workerClasses: WorkerClasses @Inject lateinit var workerClasses: WorkerClasses
override 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() var ret = Result.success()

View file

@ -5,16 +5,17 @@ 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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import javax.inject.Inject import javax.inject.Inject
class LoadStatusWorker( class LoadStatusWorker(
context: Context, params: WorkerParameters context: Context, params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin @Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
override 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() var ret = Result.success()

View file

@ -17,6 +17,7 @@ import info.nightscout.rx.events.EventNSClientNewLog
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.runBlocking import kotlinx.coroutines.runBlocking
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.max import kotlin.math.max
@ -24,7 +25,7 @@ import kotlin.math.max
class LoadTreatmentsWorker( class LoadTreatmentsWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -33,7 +34,7 @@ class LoadTreatmentsWorker(
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override 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() var ret = Result.success()

View file

@ -15,6 +15,7 @@ import info.nightscout.plugins.sync.nsclientV3.extensions.toFood
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
import kotlinx.coroutines.Dispatchers
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import javax.inject.Inject import javax.inject.Inject
@ -22,7 +23,7 @@ import javax.inject.Inject
class ProcessFoodWorker( class ProcessFoodWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var repository: AppRepository @Inject lateinit var repository: AppRepository
@ -30,7 +31,7 @@ class ProcessFoodWorker(
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override 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")

View file

@ -39,12 +39,13 @@ import info.nightscout.sdk.localmodel.treatment.NSTherapyEvent
import info.nightscout.sdk.localmodel.treatment.NSTreatment import info.nightscout.sdk.localmodel.treatment.NSTreatment
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class ProcessTreatmentsWorker( class ProcessTreatmentsWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var config: Config @Inject lateinit var config: Config
@ -57,7 +58,7 @@ class ProcessTreatmentsWorker(
@Inject lateinit var xDripBroadcast: XDripBroadcast @Inject lateinit var xDripBroadcast: XDripBroadcast
@Inject lateinit var storeDataForDb: StoreDataForDb @Inject lateinit var storeDataForDb: StoreDataForDb
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
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"))

View file

@ -1,10 +1,18 @@
package info.nightscout.androidaps package info.nightscout.androidaps
import android.content.Context
import android.util.Log
import androidx.work.Configuration
import androidx.work.testing.SynchronousExecutor
import androidx.work.testing.WorkManagerTestInitHelper
import dagger.android.HasAndroidInjector
import info.nightscout.rx.AapsSchedulers import info.nightscout.rx.AapsSchedulers
import info.nightscout.rx.TestAapsSchedulers import info.nightscout.rx.TestAapsSchedulers
import info.nightscout.rx.logging.AAPSLoggerTest import info.nightscout.rx.logging.AAPSLoggerTest
import org.junit.Before import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.junit.jupiter.MockitoExtension import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings import org.mockito.junit.jupiter.MockitoSettings
@ -13,15 +21,30 @@ import java.util.Locale
@ExtendWith(MockitoExtension::class) @ExtendWith(MockitoExtension::class)
@MockitoSettings(strictness = Strictness.LENIENT) @MockitoSettings(strictness = Strictness.LENIENT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
open class TestBase { open class TestBase {
abstract class ContextWithInjector : Context(), HasAndroidInjector
@Mock lateinit var context: ContextWithInjector
val aapsLogger = AAPSLoggerTest() val aapsLogger = AAPSLoggerTest()
val aapsSchedulers: AapsSchedulers = TestAapsSchedulers() val aapsSchedulers: AapsSchedulers = TestAapsSchedulers()
@Before @BeforeEach
fun setupLocale() { fun setupLocale() {
Locale.setDefault(Locale.ENGLISH) Locale.setDefault(Locale.ENGLISH)
System.setProperty("disableFirebase", "true") System.setProperty("disableFirebase", "true")
// Initialize WorkManager for instrumentation tests.
WorkManagerTestInitHelper.initializeTestWorkManager(
context,
Configuration.Builder()
.setMinimumLoggingLevel(Log.DEBUG)
.setExecutor(SynchronousExecutor())
.build()
)
} }
// Workaround for Kotlin nullability. // Workaround for Kotlin nullability.

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps package info.nightscout.androidaps
import android.content.Context
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.core.extensions.pureProfileFromJson import info.nightscout.core.extensions.pureProfileFromJson
@ -41,7 +40,6 @@ open class TestBaseWithProfile : TestBase() {
@Mock lateinit var iobCobCalculator: IobCobCalculator @Mock lateinit var iobCobCalculator: IobCobCalculator
@Mock lateinit var fabricPrivacy: FabricPrivacy @Mock lateinit var fabricPrivacy: FabricPrivacy
@Mock lateinit var config: Config @Mock lateinit var config: Config
@Mock lateinit var context: Context
@Mock lateinit var sp: SP @Mock lateinit var sp: SP
@Mock lateinit var repository: AppRepository @Mock lateinit var repository: AppRepository
@Mock lateinit var hardLimits: HardLimits @Mock lateinit var hardLimits: HardLimits

View file

@ -1,6 +1,5 @@
package info.nightscout.plugins.sync.nsclient package info.nightscout.plugins.sync.nsclient
import android.content.Context
import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.receivers.ReceiverStatusStore import info.nightscout.interfaces.receivers.ReceiverStatusStore
import info.nightscout.plugins.sync.R import info.nightscout.plugins.sync.R
@ -17,7 +16,6 @@ import org.mockito.Mockito.`when`
class NsClientReceiverDelegateTest : TestBase() { class NsClientReceiverDelegateTest : TestBase() {
@Mock lateinit var context: Context
@Mock lateinit var sp: SP @Mock lateinit var sp: SP
@Mock lateinit var rh: ResourceHelper @Mock lateinit var rh: ResourceHelper
val rxBus = RxBus(aapsSchedulers, aapsLogger) val rxBus = RxBus(aapsSchedulers, aapsLogger)

View file

@ -1,6 +1,5 @@
package info.nightscout.plugins.sync.nsclientV3.workers package info.nightscout.plugins.sync.nsclientV3.workers
import android.content.Context
import androidx.work.ListenableWorker.Result.Success import androidx.work.ListenableWorker.Result.Success
import androidx.work.testing.TestListenableWorkerBuilder import androidx.work.testing.TestListenableWorkerBuilder
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
@ -11,6 +10,8 @@ import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.sync.DataSyncSelector import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.interfaces.sync.NsClient import info.nightscout.interfaces.sync.NsClient
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@ -18,13 +19,11 @@ import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
@ExperimentalCoroutinesApi
internal class DataSyncWorkerTest : TestBase() { internal class DataSyncWorkerTest : TestBase() {
abstract class ContextWithInjector : Context(), HasAndroidInjector
@Mock lateinit var fabricPrivacy: FabricPrivacy @Mock lateinit var fabricPrivacy: FabricPrivacy
@Mock lateinit var dataSyncSelector: DataSyncSelector @Mock lateinit var dataSyncSelector: DataSyncSelector
@Mock lateinit var context: ContextWithInjector
@Mock lateinit var activePlugin: ActivePlugin @Mock lateinit var activePlugin: ActivePlugin
@Mock lateinit var nsClient: NsClient @Mock lateinit var nsClient: NsClient
@Mock lateinit var rxBus: RxBus @Mock lateinit var rxBus: RxBus
@ -51,7 +50,7 @@ internal class DataSyncWorkerTest : TestBase() {
} }
@Test @Test
fun doWorkAndLog() { fun doWorkAndLog() = runTest {
sut = TestListenableWorkerBuilder<DataSyncWorker>(context).build() sut = TestListenableWorkerBuilder<DataSyncWorker>(context).build()
`when`(nsClient.hasWritePermission).thenReturn(false) `when`(nsClient.hasWritePermission).thenReturn(false)
sut.doWorkAndLog() sut.doWorkAndLog()
@ -61,6 +60,5 @@ internal class DataSyncWorkerTest : TestBase() {
val result = sut.doWorkAndLog() val result = sut.doWorkAndLog()
Mockito.verify(dataSyncSelector, Mockito.times(1)).doUpload() Mockito.verify(dataSyncSelector, Mockito.times(1)).doUpload()
Assertions.assertTrue(result is Success) Assertions.assertTrue(result is Success)
} }
} }

View file

@ -0,0 +1,99 @@
package info.nightscout.plugins.sync.nsclientV3.workers
import androidx.work.ListenableWorker
import androidx.work.testing.TestListenableWorkerBuilder
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.core.utils.receivers.DataWorkerStorage
import info.nightscout.database.impl.AppRepository
import info.nightscout.interfaces.Config
import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.receivers.ReceiverStatusStore
import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.interfaces.ui.UiInteraction
import info.nightscout.interfaces.workflow.WorkerClasses
import info.nightscout.plugins.sync.nsclient.NsClientReceiverDelegate
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
import info.nightscout.rx.bus.RxBus
import info.nightscout.sdk.interfaces.NSAndroidClient
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.Mock
import org.mockito.Mockito
@OptIn(ExperimentalCoroutinesApi::class)
internal class LoadBgWorkerTest : TestBase() {
@Mock lateinit var dataWorkerStorage: DataWorkerStorage
@Mock lateinit var workerClasses: WorkerClasses
@Mock lateinit var sp: SP
@Mock lateinit var fabricPrivacy: FabricPrivacy
@Mock lateinit var rxBus: RxBus
@Mock lateinit var dateUtil: DateUtil
@Mock lateinit var nsAndroidClient: NSAndroidClient
@Mock lateinit var rh: ResourceHelper
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var config: Config
@Mock lateinit var uiInteraction: UiInteraction
@Mock lateinit var dataSyncSelector: DataSyncSelector
@Mock lateinit var repository: AppRepository
@Mock lateinit var receiverStatusStore: ReceiverStatusStore
private lateinit var nsClientV3Plugin: NSClientV3Plugin
private lateinit var nsClientReceiverDelegate: NsClientReceiverDelegate
private lateinit var sut: LoadBgWorker
private val injector = HasAndroidInjector {
AndroidInjector {
if (it is LoadBgWorker) {
it.aapsLogger = aapsLogger
it.fabricPrivacy = fabricPrivacy
it.dataWorkerStorage = dataWorkerStorage
it.sp = sp
it.rxBus = rxBus
it.context = context
it.dateUtil = dateUtil
it.nsClientV3Plugin = nsClientV3Plugin
it.workerClasses = workerClasses
}
}
}
@BeforeEach
fun setUp() {
Mockito.`when`(context.applicationContext).thenReturn(context)
Mockito.`when`(context.androidInjector()).thenReturn(injector.androidInjector())
nsClientReceiverDelegate = NsClientReceiverDelegate(rxBus, rh, sp, receiverStatusStore)
nsClientV3Plugin = NSClientV3Plugin(
injector, aapsLogger, aapsSchedulers, rxBus, rh, context, fabricPrivacy, sp, nsClientReceiverDelegate, config, dateUtil, uiInteraction, dataSyncSelector,
profileFunction, repository
)
}
@Test
fun notInitializedAndroidClient() = runTest {
sut = TestListenableWorkerBuilder<LoadBgWorker>(context).build()
val result = sut.doWorkAndLog()
Assertions.assertTrue(result is ListenableWorker.Result.Failure)
}
@Test
fun doWork() = runTest {
nsClientV3Plugin.nsAndroidClient = nsAndroidClient
sut = TestListenableWorkerBuilder<LoadBgWorker>(context).build()
Mockito.`when`(nsAndroidClient.getSgvsNewerThan(anyLong(), anyLong())).thenReturn(NSAndroidClient.ReadResponse(200, 0, emptyList()))
var result = sut.doWorkAndLog()
Assertions.assertTrue(result is ListenableWorker.Result.Success)
}
}

View file

@ -3,11 +3,12 @@ package info.nightscout.workflow
import android.content.Context import android.content.Context
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import info.nightscout.core.utils.worker.LoggingWorker import info.nightscout.core.utils.worker.LoggingWorker
import kotlinx.coroutines.Dispatchers
class DummyWorker( class DummyWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
override fun doWorkAndLog(): Result = Result.success() override suspend fun doWorkAndLog(): Result = Result.success()
} }

View file

@ -9,12 +9,13 @@ import info.nightscout.interfaces.aps.Loop
import info.nightscout.interfaces.iob.IobCobCalculator import info.nightscout.interfaces.iob.IobCobCalculator
import info.nightscout.rx.events.Event import info.nightscout.rx.events.Event
import info.nightscout.rx.events.EventNewBG import info.nightscout.rx.events.EventNewBG
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class InvokeLoopWorker( class InvokeLoopWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var iobCobCalculator: IobCobCalculator @Inject lateinit var iobCobCalculator: IobCobCalculator
@ -31,7 +32,7 @@ class InvokeLoopWorker(
the event causing the calculation is not EventNewBg. the event causing the calculation is not EventNewBg.
<p> <p>
*/ */
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as InvokeLoopData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as InvokeLoopData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -15,12 +15,13 @@ import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
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 javax.inject.Inject import javax.inject.Inject
class LoadBgDataWorker( class LoadBgDataWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@ -57,7 +58,7 @@ class LoadBgDataWorker(
} }
} }
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as LoadBgData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as LoadBgData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -16,20 +16,22 @@ import info.nightscout.interfaces.iob.IobCobCalculator
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class PrepareBasalDataWorker( class PrepareBasalDataWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var rh: ResourceHelper @Inject lateinit var rh: ResourceHelper
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
private var ctx: Context private var ctx: Context
init { init {
ctx = rh.getThemedCtx(context) ctx = rh.getThemedCtx(context)
} }
class PrepareBasalData( class PrepareBasalData(
@ -37,7 +39,7 @@ class PrepareBasalDataWorker(
val overviewData: OverviewData val overviewData: OverviewData
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBasalData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBasalData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -17,12 +17,13 @@ import info.nightscout.interfaces.profile.Profile
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.utils.Round import info.nightscout.interfaces.utils.Round
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class PrepareBgDataWorker( class PrepareBgDataWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var profileFunction: ProfileFunction
@ -35,7 +36,7 @@ class PrepareBgDataWorker(
val overviewData: OverviewData val overviewData: OverviewData
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBgData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBgData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -13,12 +13,13 @@ import info.nightscout.interfaces.iob.IobCobCalculator
import info.nightscout.interfaces.profile.DefaultValueHelper import info.nightscout.interfaces.profile.DefaultValueHelper
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class PrepareBucketedDataWorker( class PrepareBucketedDataWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var profileFunction: ProfileFunction
@ -30,7 +31,7 @@ class PrepareBucketedDataWorker(
val overviewData: OverviewData val overviewData: OverviewData
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBucketedData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBucketedData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -34,6 +34,7 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.max import kotlin.math.max
@ -42,7 +43,7 @@ import kotlin.math.min
class PrepareIobAutosensGraphDataWorker( class PrepareIobAutosensGraphDataWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@ -114,7 +115,7 @@ class PrepareIobAutosensGraphDataWorker(
} }
} }
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareIobAutosensData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareIobAutosensData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -20,6 +20,7 @@ import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import kotlinx.coroutines.Dispatchers
import java.util.Calendar import java.util.Calendar
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.ceil import kotlin.math.ceil
@ -29,7 +30,7 @@ import kotlin.math.min
class PreparePredictionsWorker( class PreparePredictionsWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var injector: HasAndroidInjector @Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var overviewData: OverviewData @Inject lateinit var overviewData: OverviewData
@ -48,7 +49,7 @@ class PreparePredictionsWorker(
val overviewData: OverviewData val overviewData: OverviewData
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PreparePredictionsData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PreparePredictionsData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -18,13 +18,14 @@ import info.nightscout.interfaces.profile.Profile
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.max import kotlin.math.max
class PrepareTemporaryTargetDataWorker( class PrepareTemporaryTargetDataWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var profileFunction: ProfileFunction
@ -33,15 +34,16 @@ class PrepareTemporaryTargetDataWorker(
@Inject lateinit var loop: Loop @Inject lateinit var loop: Loop
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
private var ctx: Context private var ctx: Context
init { init {
ctx = rh.getThemedCtx(context) ctx = rh.getThemedCtx(context)
} }
class PrepareTemporaryTargetData( class PrepareTemporaryTargetData(
val overviewData: OverviewData val overviewData: OverviewData
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareTemporaryTargetData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareTemporaryTargetData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -28,12 +28,13 @@ import info.nightscout.interfaces.utils.Round
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class PrepareTreatmentsDataWorker( class PrepareTreatmentsDataWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var dataWorkerStorage: DataWorkerStorage @Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var profileFunction: ProfileFunction
@ -48,7 +49,7 @@ class PrepareTreatmentsDataWorker(
val overviewData: OverviewData val overviewData: OverviewData
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareTreatmentsData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareTreatmentsData?
?: return Result.failure(workDataOf("Error" to "missing input data")) ?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -8,17 +8,18 @@ import info.nightscout.core.workflow.CalculationWorkflow
import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventUpdateOverviewGraph import info.nightscout.rx.events.EventUpdateOverviewGraph
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class UpdateGraphWorker( class UpdateGraphWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@Inject lateinit var activePlugin: ActivePlugin @Inject lateinit var activePlugin: ActivePlugin
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
if (inputData.getString(CalculationWorkflow.JOB) == CalculationWorkflow.MAIN_CALCULATION) if (inputData.getString(CalculationWorkflow.JOB) == CalculationWorkflow.MAIN_CALCULATION)
activePlugin.activeOverview.overviewBus.send(EventUpdateOverviewGraph("UpdateGraphWorker")) activePlugin.activeOverview.overviewBus.send(EventUpdateOverviewGraph("UpdateGraphWorker"))
else else

View file

@ -7,17 +7,18 @@ import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventUpdateOverviewIobCob import info.nightscout.rx.events.EventUpdateOverviewIobCob
import info.nightscout.rx.events.EventUpdateOverviewSensitivity import info.nightscout.rx.events.EventUpdateOverviewSensitivity
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject import javax.inject.Inject
class UpdateIobCobSensWorker( class UpdateIobCobSensWorker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.IO) {
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@Inject lateinit var activePlugin: ActivePlugin @Inject lateinit var activePlugin: ActivePlugin
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
activePlugin.activeOverview.overviewBus.send(EventUpdateOverviewIobCob("UpdateIobCobSensWorker")) activePlugin.activeOverview.overviewBus.send(EventUpdateOverviewIobCob("UpdateIobCobSensWorker"))
activePlugin.activeOverview.overviewBus.send(EventUpdateOverviewSensitivity("UpdateIobCobSensWorker")) activePlugin.activeOverview.overviewBus.send(EventUpdateOverviewSensitivity("UpdateIobCobSensWorker"))
return Result.success() return Result.success()

View file

@ -7,7 +7,6 @@ import androidx.work.workDataOf
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.core.events.EventIobCalculationProgress import info.nightscout.core.events.EventIobCalculationProgress
import info.nightscout.core.extensions.target import info.nightscout.core.extensions.target
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.core.utils.receivers.DataWorkerStorage import info.nightscout.core.utils.receivers.DataWorkerStorage
import info.nightscout.core.utils.worker.LoggingWorker import info.nightscout.core.utils.worker.LoggingWorker
import info.nightscout.core.workflow.CalculationWorkflow import info.nightscout.core.workflow.CalculationWorkflow
@ -31,6 +30,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
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 java.util.Calendar import java.util.Calendar
import java.util.GregorianCalendar import java.util.GregorianCalendar
import javax.inject.Inject import javax.inject.Inject
@ -42,7 +42,7 @@ import kotlin.math.roundToLong
class IobCobOref1Worker( class IobCobOref1Worker(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -66,7 +66,7 @@ class IobCobOref1Worker(
val cause: Event? val cause: Event?
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as IobCobOref1WorkerData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as IobCobOref1WorkerData?
?: return Result.success(workDataOf("Error" to "missing input data")) ?: return Result.success(workDataOf("Error" to "missing input data"))
@ -85,11 +85,11 @@ class IobCobOref1Worker(
val bucketedData = ads.bucketedData val bucketedData = ads.bucketedData
val autosensDataTable = ads.autosensDataTable val autosensDataTable = ads.autosensDataTable
if (bucketedData == null || bucketedData.size < 3) { if (bucketedData == null || bucketedData.size < 3) {
aapsLogger.debug(LTag.AUTOSENS, {"Aborting calculation thread (No bucketed data available): ${data.reason}"}) aapsLogger.debug(LTag.AUTOSENS) { "Aborting calculation thread (No bucketed data available): ${data.reason}" }
return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.reason}")) return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.reason}"))
} }
val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp) val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp)
aapsLogger.debug(LTag.AUTOSENS, {"Prev data time: " + dateUtil.dateAndTimeString(prevDataTime)}) aapsLogger.debug(LTag.AUTOSENS) { "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime) }
var previous = autosensDataTable[prevDataTime] var previous = autosensDataTable[prevDataTime]
// start from oldest to be able sub cob // start from oldest to be able sub cob
for (i in bucketedData.size - 4 downTo 0) { for (i in bucketedData.size - 4 downTo 0) {
@ -144,12 +144,12 @@ class IobCobOref1Worker(
val hourAgoData = ads.getAutosensDataAtTime(hourAgo) val hourAgoData = ads.getAutosensDataAtTime(hourAgo)
if (hourAgoData != null) { if (hourAgoData != null) {
val initialIndex = autosensDataTable.indexOfKey(hourAgoData.time) val initialIndex = autosensDataTable.indexOfKey(hourAgoData.time)
aapsLogger.debug(LTag.AUTOSENS, { ">>>>> bucketed_data.size()=" + bucketedData.size + " i=" + i + " hourAgoData=" + hourAgoData.toString() }) aapsLogger.debug(LTag.AUTOSENS) { ">>>>> bucketed_data.size()=" + bucketedData.size + " i=" + i + " hourAgoData=" + hourAgoData.toString() }
var past = 1 var past = 1
// try { // try {
while (past < 12) { while (past < 12) {
val ad = autosensDataTable.valueAt(initialIndex + past) val ad = autosensDataTable.valueAt(initialIndex + past)
aapsLogger.debug(LTag.AUTOSENS, { ">>>>> past=" + past + " ad=" + ad?.toString() }) aapsLogger.debug(LTag.AUTOSENS) { ">>>>> past=" + past + " ad=" + ad?.toString() }
/* /*
if (ad == null) { if (ad == null) {
aapsLogger.debug(LTag.AUTOSENS, {autosensDataTable.toString()}) aapsLogger.debug(LTag.AUTOSENS, {autosensDataTable.toString()})
@ -185,7 +185,7 @@ class IobCobOref1Worker(
// break // break
// } // }
} else { } else {
aapsLogger.debug(LTag.AUTOSENS, ">>>>> bucketed_data.size()=" + bucketedData.size + " i=" + i + " hourAgoData=" + "null") aapsLogger.debug(LTag.AUTOSENS) { ">>>>> bucketed_data.size()=${bucketedData.size} i=$i hourAgoData=null" }
} }
} }
val recentCarbTreatments = repository.getCarbsDataFromTimeToTimeExpanded(bgTime - T.mins(5).msecs(), bgTime, true).blockingGet() val recentCarbTreatments = repository.getCarbsDataFromTimeToTimeExpanded(bgTime - T.mins(5).msecs(), bgTime, true).blockingGet()
@ -291,7 +291,7 @@ class IobCobOref1Worker(
// add an extra negative deviation if a high temp target is running and exercise mode is set // add an extra negative deviation if a high temp target is running and exercise mode is set
// TODO AS-FIX // TODO AS-FIX
@Suppress("SimplifyBooleanWithConstants") @Suppress("SimplifyBooleanWithConstants", "KotlinConstantConditions")
if (false && sp.getBoolean(info.nightscout.core.utils.R.string.key_high_temptarget_raises_sensitivity, SMBDefaults.high_temptarget_raises_sensitivity)) { if (false && sp.getBoolean(info.nightscout.core.utils.R.string.key_high_temptarget_raises_sensitivity, SMBDefaults.high_temptarget_raises_sensitivity)) {
val tempTarget = repository.getTemporaryTargetActiveAt(dateUtil.now()).blockingGet() val tempTarget = repository.getTemporaryTargetActiveAt(dateUtil.now()).blockingGet()
if (tempTarget is ValueWrapper.Existing && tempTarget.value.target() >= 100) { if (tempTarget is ValueWrapper.Existing && tempTarget.value.target() >= 100) {
@ -307,14 +307,13 @@ class IobCobOref1Worker(
if (min in 0..4 && hours % 2 == 0) autosensData.extraDeviation.add(0.0) if (min in 0..4 && hours % 2 == 0) autosensData.extraDeviation.add(0.0)
previous = autosensData previous = autosensData
if (bgTime < dateUtil.now()) autosensDataTable.put(bgTime, autosensData) if (bgTime < dateUtil.now()) autosensDataTable.put(bgTime, autosensData)
aapsLogger.debug( aapsLogger.debug(LTag.AUTOSENS) {
LTag.AUTOSENS, "Running detectSensitivity from: " + dateUtil.dateAndTimeString(oldestTimeWithData) + " to: " + dateUtil.dateAndTimeString(bgTime) + " lastDataTime:" + ads.lastDataTime(dateUtil)
{"Running detectSensitivity from: " + dateUtil.dateAndTimeString(oldestTimeWithData) + " to: " + dateUtil.dateAndTimeString(bgTime) + " lastDataTime:" + ads.lastDataTime(dateUtil)} }
)
val sensitivity = activePlugin.activeSensitivity.detectSensitivity(ads, oldestTimeWithData, bgTime) val sensitivity = activePlugin.activeSensitivity.detectSensitivity(ads, oldestTimeWithData, bgTime)
aapsLogger.debug(LTag.AUTOSENS, "Sensitivity result: $sensitivity") aapsLogger.debug(LTag.AUTOSENS, "Sensitivity result: $sensitivity")
autosensData.autosensResult = sensitivity autosensData.autosensResult = sensitivity
aapsLogger.debug(LTag.AUTOSENS, {autosensData.toString()}) aapsLogger.debug(LTag.AUTOSENS) { autosensData.toString() }
} }
data.iobCobCalculator.ads = ads data.iobCobCalculator.ads = ads
Thread { Thread {
@ -323,7 +322,7 @@ class IobCobOref1Worker(
}.start() }.start()
} finally { } finally {
rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause)) rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause))
aapsLogger.debug(LTag.AUTOSENS, {"AUTOSENSDATA thread ended: ${data.reason}"}) aapsLogger.debug(LTag.AUTOSENS) { "AUTOSENSDATA thread ended: ${data.reason}" }
profiler.log(LTag.AUTOSENS, "IobCobOref1Thread", start) profiler.log(LTag.AUTOSENS, "IobCobOref1Thread", start)
} }
return Result.success() return Result.success()

View file

@ -28,6 +28,7 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
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 javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.max import kotlin.math.max
@ -37,7 +38,7 @@ import kotlin.math.roundToLong
class IobCobOrefWorker @Inject internal constructor( class IobCobOrefWorker @Inject internal constructor(
context: Context, context: Context,
params: WorkerParameters params: WorkerParameters
) : LoggingWorker(context, params) { ) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -61,15 +62,15 @@ class IobCobOrefWorker @Inject internal constructor(
val cause: Event? val cause: Event?
) )
override fun doWorkAndLog(): Result { override suspend fun doWorkAndLog(): Result {
val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as IobCobOrefWorkerData? val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as IobCobOrefWorkerData?
?: return Result.success(workDataOf("Error" to "missing input data")) ?: return Result.success(workDataOf("Error" to "missing input data"))
val start = dateUtil.now() val start = dateUtil.now()
try { try {
aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread started: ${data.reason}") aapsLogger.debug(LTag.AUTOSENS) { "AUTOSENSDATA thread started: ${data.reason}" }
if (!profileFunction.isProfileValid("IobCobThread")) { if (!profileFunction.isProfileValid("IobCobThread")) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No profile): ${data.reason}") aapsLogger.debug(LTag.AUTOSENS) { "Aborting calculation thread (No profile): ${data.reason}" }
return Result.success(workDataOf("Error" to "app still initializing")) return Result.success(workDataOf("Error" to "app still initializing"))
} }
//log.debug("Locking calculateSensitivityData"); //log.debug("Locking calculateSensitivityData");
@ -79,17 +80,17 @@ class IobCobOrefWorker @Inject internal constructor(
val bucketedData = ads.bucketedData val bucketedData = ads.bucketedData
val autosensDataTable = ads.autosensDataTable val autosensDataTable = ads.autosensDataTable
if (bucketedData == null || bucketedData.size < 3) { if (bucketedData == null || bucketedData.size < 3) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (No bucketed data available): ${data.reason}") aapsLogger.debug(LTag.AUTOSENS) { "Aborting calculation thread (No bucketed data available): ${data.reason}" }
return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.reason}")) return Result.success(workDataOf("Error" to "Aborting calculation thread (No bucketed data available): ${data.reason}"))
} }
val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp) val prevDataTime = ads.roundUpTime(bucketedData[bucketedData.size - 3].timestamp)
aapsLogger.debug(LTag.AUTOSENS, "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime)) aapsLogger.debug(LTag.AUTOSENS) { "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime) }
var previous = autosensDataTable[prevDataTime] var previous = autosensDataTable[prevDataTime]
// start from oldest to be able sub cob // start from oldest to be able sub cob
for (i in bucketedData.size - 4 downTo 0) { for (i in bucketedData.size - 4 downTo 0) {
rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100 - (100.0 * i / bucketedData.size).toInt(), data.cause)) rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100 - (100.0 * i / bucketedData.size).toInt(), data.cause))
if (isStopped) { if (isStopped) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (trigger): ${data.reason}") aapsLogger.debug(LTag.AUTOSENS) { "Aborting calculation thread (trigger): ${data.reason}" }
return Result.failure(workDataOf("Error" to "Aborting calculation thread (trigger): ${data.reason}")) return Result.failure(workDataOf("Error" to "Aborting calculation thread (trigger): ${data.reason}"))
} }
// check if data already exists // check if data already exists
@ -103,10 +104,10 @@ class IobCobOrefWorker @Inject internal constructor(
} }
val profile = profileFunction.getProfile(bgTime) val profile = profileFunction.getProfile(bgTime)
if (profile == null) { if (profile == null) {
aapsLogger.debug(LTag.AUTOSENS, "Aborting calculation thread (no profile): ${data.reason}") aapsLogger.debug(LTag.AUTOSENS) { "Aborting calculation thread (no profile): ${data.reason}" }
continue // profile not set yet continue // profile not set yet
} }
aapsLogger.debug(LTag.AUTOSENS, "Processing calculation thread: ${data.reason} ($i/${bucketedData.size})") aapsLogger.debug(LTag.AUTOSENS) { "Processing calculation thread: ${data.reason} ($i/${bucketedData.size})" }
val sens = profile.getIsfMgdl(bgTime) val sens = profile.getIsfMgdl(bgTime)
val autosensData = instantiator.provideAutosensDataObject() val autosensData = instantiator.provideAutosensDataObject()
autosensData.time = bgTime autosensData.time = bgTime
@ -138,12 +139,12 @@ class IobCobOrefWorker @Inject internal constructor(
val hourAgoData = ads.getAutosensDataAtTime(hourAgo) val hourAgoData = ads.getAutosensDataAtTime(hourAgo)
if (hourAgoData != null) { if (hourAgoData != null) {
val initialIndex = autosensDataTable.indexOfKey(hourAgoData.time) val initialIndex = autosensDataTable.indexOfKey(hourAgoData.time)
aapsLogger.debug(LTag.AUTOSENS, ">>>>> bucketed_data.size()=" + bucketedData.size + " i=" + i + " hourAgoData=" + hourAgoData.toString()) aapsLogger.debug(LTag.AUTOSENS) { ">>>>> bucketed_data.size()=" + bucketedData.size + " i=" + i + " hourAgoData=" + hourAgoData.toString() }
var past = 1 var past = 1
// try { // try {
while (past < 12) { while (past < 12) {
val ad = autosensDataTable.valueAt(initialIndex + past) val ad = autosensDataTable.valueAt(initialIndex + past)
aapsLogger.debug(LTag.AUTOSENS, ">>>>> past=" + past + " ad=" + ad?.toString()) aapsLogger.debug(LTag.AUTOSENS) { ">>>>> past=" + past + " ad=" + ad?.toString() }
// if (ad == null) { // if (ad == null) {
// aapsLogger.debug(LTag.AUTOSENS, autosensDataTable.toString()) // aapsLogger.debug(LTag.AUTOSENS, autosensDataTable.toString())
// aapsLogger.debug(LTag.AUTOSENS, bucketedData.toString()) // aapsLogger.debug(LTag.AUTOSENS, bucketedData.toString())
@ -177,7 +178,7 @@ class IobCobOrefWorker @Inject internal constructor(
// break // break
// } // }
} else { } else {
aapsLogger.debug(LTag.AUTOSENS, ">>>>> bucketed_data.size()=" + bucketedData.size + " i=" + i + " hourAgoData=" + "null") aapsLogger.debug(LTag.AUTOSENS) { ">>>>> bucketed_data.size()=${bucketedData.size} i=$i hourAgoData=null" }
} }
} }
val recentCarbTreatments = repository.getCarbsDataFromTimeToTimeExpanded(bgTime - T.mins(5).msecs(), bgTime, true).blockingGet() val recentCarbTreatments = repository.getCarbsDataFromTimeToTimeExpanded(bgTime - T.mins(5).msecs(), bgTime, true).blockingGet()
@ -247,12 +248,11 @@ class IobCobOrefWorker @Inject internal constructor(
} }
previous = autosensData previous = autosensData
if (bgTime < dateUtil.now()) autosensDataTable.put(bgTime, autosensData) if (bgTime < dateUtil.now()) autosensDataTable.put(bgTime, autosensData)
aapsLogger.debug( aapsLogger.debug(LTag.AUTOSENS) {
LTag.AUTOSENS, "Running detectSensitivity from: ${dateUtil.dateAndTimeString(oldestTimeWithData)} to: ${dateUtil.dateAndTimeString(bgTime)} lastDataTime:${ads.lastDataTime(dateUtil)}"
"Running detectSensitivity from: " + dateUtil.dateAndTimeString(oldestTimeWithData) + " to: " + dateUtil.dateAndTimeString(bgTime) + " lastDataTime:" + ads.lastDataTime(dateUtil) }
)
val sensitivity = activePlugin.activeSensitivity.detectSensitivity(ads, oldestTimeWithData, bgTime) val sensitivity = activePlugin.activeSensitivity.detectSensitivity(ads, oldestTimeWithData, bgTime)
aapsLogger.debug(LTag.AUTOSENS, "Sensitivity result: $sensitivity") aapsLogger.debug(LTag.AUTOSENS) { "Sensitivity result: $sensitivity" }
autosensData.autosensResult = sensitivity autosensData.autosensResult = sensitivity
aapsLogger.debug(LTag.AUTOSENS, autosensData.toString()) aapsLogger.debug(LTag.AUTOSENS, autosensData.toString())
} }
@ -263,7 +263,7 @@ class IobCobOrefWorker @Inject internal constructor(
}.start() }.start()
} finally { } finally {
rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause)) rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.IOB_COB_OREF, 100, data.cause))
aapsLogger.debug(LTag.AUTOSENS, "AUTOSENSDATA thread ended: ${data.reason}") aapsLogger.debug(LTag.AUTOSENS) { "AUTOSENSDATA thread ended: ${data.reason}" }
profiler.log(LTag.AUTOSENS, "IobCobThread", start) profiler.log(LTag.AUTOSENS, "IobCobThread", start)
} }
return Result.success() return Result.success()