From 9b7d31ba2ced994b2779e1d2fe71aa355063aa48 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Thu, 25 Feb 2021 19:19:06 +0100 Subject: [PATCH] ble: address review comments: renames and removed unused vars --- .../dash/driver/comm/CharacteristicType.kt | 2 +- .../dash/driver/comm/ServiceDiscoverer.kt | 5 ++- .../driver/comm/command/BleCommandType.kt | 1 + .../driver/comm/scan/BleDiscoveredDevice.kt | 32 +++++++++---------- .../dash/driver/comm/scan/ScanCollector.kt | 1 - 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/CharacteristicType.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/CharacteristicType.kt index a68ccba448..c241a6f43b 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/CharacteristicType.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/CharacteristicType.kt @@ -6,7 +6,7 @@ import java.util.* enum class CharacteristicType(val value: String) { CMD("1a7e2441-e3ed-4464-8b7e-751e03d0dc5f"), DATA("1a7e2442-e3ed-4464-8b7e-751e03d0dc5f"); - val uUID: UUID + val uuid: UUID get() = UUID( BigInteger(value.replace("-", "").substring(0, 16), 16).toLong(), BigInteger(value.replace("-", "").substring(16), 16).toLong() diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt index ccf7298a6e..997a3ee1cf 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt @@ -4,7 +4,6 @@ import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.CharacteristicType import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.callbacks.BleCommCallbacks import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CharacteristicNotFoundException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.ServiceNotFoundException @@ -24,9 +23,9 @@ class ServiceDiscoverer(private val logger: AAPSLogger, private val gatt: Blueto logger.debug(LTag.PUMPBTCOMM, "Services discovered") val service = gatt.getService(SERVICE_UUID.toUuid()) ?: throw ServiceNotFoundException(SERVICE_UUID) - val cmdChar = service.getCharacteristic(CharacteristicType.CMD.uUID) + val cmdChar = service.getCharacteristic(CharacteristicType.CMD.uuid) ?: throw CharacteristicNotFoundException(CharacteristicType.CMD.value) - val dataChar = service.getCharacteristic(CharacteristicType.DATA.uUID) // TODO: this is never used + val dataChar = service.getCharacteristic(CharacteristicType.DATA.uuid) // TODO: this is never used ?: throw CharacteristicNotFoundException(CharacteristicType.DATA.value) var chars = mapOf(CharacteristicType.CMD to cmdChar, CharacteristicType.DATA to dataChar) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt index ad3cb86fe2..9fb461b02d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/command/BleCommandType.kt @@ -5,6 +5,7 @@ enum class BleCommandType(val value: Byte) { companion object { + @JvmStatic fun byValue(value: Byte): BleCommandType = BleCommandType.values().firstOrNull { it.value == value } ?: throw IllegalArgumentException("Unknown BleCommandType: $value") diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt index af6275cd6a..64e04630a0 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/BleDiscoveredDevice.kt @@ -4,7 +4,7 @@ import android.bluetooth.le.ScanResult import android.os.ParcelUuid import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.DiscoveredInvalidPodException -class BleDiscoveredDevice(val scanResult: ScanResult, private val podID: Long) { +class BleDiscoveredDevice(val scanResult: ScanResult, private val podId: Long) { private val sequenceNo: Int private val lotNo: Long @@ -12,29 +12,29 @@ class BleDiscoveredDevice(val scanResult: ScanResult, private val podID: Long) { private fun validateServiceUUIDs() { val scanRecord = scanResult.scanRecord ?: throw DiscoveredInvalidPodException("Scan record is null"); - val serviceUUIDs = scanRecord.serviceUuids - if (serviceUUIDs.size != 9) { - throw DiscoveredInvalidPodException("Expected 9 service UUIDs, got" + serviceUUIDs.size, serviceUUIDs) + val serviceUuids = scanRecord.serviceUuids + if (serviceUuids.size != 9) { + throw DiscoveredInvalidPodException("Expected 9 service UUIDs, got" + serviceUuids.size, serviceUuids) } - if (extractUUID16(serviceUUIDs[0]) != "4024") { + if (extractUUID16(serviceUuids[0]) != MAIN_SERVICE_UUID) { // this is the service that we filtered for - throw DiscoveredInvalidPodException("The first exposed service UUID should be 4024, got " + extractUUID16(serviceUUIDs[0]), serviceUUIDs) + throw DiscoveredInvalidPodException("The first exposed service UUID should be 4024, got " + extractUUID16(serviceUuids[0]), serviceUuids) } // TODO understand what is serviceUUIDs[1]. 0x2470. Alarms? - if (extractUUID16(serviceUUIDs[2]) != "000a") { + if (extractUUID16(serviceUuids[2]) != "000a") { // constant? - throw DiscoveredInvalidPodException("The third exposed service UUID should be 000a, got " + serviceUUIDs[2], serviceUUIDs) + throw DiscoveredInvalidPodException("The third exposed service UUID should be 000a, got " + serviceUuids[2], serviceUuids) } } @Throws(DiscoveredInvalidPodException::class) - private fun validatePodID() { + private fun validatePodId() { val scanRecord = scanResult.scanRecord val serviceUUIDs = scanRecord.serviceUuids - val hexPodID = extractUUID16(serviceUUIDs[3]) + extractUUID16(serviceUUIDs[4]) - val podID = hexPodID.toLong(16) - if (this.podID != podID) { - throw DiscoveredInvalidPodException("This is not the POD we are looking for. " + this.podID + " found: " + podID, serviceUUIDs) + val hexPodId = extractUUID16(serviceUUIDs[3]) + extractUUID16(serviceUUIDs[4]) + val podId = hexPodId.toLong(16) + if (this.podId != podId) { + throw DiscoveredInvalidPodException("This is not the POD we are looking for. " + this.podId + " found: " + this.podId, serviceUUIDs) } } @@ -58,14 +58,14 @@ class BleDiscoveredDevice(val scanResult: ScanResult, private val podID: Long) { override fun toString(): String { return "BleDiscoveredDevice{" + "scanResult=" + scanResult + - ", podID=" + podID + + ", podID=" + podId + ", sequenceNo=" + sequenceNo + ", lotNo=" + lotNo + '}' } companion object { - + const val MAIN_SERVICE_UUID = "4024"; private fun extractUUID16(uuid: ParcelUuid): String { return uuid.toString().substring(4, 8) } @@ -73,7 +73,7 @@ class BleDiscoveredDevice(val scanResult: ScanResult, private val podID: Long) { init { validateServiceUUIDs() - validatePodID() + validatePodId() lotNo = parseLotNo() sequenceNo = parseSeqNo() } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt index 1164815970..0030c755eb 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/scan/ScanCollector.kt @@ -21,7 +21,6 @@ class ScanCollector(private val logger: AAPSLogger, private val podID: Long) : S } override fun onScanFailed(errorCode: Int) { - scanFailed = errorCode logger.warn(LTag.PUMPBTCOMM, "Scan failed with errorCode: $errorCode") super.onScanFailed(errorCode) }