unify xDrip broadcasts

This commit is contained in:
Milos Kozak 2021-11-18 15:47:22 +01:00
parent f3a861e1d8
commit 9fb20917c5
5 changed files with 41 additions and 57 deletions

View file

@ -7,17 +7,17 @@ import android.view.ViewGroup
import com.google.common.base.Joiner
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources
import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.databinding.DialogCalibrationBinding
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.XdripCalibrations
import info.nightscout.androidaps.utils.XDripBroadcast
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.resources.ResourceHelper
import java.text.DecimalFormat
@ -29,7 +29,7 @@ class CalibrationDialog : DialogFragmentWithDate() {
@Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var rh: ResourceHelper
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var xdripCalibrations: XdripCalibrations
@Inject lateinit var xDripBroadcast: XDripBroadcast
@Inject lateinit var uel: UserEntryLogger
@Inject lateinit var glucoseStatusProvider: GlucoseStatusProvider
@ -82,7 +82,7 @@ class CalibrationDialog : DialogFragmentWithDate() {
activity?.let { activity ->
OKDialog.showConfirmation(activity, rh.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
uel.log(Action.CALIBRATION, Sources.CalibrationDialog, ValueWithUnit.fromGlucoseUnit(bg, units.asText))
xdripCalibrations.sendIntent(bg)
xDripBroadcast.sendCalibration(bg)
})
}
} else

View file

