AndroidAPS/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.kt

78 lines
4.1 KiB
Kotlin
Raw Normal View History

2020-03-31 10:10:48 +02:00
package info.nightscout.androidaps.receivers
import android.content.Context
import android.content.Intent
2021-01-30 22:34:05 +01:00
import android.provider.Telephony
import androidx.work.Data
import androidx.work.OneTimeWorkRequest
import dagger.android.DaggerBroadcastReceiver
2020-03-31 10:10:48 +02:00
import info.nightscout.androidaps.logging.AAPSLogger
2021-01-30 22:34:05 +01:00
import info.nightscout.androidaps.logging.BundleLogger
2020-03-31 10:10:48 +02:00
import info.nightscout.androidaps.logging.LTag
2021-01-30 22:34:05 +01:00
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin
import info.nightscout.androidaps.plugins.source.*
import info.nightscout.androidaps.services.Intents
import info.nightscout.androidaps.utils.extensions.copyDouble
import info.nightscout.androidaps.utils.extensions.copyInt
import info.nightscout.androidaps.utils.extensions.copyLong
import info.nightscout.androidaps.utils.extensions.copyString
2020-03-31 10:10:48 +02:00
import javax.inject.Inject
2021-01-30 22:34:05 +01:00
open class DataReceiver : DaggerBroadcastReceiver() {
2020-03-31 10:10:48 +02:00
@Inject lateinit var aapsLogger: AAPSLogger
2021-03-20 12:30:57 +01:00
@Inject lateinit var dataWorker: DataWorker
2021-01-30 22:34:05 +01:00
2020-03-31 10:10:48 +02:00
override fun onReceive(context: Context, intent: Intent) {
2021-01-30 22:34:05 +01:00
super.onReceive(context, intent)
val bundle = intent.extras ?: return
aapsLogger.debug(LTag.DATABASE, "onReceive ${intent.action} ${BundleLogger.log(bundle)}")
2021-01-30 22:34:05 +01:00
when (intent.action) {
2021-03-19 18:49:34 +01:00
Intents.ACTION_NEW_BG_ESTIMATE ->
2021-01-30 22:34:05 +01:00
OneTimeWorkRequest.Builder(XdripPlugin.XdripWorker::class.java)
2021-03-20 12:30:57 +01:00
.setInputData(dataWorker.storeInputData(bundle, intent)).build()
2021-03-19 18:49:34 +01:00
Intents.POCTECH_BG ->
2021-01-30 22:34:05 +01:00
OneTimeWorkRequest.Builder(PoctechPlugin.PoctechWorker::class.java)
.setInputData(Data.Builder().also {
it.copyString("data", bundle)
}.build()).build()
2021-03-19 18:49:34 +01:00
Intents.GLIMP_BG ->
2021-01-30 22:34:05 +01:00
OneTimeWorkRequest.Builder(GlimpPlugin.GlimpWorker::class.java)
.setInputData(Data.Builder().also {
it.copyDouble("mySGV", bundle)
it.copyString("myTrend", bundle)
it.copyLong("myTimestamp", bundle)
}.build()).build()
2021-03-19 18:49:34 +01:00
Intents.TOMATO_BG ->
2021-03-20 12:30:57 +01:00
@Suppress("SpellCheckingInspection")
2021-01-30 22:34:05 +01:00
OneTimeWorkRequest.Builder(TomatoPlugin.TomatoWorker::class.java)
.setInputData(Data.Builder().also {
it.copyDouble("com.fanqies.tomatofn.Extras.BgEstimate", bundle)
it.copyLong("com.fanqies.tomatofn.Extras.Time", bundle)
}.build()).build()
2021-03-19 18:49:34 +01:00
Intents.NS_EMULATOR ->
2021-01-30 22:34:05 +01:00
OneTimeWorkRequest.Builder(MM640gPlugin.MM640gWorker::class.java)
.setInputData(Data.Builder().also {
it.copyDouble(Intents.EXTRA_BG_ESTIMATE, bundle)
it.copyString(Intents.EXTRA_BG_SLOPE_NAME, bundle)
it.copyLong(Intents.EXTRA_TIMESTAMP, bundle)
it.copyDouble(Intents.EXTRA_RAW, bundle)
it.copyInt(Intents.EXTRA_SENSOR_BATTERY, bundle, -1)
it.copyString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, bundle)
}.build()).build()
Telephony.Sms.Intents.SMS_RECEIVED_ACTION ->
OneTimeWorkRequest.Builder(SmsCommunicatorPlugin.SmsCommunicatorWorker::class.java)
2021-03-20 12:30:57 +01:00
.setInputData(dataWorker.storeInputData(bundle, intent)).build()
2021-03-19 18:49:34 +01:00
Intents.EVERSENSE_BG ->
2021-01-30 22:34:05 +01:00
OneTimeWorkRequest.Builder(EversensePlugin.EversenseWorker::class.java)
2021-03-20 12:30:57 +01:00
.setInputData(dataWorker.storeInputData(bundle, intent)).build()
2021-03-19 18:49:34 +01:00
Intents.DEXCOM_BG ->
2021-01-30 22:34:05 +01:00
OneTimeWorkRequest.Builder(DexcomPlugin.DexcomWorker::class.java)
2021-03-20 12:30:57 +01:00
.setInputData(dataWorker.storeInputData(bundle, intent)).build()
2021-01-30 22:34:05 +01:00
else -> null
2021-03-20 12:30:57 +01:00
}?.let { request -> dataWorker.enqueue(request) }
2020-03-31 10:10:48 +02:00
}
2021-02-09 12:32:21 +01:00
2020-03-31 10:10:48 +02:00
}