2020-03-31 10:10:48 +02:00
|
|
|
package info.nightscout.androidaps.receivers
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
2021-02-09 12:32:21 +01:00
|
|
|
import android.os.Bundle
|
2021-01-30 22:34:05 +01:00
|
|
|
import android.provider.Telephony
|
|
|
|
import androidx.work.Data
|
|
|
|
import androidx.work.ExistingWorkPolicy
|
|
|
|
import androidx.work.OneTimeWorkRequest
|
|
|
|
import androidx.work.WorkManager
|
|
|
|
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.nsclient.NSClientWorker
|
|
|
|
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin
|
|
|
|
import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin
|
|
|
|
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
|
2021-02-02 17:48:39 +01:00
|
|
|
import org.json.JSONObject
|
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-02-02 17:48:39 +01:00
|
|
|
@Inject lateinit var bundleStore: BundleStore
|
2020-03-31 10:10:48 +02:00
|
|
|
|
2021-01-30 22:34:05 +01:00
|
|
|
private val jobGroupName = "data"
|
|
|
|
|
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.DATASERVICE, "onReceive ${intent.action} ${BundleLogger.log(bundle)}")
|
|
|
|
|
|
|
|
|
|
|
|
when (intent.action) {
|
|
|
|
Intents.ACTION_NEW_BG_ESTIMATE ->
|
|
|
|
OneTimeWorkRequest.Builder(XdripPlugin.XdripWorker::class.java)
|
2021-02-09 12:32:21 +01:00
|
|
|
.setInputData(bundleInputData(bundle, intent)).build()
|
2021-01-30 22:34:05 +01:00
|
|
|
Intents.POCTECH_BG ->
|
|
|
|
OneTimeWorkRequest.Builder(PoctechPlugin.PoctechWorker::class.java)
|
|
|
|
.setInputData(Data.Builder().also {
|
|
|
|
it.copyString("data", bundle)
|
|
|
|
}.build()).build()
|
|
|
|
Intents.GLIMP_BG ->
|
|
|
|
OneTimeWorkRequest.Builder(GlimpPlugin.GlimpWorker::class.java)
|
|
|
|
.setInputData(Data.Builder().also {
|
|
|
|
it.copyDouble("mySGV", bundle)
|
|
|
|
it.copyString("myTrend", bundle)
|
|
|
|
it.copyLong("myTimestamp", bundle)
|
|
|
|
}.build()).build()
|
|
|
|
Intents.TOMATO_BG ->
|
|
|
|
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()
|
|
|
|
Intents.ACTION_NEW_PROFILE ->
|
|
|
|
OneTimeWorkRequest.Builder(NSProfilePlugin.NSProfileWorker::class.java)
|
2021-02-11 23:42:40 +01:00
|
|
|
.setInputData(bundleInputData(bundle, intent)).build()
|
2021-01-30 22:34:05 +01:00
|
|
|
Intents.ACTION_NEW_SGV ->
|
|
|
|
OneTimeWorkRequest.Builder(NSClientSourcePlugin.NSClientSourceWorker::class.java)
|
|
|
|
.setInputData(Data.Builder().also {
|
|
|
|
it.copyString("sgv", bundle, null)
|
|
|
|
it.copyString("sgvs", bundle, null)
|
|
|
|
}.build()).build()
|
|
|
|
Intents.NS_EMULATOR ->
|
|
|
|
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-02-09 12:32:21 +01:00
|
|
|
.setInputData(bundleInputData(bundle, intent)).build()
|
2021-01-30 22:34:05 +01:00
|
|
|
Intents.EVERSENSE_BG ->
|
|
|
|
OneTimeWorkRequest.Builder(EversensePlugin.EversenseWorker::class.java)
|
2021-02-09 12:32:21 +01:00
|
|
|
.setInputData(bundleInputData(bundle, intent)).build()
|
2021-01-30 22:34:05 +01:00
|
|
|
Intents.DEXCOM_BG ->
|
|
|
|
OneTimeWorkRequest.Builder(DexcomPlugin.DexcomWorker::class.java)
|
2021-02-09 12:32:21 +01:00
|
|
|
.setInputData(bundleInputData(bundle, intent)).build()
|
2021-01-30 22:34:05 +01:00
|
|
|
Intents.ACTION_NEW_TREATMENT,
|
|
|
|
Intents.ACTION_CHANGED_TREATMENT,
|
|
|
|
Intents.ACTION_REMOVED_TREATMENT,
|
|
|
|
Intents.ACTION_NEW_CAL,
|
|
|
|
Intents.ACTION_NEW_MBG ->
|
|
|
|
OneTimeWorkRequest.Builder(NSClientWorker::class.java)
|
2021-02-09 12:32:21 +01:00
|
|
|
.setInputData(bundleInputData(bundle, intent)).build()
|
2021-01-30 22:34:05 +01:00
|
|
|
else -> null
|
|
|
|
}?.let { request ->
|
|
|
|
WorkManager.getInstance(context)
|
|
|
|
.enqueueUniqueWork(jobGroupName, ExistingWorkPolicy.APPEND_OR_REPLACE , request)
|
|
|
|
}
|
2020-03-31 10:10:48 +02:00
|
|
|
}
|
2021-02-09 12:32:21 +01:00
|
|
|
|
|
|
|
private fun bundleInputData(bundle: Bundle, intent: Intent) =
|
|
|
|
Data.Builder().putLong(STORE_KEY, bundleStore.store(bundle)).putString(ACTION_KEY, intent.action).build()
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val STORE_KEY = "storeKey"
|
|
|
|
const val ACTION_KEY = "action"
|
|
|
|
}
|
2020-03-31 10:10:48 +02:00
|
|
|
}
|