2019-12-24 16:07:17 +01:00
|
|
|
package info.nightscout.androidaps.receivers
|
|
|
|
|
|
|
|
import android.app.AlarmManager
|
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.app.PendingIntent.CanceledException
|
2021-03-31 21:54:49 +02:00
|
|
|
import android.app.PendingIntent.FLAG_IMMUTABLE
|
2019-12-24 16:07:17 +01:00
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.SystemClock
|
2021-03-31 21:54:49 +02:00
|
|
|
import androidx.work.OneTimeWorkRequest
|
|
|
|
import androidx.work.WorkManager
|
|
|
|
import androidx.work.Worker
|
|
|
|
import androidx.work.WorkerParameters
|
2019-12-30 00:53:44 +01:00
|
|
|
import dagger.android.DaggerBroadcastReceiver
|
2021-03-31 21:54:49 +02:00
|
|
|
import dagger.android.HasAndroidInjector
|
2020-05-09 10:52:20 +02:00
|
|
|
import info.nightscout.androidaps.BuildConfig
|
2021-04-14 18:42:12 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.Config
|
2021-03-31 21:54:49 +02:00
|
|
|
import info.nightscout.androidaps.database.AppRepository
|
2019-12-24 16:07:17 +01:00
|
|
|
import info.nightscout.androidaps.events.EventProfileNeedsUpdate
|
2021-04-12 19:49:12 +02:00
|
|
|
import info.nightscout.androidaps.extensions.buildDeviceStatus
|
2021-04-14 00:45:30 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePlugin
|
2020-01-07 10:14:12 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.CommandQueueProvider
|
2021-04-12 19:49:12 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.IobCobCalculator
|
2020-05-09 10:52:20 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ProfileFunction
|
2019-12-30 00:53:44 +01:00
|
|
|
import info.nightscout.androidaps.logging.AAPSLogger
|
2020-01-06 15:22:28 +01:00
|
|
|
import info.nightscout.androidaps.logging.LTag
|
2020-01-03 01:45:26 +01:00
|
|
|
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin
|
2019-12-30 00:53:44 +01:00
|
|
|
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
2021-03-31 21:54:49 +02:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.RunningConfiguration
|
2019-12-24 16:07:17 +01:00
|
|
|
import info.nightscout.androidaps.queue.commands.Command
|
|
|
|
import info.nightscout.androidaps.utils.DateUtil
|
|
|
|
import info.nightscout.androidaps.utils.FabricPrivacy
|
|
|
|
import info.nightscout.androidaps.utils.LocalAlertUtils
|
|
|
|
import info.nightscout.androidaps.utils.T
|
2019-12-30 00:53:44 +01:00
|
|
|
import javax.inject.Inject
|
2019-12-24 16:07:17 +01:00
|
|
|
import kotlin.math.abs
|
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
class KeepAliveReceiver : DaggerBroadcastReceiver() {
|
2021-02-17 19:28:43 +01:00
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var aapsLogger: AAPSLogger
|
2019-12-24 16:07:17 +01:00
|
|
|
|
|
|
|
companion object {
|
2021-02-17 19:28:43 +01:00
|
|
|
|
2019-12-24 16:07:17 +01:00
|
|
|
private val KEEP_ALIVE_MILLISECONDS = T.mins(5).msecs()
|
2020-03-17 18:47:43 +01:00
|
|
|
}
|
2019-12-24 16:07:17 +01:00
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
|
|
super.onReceive(context, intent)
|
2020-01-10 23:14:58 +01:00
|
|
|
aapsLogger.debug(LTag.CORE, "KeepAlive received")
|
2021-03-31 21:54:49 +02:00
|
|
|
|
|
|
|
WorkManager.getInstance(context)
|
|
|
|
.enqueue(OneTimeWorkRequest.Builder(KeepAliveWorker::class.java).build())
|
|
|
|
}
|
|
|
|
|
|
|
|
class KeepAliveWorker(
|
|
|
|
context: Context,
|
|
|
|
params: WorkerParameters
|
|
|
|
) : Worker(context, params) {
|
|
|
|
|
|
|
|
@Inject lateinit var aapsLogger: AAPSLogger
|
|
|
|
@Inject lateinit var localAlertUtils: LocalAlertUtils
|
|
|
|
@Inject lateinit var repository: AppRepository
|
|
|
|
@Inject lateinit var config: Config
|
2021-04-12 19:49:12 +02:00
|
|
|
@Inject lateinit var iobCobCalculator: IobCobCalculator
|
2021-03-31 21:54:49 +02:00
|
|
|
@Inject lateinit var loopPlugin: LoopPlugin
|
|
|
|
@Inject lateinit var dateUtil: DateUtil
|
2021-04-14 00:45:30 +02:00
|
|
|
@Inject lateinit var activePlugin: ActivePlugin
|
2021-03-31 21:54:49 +02:00
|
|
|
@Inject lateinit var profileFunction: ProfileFunction
|
|
|
|
@Inject lateinit var runningConfiguration: RunningConfiguration
|
|
|
|
@Inject lateinit var receiverStatusStore: ReceiverStatusStore
|
|
|
|
@Inject lateinit var rxBus: RxBusWrapper
|
|
|
|
@Inject lateinit var commandQueue: CommandQueueProvider
|
|
|
|
@Inject lateinit var fabricPrivacy: FabricPrivacy
|
|
|
|
|
|
|
|
init {
|
|
|
|
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
private val STATUS_UPDATE_FREQUENCY = T.mins(15).msecs()
|
|
|
|
private const val IOB_UPDATE_FREQUENCY_IN_MINUTES = 5L
|
|
|
|
|
|
|
|
private var lastReadStatus: Long = 0
|
|
|
|
private var lastRun: Long = 0
|
|
|
|
private var lastIobUpload: Long = 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun doWork(): Result {
|
|
|
|
localAlertUtils.shortenSnoozeInterval()
|
|
|
|
localAlertUtils.checkStaleBGAlert()
|
|
|
|
checkPump()
|
|
|
|
checkAPS()
|
|
|
|
return Result.success()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usually deviceStatus is uploaded through LoopPlugin after every loop cycle.
|
|
|
|
// if there is no BG available, we have to upload anyway to have correct
|
|
|
|
// IOB displayed in NS
|
|
|
|
private fun checkAPS() {
|
|
|
|
var shouldUploadStatus = false
|
|
|
|
if (config.NSCLIENT) return
|
|
|
|
if (config.PUMPCONTROL) shouldUploadStatus = true
|
2021-04-13 18:33:44 +02:00
|
|
|
else if (!loopPlugin.isEnabled() || iobCobCalculator.ads.actualBg() == null)
|
2021-03-31 21:54:49 +02:00
|
|
|
shouldUploadStatus = true
|
2021-04-11 17:47:55 +02:00
|
|
|
else if (dateUtil.isOlderThan(activePlugin.activeAPS.lastAPSRun, 5)) shouldUploadStatus = true
|
|
|
|
if (dateUtil.isOlderThan(lastIobUpload, IOB_UPDATE_FREQUENCY_IN_MINUTES) && shouldUploadStatus) {
|
2021-04-11 17:58:50 +02:00
|
|
|
lastIobUpload = dateUtil.now()
|
2021-04-12 19:49:12 +02:00
|
|
|
buildDeviceStatus(dateUtil, loopPlugin, iobCobCalculator, profileFunction,
|
2021-03-31 21:54:49 +02:00
|
|
|
activePlugin.activePump, receiverStatusStore, runningConfiguration,
|
|
|
|
BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION)?.also {
|
|
|
|
repository.insert(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun checkPump() {
|
|
|
|
val pump = activePlugin.activePump
|
|
|
|
val profile = profileFunction.getProfile() ?: return
|
|
|
|
val lastConnection = pump.lastDataTime()
|
|
|
|
val isStatusOutdated = lastConnection + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis()
|
|
|
|
val isBasalOutdated = abs(profile.basal - pump.baseBasalRate) > pump.pumpDescription.basalStep
|
|
|
|
aapsLogger.debug(LTag.CORE, "Last connection: " + dateUtil.dateAndTimeString(lastConnection))
|
|
|
|
// sometimes keep alive broadcast stops
|
|
|
|
// as as workaround test if readStatus was requested before an alarm is generated
|
|
|
|
if (lastReadStatus != 0L && lastReadStatus > System.currentTimeMillis() - T.mins(5).msecs()) {
|
|
|
|
localAlertUtils.checkPumpUnreachableAlarm(lastConnection, isStatusOutdated, loopPlugin.isDisconnected)
|
|
|
|
}
|
|
|
|
if (!pump.isThisProfileSet(profile) && !commandQueue.isRunning(Command.CommandType.BASAL_PROFILE)) {
|
|
|
|
rxBus.send(EventProfileNeedsUpdate())
|
|
|
|
} else if (isStatusOutdated && !pump.isBusy()) {
|
|
|
|
lastReadStatus = System.currentTimeMillis()
|
|
|
|
commandQueue.readStatus("KeepAlive. Status outdated.", null)
|
|
|
|
} else if (isBasalOutdated && !pump.isBusy()) {
|
|
|
|
lastReadStatus = System.currentTimeMillis()
|
|
|
|
commandQueue.readStatus("KeepAlive. Basal outdated.", null)
|
|
|
|
}
|
|
|
|
if (lastRun != 0L && System.currentTimeMillis() - lastRun > T.mins(10).msecs()) {
|
|
|
|
aapsLogger.error(LTag.CORE, "KeepAlive fail")
|
|
|
|
fabricPrivacy.logCustom("KeepAliveFail")
|
|
|
|
}
|
|
|
|
lastRun = System.currentTimeMillis()
|
|
|
|
}
|
2019-12-24 16:07:17 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 23:14:58 +01:00
|
|
|
class KeepAliveManager @Inject constructor(
|
|
|
|
private val aapsLogger: AAPSLogger,
|
|
|
|
private val localAlertUtils: LocalAlertUtils
|
|
|
|
) {
|
|
|
|
|
2019-12-24 16:07:17 +01:00
|
|
|
//called by MainApp at first app start
|
|
|
|
fun setAlarm(context: Context) {
|
2020-01-06 15:22:28 +01:00
|
|
|
aapsLogger.debug(LTag.CORE, "KeepAlive scheduled")
|
2019-12-24 16:07:17 +01:00
|
|
|
SystemClock.sleep(5000) // wait for app initialization
|
2020-01-10 23:14:58 +01:00
|
|
|
localAlertUtils.shortenSnoozeInterval()
|
2020-12-25 12:06:54 +01:00
|
|
|
localAlertUtils.preSnoozeAlarms()
|
2019-12-24 16:07:17 +01:00
|
|
|
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
|
|
|
val i = Intent(context, KeepAliveReceiver::class.java)
|
2021-03-31 21:54:49 +02:00
|
|
|
val pi = PendingIntent.getBroadcast(context, 0, i, FLAG_IMMUTABLE)
|
2019-12-24 16:07:17 +01:00
|
|
|
try {
|
|
|
|
pi.send()
|
|
|
|
} catch (e: CanceledException) {
|
|
|
|
}
|
|
|
|
am.cancel(pi)
|
|
|
|
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), KEEP_ALIVE_MILLISECONDS, pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun cancelAlarm(context: Context) {
|
2020-01-06 15:22:28 +01:00
|
|
|
aapsLogger.debug(LTag.CORE, "KeepAlive canceled")
|
2019-12-24 16:07:17 +01:00
|
|
|
val intent = Intent(context, KeepAliveReceiver::class.java)
|
2021-03-31 21:54:49 +02:00
|
|
|
val sender = PendingIntent.getBroadcast(context, 0, intent, FLAG_IMMUTABLE)
|
2019-12-24 16:07:17 +01:00
|
|
|
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
|
|
|
alarmManager.cancel(sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|