Merge pull request #26 from 0pen-dash/bart/create-responses

Parse responses
This commit is contained in:
bartsopers 2021-03-31 11:27:24 +02:00 committed by GitHub
commit 210872d6c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
59 changed files with 906 additions and 400 deletions

View file

@ -23,7 +23,10 @@ abstract class OmnipodWizardModule {
@Provides
@OmnipodPluginQualifier
fun providesViewModelFactory(@OmnipodPluginQualifier viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>): ViewModelProvider.Factory {
fun providesViewModelFactory(
@OmnipodPluginQualifier
viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
): ViewModelProvider.Factory {
return ViewModelFactory(viewModels)
}
}

View file

@ -41,7 +41,11 @@ class AttachPodFragment : InfoFragmentBase() {
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(getString(getTitleId()))
.setMessage(getString(R.string.omnipod_common_pod_activation_wizard_attach_pod_confirm_insert_cannula_text))
.setPositiveButton(getString(R.string.omnipod_common_ok)) { _, _ -> findNavController().navigate(getNextPageActionId()) }
.setPositiveButton(getString(R.string.omnipod_common_ok)) { _, _ ->
findNavController().navigate(
getNextPageActionId()
)
}
.setNegativeButton(getString(R.string.omnipod_common_cancel), null)
.show()
}

View file

@ -24,7 +24,8 @@ abstract class ActionFragmentBase : WizardFragmentBase() {
binding.navButtonsLayout.buttonNext.isEnabled = false
view.findViewById<TextView>(R.id.omnipod_wizard_action_page_text).setText(getTextId())
view.findViewById<Button>(R.id.omnipod_wizard_button_retry).setOnClickListener { actionViewModel.executeAction() }
view.findViewById<Button>(R.id.omnipod_wizard_button_retry)
.setOnClickListener { actionViewModel.executeAction() }
actionViewModel.isActionExecutingLiveData.observe(viewLifecycleOwner, { isExecuting ->
if (isExecuting) {
@ -33,7 +34,8 @@ abstract class ActionFragmentBase : WizardFragmentBase() {
view.findViewById<Button>(R.id.omnipod_wizard_button_discard_pod).visibility = View.GONE
view.findViewById<Button>(R.id.omnipod_wizard_button_retry).visibility = View.GONE
}
view.findViewById<ProgressBar>(R.id.omnipod_wizard_action_progress_indication).visibility = isExecuting.toVisibility()
view.findViewById<ProgressBar>(R.id.omnipod_wizard_action_progress_indication).visibility =
isExecuting.toVisibility()
view.findViewById<Button>(R.id.button_cancel).isEnabled = !isExecuting
})
@ -42,9 +44,12 @@ abstract class ActionFragmentBase : WizardFragmentBase() {
val isExecuting = isActionExecuting()
view.findViewById<Button>(R.id.button_next).isEnabled = result.success
view.findViewById<ImageView>(R.id.omnipod_wizard_action_success).visibility = result.success.toVisibility()
view.findViewById<TextView>(R.id.omnipod_wizard_action_error).visibility = (!isExecuting && !result.success).toVisibility()
view.findViewById<Button>(R.id.omnipod_wizard_button_retry).visibility = (!isExecuting && !result.success).toVisibility()
view.findViewById<ImageView>(R.id.omnipod_wizard_action_success).visibility =
result.success.toVisibility()
view.findViewById<TextView>(R.id.omnipod_wizard_action_error).visibility =
(!isExecuting && !result.success).toVisibility()
view.findViewById<Button>(R.id.omnipod_wizard_button_retry).visibility =
(!isExecuting && !result.success).toVisibility()
if (!result.success) {
view.findViewById<TextView>(R.id.omnipod_wizard_action_error).text = result.comment

View file

@ -47,7 +47,8 @@ abstract class WizardFragmentBase : DaggerFragment() {
if (nextPage == null) {
binding.navButtonsLayout.buttonNext.text = getString(R.string.omnipod_common_wizard_button_finish)
binding.navButtonsLayout.buttonNext.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.omnipod_wizard_finish_button, context?.theme))
binding.navButtonsLayout.buttonNext.backgroundTintList =
ColorStateList.valueOf(resources.getColor(R.color.omnipod_wizard_finish_button, context?.theme))
}
updateProgressIndication()

View file

@ -33,13 +33,13 @@ class OmnipodDashPumpPlugin @Inject constructor(
companion object {
private val pluginDescription = PluginDescription() //
.mainType(PluginType.PUMP) //
.fragmentClass(OmnipodDashOverviewFragment::class.java.name) //
private val pluginDescription = PluginDescription()
.mainType(PluginType.PUMP)
.fragmentClass(OmnipodDashOverviewFragment::class.java.name)
.pluginIcon(R.drawable.ic_pod_128)
.pluginName(R.string.omnipod_dash_name) //
.shortName(R.string.omnipod_dash_name_short) //
.preferencesId(R.xml.omnipod_dash_preferences) //
.pluginName(R.string.omnipod_dash_name)
.shortName(R.string.omnipod_dash_name_short)
.preferencesId(R.xml.omnipod_dash_preferences)
.description(R.string.omnipod_dash_pump_description)
private val pumpDescription = PumpDescription(PumpType.Omnipod_Dash)

View file

@ -5,6 +5,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definitio
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.AlertTrigger
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.AlertType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BasalProgram
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BeepType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.ResponseType
import io.reactivex.Observable
import java.util.*
@ -14,9 +16,9 @@ interface OmnipodDashManager {
fun activatePodPart2(basalProgram: BasalProgram): Observable<PodEvent>
fun getStatus(): Observable<PodEvent>
fun getStatus(type: ResponseType.StatusResponseType): Observable<PodEvent>
fun setBasalProgram(program: BasalProgram): Observable<PodEvent>
fun setBasalProgram(basalProgram: BasalProgram): Observable<PodEvent>
fun suspendDelivery(): Observable<PodEvent>
@ -26,15 +28,15 @@ interface OmnipodDashManager {
fun cancelTempBasal(): Observable<PodEvent>
fun bolus(amount: Double): Observable<PodEvent>
fun bolus(units: Double, confirmationBeeps: Boolean, completionBeeps: Boolean): Observable<PodEvent>
fun cancelBolus(): Observable<PodEvent>
fun programBeeps(): Observable<PodEvent>
fun playBeep(beepType: BeepType): Observable<PodEvent>
fun programAlerts(alertConfigurations: List<AlertConfiguration>): Observable<PodEvent>
fun silenceAlerts(alerts: EnumSet<AlertType>): Observable<PodEvent>
fun silenceAlerts(alertTypes: EnumSet<AlertType>): Observable<PodEvent>
fun deactivatePod(): Observable<PodEvent>
}

View file

@ -26,11 +26,17 @@ class OmnipodDashManagerImpl @Inject constructor(
private val aapsSchedulers: AapsSchedulers
) : OmnipodDashManager {
companion object {
const val NONCE = 1229869870 // The Omnipod Dash seems to use a fixed nonce
}
private val observePodReadyForActivationPart1: Observable<PodEvent>
get() = Observable.defer {
if (podStateManager.activationProgress.isBefore(ActivationProgress.PHASE_1_COMPLETED)) {
Observable.empty()
} else {
// TODO introduce specialized Exception
Observable.error(IllegalStateException("Pod is in an incorrect state"))
}
}
@ -42,6 +48,27 @@ class OmnipodDashManagerImpl @Inject constructor(
) {
Observable.empty()
} else {
// TODO introduce specialized Exception
Observable.error(IllegalStateException("Pod is in an incorrect state"))
}
}
private val observeUniqueIdSet: Observable<PodEvent>
get() = Observable.defer {
if (podStateManager.activationProgress.isAtLeast(ActivationProgress.SET_UNIQUE_ID)) {
Observable.empty()
} else {
// TODO introduce specialized Exception
Observable.error(IllegalStateException("Pod is in an incorrect state"))
}
}
private val observePodRunning: Observable<PodEvent>
get() = Observable.defer {
if (podStateManager.activationProgress == ActivationProgress.COMPLETED && podStateManager.podStatus!!.isRunning()) {
Observable.empty()
} else {
// TODO introduce specialized Exception
Observable.error(IllegalStateException("Pod is in an incorrect state"))
}
}
@ -67,41 +94,48 @@ class OmnipodDashManagerImpl @Inject constructor(
ProgramBolusCommand.Builder()
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setNonce(1229869870) // TODO
.setNonce(NONCE)
.setNumberOfUnits(units)
.setDelayBetweenPulsesInEighthSeconds(rateInEighthPulsesPerSeconds)
.setProgramReminder(ProgramReminder(confirmationBeeps, completionBeeps, 0))
.build()
.build(),
DefaultStatusResponse::class
)
}
}
private fun observeSendGetPodStatusCommand(type: ResponseType.StatusResponseType = ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE): Observable<PodEvent> {
// TODO move somewhere else
val expectedResponseType = when (type) {
ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE -> DefaultStatusResponse::class
ResponseType.StatusResponseType.ALARM_STATUS -> AlarmStatusResponse::class
else -> return Observable.error(UnsupportedOperationException("No response type to class mapping for ${type.name}"))
}
return Observable.defer {
bleManager.sendCommand(
GetStatusCommand.Builder()
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setStatusResponseType(type)
.build()
.build(),
expectedResponseType
)
}
}
private val observeVerifyCannulaInsertion: Observable<PodEvent>
get() = Observable.defer {
observeSendGetPodStatusCommand()
.ignoreElements() //
.andThen(
Observable.defer {
if (podStateManager.podStatus == PodStatus.RUNNING_ABOVE_MIN_VOLUME) {
Observable.empty()
} else {
Observable.error(IllegalStateException("Unexpected Pod status"))
}
}
)
}
get() = Observable.concat(
observeSendGetPodStatusCommand(),
Observable.defer {
if (podStateManager.podStatus == PodStatus.RUNNING_ABOVE_MIN_VOLUME) {
Observable.empty()
} else {
Observable.error(IllegalStateException("Unexpected Pod status"))
}
}
)
private fun observeSendProgramAlertsCommand(
alertConfigurations: List<AlertConfiguration>,
@ -112,74 +146,79 @@ class OmnipodDashManagerImpl @Inject constructor(
ProgramAlertsCommand.Builder()
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setNonce(1229869870) // TODO
.setNonce(NONCE)
.setAlertConfigurations(alertConfigurations)
.build()
.setMultiCommandFlag(multiCommandFlag)
.build(),
DefaultStatusResponse::class
)
}
}
private fun observeProgramBasalCommand(basalProgram: BasalProgram): Observable<PodEvent> {
private fun observeSendProgramBasalCommand(basalProgram: BasalProgram): Observable<PodEvent> {
return Observable.defer {
val currentTime = Date()
logger.debug(LTag.PUMPCOMM, "Programming basal. currentTime={}, basalProgram={}", currentTime, basalProgram)
bleManager.sendCommand(
ProgramBasalCommand.Builder()
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setNonce(1229869870) // TODO
.setNonce(NONCE)
.setProgramReminder(ProgramReminder(atStart = false, atEnd = false, atInterval = 0))
.setBasalProgram(basalProgram)
.setCurrentTime(Date())
.build()
.setCurrentTime(currentTime)
.build(),
DefaultStatusResponse::class
)
}
}
private val observeVerifyPrime: Observable<PodEvent>
get() = Observable.defer {
observeSendGetPodStatusCommand()
.ignoreElements() //
.andThen(
Observable.defer {
if (podStateManager.podStatus == PodStatus.CLUTCH_DRIVE_ENGAGED) {
Observable.empty()
} else {
Observable.error(IllegalStateException("Unexpected Pod status"))
}
}
)
}
get() = Observable.concat(
observeSendGetPodStatusCommand(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE),
Observable.defer {
if (podStateManager.podStatus == PodStatus.CLUTCH_DRIVE_ENGAGED) {
Observable.empty()
} else {
Observable.error(IllegalStateException("Unexpected Pod status: got ${podStateManager.podStatus}, expected CLUTCH_DRIVE_ENGAGED"))
}
}
)
private val observeSendSetUniqueIdCommand: Observable<PodEvent>
get() = Observable.defer {
bleManager.sendCommand(
SetUniqueIdCommand.Builder() //
.setSequenceNumber(podStateManager.messageSequenceNumber) //
.setUniqueId(podStateManager.uniqueId!!.toInt()) //
.setLotNumber(podStateManager.lotNumber!!.toInt()) //
SetUniqueIdCommand.Builder()
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setLotNumber(podStateManager.lotNumber!!.toInt())
.setPodSequenceNumber(podStateManager.podSequenceNumber!!.toInt())
.setInitializationTime(Date())
.build()
) //
.build(),
SetUniqueIdResponse::class
)
}
private val observeSendGetVersionCommand: Observable<PodEvent>
get() = Observable.defer {
bleManager.sendCommand(
GetVersionCommand.Builder() //
.setSequenceNumber(podStateManager.messageSequenceNumber) //
.setUniqueId(DEFAULT_UNIQUE_ID) //
.build()
) //
GetVersionCommand.Builder()
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setUniqueId(DEFAULT_UNIQUE_ID)
.build(),
VersionResponse::class
)
}
override fun activatePodPart1(lowReservoirAlertTrigger: AlertTrigger.ReservoirVolumeTrigger?): Observable<PodEvent> {
return Observable.concat(
observePodReadyForActivationPart1,
observePairNewPod,
observeConnectToPod, // FIXME needed after disconnect; observePairNewPod does not connect in that case.
observeActivationPart1Commands(lowReservoirAlertTrigger)
).doOnComplete(ActivationProgressUpdater(ActivationProgress.PHASE_1_COMPLETED))
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor()) //
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
@ -204,12 +243,24 @@ class OmnipodDashManagerImpl @Inject constructor(
}
if (podStateManager.activationProgress.isBefore(ActivationProgress.PRIMING)) {
observables.add(
observeSendProgramBolusCommand(
podStateManager.firstPrimeBolusVolume!! * 0.05,
podStateManager.primePulseRate!!.toByte(),
confirmationBeeps = false,
completionBeeps = false
).doOnComplete(ActivationProgressUpdater(ActivationProgress.PRIMING))
Observable.defer {
Observable.timer(podStateManager.firstPrimeBolusVolume!!.toLong(), TimeUnit.SECONDS)
.flatMap { Observable.empty() }
})
observables.add(
Observable.defer {
bleManager.sendCommand(
ProgramBolusCommand.Builder()
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setNonce(NONCE)
.setNumberOfUnits(podStateManager.firstPrimeBolusVolume!! * 0.05)
.setDelayBetweenPulsesInEighthSeconds(podStateManager.primePulseRate!!.toByte())
.setProgramReminder(ProgramReminder(atStart = false, atEnd = false, atInterval = 0))
.build(),
DefaultStatusResponse::class
)
}.doOnComplete(ActivationProgressUpdater(ActivationProgress.PRIMING))
)
}
if (podStateManager.activationProgress.isBefore(ActivationProgress.REPROGRAMMED_LUMP_OF_COAL_ALERT)) {
@ -267,7 +318,7 @@ class OmnipodDashManagerImpl @Inject constructor(
observeActivationPart2Commands(basalProgram)
).doOnComplete(ActivationProgressUpdater(ActivationProgress.COMPLETED))
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor()) //
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
@ -292,6 +343,11 @@ class OmnipodDashManagerImpl @Inject constructor(
)
}
if (podStateManager.activationProgress.isBefore(ActivationProgress.INSERTING_CANNULA)) {
observables.add(
Observable.defer {
Observable.timer(podStateManager.secondPrimeBolusVolume!!.toLong(), TimeUnit.SECONDS)
.flatMap { Observable.empty() }
})
observables.add(
observeSendProgramBolusCommand(
podStateManager.secondPrimeBolusVolume!! * 0.05,
@ -335,7 +391,7 @@ class OmnipodDashManagerImpl @Inject constructor(
}
if (podStateManager.activationProgress.isBefore(ActivationProgress.PROGRAMMED_BASAL)) {
observables.add(
observeProgramBasalCommand(basalProgram)
observeSendProgramBasalCommand(basalProgram)
.doOnComplete(ActivationProgressUpdater(ActivationProgress.PROGRAMMED_BASAL))
)
}
@ -343,70 +399,233 @@ class OmnipodDashManagerImpl @Inject constructor(
return observables.reversed()
}
override fun getStatus(): Observable<PodEvent> {
// TODO
return Observable.empty()
override fun getStatus(type: ResponseType.StatusResponseType): Observable<PodEvent> {
return Observable.concat(
observeUniqueIdSet,
observeConnectToPod,
observeSendGetPodStatusCommand(type)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun setBasalProgram(program: BasalProgram): Observable<PodEvent> {
// TODO
return Observable.empty()
override fun setBasalProgram(basalProgram: BasalProgram): Observable<PodEvent> {
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendProgramBasalCommand(basalProgram)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
private fun observeSendStopDeliveryCommand(deliveryType: StopDeliveryCommand.DeliveryType): Observable<PodEvent> {
return Observable.defer {
bleManager.sendCommand(
StopDeliveryCommand.Builder()
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setNonce(NONCE)
.setDeliveryType(deliveryType)
.build(),
DefaultStatusResponse::class
)
}
}
override fun suspendDelivery(): Observable<PodEvent> {
// TODO
return Observable.empty()
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendStopDeliveryCommand(StopDeliveryCommand.DeliveryType.ALL)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun setTime(): Observable<PodEvent> {
// TODO
logger.error(LTag.PUMPCOMM, "NOT IMPLEMENTED: setTime()")
return Observable.empty()
}
private fun observeSendProgramTempBasalCommand(rate: Double, durationInMinutes: Short): Observable<PodEvent> {
return Observable.defer {
// TODO cancel current temp basal (if active)
bleManager.sendCommand(
ProgramTempBasalCommand.Builder()
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setNonce(NONCE)
.setRateInUnitsPerHour(rate)
.setDurationInMinutes(durationInMinutes)
.build(),
DefaultStatusResponse::class
)
}
}
override fun setTempBasal(rate: Double, durationInMinutes: Short): Observable<PodEvent> {
// TODO
return Observable.empty()
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendProgramTempBasalCommand(rate, durationInMinutes)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun cancelTempBasal(): Observable<PodEvent> {
// TODO
return Observable.empty()
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendStopDeliveryCommand(StopDeliveryCommand.DeliveryType.TEMP_BASAL)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun bolus(amount: Double): Observable<PodEvent> {
// TODO
return Observable.empty()
override fun bolus(units: Double, confirmationBeeps: Boolean, completionBeeps: Boolean): Observable<PodEvent> {
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendProgramBolusCommand(
units,
podStateManager.pulseRate!!.toByte(),
confirmationBeeps,
completionBeeps
)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun cancelBolus(): Observable<PodEvent> {
// TODO
return Observable.empty()
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendStopDeliveryCommand(StopDeliveryCommand.DeliveryType.BOLUS)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun programBeeps(): Observable<PodEvent> {
// TODO
return Observable.empty()
private fun observeSendConfigureBeepsCommand(
basalReminder: ProgramReminder = ProgramReminder(atStart = false, atEnd = false, atInterval = 0),
tempBasalReminder: ProgramReminder = ProgramReminder(atStart = false, atEnd = false, atInterval = 0),
bolusReminder: ProgramReminder = ProgramReminder(atStart = false, atEnd = false, atInterval = 0),
immediateBeepType: BeepType = BeepType.SILENT
): Observable<PodEvent> {
return Observable.defer {
bleManager.sendCommand(
ProgramBeepsCommand.Builder()
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setBasalReminder(basalReminder)
.setTempBasalReminder(tempBasalReminder)
.setBolusReminder(bolusReminder)
.setImmediateBeepType(immediateBeepType)
.build(),
DefaultStatusResponse::class
)
}
}
override fun playBeep(beepType: BeepType): Observable<PodEvent> {
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendConfigureBeepsCommand(immediateBeepType = beepType)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun programAlerts(alertConfigurations: List<AlertConfiguration>): Observable<PodEvent> {
// TODO
return Observable.empty()
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendProgramAlertsCommand(alertConfigurations)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
override fun silenceAlerts(alerts: EnumSet<AlertType>): Observable<PodEvent> {
// TODO
return Observable.empty()
private fun observeSendSilenceAlertsCommand(alertTypes: EnumSet<AlertType>): Observable<PodEvent> {
return Observable.defer {
bleManager.sendCommand(
SilenceAlertsCommand.Builder()
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setNonce(NONCE)
.setAlertTypes(alertTypes)
.build(),
DefaultStatusResponse::class
)
}
}
override fun silenceAlerts(alertTypes: EnumSet<AlertType>): Observable<PodEvent> {
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendSilenceAlertsCommand(alertTypes)
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
}
private val observeSendDeactivateCommand: Observable<PodEvent>
get() = Observable.defer {
bleManager.sendCommand(
DeactivateCommand.Builder()
.setSequenceNumber(podStateManager.messageSequenceNumber)
.setUniqueId(podStateManager.uniqueId!!.toInt())
.setNonce(NONCE)
.build(),
DefaultStatusResponse::class
)
}
override fun deactivatePod(): Observable<PodEvent> {
// TODO
return Observable.empty()
return Observable.concat(
observePodRunning,
observeConnectToPod,
observeSendDeactivateCommand
)
// TODO these would be common for any observable returned in a public function in this class
.doOnNext(PodEventInterceptor())
.doOnError(ErrorInterceptor())
.subscribeOn(aapsSchedulers.io)
//
.doOnComplete(podStateManager::reset)
}
inner class PodEventInterceptor : Consumer<PodEvent> {
override fun accept(event: PodEvent) {
logger.debug(LTag.PUMP, "Intercepted PodEvent in OmnipodDashManagerImpl: ${event.javaClass.simpleName}")
when (event) {
is PodEvent.AlreadyConnected -> {
podStateManager.bluetoothAddress = event.bluetoothAddress

View file

@ -3,11 +3,13 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.status.ConnectionStatus
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response
import io.reactivex.Observable
import kotlin.reflect.KClass
interface OmnipodDashBleManager {
fun sendCommand(cmd: Command): Observable<PodEvent>
fun sendCommand(cmd: Command, responseType: KClass<out Response>): Observable<PodEvent>
fun getStatus(): ConnectionStatus

View file

@ -24,6 +24,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session.
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.status.ConnectionStatus
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
import info.nightscout.androidaps.utils.extensions.toHex
import io.reactivex.Observable
@ -32,6 +33,7 @@ import java.util.concurrent.LinkedBlockingDeque
import java.util.concurrent.TimeoutException
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.reflect.KClass
@Singleton
class OmnipodDashBleManagerImpl @Inject constructor(
@ -90,39 +92,39 @@ class OmnipodDashBleManagerImpl @Inject constructor(
return bleIO
}
override fun sendCommand(cmd: Command): Observable<PodEvent> = Observable.create { emitter ->
try {
val keys = sessionKeys
val mIO = msgIO
if (keys == null || mIO == null) {
throw Exception("Not connected")
override fun sendCommand(cmd: Command, responseType: KClass<out Response>): Observable<PodEvent> =
Observable.create { emitter ->
try {
val keys = sessionKeys
val mIO = msgIO
if (keys == null || mIO == null) {
throw Exception("Not connected")
}
emitter.onNext(PodEvent.CommandSending(cmd))
// TODO switch to RX
emitter.onNext(PodEvent.CommandSent(cmd))
val enDecrypt = EnDecrypt(
aapsLogger,
keys.nonce,
keys.ck
)
val session = Session(
aapsLogger = aapsLogger,
msgIO = mIO,
myId = myId,
podId = podId,
sessionKeys = keys,
enDecrypt = enDecrypt
)
val response = session.sendCommand(cmd, responseType)
emitter.onNext(PodEvent.ResponseReceived(cmd, response))
emitter.onComplete()
} catch (ex: Exception) {
emitter.tryOnError(ex)
}
emitter.onNext(PodEvent.CommandSending(cmd))
// TODO switch to RX
emitter.onNext(PodEvent.CommandSent(cmd))
val enDecrypt = EnDecrypt(
aapsLogger,
keys.nonce,
keys.ck
)
val session = Session(
aapsLogger = aapsLogger,
msgIO = mIO,
myId = myId,
podId = podId,
sessionKeys = keys,
enDecrypt = enDecrypt
)
val response = session.sendCommand(cmd)
emitter.onNext(PodEvent.ResponseReceived(response))
emitter.onComplete()
} catch (ex: Exception) {
emitter.tryOnError(ex)
}
}
override fun getStatus(): ConnectionStatus {
var s: ConnectionStatus

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
class CouldNotParseResponseException(message: String?) : Exception(message)

View file

@ -0,0 +1,9 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response
import kotlin.reflect.KClass
class IllegalResponseException(
expectedResponseType: KClass<out Response>,
actualResponse: Response
) : Exception("Illegal response: expected ${expectedResponseType.simpleName} but got $actualResponse")

View file

@ -0,0 +1,5 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.NakResponse
class NakResponseException(val response: NakResponse) : Exception("Received NAK response: ${response.nakErrorType.value} ${response.nakErrorType.name}")

View file

@ -0,0 +1,13 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse
import java.util.*
class PodAlarmException(val response: AlarmStatusResponse) : Exception(
String.format(
Locale.getDefault(),
"Pod is in alarm: %03d %s",
response.alarmType.value.toInt() and 0xff,
response.alarmType.name
)
)

View file

@ -0,0 +1,44 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotParseResponseException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.*
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.util.byValue
object ResponseUtil {
@Throws(CouldNotParseResponseException::class, UnsupportedOperationException::class)
fun parseResponse(payload: ByteArray): Response {
return when (byValue(payload[0], ResponseType.UNKNOWN)) {
ResponseType.ACTIVATION_RESPONSE -> parseActivationResponse(payload)
ResponseType.DEFAULT_STATUS_RESPONSE -> DefaultStatusResponse(payload)
ResponseType.ADDITIONAL_STATUS_RESPONSE -> parseAdditionalStatusResponse(payload)
ResponseType.NAK_RESPONSE -> NakResponse(payload)
ResponseType.UNKNOWN -> throw CouldNotParseResponseException("Unrecognized message type: ${payload[0]}")
}
}
@Throws(CouldNotParseResponseException::class)
private fun parseActivationResponse(payload: ByteArray): Response {
return when (byValue(payload[1], ResponseType.ActivationResponseType.UNKNOWN)) {
ResponseType.ActivationResponseType.GET_VERSION_RESPONSE -> VersionResponse(payload)
ResponseType.ActivationResponseType.SET_UNIQUE_ID_RESPONSE -> SetUniqueIdResponse(payload)
ResponseType.ActivationResponseType.UNKNOWN -> throw CouldNotParseResponseException("Unrecognized activation response type: ${payload[1]}")
}
}
@Throws(CouldNotParseResponseException::class, UnsupportedOperationException::class)
private fun parseAdditionalStatusResponse(payload: ByteArray): Response {
return when (byValue(payload[2], ResponseType.StatusResponseType.UNKNOWN)) {
ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE -> DefaultStatusResponse(payload) // Unreachable; this response type is only used for requesting a default status response
ResponseType.StatusResponseType.STATUS_RESPONSE_PAGE_1 -> throw UnsupportedOperationException("Status response page 1 is not (yet) implemented")
ResponseType.StatusResponseType.ALARM_STATUS -> AlarmStatusResponse(payload)
ResponseType.StatusResponseType.STATUS_RESPONSE_PAGE_3 -> throw UnsupportedOperationException("Status response page 3 is not (yet) implemented")
ResponseType.StatusResponseType.STATUS_RESPONSE_PAGE_5 -> throw UnsupportedOperationException("Status response page 5 is not (yet) implemented")
ResponseType.StatusResponseType.STATUS_RESPONSE_PAGE_6 -> throw UnsupportedOperationException("Status response page 6 is not (yet) implemented")
ResponseType.StatusResponseType.STATUS_RESPONSE_PAGE_70 -> throw UnsupportedOperationException("Status response page 70 is not (yet) implemented")
ResponseType.StatusResponseType.STATUS_RESPONSE_PAGE_80 -> throw UnsupportedOperationException("Status response page 80 is not (yet) implemented")
ResponseType.StatusResponseType.STATUS_RESPONSE_PAGE_81 -> throw UnsupportedOperationException("Status response page 81 is not (yet) implemented")
ResponseType.StatusResponseType.UNKNOWN -> throw CouldNotParseResponseException("Unrecognized additional status response type: ${payload[2]}")
}
}
}

View file

@ -4,15 +4,21 @@ import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.EnDecrypt
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotParseResponseException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.IllegalResponseException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.NakResponseException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.PodAlarmException
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessagePacket
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.StringLengthPrefixEncoding
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.StringLengthPrefixEncoding.Companion.parseKeys
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.NakResponse
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response
import info.nightscout.androidaps.utils.extensions.toHex
import kotlin.reflect.KClass
class Session(
private val aapsLogger: AAPSLogger,
@ -29,9 +35,13 @@ class Session(
* <- response, ACK TODO: retries?
* -> ACK
*/
fun sendCommand(cmd: Command): Response {
@Throws(CouldNotParseResponseException::class, UnsupportedOperationException::class)
fun sendCommand(cmd: Command, responseType: KClass<out Response>): Response {
sessionKeys.msgSequenceNumber++
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending command: ${cmd.encoded.toHex()} in packet $cmd")
aapsLogger.debug(
LTag.PUMPBTCOMM,
"Sending command: ${cmd.javaClass.simpleName}: ${cmd.encoded.toHex()} in packet $cmd"
)
val msg = getCmdMessage(cmd)
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending command(wrapped): ${msg.payload.toHex()}")
@ -40,8 +50,19 @@ class Session(
val responseMsg = msgIO.receiveMessage()
val decrypted = enDecrypt.decrypt(responseMsg)
aapsLogger.debug(LTag.PUMPBTCOMM, "Received response: $decrypted")
val response = parseResponse(decrypted)
if (!responseType.isInstance(response)) {
if (response is AlarmStatusResponse) {
throw PodAlarmException(response)
}
if (response is NakResponse) {
throw NakResponseException(response)
}
throw IllegalResponseException(responseType, response)
}
sessionKeys.msgSequenceNumber++
val ack = getAck(responseMsg)
aapsLogger.debug(LTag.PUMPBTCOMM, "Sending ACK: ${ack.payload.toHex()} in packet $ack")
@ -49,11 +70,22 @@ class Session(
return response
}
@Throws(CouldNotParseResponseException::class, UnsupportedOperationException::class)
private fun parseResponse(decrypted: MessagePacket): Response {
val payload = parseKeys(arrayOf(RESPONSE_PREFIX), decrypted.payload)[0]
aapsLogger.info(LTag.PUMPBTCOMM, "Received decrypted response: ${payload.toHex()} in packet: $decrypted")
return NakResponse(payload)
val data = parseKeys(arrayOf(RESPONSE_PREFIX), decrypted.payload)[0]
aapsLogger.info(LTag.PUMPBTCOMM, "Received decrypted response: ${data.toHex()} in packet: $decrypted")
// TODO verify length
val uniqueId = data.copyOfRange(0, 4)
val lenghtAndSequenceNumber = data.copyOfRange(4, 6)
val payload = data.copyOfRange(6, data.size - 2)
val crc = data.copyOfRange(data.size - 2, data.size)
// TODO validate uniqueId, sequenceNumber and crc
return ResponseUtil.parseResponse(payload)
}
private fun getAck(response: MessagePacket): MessagePacket {

View file

@ -7,18 +7,57 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.
sealed class PodEvent {
/* BT connection events */
class AlreadyConnected(val bluetoothAddress: String) : PodEvent()
class AlreadyConnected(val bluetoothAddress: String) : PodEvent() {
override fun toString(): String {
return "AlreadyConnected(bluetoothAddress='$bluetoothAddress')"
}
}
object AlreadyPaired : PodEvent()
object Scanning : PodEvent()
object BluetoothConnecting : PodEvent()
class BluetoothConnected(val bluetoothAddress: String) : PodEvent()
class BluetoothConnected(val bluetoothAddress: String) : PodEvent() {
override fun toString(): String {
return "BluetoothConnected(bluetoothAddress='$bluetoothAddress')"
}
}
object Pairing : PodEvent()
class Paired(val uniqueId: Id) : PodEvent()
class Paired(val uniqueId: Id) : PodEvent() {
override fun toString(): String {
return "Paired(uniqueId=$uniqueId)"
}
}
object EstablishingSession : PodEvent()
object Connected : PodEvent()
/* Message exchange events */
class CommandSending(val command: Command) : PodEvent()
class CommandSent(val command: Command) : PodEvent()
class ResponseReceived(val response: Response) : PodEvent()
class CommandSending(val command: Command) : PodEvent() {
override fun toString(): String {
return "CommandSending(command=$command)"
}
}
class CommandSent(val command: Command) : PodEvent() {
override fun toString(): String {
return "CommandSent(command=$command)"
}
}
class ResponseReceived(
val command: Command,
val response: Response
) : PodEvent() {
override fun toString(): String {
return "ResponseReceived(command=$command, response=$response)"
}
}
}

View file

@ -14,11 +14,11 @@ class DeactivateCommand private constructor(
override val encoded: ByteArray
get() = appendCrc(
ByteBuffer.allocate(LENGTH + HEADER_LENGTH) //
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag)) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.putInt(nonce) //
ByteBuffer.allocate(LENGTH + HEADER_LENGTH)
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag))
.put(commandType.value)
.put(BODY_LENGTH)
.putInt(nonce)
.array()
)

View file

@ -15,11 +15,11 @@ class GetStatusCommand private constructor(
override val encoded: ByteArray
get() = appendCrc(
ByteBuffer.allocate(LENGTH + HEADER_LENGTH) //
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag)) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.put(statusResponseType.value) //
ByteBuffer.allocate(LENGTH + HEADER_LENGTH)
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag))
.put(commandType.value)
.put(BODY_LENGTH)
.put(statusResponseType.value)
.array()
)

View file

@ -13,11 +13,11 @@ class GetVersionCommand private constructor(
override val encoded: ByteArray
get() = appendCrc(
ByteBuffer.allocate(LENGTH + HEADER_LENGTH) //
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag)) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.putInt(uniqueId) //
ByteBuffer.allocate(LENGTH + HEADER_LENGTH)
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag))
.put(commandType.value)
.put(BODY_LENGTH)
.putInt(uniqueId)
.array()
)

View file

@ -27,10 +27,10 @@ class ProgramAlertsCommand private constructor(
override val encoded: ByteArray
get() {
val byteBuffer: ByteBuffer = ByteBuffer.allocate(getLength() + HEADER_LENGTH) //
.put(encodeHeader(uniqueId, sequenceNumber, getLength(), multiCommandFlag)) //
.put(commandType.value) //
.put(getBodyLength()) //
val byteBuffer: ByteBuffer = ByteBuffer.allocate(getLength() + HEADER_LENGTH)
.put(encodeHeader(uniqueId, sequenceNumber, getLength(), multiCommandFlag))
.put(commandType.value)
.put(getBodyLength())
.putInt(nonce)
for (configuration in alertConfigurations) {
byteBuffer.put(configuration.encoded)

View file

@ -36,12 +36,12 @@ class ProgramBasalCommand private constructor(
override val encoded: ByteArray
get() {
val buffer = ByteBuffer.allocate(length.toInt()) //
.put(commandType.value) //
.put(bodyLength) //
.put(programReminder.encoded) //
.put(currentInsulinProgramElementIndex) //
.putShort(remainingTenthPulsesInCurrentInsulinProgramElement) //
val buffer = ByteBuffer.allocate(length.toInt())
.put(commandType.value)
.put(bodyLength)
.put(programReminder.encoded)
.put(currentInsulinProgramElementIndex)
.putShort(remainingTenthPulsesInCurrentInsulinProgramElement)
.putInt(delayUntilNextTenthPulseInUsec)
for (insulinProgramElement in insulinProgramElements) {
buffer.put(insulinProgramElement.encoded)
@ -55,10 +55,10 @@ class ProgramBasalCommand private constructor(
multiCommandFlag
)
return appendCrc(
ByteBuffer.allocate(basalCommand.size + interlockCommand.size + header.size) //
.put(header) //
.put(interlockCommand) //
.put(basalCommand) //
ByteBuffer.allocate(basalCommand.size + interlockCommand.size + header.size)
.put(header)
.put(interlockCommand)
.put(basalCommand)
.array()
)
}

View file

@ -19,14 +19,14 @@ class ProgramBeepsCommand private constructor(
override val encoded: ByteArray
get() = appendCrc(
ByteBuffer.allocate(LENGTH + HEADER_LENGTH) //
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag)) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.put(immediateBeepType.value) //
.put(basalReminder.encoded) //
.put(tempBasalReminder.encoded) //
.put(bolusReminder.encoded) //
ByteBuffer.allocate(LENGTH + HEADER_LENGTH)
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag))
.put(commandType.value)
.put(BODY_LENGTH)
.put(immediateBeepType.value)
.put(basalReminder.encoded)
.put(tempBasalReminder.encoded)
.put(bolusReminder.encoded)
.array()
)

View file

@ -21,12 +21,12 @@ class ProgramBolusCommand private constructor(
override val encoded: ByteArray
get() {
val bolusCommand = ByteBuffer.allocate(LENGTH.toInt()) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.put(programReminder.encoded) //
.putShort(numberOfTenthPulses) //
.putInt(delayUntilFirstTenthPulseInUsec) //
val bolusCommand = ByteBuffer.allocate(LENGTH.toInt())
.put(commandType.value)
.put(BODY_LENGTH)
.put(programReminder.encoded)
.putShort(numberOfTenthPulses)
.putInt(delayUntilFirstTenthPulseInUsec)
.putShort(0.toShort()) // Extended bolus pulses
.putInt(0) // Delay between tenth extended pulses in usec
.array()
@ -38,10 +38,10 @@ class ProgramBolusCommand private constructor(
multiCommandFlag
)
return appendCrc(
ByteBuffer.allocate(header.size + interlockCommand.size + bolusCommand.size) //
.put(header) //
.put(interlockCommand) //
.put(bolusCommand) //
ByteBuffer.allocate(header.size + interlockCommand.size + bolusCommand.size)
.put(header)
.put(interlockCommand)
.put(bolusCommand)
.array()
)
}
@ -120,11 +120,11 @@ class ProgramBolusCommand private constructor(
private const val BODY_LENGTH: Byte = 13
private fun calculateChecksum(numberOfSlots: Byte, byte10And11: Short, numberOfPulses: Short): Short {
return MessageUtil.calculateChecksum(
ByteBuffer.allocate(7) //
.put(numberOfSlots) //
.putShort(byte10And11) //
.putShort(numberOfPulses) //
.putShort(numberOfPulses) //
ByteBuffer.allocate(7)
.put(numberOfSlots)
.putShort(byte10And11)
.putShort(numberOfPulses)
.putShort(numberOfPulses)
.array()
)
}

View file

@ -37,12 +37,12 @@ class ProgramInsulinCommand internal constructor(
override val encoded: ByteArray
get() {
val buffer = ByteBuffer.allocate(getLength().toInt()) //
.put(commandType.value) //
.put(getBodyLength()) //
.putInt(nonce) //
.put(deliveryType.getValue()) //
.putShort(checksum) //
val buffer = ByteBuffer.allocate(getLength().toInt())
.put(commandType.value)
.put(getBodyLength())
.putInt(nonce)
.put(deliveryType.getValue())
.putShort(checksum)
.put(byte9) // BASAL: currentSlot // BOLUS: number of ShortInsulinProgramElements
.putShort(byte10And11) // BASAL: remainingEighthSecondsInCurrentSlot // BOLUS: immediate pulses multiplied by delay between pulses in eighth seconds
.putShort(byte12And13) // BASAL: remainingPulsesInCurrentSlot // BOLUS: immediate pulses

View file

@ -103,12 +103,12 @@ class ProgramTempBasalCommand private constructor(
delayUntilNextTenthPulseInUsec =
(firstProgramElement.numberOfSlots.toLong() * 1800.0 / remainingTenthPulsesInFirstElement * 1000000).toInt()
}
val buffer = ByteBuffer.allocate(getLength().toInt()) //
.put(commandType.value) //
.put(getBodyLength()) //
.put(programReminder.encoded) //
val buffer = ByteBuffer.allocate(getLength().toInt())
.put(commandType.value)
.put(getBodyLength())
.put(programReminder.encoded)
.put(0x00.toByte()) // Current slot index
.putShort(remainingTenthPulsesInFirstElement) //
.putShort(remainingTenthPulsesInFirstElement)
.putInt(delayUntilNextTenthPulseInUsec)
for (element in insulinProgramElements) {
buffer.put(element.encoded)
@ -122,10 +122,10 @@ class ProgramTempBasalCommand private constructor(
multiCommandFlag
)
return appendCrc(
ByteBuffer.allocate(header.size + interlockCommand.size + tempBasalCommand.size) //
.put(header) //
.put(interlockCommand) //
.put(tempBasalCommand) //
ByteBuffer.allocate(header.size + interlockCommand.size + tempBasalCommand.size)
.put(header)
.put(interlockCommand)
.put(tempBasalCommand)
.array()
)
}

View file

@ -17,16 +17,16 @@ class SetUniqueIdCommand private constructor(
override val encoded: ByteArray
get() = appendCrc(
ByteBuffer.allocate(LENGTH + HEADER_LENGTH) //
.put(encodeHeader(DEFAULT_UNIQUE_ID, sequenceNumber, LENGTH, multiCommandFlag)) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.putInt(uniqueId) //
ByteBuffer.allocate(LENGTH + HEADER_LENGTH)
.put(encodeHeader(DEFAULT_UNIQUE_ID, sequenceNumber, LENGTH, multiCommandFlag))
.put(commandType.value)
.put(BODY_LENGTH)
.putInt(uniqueId)
.put(0x14.toByte()) // FIXME ??
.put(0x04.toByte()) // FIXME ??
.put(encodeInitializationTime(initializationTime)) //
.putInt(lotNumber) //
.putInt(podSequenceNumber) //
.put(encodeInitializationTime(initializationTime))
.putInt(lotNumber)
.putInt(podSequenceNumber)
.array()
)
@ -86,12 +86,12 @@ class SetUniqueIdCommand private constructor(
private fun encodeInitializationTime(date: Date): ByteArray {
val instance = Calendar.getInstance()
instance.time = date
return byteArrayOf( //
(instance[Calendar.MONTH] + 1).toByte(), //
instance[Calendar.DATE].toByte(), //
(instance[Calendar.YEAR] % 100).toByte(), //
instance[Calendar.HOUR_OF_DAY].toByte(), //
instance[Calendar.MINUTE].toByte() //
return byteArrayOf(
(instance[Calendar.MONTH] + 1).toByte(),
instance[Calendar.DATE].toByte(),
(instance[Calendar.YEAR] % 100).toByte(),
instance[Calendar.HOUR_OF_DAY].toByte(),
instance[Calendar.MINUTE].toByte()
)
}
}

View file

@ -19,12 +19,12 @@ class SilenceAlertsCommand private constructor(
override val encoded: ByteArray
get() =
appendCrc(
ByteBuffer.allocate(LENGTH + HEADER_LENGTH) //
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag)) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.putInt(nonce) //
.put(AlertUtil.encodeAlertSet(alertTypes)) //
ByteBuffer.allocate(LENGTH + HEADER_LENGTH)
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag))
.put(commandType.value)
.put(BODY_LENGTH)
.putInt(nonce)
.put(AlertUtil.encodeAlertSet(alertTypes))
.array()
)

View file

@ -20,12 +20,12 @@ class StopDeliveryCommand private constructor(
override val encoded: ByteArray
get() {
return appendCrc(
ByteBuffer.allocate(LENGTH + HEADER_LENGTH) //
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag)) //
.put(commandType.value) //
.put(BODY_LENGTH) //
.putInt(nonce) //
.put((beepType.value.toInt() shl 4 or deliveryType.encoded[0].toInt()).toByte()) //
ByteBuffer.allocate(LENGTH + HEADER_LENGTH)
.put(encodeHeader(uniqueId, sequenceNumber, LENGTH, multiCommandFlag))
.put(commandType.value)
.put(BODY_LENGTH)
.putInt(nonce)
.put((beepType.value.toInt() shl 4 or deliveryType.encoded[0].toInt()).toByte())
.array()
)
}

View file

@ -13,9 +13,9 @@ abstract class HeaderEnabledCommand protected constructor(
companion object {
internal fun appendCrc(command: ByteArray): ByteArray =
ByteBuffer.allocate(command.size + 2) //
.put(command) //
.putShort(MessageUtil.createCrc(command)) //
ByteBuffer.allocate(command.size + 2)
.put(command)
.putShort(MessageUtil.createCrc(command))
.array()
internal fun encodeHeader(
@ -24,9 +24,9 @@ abstract class HeaderEnabledCommand protected constructor(
length: Short,
multiCommandFlag: Boolean
): ByteArray =
ByteBuffer.allocate(6) //
.putInt(uniqueId) //
.putShort((sequenceNumber.toInt() and 0x0f shl 10 or length.toInt() or ((if (multiCommandFlag) 1 else 0) shl 15)).toShort()) //
ByteBuffer.allocate(6)
.putInt(uniqueId)
.putShort((sequenceNumber.toInt() and 0x0f shl 10 or length.toInt() or ((if (multiCommandFlag) 1 else 0) shl 15)).toShort())
.array()
internal const val HEADER_LENGTH: Short = 6

View file

@ -12,9 +12,9 @@ open class BasalInsulinProgramElement(
) : Encodable, Serializable {
override val encoded: ByteArray
get() = ByteBuffer.allocate(6) //
.putShort(totalTenthPulses) //
.putInt(if (totalTenthPulses.toInt() == 0) Int.MIN_VALUE or delayBetweenTenthPulsesInUsec else delayBetweenTenthPulsesInUsec) //
get() = ByteBuffer.allocate(6)
.putShort(totalTenthPulses)
.putInt(if (totalTenthPulses.toInt() == 0) Int.MIN_VALUE or delayBetweenTenthPulsesInUsec else delayBetweenTenthPulsesInUsec)
.array()
val durationInSeconds: Short
get() = (numberOfSlots * 1800).toShort()

View file

@ -12,13 +12,13 @@ class BasalShortInsulinProgramElement(
override val encoded: ByteArray
get() {
val firstByte = (
numberOfSlots - 1 and 0x0f shl 4 //
or ((if (extraAlternatePulse) 1 else 0) shl 3) //
numberOfSlots - 1 and 0x0f shl 4
or ((if (extraAlternatePulse) 1 else 0) shl 3)
or (pulsesPerSlot.toInt() ushr 8 and 0x03)
).toByte()
return ByteBuffer.allocate(2) //
.put(firstByte) //
.put((pulsesPerSlot and 0xff).toByte()) //
return ByteBuffer.allocate(2)
.put(firstByte)
.put((pulsesPerSlot and 0xff).toByte())
.array()
}

View file

@ -13,10 +13,10 @@ class TempBasalInsulinProgramElement(
val buffer = ByteBuffer.allocate(6)
if (totalTenthPulses.toInt() == 0) {
val i = (durationInSeconds.toDouble() * 1000000.0 / numberOfSlots.toDouble()).toInt() or Int.MIN_VALUE
buffer.putShort(numberOfSlots.toShort()) //
buffer.putShort(numberOfSlots.toShort())
.putInt(i)
} else {
buffer.putShort(totalTenthPulses) //
buffer.putShort(totalTenthPulses)
.putInt(delayBetweenTenthPulsesInUsec)
}
return buffer.array()

View file

@ -56,6 +56,7 @@ object ProgramBasalUtil {
fun mapPulsesPerSlotToShortInsulinProgramElements(pulsesPerSlot: ShortArray?): List<ShortInsulinProgramElement> {
require(pulsesPerSlot!!.size <= NUMBER_OF_BASAL_SLOTS) { "Basal program must contain at most 48 slots" }
val elements: MutableList<ShortInsulinProgramElement> = ArrayList()
var extraAlternatePulse = false
var previousPulsesPerSlot: Short = 0
@ -84,7 +85,7 @@ object ProgramBasalUtil {
extraAlternatePulse = false
}
currentTotalNumberOfSlots++
} else if (numberOfSlotsInCurrentElement.toInt() == 1 && pulsesPerSlot[currentTotalNumberOfSlots.toInt()].toInt() == previousPulsesPerSlot + 1) {
} else if (numberOfSlotsInCurrentElement.toInt() == 1 && !extraAlternatePulse && pulsesPerSlot[currentTotalNumberOfSlots.toInt()].toInt() == previousPulsesPerSlot + 1) {
// Second slot of segment with extra alternate pulse
var expectAlternatePulseForNextSegment = false
currentTotalNumberOfSlots++
@ -94,10 +95,10 @@ object ProgramBasalUtil {
// Loop rest alternate pulse segment
if (pulsesPerSlot[currentTotalNumberOfSlots.toInt()].toInt() == previousPulsesPerSlot + (if (expectAlternatePulseForNextSegment) 1 else 0)) {
// Still in alternate pulse segment
currentTotalNumberOfSlots++
expectAlternatePulseForNextSegment = !expectAlternatePulseForNextSegment
if (numberOfSlotsInCurrentElement < MAX_NUMBER_OF_SLOTS_IN_INSULIN_PROGRAM_ELEMENT) {
numberOfSlotsInCurrentElement++
currentTotalNumberOfSlots++
} else {
// End of alternate pulse segment (no slots left in element)
elements.add(
@ -110,6 +111,7 @@ object ProgramBasalUtil {
previousPulsesPerSlot = pulsesPerSlot[currentTotalNumberOfSlots.toInt()]
numberOfSlotsInCurrentElement = 1
extraAlternatePulse = false
currentTotalNumberOfSlots++
break
}
} else {
@ -193,6 +195,7 @@ object ProgramBasalUtil {
val hourOfDay = instance[Calendar.HOUR_OF_DAY]
val minuteOfHour = instance[Calendar.MINUTE]
val secondOfMinute = instance[Calendar.SECOND]
val index = ((hourOfDay * 60 + minuteOfHour) / 30).toByte()
val secondOfDay = secondOfMinute + hourOfDay * 3600 + minuteOfHour * 60
val secondsRemaining = ((index + 1) * 1800 - secondOfDay).toShort()
@ -245,9 +248,9 @@ object ProgramBasalUtil {
}
fun calculateChecksum(pulsesPerSlot: ShortArray?, currentSlot: CurrentSlot): Short {
val buffer = ByteBuffer.allocate(1 + 2 + 2 + NUMBER_OF_BASAL_SLOTS * 2) //
.put(currentSlot.index) //
.putShort(currentSlot.pulsesRemaining) //
val buffer = ByteBuffer.allocate(1 + 2 + 2 + NUMBER_OF_BASAL_SLOTS * 2)
.put(currentSlot.index)
.putShort(currentSlot.pulsesRemaining)
.putShort(currentSlot.eighthSecondsRemaining)
for (pulses in pulsesPerSlot!!) {
buffer.putShort(pulses)

View file

@ -53,9 +53,9 @@ object ProgramTempBasalUtil {
}
fun calculateChecksum(totalNumberOfSlots: Byte, pulsesInFirstSlot: Short, pulsesPerSlot: ShortArray?): Short {
val buffer = ByteBuffer.allocate(1 + 2 + 2 + 2 * pulsesPerSlot!!.size) //
.put(totalNumberOfSlots) //
.putShort(0x3840.toShort()) //
val buffer = ByteBuffer.allocate(1 + 2 + 2 + 2 * pulsesPerSlot!!.size)
.put(totalNumberOfSlots)
.putShort(0x3840.toShort())
.putShort(pulsesInFirstSlot)
for (pulses in pulsesPerSlot) {
buffer.putShort(pulses)

View file

@ -26,9 +26,9 @@ class AlertConfiguration(
firstByte = firstByte or (1 shl 1)
}
firstByte = firstByte or ((durationInMinutes.toInt() shr 8 and 0x01).toByte())
return ByteBuffer.allocate(6) //
return ByteBuffer.allocate(6)
.put(firstByte)
.put(durationInMinutes.toByte()) //
.put(durationInMinutes.toByte())
.putShort(
when (trigger) {
is AlertTrigger.ReservoirVolumeTrigger -> {
@ -39,9 +39,9 @@ class AlertConfiguration(
trigger.offsetInMinutes
}
}
) //
.put(beepRepetition.value) //
.put(beepType.value) //
)
.put(beepRepetition.value)
.put(beepType.value)
.array()
}

View file

@ -17,4 +17,8 @@ class ProgramReminder(
or ((atInterval and 0x3f).toInt())
).toByte()
)
override fun toString(): String {
return "ProgramReminder(atStart=$atStart, atEnd=$atEnd, atInterval=$atInterval)"
}
}

View file

@ -216,8 +216,8 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
override fun updateFromSetUniqueIdResponse(response: SetUniqueIdResponse) {
podState.pulseRate = response.pumpRate
podState.primePulseRate = response.primePumpRate
podState.firstPrimeBolusVolume = response.numberOfPrimePulses
podState.secondPrimeBolusVolume = response.numberOfEngagingClutchDrivePulses
podState.firstPrimeBolusVolume = response.numberOfEngagingClutchDrivePulses
podState.secondPrimeBolusVolume = response.numberOfPrimePulses
podState.podLifeInHours = response.podExpirationTimeInHours
podState.bleVersion = SoftwareVersion(
response.bleVersionMajor,

View file

@ -25,9 +25,6 @@ import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject
/**
* Created by andy on 30/08/2019
*/
class DashPodManagementActivity : NoSplashAppCompatActivity() {
@Inject lateinit var rxBus: RxBusWrapper
@ -70,7 +67,7 @@ class DashPodManagementActivity : NoSplashAppCompatActivity() {
this,
resourceHelper.gs(R.string.omnipod_common_pod_management_discard_pod_confirmation),
Thread {
// TODO discard Pod
podStateManager.reset()
}
)
}

View file

@ -295,7 +295,10 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
// total delivered
podInfoBinding.totalDelivered.text =
if (podStateManager.isActivationCompleted && podStateManager.pulsesDelivered != null) {
resourceHelper.gs(R.string.omnipod_common_overview_total_delivered_value, podStateManager.pulseRate)
resourceHelper.gs(
R.string.omnipod_common_overview_total_delivered_value,
podStateManager.pulsesDelivered!! * 0.05
)
} else {
PLACEHOLDER
}
@ -313,7 +316,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
podInfoBinding.reservoir.text = resourceHelper.gs(
R.string.omnipod_common_overview_reservoir_value,
podStateManager.pulsesRemaining
(podStateManager.pulsesRemaining!! * 0.05)
)
podInfoBinding.reservoir.setTextColor(
if (podStateManager.pulsesRemaining!! < lowReservoirThreshold) {

View file

@ -32,7 +32,14 @@ class DashInsertCannulaViewModel @Inject constructor(
if (profile == null) {
source.onError(IllegalStateException("No profile set"))
} else {
val disposable = omnipodManager.activatePodPart2(mapProfileToBasalProgram(profile)).subscribeBy(
val basalProgram = mapProfileToBasalProgram(profile)
logger.debug(
LTag.PUMPCOMM,
"Mapped profile to basal program. profile={}, basalProgram={}",
profile,
basalProgram
)
val disposable = omnipodManager.activatePodPart2(basalProgram).subscribeBy(
onNext = { podEvent ->
logger.debug(
LTag.PUMP,

View file

@ -4,24 +4,43 @@ import androidx.annotation.StringRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.pump.omnipod.common.R
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.deactivation.viewmodel.action.DeactivatePodViewModel
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.OmnipodDashManager
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
import io.reactivex.Single
import io.reactivex.rxkotlin.subscribeBy
import javax.inject.Inject
class DashDeactivatePodViewModel @Inject constructor(
private val omnipodManager: OmnipodDashManager,
private val podStateManager: OmnipodDashPodStateManager,
injector: HasAndroidInjector,
logger: AAPSLogger
) : DeactivatePodViewModel(injector, logger) {
override fun doExecuteAction(): Single<PumpEnactResult> = Single.just(
PumpEnactResult(injector).success(true).comment("TODO")
) // TODO
override fun doExecuteAction(): Single<PumpEnactResult> = Single.create { source ->
omnipodManager.deactivatePod().subscribeBy(
onNext = { podEvent ->
logger.debug(
LTag.PUMP,
"Received PodEvent in Pod deactivation: $podEvent"
)
},
onError = { throwable ->
logger.error(LTag.PUMP, "Error in Pod deactivation", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).comment(throwable.message))
},
onComplete = {
logger.debug("Pod deactivation completed")
source.onSuccess(PumpEnactResult(injector).success(true))
}
)
}
override fun discardPod() {
// TODO
podStateManager.reset()
}
@StringRes

View file

@ -41,7 +41,7 @@ fun mapProfileToBasalProgram(profile: Profile): BasalProgram {
}
if (entries.size == 0 && basalValue.timeAsSeconds != 0) {
throw java.lang.IllegalArgumentException("First basal segment start time should be 0")
throw IllegalArgumentException("First basal segment start time should be 0")
}
if (entries.size > 0 && entries[entries.size - 1].endSlotIndex != startSlotIndex) {

View file

@ -119,6 +119,7 @@
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<!-- FIXME visible for development -->
<info.nightscout.androidaps.utils.ui.SingleClickButton
android:id="@+id/button_discard_pod"
style="?android:attr/buttonStyle"
@ -127,7 +128,7 @@
android:drawableTop="@drawable/ic_pod_management_discard_pod"
android:text="@string/omnipod_common_pod_management_button_discard_pod"
android:textAllCaps="false"
android:visibility="gone"
android:visibility="visible"
app:layout_constrainedHeight="@+id/Actions_Row_2_horizontal_guideline"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/Actions_Col_1_Row_2_vertical_guideline"

View file

@ -4,7 +4,7 @@ import com.google.crypto.tink.subtle.Hex
import info.nightscout.androidaps.logging.AAPSLoggerTest
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id
import info.nightscout.androidaps.utils.extensions.toHex
import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Test
class MessagePacketTest {

View file

@ -7,6 +7,7 @@ import org.junit.Test
import java.util.*
class PayloadSplitJoinTest {
private val random = Random(42)
@Test fun testSplitAndJoinBack() {

View file

@ -7,6 +7,7 @@ import org.junit.Assert.assertTrue
import org.junit.Test
class PayloadSplitterTest {
@Test fun testSplitter() {
val f1 = "00,01,54,57,10,23,03,00,00,c0,ff,ff,ff,fe,08,20,2e,a8,50,30".replace(",", "")
val f2 = "01,04,bc,20,1f,f6,3d,00,01,a5,ff,ff,ff,fe,08,20,2e,a8,50,30".replace(",", "")

View file

@ -2,10 +2,11 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message
import com.google.crypto.tink.subtle.Hex
import info.nightscout.androidaps.utils.extensions.toHex
import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Test
class StringLengthPrefixEncodingTest {
private val p0Payload = Hex.decode("50,30,3d,00,01,a5".replace(",", "")) // from logs
private val p0Content = Hex.decode("a5")

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.session
import info.nightscout.androidaps.logging.AAPSLoggerTest
import info.nightscout.androidaps.utils.extensions.toHex
import org.junit.Assert
import org.junit.Assert.*
import org.junit.Test
import org.spongycastle.util.encoders.Hex
@ -11,7 +10,8 @@ class EapMessageTest {
@Test fun testParseAndBack() {
val aapsLogger = AAPSLoggerTest()
val payload = Hex.decode("01bd0038170100000205000000c55c78e8d3b9b9e935860a7259f6c001050000c2cd1248451103bd77a6c7ef88c441ba7e0200006cff5d18")
val payload =
Hex.decode("01bd0038170100000205000000c55c78e8d3b9b9e935860a7259f6c001050000c2cd1248451103bd77a6c7ef88c441ba7e0200006cff5d18")
val eapMsg = EapMessage.parse(aapsLogger, payload)
val back = eapMsg.toByteArray()
Assert.assertEquals(back.toHex(), payload.toHex())

View file

@ -8,11 +8,11 @@ import org.junit.Test
class DeactivateCommandTest {
@Test @Throws(DecoderException::class) fun testEncoding() {
val encoded = DeactivateCommand.Builder() //
.setUniqueId(37879809) //
.setSequenceNumber(5.toShort()) //
.setNonce(1229869870) //
.build() //
val encoded = DeactivateCommand.Builder()
.setUniqueId(37879809)
.setSequenceNumber(5.toShort())
.setNonce(1229869870)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("0242000114061C04494E532E001C"), encoded)

View file

@ -9,11 +9,11 @@ import org.junit.Test
class GetStatusCommandTest {
@Test @Throws(DecoderException::class) fun testGetDefaultStatusResponse() {
val encoded = GetStatusCommand.Builder() //
.setUniqueId(37879810) //
.setSequenceNumber(15.toShort()) //
.setStatusResponseType(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE) //
.build() //
val encoded = GetStatusCommand.Builder()
.setUniqueId(37879810)
.setSequenceNumber(15.toShort())
.setStatusResponseType(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("024200023C030E0100024C"), encoded)

View file

@ -8,10 +8,10 @@ import org.junit.Test
class GetVersionCommandTest {
@Test @Throws(DecoderException::class) fun testEncoding() {
val encoded = GetVersionCommand.Builder() //
.setSequenceNumber(0.toShort()) //
.setUniqueId(GetVersionCommand.DEFAULT_UNIQUE_ID) //
.build() //
val encoded = GetVersionCommand.Builder()
.setSequenceNumber(0.toShort())
.setUniqueId(GetVersionCommand.DEFAULT_UNIQUE_ID)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("FFFFFFFF00060704FFFFFFFF82B2"), encoded)

View file

@ -15,16 +15,36 @@ class ProgramAlertsCommandTest {
@Test @Throws(DecoderException::class) fun testExpirationAlerts() {
val configurations: MutableList<AlertConfiguration> = ArrayList()
configurations.add(AlertConfiguration(AlertType.EXPIRATION, true, 420.toShort(), false, AlertTrigger.TimerTrigger(4305.toShort()), BeepType.FOUR_TIMES_BIP_BEEP, BeepRepetitionType.XXX3))
configurations.add(AlertConfiguration(AlertType.EXPIRATION_IMMINENT, true, 0.toShort(), false, AlertTrigger.TimerTrigger(4725.toShort()), BeepType.FOUR_TIMES_BIP_BEEP, BeepRepetitionType.XXX4))
configurations.add(
AlertConfiguration(
AlertType.EXPIRATION,
true,
420.toShort(),
false,
AlertTrigger.TimerTrigger(4305.toShort()),
BeepType.FOUR_TIMES_BIP_BEEP,
BeepRepetitionType.XXX3
)
)
configurations.add(
AlertConfiguration(
AlertType.EXPIRATION_IMMINENT,
true,
0.toShort(),
false,
AlertTrigger.TimerTrigger(4725.toShort()),
BeepType.FOUR_TIMES_BIP_BEEP,
BeepRepetitionType.XXX4
)
)
val encoded = ProgramAlertsCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(3.toShort()) //
.setMultiCommandFlag(true) //
.setNonce(1229869870) //
.setAlertConfigurations(configurations) //
.build() //
val encoded = ProgramAlertsCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(3.toShort())
.setMultiCommandFlag(true)
.setNonce(1229869870)
.setAlertConfigurations(configurations)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("024200038C121910494E532E79A410D1050228001275060280F5"), encoded)
@ -32,14 +52,24 @@ class ProgramAlertsCommandTest {
@Test @Throws(DecoderException::class) fun testLowReservoirAlert() {
val configurations: MutableList<AlertConfiguration> = ArrayList()
configurations.add(AlertConfiguration(AlertType.LOW_RESERVOIR, true, 0.toShort(), false, AlertTrigger.ReservoirVolumeTrigger(200.toShort()), BeepType.FOUR_TIMES_BIP_BEEP, BeepRepetitionType.XXX))
configurations.add(
AlertConfiguration(
AlertType.LOW_RESERVOIR,
true,
0.toShort(),
false,
AlertTrigger.ReservoirVolumeTrigger(200.toShort()),
BeepType.FOUR_TIMES_BIP_BEEP,
BeepRepetitionType.XXX
)
)
val encoded = ProgramAlertsCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(8.toShort()) //
.setNonce(1229869870) //
.setAlertConfigurations(configurations) //
.build() //
val encoded = ProgramAlertsCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(8.toShort())
.setNonce(1229869870)
.setAlertConfigurations(configurations)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("02420003200C190A494E532E4C0000C801020149"), encoded)
@ -47,14 +77,24 @@ class ProgramAlertsCommandTest {
@Test @Throws(DecoderException::class) fun testUserExpirationAlert() {
val configurations: MutableList<AlertConfiguration> = ArrayList()
configurations.add(AlertConfiguration(AlertType.USER_SET_EXPIRATION, true, 0.toShort(), false, AlertTrigger.TimerTrigger(4079.toShort()), BeepType.FOUR_TIMES_BIP_BEEP, BeepRepetitionType.XXX2))
configurations.add(
AlertConfiguration(
AlertType.USER_SET_EXPIRATION,
true,
0.toShort(),
false,
AlertTrigger.TimerTrigger(4079.toShort()),
BeepType.FOUR_TIMES_BIP_BEEP,
BeepRepetitionType.XXX2
)
)
val encoded = ProgramAlertsCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(15.toShort()) //
.setNonce(1229869870) //
.setAlertConfigurations(configurations) //
.build() //
val encoded = ProgramAlertsCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(15.toShort())
.setNonce(1229869870)
.setAlertConfigurations(configurations)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("024200033C0C190A494E532E38000FEF030203E2"), encoded)
@ -62,15 +102,25 @@ class ProgramAlertsCommandTest {
@Test @Throws(DecoderException::class) fun testLumpOfCoalAlert() {
val configurations: MutableList<AlertConfiguration> = ArrayList()
configurations.add(AlertConfiguration(AlertType.EXPIRATION, true, 55.toShort(), false, AlertTrigger.TimerTrigger(5.toShort()), BeepType.FOUR_TIMES_BIP_BEEP, BeepRepetitionType.XXX5))
configurations.add(
AlertConfiguration(
AlertType.EXPIRATION,
true,
55.toShort(),
false,
AlertTrigger.TimerTrigger(5.toShort()),
BeepType.FOUR_TIMES_BIP_BEEP,
BeepRepetitionType.XXX5
)
)
val encoded = ProgramAlertsCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(10.toShort()) //
.setMultiCommandFlag(false) //
.setNonce(1229869870) //
.setAlertConfigurations(configurations) //
.build() //
val encoded = ProgramAlertsCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(10.toShort())
.setMultiCommandFlag(false)
.setNonce(1229869870)
.setAlertConfigurations(configurations)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("02420003280C190A494E532E7837000508020356"), encoded)

View file

@ -15,18 +15,45 @@ class ProgramBasalCommandTest {
BasalProgram.Segment(0.toShort(), 48.toShort(), 300)
)
val basalProgram = BasalProgram(segments)
val date = Date(2021, 1, 17, 14, 47, 43)
val date = Date(121, 1, 17, 14, 47, 43)
val encoded = ProgramBasalCommand.Builder() //
.setUniqueId(37879809) //
.setNonce(1229869870) //
.setSequenceNumber(10.toShort()) //
.setBasalProgram(basalProgram) //
.setCurrentTime(date) //
.setProgramReminder(ProgramReminder(false, true, 0.toByte())) //
.build() //
val encoded = ProgramBasalCommand.Builder()
.setUniqueId(37879809)
.setNonce(1229869870)
.setSequenceNumber(10.toShort())
.setBasalProgram(basalProgram)
.setCurrentTime(date)
.setProgramReminder(ProgramReminder(false, true, 0.toByte()))
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("0242000128241A12494E532E0005E81D1708000CF01EF01EF01E130E40001593004C4B403840005B8D80827C"), encoded)
Assert.assertArrayEquals(
Hex.decodeHex("0242000128241A12494E532E0005E81D1708000CF01EF01EF01E130E40001593004C4B403840005B8D80827C"),
encoded
)
}
@Test @Throws(DecoderException::class) fun testProgramBasalCommandWithExtraAlternateSegmentPulse() {
val segments = listOf(
BasalProgram.Segment(0.toShort(), 48.toShort(), 5)
)
val basalProgram = BasalProgram(segments)
val date = Date(121, 0, 30, 23, 21, 46)
val encoded = ProgramBasalCommand.Builder()
.setUniqueId(4241)
.setNonce(1229869870)
.setSequenceNumber(12.toShort())
.setBasalProgram(basalProgram)
.setCurrentTime(date)
.setProgramReminder(ProgramReminder(atStart = false, atEnd = false, atInterval = 0.toByte()))
.build()
.encoded
Assert.assertArrayEquals(
Hex.decodeHex("0000109130241a12494e532e0000c52e0f700000f800f800f800130e0000000707fcad8000f015752a00033b"),
encoded
)
}
}

View file

@ -10,14 +10,14 @@ import org.junit.Test
class ProgramBeepsCommandTest {
@Test @Throws(DecoderException::class) fun testPlayTestBeep() {
val encoded = ProgramBeepsCommand.Builder() //
.setUniqueId(37879810) //
.setSequenceNumber(11.toShort()) //
.setImmediateBeepType(BeepType.FOUR_TIMES_BIP_BEEP) //
.setBasalReminder(ProgramReminder(false, false, 0.toByte())) //
.setTempBasalReminder(ProgramReminder(false, false, 0.toByte())) //
.setBolusReminder(ProgramReminder(false, false, 0.toByte())) //
.build() //
val encoded = ProgramBeepsCommand.Builder()
.setUniqueId(37879810)
.setSequenceNumber(11.toShort())
.setImmediateBeepType(BeepType.FOUR_TIMES_BIP_BEEP)
.setBasalReminder(ProgramReminder(false, false, 0.toByte()))
.setTempBasalReminder(ProgramReminder(false, false, 0.toByte()))
.setBolusReminder(ProgramReminder(false, false, 0.toByte()))
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("024200022C061E0402000000800F"), encoded)

View file

@ -9,16 +9,19 @@ import org.junit.Test
class ProgramBolusCommandTest {
@Test @Throws(DecoderException::class) fun testProgramBolusCommand() {
val encoded = ProgramBolusCommand.Builder() //
.setNumberOfUnits(5.0) //
.setProgramReminder(ProgramReminder(false, true, 0.toByte())) //
.setDelayBetweenPulsesInEighthSeconds(16.toByte()) //
.setUniqueId(37879809) //
.setSequenceNumber(14.toShort()) //
.setNonce(1229869870) //
.build() //
val encoded = ProgramBolusCommand.Builder()
.setNumberOfUnits(5.0)
.setProgramReminder(ProgramReminder(false, true, 0.toByte()))
.setDelayBetweenPulsesInEighthSeconds(16.toByte())
.setUniqueId(37879809)
.setSequenceNumber(14.toShort())
.setNonce(1229869870)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("02420001381F1A0E494E532E02010F01064000640064170D4003E800030D4000000000000080F6"), encoded)
Assert.assertArrayEquals(
Hex.decodeHex("02420001381F1A0E494E532E02010F01064000640064170D4003E800030D4000000000000080F6"),
encoded
)
}
}

View file

@ -9,28 +9,34 @@ import org.junit.Test
class ProgramTempBasalCommandTest {
@Test @Throws(DecoderException::class) fun testExtraAlternateSegmentPulseTempBasal() {
val command = ProgramTempBasalCommand.Builder() //
.setUniqueId(37879809) //
.setNonce(1229869870) //
.setSequenceNumber(15.toShort()) //
.setRateInUnitsPerHour(5.05) //
.setDurationInMinutes(60.toShort()) //
.setProgramReminder(ProgramReminder(false, true, 0.toByte())) //
val command = ProgramTempBasalCommand.Builder()
.setUniqueId(37879809)
.setNonce(1229869870)
.setSequenceNumber(15.toShort())
.setRateInUnitsPerHour(5.05)
.setDurationInMinutes(60.toShort())
.setProgramReminder(ProgramReminder(false, true, 0.toByte()))
.build()
Assert.assertArrayEquals(Hex.decodeHex("024200013C201A0E494E532E01011102384000321832160E400003F20036634403F20036634482A6"), command.encoded)
Assert.assertArrayEquals(
Hex.decodeHex("024200013C201A0E494E532E01011102384000321832160E400003F20036634403F20036634482A6"),
command.encoded
)
}
@Test @Throws(DecoderException::class) fun testZeroTempBasal() {
val command = ProgramTempBasalCommand.Builder() //
.setUniqueId(37879809) //
.setNonce(1229869870) //
.setSequenceNumber(7.toShort()) //
.setRateInUnitsPerHour(0.0) //
.setDurationInMinutes(300.toShort()) //
.setProgramReminder(ProgramReminder(true, true, 0.toByte())) //
val command = ProgramTempBasalCommand.Builder()
.setUniqueId(37879809)
.setNonce(1229869870)
.setSequenceNumber(7.toShort())
.setRateInUnitsPerHour(0.0)
.setDurationInMinutes(300.toShort())
.setProgramReminder(ProgramReminder(true, true, 0.toByte()))
.build()
Assert.assertArrayEquals(Hex.decodeHex("024200011C201A0E494E532E0100820A384000009000160EC000000A6B49D200000AEB49D20001DE"), command.encoded)
Assert.assertArrayEquals(
Hex.decodeHex("024200011C201A0E494E532E0100820A384000009000160EC000000A6B49D200000AEB49D20001DE"),
command.encoded
)
}
}

View file

@ -9,13 +9,13 @@ import java.util.*
class SetUniqueIdCommandTest {
@Test @Throws(DecoderException::class) fun testEncoding() {
val encoded = SetUniqueIdCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(6.toShort()) //
.setLotNumber(135556289) //
.setPodSequenceNumber(681767) //
.setInitializationTime(Date(2021, 1, 10, 14, 41)) //
.build() //
val encoded = SetUniqueIdCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(6.toShort())
.setLotNumber(135556289)
.setPodSequenceNumber(681767)
.setInitializationTime(Date(2021, 1, 10, 14, 41))
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("FFFFFFFF18150313024200031404020A150E2908146CC1000A67278344"), encoded)

View file

@ -10,12 +10,12 @@ import java.util.*
class SilenceAlertsCommandTest {
@Test @Throws(DecoderException::class) fun testSilenceLowReservoirAlert() {
val encoded = SilenceAlertsCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(1.toShort()) //
.setNonce(1229869870) //
.setAlertTypes(EnumSet.of(AlertType.LOW_RESERVOIR)) //
.build() //
val encoded = SilenceAlertsCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(1.toShort())
.setNonce(1229869870)
.setAlertTypes(EnumSet.of(AlertType.LOW_RESERVOIR))
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("0242000304071105494E532E1081CE"), encoded)

View file

@ -9,26 +9,26 @@ import org.junit.Test
class StopDeliveryCommandTest {
@Test @Throws(DecoderException::class) fun testStopTempBasal() {
val encoded = StopDeliveryCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(0.toShort()) //
.setNonce(1229869870) //
.setDeliveryType(StopDeliveryCommand.DeliveryType.TEMP_BASAL) //
.setBeepType(BeepType.LONG_SINGLE_BEEP) //
.build() //
val encoded = StopDeliveryCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(0.toShort())
.setNonce(1229869870)
.setDeliveryType(StopDeliveryCommand.DeliveryType.TEMP_BASAL)
.setBeepType(BeepType.LONG_SINGLE_BEEP)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("0242000300071F05494E532E6201B1"), encoded)
}
@Test @Throws(DecoderException::class) fun testSuspendDelivery() {
val encoded = StopDeliveryCommand.Builder() //
.setUniqueId(37879811) //
.setSequenceNumber(2.toShort()) //
.setNonce(1229869870) //
.setDeliveryType(StopDeliveryCommand.DeliveryType.ALL) //
.setBeepType(BeepType.SILENT) //
.build() //
val encoded = StopDeliveryCommand.Builder()
.setUniqueId(37879811)
.setSequenceNumber(2.toShort())
.setNonce(1229869870)
.setDeliveryType(StopDeliveryCommand.DeliveryType.ALL)
.setBeepType(BeepType.SILENT)
.build()
.encoded
Assert.assertArrayEquals(Hex.decodeHex("0242000308071F05494E532E078287"), encoded)