@ -77,7 +77,7 @@ class SmsCommunicatorPlugin @Inject constructor(
private val commandQueue: CommandQueue,
private val loop: Loop,
private val iobCobCalculator: IobCobCalculator,
private val xdripCalibrations: XdripCalibrations,
private val xDripBroadcast: XDripBroadcast,
private var otp: OneTimePassword,
private val config: Config,
private val dateUtil: DateUtil,
@ -1064,7 +1064,7 @@ class SmsCommunicatorPlugin @Inject constructor(
receivedSms.processed = true
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false, cal) {
override fun run() {
val result = xdripCalibrations.sendIntent(aDouble!!)
val result = xDripBroadcast.sendCalibration(aDouble!!)
val replyText =
if (result) rh.gs(R.string.smscommunicator_calibrationsent) else rh.gs(R.string.smscommunicator_calibrationfailed)
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText))

View file

@ -4,10 +4,14 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import info.nightscout.androidaps.R
import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.androidaps.database.entities.GlucoseValue
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.services.Intents
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import org.json.JSONArray
import org.json.JSONException
@ -18,13 +22,37 @@ import javax.inject.Inject
import javax.inject.Singleton
@Suppress("SpellCheckingInspection")
@OpenForTesting
@Singleton
class XDripBroadcast @Inject constructor(
private val context: Context,
private val aapsLogger: AAPSLogger,
private val sp: SP
private val sp: SP,
private val rh: ResourceHelper,
private val profileFunction: ProfileFunction
) {
fun sendCalibration(bg: Double): Boolean {
val bundle = Bundle()
bundle.putDouble("glucose_number", bg)
bundle.putString("units", if (profileFunction.getUnits() == GlucoseUnit.MGDL) "mgdl" else "mmol")
bundle.putLong("timestamp", System.currentTimeMillis())
val intent = Intent(Intents.ACTION_REMOTE_CALIBRATION)
intent.putExtras(bundle)
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
context.sendBroadcast(intent)
val q = context.packageManager.queryBroadcastReceivers(intent, 0)
return if (q.size < 1) {
ToastUtils.showToastInUiThread(context, rh.gs(R.string.xdripnotinstalled))
aapsLogger.debug(rh.gs(R.string.xdripnotinstalled))
false
} else {
ToastUtils.showToastInUiThread(context, rh.gs(R.string.calibrationsent))
aapsLogger.debug(rh.gs(R.string.calibrationsent))
true
}
}
// sent in 640G mode
fun send(glucoseValue: GlucoseValue) {
if (sp.getBoolean(R.string.key_dexcomg5_xdripupload, false)) {

View file

@ -1,45 +0,0 @@
package info.nightscout.androidaps.utils
import android.content.Context
import android.content.Intent
import android.os.Bundle
import info.nightscout.androidaps.R
import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.services.Intents
import info.nightscout.androidaps.utils.resources.ResourceHelper
import javax.inject.Inject
import javax.inject.Singleton
@OpenForTesting
@Singleton
class XdripCalibrations @Inject constructor(
private val aapsLogger: AAPSLogger,
private val rh: ResourceHelper,
private val context: Context,
private val profileFunction: ProfileFunction
) {
fun sendIntent(bg: Double): Boolean {
val bundle = Bundle()
bundle.putDouble("glucose_number", bg)
bundle.putString("units", if (profileFunction.getUnits() == GlucoseUnit.MGDL) "mgdl" else "mmol")
bundle.putLong("timestamp", System.currentTimeMillis())
val intent = Intent(Intents.ACTION_REMOTE_CALIBRATION)
intent.putExtras(bundle)
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
context.sendBroadcast(intent)
val q = context.packageManager.queryBroadcastReceivers(intent, 0)
return if (q.size < 1) {
ToastUtils.showToastInUiThread(context, rh.gs(R.string.xdripnotinstalled))
aapsLogger.debug(rh.gs(R.string.xdripnotinstalled))
false
} else {
ToastUtils.showToastInUiThread(context, rh.gs(R.string.calibrationsent))
aapsLogger.debug(rh.gs(R.string.calibrationsent))
true
}
}
}

View file

@ -29,7 +29,7 @@ import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.XdripCalibrations
import info.nightscout.androidaps.utils.XDripBroadcast
import info.nightscout.androidaps.utils.buildHelper.ConfigImpl
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.Single
@ -55,7 +55,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
@Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin
@Mock lateinit var localProfilePlugin: LocalProfilePlugin
@Mock lateinit var otp: OneTimePassword
@Mock lateinit var xdripCalibrations: XdripCalibrations
@Mock lateinit var xDripBroadcast: XDripBroadcast
@Mock lateinit var uel: UserEntryLogger
@Mock lateinit var repository: AppRepository
@Mock lateinit var dateUtilMocked: DateUtil
@ -98,7 +98,8 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
}))
val glucoseStatusProvider = GlucoseStatusProvider(aapsLogger = aapsLogger, iobCobCalculator = iobCobCalculator, dateUtil = dateUtilMocked)
smsCommunicatorPlugin = SmsCommunicatorPlugin(injector, aapsLogger, rh, smsManager, aapsSchedulers, sp, constraintChecker, rxBus, profileFunction, fabricPrivacy, activePlugin, commandQueue, loopPlugin, iobCobCalculator, xdripCalibrations, otp, ConfigImpl(), dateUtilMocked, uel, glucoseStatusProvider, repository)
smsCommunicatorPlugin = SmsCommunicatorPlugin(injector, aapsLogger, rh, smsManager, aapsSchedulers, sp, constraintChecker, rxBus, profileFunction, fabricPrivacy, activePlugin, commandQueue,
loopPlugin, iobCobCalculator, xDripBroadcast, otp, ConfigImpl(), dateUtilMocked, uel, glucoseStatusProvider, repository)
smsCommunicatorPlugin.setPluginEnabled(PluginType.GENERAL, true)
Mockito.doAnswer { invocation: InvocationOnMock ->
val callback = invocation.getArgument<Callback>(1)
@ -980,7 +981,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
smsCommunicatorPlugin.processSms(sms)
Assert.assertEquals("CAL 0", smsCommunicatorPlugin.messages[0].text)
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text)
`when`(xdripCalibrations.sendIntent(ArgumentMatchers.anyDouble())).thenReturn(true)
`when`(xDripBroadcast.sendCalibration(ArgumentMatchers.anyDouble())).thenReturn(true)
//CAL 1
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "CAL 1")