diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt index 05478bb4a0..dd43c9bf3f 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt @@ -195,7 +195,11 @@ class MedtronicPumpPlugin @Inject constructor( } } - override fun onStartCustomActions() { + override fun hasService(): Boolean { + return true + } + + override fun onStartScheduledPumpActions() { // check status every minute (if any status needs refresh we send readStatus command) Thread { @@ -674,7 +678,7 @@ class MedtronicPumpPlugin @Inject constructor( } // if enforceNew===true current temp basal is canceled and new TBR set (duration is prolonged), -// if false and the same rate is requested enacted=false and success=true is returned and TBR is not changed + // if false and the same rate is requested enacted=false and success=true is returned and TBR is not changed @Synchronized override fun setTempBasalAbsolute(absoluteRate: Double, durationInMinutes: Int, profile: Profile, enforceNew: Boolean, tbrType: TemporaryBasalType): PumpEnactResult { setRefreshButtonEnabled(false) @@ -744,7 +748,7 @@ class MedtronicPumpPlugin @Inject constructor( PumpEnactResult(injector).success(false).enacted(false) // .comment(R.string.medtronic_cmd_tbr_could_not_be_delivered) } else { - medtronicPumpStatus.tempBasalStart = Date() + medtronicPumpStatus.tempBasalStart = System.currentTimeMillis() medtronicPumpStatus.tempBasalAmount = absoluteRate medtronicPumpStatus.tempBasalLength = durationInMinutes diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt index 504994800f..82238a9e3f 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt @@ -122,7 +122,7 @@ class MedtronicPumpStatus @Inject constructor(private val rh: ResourceHelper, get() { if (tempBasalStart == null) return null if (tempBasalEnd == null) { - val startTime = tempBasalStart!!.time + val startTime = tempBasalStart!! tempBasalEnd = startTime + tempBasalLength!! * 60 * 1000 } if (System.currentTimeMillis() > tempBasalEnd!!) { diff --git a/pump-common/src/main/AndroidManifest.xml b/pump-common/src/main/AndroidManifest.xml index b935efb489..84d3bd3279 100644 --- a/pump-common/src/main/AndroidManifest.xml +++ b/pump-common/src/main/AndroidManifest.xml @@ -1,6 +1,18 @@ - + + + + + + + + + diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt index 8ae4202a56..a302ffa3f5 100644 --- a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.content.ServiceConnection import android.text.format.DateFormat +import com.google.gson.GsonBuilder import dagger.android.HasAndroidInjector import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.data.PumpEnactResult @@ -69,30 +70,41 @@ abstract class PumpPluginAbstract protected constructor( protected var displayConnectionMessages = false var pumpType: PumpType = PumpType.GENERIC_AAPS + get() = field set(value) { field = value pumpDescription.fillFor(value) } + protected var gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() + abstract fun initPumpStatusData() + open fun hasService(): Boolean { + return true + } + override fun onStart() { super.onStart() initPumpStatusData() - val intent = Intent(context, serviceClass) - context.bindService(intent, serviceConnection!!, Context.BIND_AUTO_CREATE) + if (hasService()) { + val intent = Intent(context, serviceClass) + context.bindService(intent, serviceConnection!!, Context.BIND_AUTO_CREATE) + disposable.add(rxBus + .toObservable(EventAppExit::class.java) + .observeOn(aapsSchedulers.io) + .subscribe({ _ -> context.unbindService(serviceConnection!!) }) { throwable: Throwable? -> fabricPrivacy.logException(throwable!!) } + ) + } serviceRunning = true - disposable.add(rxBus - .toObservable(EventAppExit::class.java) - .observeOn(aapsSchedulers.io) - .subscribe({ context.unbindService(serviceConnection!!) }) { throwable: Throwable? -> fabricPrivacy.logException(throwable!!) } - ) - onStartCustomActions() + onStartScheduledPumpActions() } override fun onStop() { - aapsLogger.debug(LTag.PUMP, deviceID() + " onStop()") - context.unbindService(serviceConnection!!) + aapsLogger.debug(LTag.PUMP, model().model + " onStop()") + if (hasService()) { + context.unbindService(serviceConnection!!) + } serviceRunning = false disposable.clear() super.onStop() @@ -101,7 +113,7 @@ abstract class PumpPluginAbstract protected constructor( /** * If we need to run any custom actions in onStart (triggering events, etc) */ - abstract fun onStartCustomActions() + abstract fun onStartScheduledPumpActions() /** * Service class (same one you did serviceConnection for) @@ -232,7 +244,7 @@ abstract class PumpPluginAbstract protected constructor( val extended = JSONObject() try { battery.put("percent", pumpStatusData.batteryRemaining) - status.put("status", pumpStatusData.pumpStatusType.status) + status.put("status", pumpStatusData.pumpRunningState.status) extended.put("Version", version) try { extended.put("ActiveProfile", profileName) @@ -298,6 +310,7 @@ abstract class PumpPluginAbstract protected constructor( // bolus needed, ask pump to deliver it deliverBolus(detailedBolusInfo) } else { + detailedBolusInfo.timestamp = System.currentTimeMillis() // no bolus required, carb only treatment pumpSyncStorage.addCarbs(PumpDbEntryCarbs(detailedBolusInfo, this)) @@ -334,4 +347,4 @@ abstract class PumpPluginAbstract protected constructor( pumpDescription.fillFor(pumpType) this.pumpType = pumpType } -} \ No newline at end of file +} diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BondStateReceiver.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BondStateReceiver.kt new file mode 100644 index 0000000000..37df0f9e0c --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BondStateReceiver.kt @@ -0,0 +1,77 @@ +package info.nightscout.androidaps.plugins.pump.common.ble + +import android.bluetooth.BluetoothDevice +import android.content.Context +import android.content.Intent +import androidx.annotation.StringRes +import com.google.gson.Gson +import dagger.android.DaggerBroadcastReceiver +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.pump.common.events.EventPumpConnectionParametersChanged +import info.nightscout.androidaps.interfaces.ResourceHelper +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag +import info.nightscout.shared.sharedPreferences.SP +import javax.inject.Inject + +class BondStateReceiver( + @StringRes var deviceAddress: Int, + @StringRes var bondedFlag: Int, + var targetDevice: String, + var targetState: Int +) : DaggerBroadcastReceiver() { + + @Inject lateinit var sp: SP + @Inject lateinit var context: Context + @Inject lateinit var rh: ResourceHelper + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var rxBus: RxBus + + var TAG = LTag.PUMPBTCOMM + var gson = Gson() + var applicationContext: Context? = null + + override fun onReceive(context: Context, intent: Intent) { + super.onReceive(context, intent) + val action = intent.action + val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) + aapsLogger.info(TAG, "in onReceive: INTENT" + gson.toJson(intent)) + if (device == null) { + aapsLogger.error(TAG, "onReceive. Device is null. Exiting.") + return + } else { + if (device.address != targetDevice) { + aapsLogger.error(TAG, "onReceive. Device is not the same as targetDevice. Exiting.") + return + } + } + + // Check if action is valid + if (action == null) return + + // Take action depending on new bond state + if (action == BluetoothDevice.ACTION_BOND_STATE_CHANGED) { + val bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR) + val previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1) + aapsLogger.info(TAG, "in onReceive: bondState=$bondState, previousBondState=$previousBondState") + if (bondState == targetState) { + aapsLogger.info(TAG, "onReceive: found targeted state: $targetState") + val currentDeviceSettings = sp.getString(deviceAddress, "") + if (currentDeviceSettings.equals(targetDevice)) { + if (targetState == 12) { + sp.putBoolean(bondedFlag, true) + rxBus.send(EventPumpConnectionParametersChanged()) + } else if (targetState == 10) { + sp.putBoolean(bondedFlag, false) + rxBus.send(EventPumpConnectionParametersChanged()) + } + context.unregisterReceiver(this) + } else { + aapsLogger.error(TAG, "onReceive: Device stored in SP is not the same as target device, process interrupted") + } + } else { + aapsLogger.info(TAG, "onReceive: currentBondState=$bondState, targetBondState=$targetState") + } + } + } +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt index 9ecd30bca7..9c4ea272d9 100644 --- a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt @@ -1,6 +1,6 @@ package info.nightscout.androidaps.plugins.pump.common.data -import info.nightscout.androidaps.plugins.pump.common.defs.PumpStatusType +import info.nightscout.androidaps.plugins.pump.common.defs.PumpRunningState import info.nightscout.androidaps.plugins.pump.common.defs.PumpType import java.util.* @@ -33,12 +33,13 @@ abstract class PumpStatus(var pumpType: PumpType) { var dailyTotalUnits: Double? = null var maxDailyTotalUnits: String? = null var units: String? = null // Constants.MGDL or Constants.MMOL - var pumpStatusType = PumpStatusType.Running + var pumpRunningState = PumpRunningState.Running var basalsByHour: DoubleArray? = null - var tempBasalStart: Date? = null + var tempBasalStart: Long? = null var tempBasalAmount: Double? = 0.0 var tempBasalLength: Int? = 0 var tempBasalEnd: Long? = null + var pumpTime: PumpTimeDifferenceDto? = null abstract fun initSettings() diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpTimeDifferenceDto.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpTimeDifferenceDto.kt new file mode 100755 index 0000000000..561967c714 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpTimeDifferenceDto.kt @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.plugins.pump.common.data + +import org.joda.time.DateTime +import org.joda.time.Seconds + +/** + * Created by andy on 28/05/2021. + */ +class PumpTimeDifferenceDto constructor(var localDeviceTime: DateTime, + var pumpTime: DateTime) { + + var timeDifference = 0 + + fun calculateDifference() { + val secondsBetween = Seconds.secondsBetween(localDeviceTime, pumpTime) + timeDifference = secondsBetween.seconds + + // val diff = localDeviceTime - pumpTime + // timeDifference = (diff / 1000.0).toInt() + } + + init { + calculateDifference() + } +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/BasalProfileStatus.java b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/BasalProfileStatus.java new file mode 100755 index 0000000000..0d56ee4ef8 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/BasalProfileStatus.java @@ -0,0 +1,14 @@ +package info.nightscout.androidaps.plugins.pump.common.defs; + +/** + * Created by andy on 1/20/19. + */ + +public enum BasalProfileStatus { + + NotInitialized, // + ProfileOK, // + ProfileChanged, // + ; + +} diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpDriverState.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpDriverState.kt index f72163ce92..6f6124005d 100644 --- a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpDriverState.kt +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpDriverState.kt @@ -1,14 +1,26 @@ package info.nightscout.androidaps.plugins.pump.common.defs -enum class PumpDriverState { +import info.nightscout.androidaps.plugins.pump.common.R - NotInitialized, - Connecting, - Connected, - Initialized, - Ready, Busy, - Suspended; +// TODO there are 3 classes now, that do similar things, sort of, need to define exact rules: PumpDeviceState, PumpDriverState, PumpStatusState + +// TODO split this enum into 2 +enum class PumpDriverState(var resourceId: Int) { + + NotInitialized(R.string.pump_status_not_initialized), // this state should be set only when driver is created + Connecting(R.string.connecting), // + Connected(R.string.connected), // + Initialized(R.string.pump_status_initialized), // this is weird state that probably won't be used, since its more driver centric that communication centric + EncryptCommunication(R.string.pump_status_encrypt), // + Ready(R.string.pump_status_ready), + Busy(R.string.pump_status_busy), // + Suspended(R.string.pump_status_suspended), // + Sleeping(R.string.pump_status_sleeping), + ExecutingCommand(R.string.pump_status_executing_command), + Disconnecting(R.string.disconnecting), + Disconnected(R.string.disconnected), + ErrorCommunicatingWithPump(R.string.pump_status_error_comm); fun isConnected(): Boolean = this == Connected || this == Initialized || this == Busy || this == Suspended fun isInitialized(): Boolean = this == Initialized || this == Busy || this == Suspended -} \ No newline at end of file +} diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpHistoryEntryGroup.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpHistoryEntryGroup.kt index 4a98471dc4..189353809a 100644 --- a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpHistoryEntryGroup.kt +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpHistoryEntryGroup.kt @@ -2,7 +2,7 @@ package info.nightscout.androidaps.plugins.pump.common.defs import info.nightscout.androidaps.plugins.pump.common.R import info.nightscout.androidaps.interfaces.ResourceHelper -import java.util.* +import kotlin.streams.toList /** * This file was taken from GGC - GNU Gluco Control (ggc.sourceforge.net), application for diabetes @@ -11,9 +11,10 @@ import java.util.* * * Author: Andy {andy.rozman@gmail.com} */ -enum class PumpHistoryEntryGroup(val resourceId: Int) { +enum class PumpHistoryEntryGroup(val resourceId: Int, val pumpTypeGroupConfig: PumpTypeGroupConfig = PumpTypeGroupConfig.All) { All(R.string.history_group_all), + Base(R.string.history_group_base), Bolus(R.string.history_group_bolus), Basal(R.string.history_group_basal), Prime(R.string.history_group_prime), @@ -22,7 +23,14 @@ enum class PumpHistoryEntryGroup(val resourceId: Int) { Glucose(R.string.history_group_glucose), Notification(R.string.history_group_notification), Statistic(R.string.history_group_statistic), - Unknown(R.string.history_group_unknown); + Other(R.string.history_group_other), + Unknown(R.string.history_group_unknown), + + // Ypso + EventsOnly(R.string.history_group_events), + EventsNoStat(R.string.history_group_events_no_stat) + + ; var translated: String? = null private set @@ -33,9 +41,10 @@ enum class PumpHistoryEntryGroup(val resourceId: Int) { companion object { - private var translatedList: MutableList? = null + @JvmStatic private var translatedList: MutableList? = null - private fun doTranslation(rh: ResourceHelper) { + fun doTranslation(rh: ResourceHelper) { + if (translatedList != null) return translatedList = ArrayList() for (pumpHistoryEntryGroup in values()) { pumpHistoryEntryGroup.translated = rh.gs(pumpHistoryEntryGroup.resourceId) @@ -43,9 +52,27 @@ enum class PumpHistoryEntryGroup(val resourceId: Int) { } } + // FIXME this is just for Java compatibility reasons (can be removed when all drivers using it are in Kotlin - OmnipodEros still in java) fun getTranslatedList(rh: ResourceHelper): List { + return getTranslatedList(rh, PumpTypeGroupConfig.All) + } + + fun getTranslatedList(rh: ResourceHelper, pumpTypeGroupConfig: PumpTypeGroupConfig = PumpTypeGroupConfig.All): List { if (translatedList == null) doTranslation(rh) - return translatedList!! + + val outList: List + + if (pumpTypeGroupConfig == PumpTypeGroupConfig.All) { + outList = translatedList!!.stream() + .filter { pre -> pre.pumpTypeGroupConfig == PumpTypeGroupConfig.All } + .toList(); + } else { + outList = translatedList!!.stream() + .filter { pre -> (pre.pumpTypeGroupConfig == PumpTypeGroupConfig.All || pre.pumpTypeGroupConfig == pumpTypeGroupConfig) } + .toList(); + } + + return outList } } diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpRunningState.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpRunningState.kt new file mode 100755 index 0000000000..f9dcabbf98 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpRunningState.kt @@ -0,0 +1,9 @@ +package info.nightscout.androidaps.plugins.pump.common.defs + +// TODO there are 3 classes now, that do similar things, sort of, need to define exact rules: PumpDeviceState, PumpDriverState, PumpStatusState + +enum class PumpRunningState(val status: String) { + + Running("normal"), + Suspended("suspended"); +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpTypeGroupConfig.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpTypeGroupConfig.kt new file mode 100644 index 0000000000..cc553c732f --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpTypeGroupConfig.kt @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.plugins.pump.common.defs + +enum class PumpTypeGroupConfig { + All, + Medtronic, + OmnipodEros, + YpsoPump +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpUpdateFragmentType.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpUpdateFragmentType.kt new file mode 100755 index 0000000000..0558597127 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpUpdateFragmentType.kt @@ -0,0 +1,49 @@ +package info.nightscout.androidaps.plugins.pump.common.defs + +import java.util.* + +enum class PumpUpdateFragmentType { + None, + PumpStatus, + DriverStatus, + Queue, + Bolus, + TBR, + ProfileChange, + TBRCount, + BolusCount, + TreatmentValues(Arrays.asList(Bolus, TBR, TBRCount, BolusCount, ProfileChange)), // Last Bolus, TBR, Profile Change, TBR Count, Bolus Count + Full, + Configuration, // Firmware, Errors + Battery, + Reservoir, + OtherValues(Arrays.asList(Battery, Reservoir)), // Battery, Reservoir + Custom_1, + Custom_2, + Custom_3, + Custom_4, + Custom_5, + Custom_6, + Custom_7, + Custom_8 + ; + + final var children: List? = null + + constructor() { + } + + constructor(children: List) { + this.children = children; + } + + fun isOptionIncluded(type: PumpUpdateFragmentType): Boolean { + if (this == type) + return true + else if (this.children != null && this.children!!.contains(type)) + return true; + + return false; + } + +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/PumpDriverConfiguration.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/PumpDriverConfiguration.kt new file mode 100644 index 0000000000..c5f28a4645 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/PumpDriverConfiguration.kt @@ -0,0 +1,11 @@ +package info.nightscout.androidaps.plugins.pump.common.driver + +import info.nightscout.androidaps.plugins.pump.common.driver.history.PumpHistoryDataProvider + +interface PumpDriverConfiguration { + + fun getPumpBLESelector(): PumpBLESelector + + fun getPumpHistoryDataProvider(): PumpHistoryDataProvider + +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/PumpDriverConfigurationCapable.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/PumpDriverConfigurationCapable.kt new file mode 100644 index 0000000000..3c8e66fde3 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/PumpDriverConfigurationCapable.kt @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.pump.common.driver + +interface PumpDriverConfigurationCapable { + + fun getPumpDriverConfiguration(): PumpDriverConfiguration + +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/ble/PumpBLESelector.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/ble/PumpBLESelector.kt new file mode 100644 index 0000000000..530c127a4e --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/ble/PumpBLESelector.kt @@ -0,0 +1,105 @@ +package info.nightscout.androidaps.plugins.pump.common.driver + +import android.bluetooth.BluetoothDevice +import android.bluetooth.le.ScanFilter +import android.bluetooth.le.ScanSettings +import android.content.Context + +interface PumpBLESelector { + + /** + * Called on resume + */ + fun onResume() + + /** + * Called on destory + */ + fun onDestroy() + + /** + * This method is called when device is being removed (it can be empty if you don't need to do any special action, but if you + * have to unbound (for example), then this is method where to call it. For unbounding removeBond is available + */ + fun removeDevice(device: BluetoothDevice) + + /** + * Cleanup method after device was removed + */ + fun cleanupAfterDeviceRemoved() + + /** + * operations when scan failed + */ + fun onScanFailed(context: Context, errorCode: Int) + + /** + * operations when scan starts + */ + fun onStartLeDeviceScan(context: Context) + + /** + * operations when scan stops + */ + fun onStopLeDeviceScan(context: Context) + + /** + * operations when scan was stopped manualy (press on button) + */ + fun onManualStopLeDeviceScan(context: Context) + + /** + * operations when on non manual stop of scan (on timeout) + */ + fun onNonManualStopLeDeviceScan(context: Context) + + /** + * get Scan Filters + */ + fun getScanFilters(): List? + + /** + * get Scan Settings + */ + fun getScanSettings(): ScanSettings? + + /** + * filter device on search (for cases where we can't do it with Scan Filters + */ + fun filterDevice(device: BluetoothDevice): BluetoothDevice? + + /** + * operations when device selected + */ + fun onDeviceSelected(bluetoothDevice: BluetoothDevice, bleAddress: String, deviceName: String) + + /** + * If pump has no name, this name will be used + */ + fun getUnknownPumpName(): String + + /** + * get Address of Currently selected pump, empty string if none + */ + fun currentlySelectedPumpAddress(): String + + /** + * get Name of Currently selected pump, getUnknownPumpName() string if none + */ + fun currentlySelectedPumpName(): String + + /** + * Get Translation Text + */ + fun getText(key: PumpBLESelectorText): String + +} + +enum class PumpBLESelectorText { + SCAN_TITLE, + SELECTED_PUMP_TITLE, + REMOVE_TITLE, + REMOVE_TEXT, + NO_SELECTED_PUMP, + PUMP_CONFIGURATION +} diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/ble/PumpBLESelectorAbstract.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/ble/PumpBLESelectorAbstract.kt new file mode 100644 index 0000000000..872645fa30 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/ble/PumpBLESelectorAbstract.kt @@ -0,0 +1,108 @@ +package info.nightscout.androidaps.plugins.pump.common.driver.ble + +import android.bluetooth.BluetoothDevice +import android.bluetooth.le.ScanFilter +import android.bluetooth.le.ScanSettings +import android.content.Context +import android.widget.Toast +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.pump.common.R +import info.nightscout.androidaps.plugins.pump.common.driver.PumpBLESelector +import info.nightscout.androidaps.interfaces.ResourceHelper +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag +import info.nightscout.shared.sharedPreferences.SP + +abstract class PumpBLESelectorAbstract constructor( + var resourceHelper: ResourceHelper, + var aapsLogger: AAPSLogger, + var sp: SP, + var rxBus: RxBus, + var context: Context +) : PumpBLESelector { + + protected val TAG = LTag.PUMPBTCOMM + + override fun getScanSettings(): ScanSettings? { + return ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build() + } + + override fun getScanFilters(): MutableList? { + return null + } + + override fun filterDevice(device: BluetoothDevice): BluetoothDevice? { + return device + } + + override fun onResume() { + } + + override fun onDestroy() { + } + + override fun removeDevice(device: BluetoothDevice) { + } + + override fun cleanupAfterDeviceRemoved() { + } + + override fun onManualStopLeDeviceScan(context: Context) { + } + + override fun onNonManualStopLeDeviceScan(context: Context) { + } + + //fun onDeviceSelected(bluetoothDevice: BluetoothDevice, bleAddress: String, deviceName: String) + + override fun onScanFailed(context: Context, errorCode: Int) { + Toast.makeText( + context, resourceHelper.gs(R.string.ble_config_scan_error, errorCode), + Toast.LENGTH_LONG + ).show() + } + + override fun onStartLeDeviceScan(context: Context) { + Toast.makeText(context, R.string.ble_config_scan_scanning, Toast.LENGTH_SHORT).show() + } + + override fun onStopLeDeviceScan(context: Context) { + Toast.makeText(context, R.string.ble_config_scan_finished, Toast.LENGTH_SHORT).show() + } + + protected fun removeBond(bluetoothDevice: BluetoothDevice): Boolean { + return try { + val method = bluetoothDevice.javaClass.getMethod("removeBond") + val resultObject = method.invoke(bluetoothDevice) + if (resultObject == null) { + aapsLogger.error(TAG, "ERROR: result object is null") + false + } else { + val result = resultObject as Boolean + if (result) { + aapsLogger.info(TAG, "Successfully removed bond") + } else { + aapsLogger.warn(TAG, "Bond was not removed") + } + result + } + } catch (e: Exception) { + aapsLogger.error(TAG, "ERROR: could not remove bond") + e.printStackTrace() + false + } + } + + protected fun getBondingStatusDescription(state: Int): String { + return if (state == 10) { + "BOND_NONE" + } else if (state == 11) { + "BOND_BONDING" + } else if (state == 12) { + "BOND_BONDED" + } else { + "UNKNOWN BOND STATUS ($state)" + } + } + +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpDataConverter.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpDataConverter.kt new file mode 100644 index 0000000000..0c8c8d1b25 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpDataConverter.kt @@ -0,0 +1,4 @@ +package info.nightscout.androidaps.plugins.pump.common.driver.history + +interface PumpDataConverter { +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryDataProvider.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryDataProvider.kt new file mode 100644 index 0000000000..6f2389338e --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryDataProvider.kt @@ -0,0 +1,75 @@ +package info.nightscout.androidaps.plugins.pump.common.driver.history + +import androidx.annotation.StringRes +import info.nightscout.androidaps.plugins.pump.common.R +import info.nightscout.androidaps.plugins.pump.common.defs.PumpHistoryEntryGroup + +interface PumpHistoryDataProvider { + + /** + * Get Data, specified with PumpHistoryPeriod + */ + fun getData(period: PumpHistoryPeriod): List + + /** + * Get Initial Period + */ + fun getInitialPeriod(): PumpHistoryPeriod + + /** + * Get InitialData + */ + fun getInitialData(): List + + /** + * Get Allowed Pump History Groups (for specific pump) + */ + fun getAllowedPumpHistoryGroups(): List + + /** + * Get Spinner Width in pixels (same as specifying 150dp) + */ + fun getSpinnerWidthInPixels(): Int + + /** + * Get Translation Text + */ + fun getText(key: PumpHistoryText): String + + /** + * For filtering of items + */ + fun isItemInSelection(itemGroup: PumpHistoryEntryGroup, targetGroup: PumpHistoryEntryGroup): Boolean + +} + +enum class PumpHistoryPeriod constructor( + @StringRes var stringId: Int, + var isHours: Boolean = false +) { + + TODAY(R.string.time_today), + LAST_HOUR(R.string.time_last_hour, true), + LAST_3_HOURS(R.string.time_last_3_hours, true), + LAST_6_HOURS(R.string.time_last_6_hours, true), + LAST_12_HOURS(R.string.time_last_12_hours, true), + LAST_2_DAYS(R.string.time_last_2_days), + LAST_4_DAYS(R.string.time_last_4_days), + LAST_WEEK(R.string.time_last_week), + LAST_MONTH(R.string.time_last_month), + ALL(R.string.history_group_all) + +} + +enum class PumpHistoryText { + + PUMP_HISTORY, + + // OLD ONES + SCAN_TITLE, + SELECTED_PUMP_TITLE, + REMOVE_TITLE, + REMOVE_TEXT, + NO_SELECTED_PUMP, + PUMP_CONFIGURATION +} diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryDataProviderAbstract.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryDataProviderAbstract.kt new file mode 100644 index 0000000000..53dbbc0aba --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryDataProviderAbstract.kt @@ -0,0 +1,46 @@ +package info.nightscout.androidaps.plugins.pump.common.driver.history + +import info.nightscout.androidaps.plugins.pump.common.defs.PumpHistoryEntryGroup +import java.util.* + +abstract class PumpHistoryDataProviderAbstract : PumpHistoryDataProvider { + + override fun getInitialData(): List { + return getData(getInitialPeriod()); + } + + override fun getSpinnerWidthInPixels(): Int { + return 150 + } + + protected fun getStartingTimeForData(period: PumpHistoryPeriod): Long { + val gregorianCalendar = GregorianCalendar() + + if (!period.isHours) { + gregorianCalendar.set(Calendar.HOUR_OF_DAY, 0) + gregorianCalendar.set(Calendar.MINUTE, 0) + gregorianCalendar.set(Calendar.SECOND, 0) + gregorianCalendar.set(Calendar.MILLISECOND, 0) + } + + when (period) { + PumpHistoryPeriod.TODAY -> return gregorianCalendar.timeInMillis + PumpHistoryPeriod.ALL -> return 0L + PumpHistoryPeriod.LAST_2_DAYS -> gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1) + PumpHistoryPeriod.LAST_4_DAYS -> gregorianCalendar.add(Calendar.DAY_OF_MONTH, -3) + PumpHistoryPeriod.LAST_WEEK -> gregorianCalendar.add(Calendar.WEEK_OF_YEAR, -1) + PumpHistoryPeriod.LAST_MONTH -> gregorianCalendar.add(Calendar.MONTH, -1) + PumpHistoryPeriod.LAST_HOUR -> gregorianCalendar.add(Calendar.HOUR_OF_DAY, -1) + PumpHistoryPeriod.LAST_3_HOURS -> gregorianCalendar.add(Calendar.HOUR_OF_DAY, -3) + PumpHistoryPeriod.LAST_6_HOURS -> gregorianCalendar.add(Calendar.HOUR_OF_DAY, -6) + PumpHistoryPeriod.LAST_12_HOURS -> gregorianCalendar.add(Calendar.HOUR_OF_DAY, -12) + } + + return gregorianCalendar.timeInMillis + } + + override fun isItemInSelection(itemGroup: PumpHistoryEntryGroup, targetGroup: PumpHistoryEntryGroup): Boolean { + return itemGroup === targetGroup + } + +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryEntry.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryEntry.kt new file mode 100644 index 0000000000..02e4e91b4c --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/driver/history/PumpHistoryEntry.kt @@ -0,0 +1,18 @@ +package info.nightscout.androidaps.plugins.pump.common.driver.history + +import info.nightscout.androidaps.plugins.pump.common.defs.PumpHistoryEntryGroup +import info.nightscout.androidaps.interfaces.ResourceHelper + +interface PumpHistoryEntry { + + fun prepareEntryData(resourceHelper: ResourceHelper, pumpDataConverter: PumpDataConverter) + + fun getEntryDateTime(): String + + fun getEntryType(): String + + fun getEntryValue(): String + + fun getEntryTypeGroup(): PumpHistoryEntryGroup + +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventBondChanged.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventBondChanged.kt new file mode 100644 index 0000000000..8e2c8b978c --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventBondChanged.kt @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.plugins.pump.common.events + +import info.nightscout.androidaps.events.Event + +class EventBondChanged( + var connectionAddress: String, + var bondStatus: Boolean +) : Event() diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpChanged.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpChanged.kt new file mode 100755 index 0000000000..36b1107882 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpChanged.kt @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.plugins.pump.common.events + +import info.nightscout.androidaps.events.Event + +class EventPumpChanged(var serialNumber: String, + var connectionAddress: String, + var parameters: MutableMap? = null) : Event() { +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpConnectionParametersChanged.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpConnectionParametersChanged.kt new file mode 100644 index 0000000000..d6c5b1563a --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpConnectionParametersChanged.kt @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.plugins.pump.common.events + +import info.nightscout.androidaps.events.Event + +class EventPumpConnectionParametersChanged : Event() { +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpFragmentValuesChanged.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpFragmentValuesChanged.kt new file mode 100755 index 0000000000..fcb55dcd11 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventPumpFragmentValuesChanged.kt @@ -0,0 +1,14 @@ +package info.nightscout.androidaps.plugins.pump.common.events + +import info.nightscout.androidaps.events.Event +import info.nightscout.androidaps.plugins.pump.common.defs.PumpUpdateFragmentType + +class EventPumpFragmentValuesChanged : Event { + + var updateType: PumpUpdateFragmentType = PumpUpdateFragmentType.None + + constructor(updateType: PumpUpdateFragmentType) { + this.updateType = updateType + } + +} \ No newline at end of file diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventRefreshButtonState.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventRefreshButtonState.kt old mode 100644 new mode 100755 similarity index 100% rename from rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventRefreshButtonState.kt rename to pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/events/EventRefreshButtonState.kt diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpBLEConfigActivity.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpBLEConfigActivity.kt new file mode 100755 index 0000000000..6333af2cc2 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpBLEConfigActivity.kt @@ -0,0 +1,338 @@ +package info.nightscout.androidaps.plugins.pump.common.ui + +import android.annotation.SuppressLint +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothDevice +import android.bluetooth.BluetoothManager +import android.bluetooth.le.BluetoothLeScanner +import android.bluetooth.le.ScanCallback +import android.bluetooth.le.ScanFilter +import android.bluetooth.le.ScanResult +import android.bluetooth.le.ScanSettings +import android.content.Context +import android.os.Bundle +import android.os.Handler +import android.os.HandlerThread +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.AdapterView.OnItemClickListener +import android.widget.BaseAdapter +import android.widget.TextView +import dagger.android.support.DaggerAppCompatActivity +import info.nightscout.androidaps.interfaces.ActivePlugin +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.pump.common.R +import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck +import info.nightscout.androidaps.plugins.pump.common.databinding.PumpBleConfigActivityBinding +import info.nightscout.androidaps.plugins.pump.common.driver.PumpBLESelector +import info.nightscout.androidaps.plugins.pump.common.driver.PumpBLESelectorText +import info.nightscout.androidaps.plugins.pump.common.driver.PumpDriverConfigurationCapable +import info.nightscout.androidaps.utils.alertDialogs.OKDialog +import info.nightscout.androidaps.interfaces.ResourceHelper +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag +import info.nightscout.shared.sharedPreferences.SP +import org.apache.commons.lang3.StringUtils +import javax.inject.Inject + +@SuppressLint("MissingPermission") +class PumpBLEConfigActivity : DaggerAppCompatActivity() { + + @Inject lateinit var resourceHelper: ResourceHelper + @Inject lateinit var activePlugin: ActivePlugin + @Inject lateinit var sp: SP + @Inject lateinit var blePreCheck: BlePreCheck + @Inject lateinit var context: Context + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var rxBus: RxBus + + private lateinit var binding: PumpBleConfigActivityBinding + private lateinit var bleSelector: PumpBLESelector + + private var settings: ScanSettings? = null + private var filters: List? = null + private var bleScanner: BluetoothLeScanner? = null + private var deviceListAdapter = LeDeviceListAdapter() + private val handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper) + private val bluetoothAdapter: BluetoothAdapter? get() = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter + var scanning = false + private val devicesMap: MutableMap = HashMap() + + private val stopScanAfterTimeoutRunnable = Runnable { + if (scanning) { + stopLeDeviceScan(false) + } + } + + @SuppressLint("MissingPermission") + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = PumpBleConfigActivityBinding.inflate(layoutInflater) + setContentView(binding.root) + + if (!blePreCheck.prerequisitesCheck(this)) { + aapsLogger.error(TAG, "prerequisitesCheck failed.") + finish() + return + } + + // Configuration + val activePump = activePlugin.activePump + + if (activePump is PumpDriverConfigurationCapable) { + bleSelector = activePump.getPumpDriverConfiguration().getPumpBLESelector() + } else { + throw RuntimeException("PumpBLEConfigActivity can be used only with PumpDriverConfigurationCapable pump driver.") + } + + binding.pumpBleConfigCurrentlySelectedText.text = bleSelector.getText(PumpBLESelectorText.SELECTED_PUMP_TITLE) + binding.pumpBleConfigScanTitle.text = bleSelector.getText(PumpBLESelectorText.SCAN_TITLE) + + title = bleSelector.getText(PumpBLESelectorText.PUMP_CONFIGURATION) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + supportActionBar?.setDisplayShowHomeEnabled(true) + + binding.pumpBleConfigScanDeviceList.adapter = deviceListAdapter + binding.pumpBleConfigScanDeviceList.onItemClickListener = OnItemClickListener { _: AdapterView<*>?, view: View, _: Int, _: Long -> + // stop scanning if still active + if (scanning) { + stopLeDeviceScan(true) + } + val bleAddress = (view.findViewById(R.id.pump_ble_config_scan_item_device_address) as TextView).text.toString() + val deviceName = (view.findViewById(R.id.pump_ble_config_scan_item_device_name) as TextView).text.toString() + + if (devicesMap.containsKey(bleAddress)) { + aapsLogger.debug(TAG, "Device FOUND in deviceMap: $bleAddress") + val bluetoothDevice = devicesMap[bleAddress] + bleSelector.onDeviceSelected(bluetoothDevice!!, bleAddress, deviceName) + } else { + aapsLogger.debug(TAG, "Device NOT found in deviceMap: $bleAddress") + } + + finish() + } + binding.pumpBleConfigScanStart.setOnClickListener { startLeDeviceScan() } + binding.pumpBleConfigButtonScanStop.setOnClickListener { + if (scanning) { + stopLeDeviceScan(true) + } + } + + binding.pumpBleConfigButtonRemove.setOnClickListener { + OKDialog.showConfirmation( + this@PumpBLEConfigActivity, + bleSelector.getText(PumpBLESelectorText.REMOVE_TITLE), + bleSelector.getText(PumpBLESelectorText.REMOVE_TEXT), + Runnable { + val deviceAddress: String = binding.pumpBleConfigCurrentlySelectedPumpAddress.text.toString() + aapsLogger.debug(TAG, "Removing device as selected: $deviceAddress") + if (devicesMap.containsKey(deviceAddress)) { + val bluetoothDevice = devicesMap[deviceAddress] + aapsLogger.debug(TAG, "Device can be detected near, so trying to remove bond if possible.") + bleSelector.removeDevice(bluetoothDevice!!) + } else { + val remoteDevice = bluetoothAdapter!!.getRemoteDevice(deviceAddress) + if (remoteDevice != null) { + bleSelector.removeDevice(remoteDevice) + } + } + bleSelector.cleanupAfterDeviceRemoved() + updateCurrentlySelectedBTDevice() + }) + } + } + + private fun updateCurrentlySelectedBTDevice() { + val address = bleSelector.currentlySelectedPumpAddress() + if (StringUtils.isEmpty(address)) { + binding.pumpBleConfigCurrentlySelectedPumpName.text = bleSelector.getText(PumpBLESelectorText.NO_SELECTED_PUMP) + binding.pumpBleConfigCurrentlySelectedPumpAddress.visibility = View.GONE + binding.pumpBleConfigButtonRemove.visibility = View.GONE + } else { + binding.pumpBleConfigCurrentlySelectedPumpAddress.visibility = View.VISIBLE + binding.pumpBleConfigButtonRemove.visibility = View.VISIBLE + binding.pumpBleConfigCurrentlySelectedPumpName.text = bleSelector.currentlySelectedPumpName() + binding.pumpBleConfigCurrentlySelectedPumpAddress.text = address + } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean = + when (item.itemId) { + android.R.id.home -> { + finish() + true + } + + else -> false + } + + override fun onResume() { + super.onResume() + bleSelector.onResume() + prepareForScanning() + updateCurrentlySelectedBTDevice() + } + + override fun onDestroy() { + super.onDestroy() + if (scanning) { + stopLeDeviceScan(false) + } + bleSelector.onDestroy() + } + + private fun prepareForScanning() { + bleScanner = bluetoothAdapter!!.bluetoothLeScanner + settings = bleSelector.getScanSettings() + filters = bleSelector.getScanFilters() + } + + private val bleScanCallback: ScanCallback = object : ScanCallback() { + + override fun onScanResult(callbackType: Int, scanRecord: ScanResult) { + aapsLogger.debug(TAG, scanRecord.toString()) + runOnUiThread { if (addDevice(scanRecord)) deviceListAdapter.notifyDataSetChanged() } + } + + override fun onBatchScanResults(results: List) { + runOnUiThread { + var added = false + for (result in results) { + aapsLogger.debug(TAG, "SCAN: " + result.advertisingSid + " name=" + result.device.address) + if (addDevice(result)) added = true + } + if (added) + deviceListAdapter.notifyDataSetChanged() + } + } + + private fun addDevice(result: ScanResult): Boolean { + var device = result.device + + device = bleSelector.filterDevice(device) + + if (device == null) { + return false + } + + deviceListAdapter.addDevice(result) + if (!devicesMap.containsKey(device.address)) { + devicesMap[device.address] = device + } + return true + } + + override fun onScanFailed(errorCode: Int) { + aapsLogger.error(TAG, "Scan Failed - Error Code: $errorCode") + bleSelector.onScanFailed(this@PumpBLEConfigActivity, errorCode) + } + } + + private fun startLeDeviceScan() { + if (bleScanner == null) { + aapsLogger.error(LTag.PUMPBTCOMM, "startLeDeviceScan failed: bleScanner is null") + return + } + deviceListAdapter.clear() + deviceListAdapter.notifyDataSetChanged() + handler.postDelayed(stopScanAfterTimeoutRunnable, SCAN_PERIOD_MILLIS) + runOnUiThread { + binding.pumpBleConfigScanStart.isEnabled = false + binding.pumpBleConfigButtonScanStop.visibility = View.VISIBLE + } + scanning = true + bleScanner!!.startScan(filters, settings, bleScanCallback) + aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start") + bleSelector.onStartLeDeviceScan(this@PumpBLEConfigActivity) + } + + private fun stopLeDeviceScan(manualStop: Boolean) { + if (scanning) { + scanning = false + bleScanner!!.stopScan(bleScanCallback) + aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop") + bleSelector.onStopLeDeviceScan(this@PumpBLEConfigActivity) + handler.removeCallbacks(stopScanAfterTimeoutRunnable) + } + if (manualStop) { + bleSelector.onManualStopLeDeviceScan(this@PumpBLEConfigActivity) + } else { + bleSelector.onNonManualStopLeDeviceScan(this@PumpBLEConfigActivity) + } + + runOnUiThread { + binding.pumpBleConfigScanStart.isEnabled = true + binding.pumpBleConfigButtonScanStop.visibility = View.GONE + } + } + + private inner class LeDeviceListAdapter : BaseAdapter() { + + private var devicesList: ArrayList = arrayListOf() + private var devicesMap: MutableMap = mutableMapOf() + + fun addDevice(result: ScanResult) { + if (!devicesList.contains(result.device)) { + devicesList.add(result.device) + } + devicesMap[result.device] = result.rssi + notifyDataSetChanged() + } + + fun clear() { + devicesList.clear() + devicesMap.clear() + notifyDataSetChanged() + } + + override fun getCount(): Int { + val c = devicesList.size + aapsLogger.info(TAG, "D: count=$c") + return c + } + + override fun getItem(i: Int): Any = devicesList[i] + override fun getItemId(i: Int): Long = i.toLong() + + override fun getView(i: Int, convertView: View?, viewGroup: ViewGroup?): View { + var v = convertView + val holder: ViewHolder + if (v == null) { + v = View.inflate(applicationContext, R.layout.pump_ble_config_scan_item, null) + holder = ViewHolder() + holder.deviceAddress = v.findViewById(R.id.pump_ble_config_scan_item_device_address) + holder.deviceName = v.findViewById(R.id.pump_ble_config_scan_item_device_name) + v.tag = holder + } else { + // reuse view if already exists + holder = v.tag as ViewHolder + } + + val device = devicesList[i] + var deviceName = device.name + if (StringUtils.isBlank(deviceName)) { + deviceName = bleSelector.getUnknownPumpName() + } + deviceName += " [" + devicesMap[device] + "]" + val currentlySelectedAddress = bleSelector.currentlySelectedPumpAddress() // TODO + if (currentlySelectedAddress == device.address) { + deviceName += " (" + resources.getString(R.string.ble_config_scan_selected) + ")" + } + holder.deviceName!!.text = deviceName + holder.deviceAddress!!.text = device.address + return v!! + } + } + + internal class ViewHolder { + var deviceName: TextView? = null + var deviceAddress: TextView? = null + } + + companion object { + private val TAG = LTag.PUMPBTCOMM + private const val SCAN_PERIOD_MILLIS: Long = 15000 + } +} \ No newline at end of file diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpHistoryActivity.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpHistoryActivity.kt new file mode 100755 index 0000000000..a29cf41094 --- /dev/null +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpHistoryActivity.kt @@ -0,0 +1,217 @@ +package info.nightscout.androidaps.plugins.pump.common.ui + +import android.content.Context +import android.os.Bundle +import android.os.SystemClock +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter +import android.widget.TextView +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import dagger.android.support.DaggerAppCompatActivity +import info.nightscout.androidaps.interfaces.ActivePlugin +import info.nightscout.androidaps.plugins.pump.common.R +import info.nightscout.androidaps.plugins.pump.common.databinding.PumpHistoryActivityBinding +import info.nightscout.androidaps.plugins.pump.common.defs.PumpHistoryEntryGroup +import info.nightscout.androidaps.plugins.pump.common.driver.PumpDriverConfigurationCapable +import info.nightscout.androidaps.plugins.pump.common.driver.history.PumpHistoryDataProvider +import info.nightscout.androidaps.plugins.pump.common.driver.history.PumpHistoryEntry +import info.nightscout.androidaps.plugins.pump.common.driver.history.PumpHistoryText +import info.nightscout.androidaps.interfaces.ResourceHelper +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag +import javax.inject.Inject + +class PumpHistoryActivity : DaggerAppCompatActivity() { + + @Inject lateinit var resourceHelper: ResourceHelper + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var activePlugin: ActivePlugin + @Inject lateinit var context: Context + + var filteredHistoryList: MutableList = mutableListOf() + var typeListFull: List? = null + var fullList: MutableList = mutableListOf() + + private lateinit var historyDataProvider: PumpHistoryDataProvider + private lateinit var binding: PumpHistoryActivityBinding + + var manualChange = false + + lateinit var recyclerViewAdapter: RecyclerViewAdapter + + private fun prepareData() { + + val allData = historyDataProvider.getInitialData() + + aapsLogger.info(LTag.PUMP, "Loaded ${allData.size} items from database. [initialSize=${historyDataProvider.getInitialPeriod()}]") + + this.fullList.addAll(allData) + } + + private fun filterHistory(group: PumpHistoryEntryGroup) { + filteredHistoryList.clear() + + if (group === PumpHistoryEntryGroup.All) { + filteredHistoryList.addAll(fullList) + } else { + for (pumpHistoryEntry in fullList) { + if (historyDataProvider.isItemInSelection(pumpHistoryEntry.getEntryTypeGroup(), group)) { + filteredHistoryList.add(pumpHistoryEntry) + } + } + } + + aapsLogger.info(LTag.PUMP, "Filtered list ${filteredHistoryList.size} items (group ${group}), from full list (${fullList.size}).") + + recyclerViewAdapter.setHistoryListInternal(filteredHistoryList) + recyclerViewAdapter.notifyDataSetChanged() + + } + + override fun onResume() { + super.onResume() + //filterHistory(selectedGroup) + //setHistoryTypeSpinner() + //aapsLogger.info(LTag.PUMP, "onResume") + //binding.pumpHistoryRoot.requestLayout() + } + + private fun setHistoryTypeSpinner() { + manualChange = true + for (i in typeListFull!!.indices) { + if (typeListFull!![i].entryGroup === selectedGroup) { + binding.pumpHistoryType.setSelection(i) + break + } + } + SystemClock.sleep(200) + manualChange = false + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = PumpHistoryActivityBinding.inflate(layoutInflater) + setContentView(binding.root) + + // Configuration + val activePump = activePlugin.activePump + + if (activePump is PumpDriverConfigurationCapable) { + historyDataProvider = activePump.getPumpDriverConfiguration().getPumpHistoryDataProvider() + } else { + throw RuntimeException("PumpHistoryActivity can be used only with PumpDriverConfigurationCapable pump driver.") + } + + prepareData() + + binding.pumpHistoryRecyclerView.setHasFixedSize(true) + binding.pumpHistoryRecyclerView.layoutManager = LinearLayoutManager(this) + recyclerViewAdapter = RecyclerViewAdapter(filteredHistoryList) + binding.pumpHistoryRecyclerView.adapter = recyclerViewAdapter + binding.pumpHistoryStatus.visibility = View.GONE + typeListFull = getTypeList(historyDataProvider.getAllowedPumpHistoryGroups()) + val spinnerAdapter = ArrayAdapter(this, R.layout.spinner_centered, typeListFull!!) + + binding.pumpHistoryText.text = historyDataProvider.getText(PumpHistoryText.PUMP_HISTORY) + + binding.pumpHistoryType.adapter = spinnerAdapter + binding.pumpHistoryType.getLayoutParams().width = fromDpToSize(historyDataProvider.getSpinnerWidthInPixels()) + binding.pumpHistoryType.requestLayout(); + binding.pumpHistoryType.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) { + if (manualChange) return + val selected = binding.pumpHistoryType.getSelectedItem() as TypeList + showingType = selected + selectedGroup = selected.entryGroup + filterHistory(selectedGroup) + val selectedText = parent!!.getChildAt(0) as TextView + selectedText.textSize = 15.0f // FIXME hack for selected item, also concerns pump_type marginTop + + binding.pumpHistoryTop.requestLayout() + } + + override fun onNothingSelected(parent: AdapterView<*>?) { + if (manualChange) return + filterHistory(PumpHistoryEntryGroup.All) + } + }) + binding.pumpHistoryTypeText.requestLayout() + } + + private fun getTypeList(list: List?): List { + val typeList = ArrayList() + for (pumpHistoryEntryGroup in list!!) { + typeList.add(TypeList(pumpHistoryEntryGroup)) + } + return typeList + } + + fun fromDpToSize(dpSize: Int): Int { + val scale = context.resources.displayMetrics.density + val pixelsFl = ((dpSize * scale) + 0.5f) + return pixelsFl.toInt() + } + + class TypeList internal constructor(var entryGroup: PumpHistoryEntryGroup) { + + var name: String + override fun toString(): String { + return name + } + + init { + name = entryGroup.translated!! + } + } + + class RecyclerViewAdapter internal constructor( + var historyList: List + ) : RecyclerView.Adapter() { + + fun setHistoryListInternal(historyList: List) { + this.historyList = historyList + } + + override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): HistoryViewHolder { + val v = LayoutInflater.from(viewGroup.context).inflate( + R.layout.pump_history_item, // + viewGroup, false + ) + return HistoryViewHolder(v) + } + + override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) { + val record = historyList[position] + holder.timeView.text = record.getEntryDateTime() + holder.typeView.text = record.getEntryType() + holder.valueView.text = record.getEntryValue() + } + + override fun getItemCount(): Int { + return historyList.size + } + + class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + + var timeView: TextView + var typeView: TextView + var valueView: TextView + + init { + timeView = itemView.findViewById(R.id.pump_history_time) + typeView = itemView.findViewById(R.id.pump_history_source) + valueView = itemView.findViewById(R.id.pump_history_description) + } + } + + } + + companion object { + var showingType: TypeList? = null + var selectedGroup = PumpHistoryEntryGroup.All + } +} \ No newline at end of file diff --git a/pump-common/src/main/res/layout/pump_ble_config_activity.xml b/pump-common/src/main/res/layout/pump_ble_config_activity.xml new file mode 100755 index 0000000000..b72fbd7e3f --- /dev/null +++ b/pump-common/src/main/res/layout/pump_ble_config_activity.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + +