diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt index db7cbba831..64f08720e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt @@ -30,6 +30,8 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobOref1Thread import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobThread import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkCommunicationManager +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLinkBLE +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RadioResponse import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.* import info.nightscout.androidaps.plugins.pump.medtronic.comm.MedtronicCommunicationManager import info.nightscout.androidaps.plugins.pump.medtronic.comm.ui.MedtronicUITask @@ -199,6 +201,8 @@ interface AppComponent : AndroidInjector { fun injectInitializePumpManagerTask(initializePumpManagerTask: InitializePumpManagerTask) fun injectResetRileyLinkConfigurationTask(resetRileyLinkConfigurationTask: ResetRileyLinkConfigurationTask) fun injectWakeAndTuneTask(wakeAndTuneTask: WakeAndTuneTask) + fun injectRadioResponse(radioResponse: RadioResponse) + fun injectRileyLinkBLE(rileyLinkBLE: RileyLinkBLE) @Component.Builder interface Builder { diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt index 5273cb61d5..25d3d58668 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -47,6 +47,8 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobOref1Thread import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobThread import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkCommunicationManager +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLinkBLE +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RadioResponse import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.* import info.nightscout.androidaps.plugins.pump.medtronic.comm.MedtronicCommunicationManager import info.nightscout.androidaps.plugins.pump.medtronic.comm.ui.MedtronicUITask @@ -292,6 +294,8 @@ open class AppModule { @ContributesAndroidInjector fun initializePumpManagerTaskProvider(): InitializePumpManagerTask @ContributesAndroidInjector fun resetRileyLinkConfigurationTaskProvider(): ResetRileyLinkConfigurationTask @ContributesAndroidInjector fun wakeAndTuneTaskProvider(): WakeAndTuneTask + @ContributesAndroidInjector fun radioResponseProvider(): RadioResponse + @ContributesAndroidInjector fun rileyLinkBLEProvider(): RileyLinkBLE @Binds fun bindActivePluginProvider(pluginStore: PluginStore): ActivePluginProvider diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java index 695f6e3292..e96ab06f27 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java @@ -49,7 +49,6 @@ public abstract class RileyLinkCommunicationManager { protected String receiverDeviceID; // String representation of receiver device (ex. Pump (xxxxxx) or Pod (yyyyyy)) protected long lastGoodReceiverCommunicationTime = 0; // protected PumpStatus pumpStatus; - protected RileyLinkServiceData rileyLinkServiceData; private long nextWakeUpRequired = 0L; private int timeoutCount = 0; @@ -59,7 +58,6 @@ public abstract class RileyLinkCommunicationManager { this.injector = injector; injector.androidInjector().inject(this); this.rfspy = rfspy; - this.rileyLinkServiceData = rileyLinkUtil.getRileyLinkServiceData(); } @@ -92,7 +90,7 @@ public abstract class RileyLinkCommunicationManager { RFSpyResponse rfSpyResponse = rfspy.transmitThenReceive(new RadioPacket(msg.getTxData()), (byte) 0, (byte) repeatCount, (byte) 0, (byte) 0, timeout_ms, (byte) retryCount, extendPreamble_ms); - RadioResponse radioResponse = rfSpyResponse.getRadioResponse(); + RadioResponse radioResponse = rfSpyResponse.getRadioResponse(injector); E response = createResponseMessage(radioResponse.getPayload(), clazz); if (response.isValid()) { @@ -122,7 +120,7 @@ public abstract class RileyLinkCommunicationManager { } if (showPumpMessages) { - aapsLogger.info(LTag.PUMPBTCOMM, "Received:" + ByteUtil.shortHexString(rfSpyResponse.getRadioResponse().getPayload())); + aapsLogger.info(LTag.PUMPBTCOMM, "Received:" + ByteUtil.shortHexString(rfSpyResponse.getRadioResponse(injector).getPayload())); } return response; @@ -246,7 +244,7 @@ public abstract class RileyLinkCommunicationManager { if (resp.wasTimeout()) { aapsLogger.error(LTag.PUMPBTCOMM, "scanForPump: Failed to find pump at frequency {}", frequencies[i]); } else if (resp.looksLikeRadioPacket()) { - RadioResponse radioResponse = new RadioResponse(); + RadioResponse radioResponse = new RadioResponse(injector); try { @@ -334,7 +332,7 @@ public abstract class RileyLinkCommunicationManager { if (resp.wasTimeout()) { aapsLogger.warn(LTag.PUMPBTCOMM, "tune_tryFrequency: no pump response at frequency {}", freqMHz); } else if (resp.looksLikeRadioPacket()) { - RadioResponse radioResponse = new RadioResponse(); + RadioResponse radioResponse = new RadioResponse(injector); try { radioResponse.init(resp.getRaw()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkUtil.java index d0c5eb73a5..960173a8bf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkUtil.java @@ -46,13 +46,12 @@ import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicDe @Singleton public class RileyLinkUtil { - public List historyRileyLink = new ArrayList<>(); + private List historyRileyLink = new ArrayList<>(); public RileyLinkCommunicationManager rileyLinkCommunicationManager; + @Deprecated static ServiceTask currentTask; - private RileyLinkServiceData rileyLinkServiceData; private RileyLinkTargetFrequency rileyLinkTargetFrequency; - private RileyLinkTargetDevice targetDevice; private RileyLinkEncodingType encoding; private RileyLinkSelectPreference rileyLinkSelectPreference; private Encoding4b6b encoding4b6b; @@ -104,62 +103,8 @@ public class RileyLinkUtil { LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } - - public void setServiceState(RileyLinkServiceState newState) { - setServiceState(newState, null); - } - - public RileyLinkServiceState getServiceState() { - return workWithServiceState(null, null, false); - } - - - public void setServiceState(RileyLinkServiceState newState, RileyLinkError errorCode) { - workWithServiceState(newState, errorCode, true); - } - - - private synchronized RileyLinkServiceState workWithServiceState(RileyLinkServiceState newState, - RileyLinkError errorCode, boolean set) { - - if (set) { - - rileyLinkServiceData.serviceState = newState; - rileyLinkServiceData.errorCode = errorCode; - - aapsLogger.info(LTag.PUMP, "RileyLink State Changed: {} {}", newState, errorCode == null ? "" : " - Error State: " - + errorCode.name()); - - historyRileyLink.add(new RLHistoryItem(rileyLinkServiceData.serviceState, - rileyLinkServiceData.errorCode, targetDevice)); - rxBus.send(new EventMedtronicDeviceStatusChange(newState, errorCode)); - return null; - - } else { - return (rileyLinkServiceData == null || rileyLinkServiceData.serviceState == null) ? // - RileyLinkServiceState.NotStarted - : rileyLinkServiceData.serviceState; - } - - } - - - public RileyLinkServiceData getRileyLinkServiceData() { - return rileyLinkServiceData; - } - - - @Deprecated - public void setRileyLinkServiceData(RileyLinkServiceData rileyLinkServiceData) { - this.rileyLinkServiceData = rileyLinkServiceData; - } - - - public boolean hasPumpBeenTunned() { - return rileyLinkServiceData.tuneUpDone; - } - // FIXME remove ? + @Deprecated public static void setCurrentTask(ServiceTask task) { if (currentTask == null) { currentTask = task; @@ -169,6 +114,7 @@ public class RileyLinkUtil { } + @Deprecated public static void finishCurrentTask(ServiceTask task) { if (task != currentTask) { //LOG.error("finishCurrentTask: task does not match"); @@ -257,42 +203,26 @@ public class RileyLinkUtil { return new BleAdvertisedData(uuids, name); } - public List getRileyLinkHistory() { return historyRileyLink; } - - public RileyLinkTargetDevice getTargetDevice() { - return targetDevice; - } - - - public void setTargetDevice(RileyLinkTargetDevice targetDevice) { - this.targetDevice = targetDevice; - } - - public void setRileyLinkSelectPreference(RileyLinkSelectPreference rileyLinkSelectPreference) { this.rileyLinkSelectPreference = rileyLinkSelectPreference; } - public RileyLinkSelectPreference getRileyLinkSelectPreference() { return rileyLinkSelectPreference; } - public Encoding4b6b getEncoding4b6b() { return encoding4b6b; } - public void setFirmwareVersion(RileyLinkFirmwareVersion firmwareVersion) { this.firmwareVersion = firmwareVersion; } - public RileyLinkFirmwareVersion getFirmwareVersion() { return firmwareVersion; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java index 49f1b426e3..cb4bc6c615 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java @@ -12,15 +12,16 @@ import android.content.Context; import android.os.SystemClock; import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.List; import java.util.UUID; import java.util.concurrent.Semaphore; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import javax.inject.Inject; + +import dagger.android.HasAndroidInjector; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.GattAttributes; @@ -31,6 +32,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.operation import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.operations.DescriptorWriteOperation; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.common.utils.ThreadUtil; @@ -40,7 +42,9 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ThreadUtil; */ public class RileyLinkBLE { - private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPBTCOMM); + @Inject AAPSLogger aapsLogger; + @Inject RileyLinkServiceData rileyLinkServiceData; + @Inject RileyLinkUtil rileyLinkUtil; private final Context context; public boolean gattDebugEnabled = true; @@ -55,24 +59,24 @@ public class RileyLinkBLE { private boolean mIsConnected = false; - public RileyLinkBLE(final Context context) { + public RileyLinkBLE(HasAndroidInjector injector, final Context context) { + injector.androidInjector().inject(this); this.context = context; this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - if (isLogEnabled()) - LOG.debug("BT Adapter: " + this.bluetoothAdapter); + aapsLogger.debug(LTag.PUMPBTCOMM, "BT Adapter: " + this.bluetoothAdapter); bluetoothGattCallback = new BluetoothGattCallback() { @Override public void onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); - if (gattDebugEnabled && isLogEnabled()) { - LOG.trace(ThreadUtil.sig() + "onCharacteristicChanged " + if (gattDebugEnabled) { + aapsLogger.debug(LTag.PUMPBTCOMM, ThreadUtil.sig() + "onCharacteristicChanged " + GattAttributes.lookup(characteristic.getUuid()) + " " + ByteUtil.getHex(characteristic.getValue())); if (characteristic.getUuid().equals(UUID.fromString(GattAttributes.CHARA_RADIO_RESPONSE_COUNT))) { - LOG.debug("Response Count is " + ByteUtil.shortHexString(characteristic.getValue())); + aapsLogger.debug(LTag.PUMPBTCOMM, "Response Count is " + ByteUtil.shortHexString(characteristic.getValue())); } } if (radioResponseCountNotified != null) { @@ -87,8 +91,8 @@ public class RileyLinkBLE { super.onCharacteristicRead(gatt, characteristic, status); final String statusMessage = getGattStatusMessage(status); - if (gattDebugEnabled && isLogEnabled()) { - LOG.trace(ThreadUtil.sig() + "onCharacteristicRead (" + if (gattDebugEnabled) { + aapsLogger.debug(LTag.PUMPBTCOMM, ThreadUtil.sig() + "onCharacteristicRead (" + GattAttributes.lookup(characteristic.getUuid()) + ") " + statusMessage + ":" + ByteUtil.getHex(characteristic.getValue())); } @@ -102,8 +106,8 @@ public class RileyLinkBLE { super.onCharacteristicWrite(gatt, characteristic, status); final String uuidString = GattAttributes.lookup(characteristic.getUuid()); - if (gattDebugEnabled && isLogEnabled()) { - LOG.trace(ThreadUtil.sig() + "onCharacteristicWrite " + getGattStatusMessage(status) + " " + if (gattDebugEnabled) { + aapsLogger.debug(LTag.PUMPBTCOMM, ThreadUtil.sig() + "onCharacteristicWrite " + getGattStatusMessage(status) + " " + uuidString + " " + ByteUtil.shortHexString(characteristic.getValue())); } mCurrentOperation.gattOperationCompletionCallback(characteristic.getUuid(), characteristic.getValue()); @@ -116,7 +120,7 @@ public class RileyLinkBLE { // https://github.com/NordicSemiconductor/puck-central-android/blob/master/PuckCentral/app/src/main/java/no/nordicsemi/puckcentral/bluetooth/gatt/GattManager.java#L117 if (status == 133) { - LOG.error("Got the status 133 bug, closing gatt"); + aapsLogger.error(LTag.PUMPBTCOMM, "Got the status 133 bug, closing gatt"); disconnect(); SystemClock.sleep(500); return; @@ -136,29 +140,27 @@ public class RileyLinkBLE { stateMessage = "UNKNOWN newState (" + newState + ")"; } - if (isLogEnabled()) - LOG.warn("onConnectionStateChange " + getGattStatusMessage(status) + " " + stateMessage); + aapsLogger.warn(LTag.PUMPBTCOMM, "onConnectionStateChange " + getGattStatusMessage(status) + " " + stateMessage); } if (newState == BluetoothProfile.STATE_CONNECTED) { if (status == BluetoothGatt.GATT_SUCCESS) { - RileyLinkUtil.getInstance().sendBroadcastMessage(RileyLinkConst.Intents.BluetoothConnected); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.BluetoothConnected); } else { - if (isLogEnabled()) - LOG.debug("BT State connected, GATT status {} ({})", status, getGattStatusMessage(status)); + aapsLogger.debug(LTag.PUMPBTCOMM, "BT State connected, GATT status {} ({})", status, getGattStatusMessage(status)); } } else if ((newState == BluetoothProfile.STATE_CONNECTING) || // (newState == BluetoothProfile.STATE_DISCONNECTING)) { - // LOG.debug("We are in {} state.", status == BluetoothProfile.STATE_CONNECTING ? "Connecting" : + // aapsLogger.debug(LTag.PUMPBTCOMM,"We are in {} state.", status == BluetoothProfile.STATE_CONNECTING ? "Connecting" : // "Disconnecting"); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { - RileyLinkUtil.getInstance().sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnected); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnected); if (manualDisconnect) close(); - LOG.warn("RileyLink Disconnected."); + aapsLogger.warn(LTag.PUMPBTCOMM, "RileyLink Disconnected."); } else { - LOG.warn("Some other state: (status={},newState={})", status, newState); + aapsLogger.warn(LTag.PUMPBTCOMM, "Some other state: (status={},newState={})", status, newState); } } @@ -166,8 +168,8 @@ public class RileyLinkBLE { @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorWrite(gatt, descriptor, status); - if (gattDebugEnabled && isLogEnabled()) { - LOG.warn("onDescriptorWrite " + GattAttributes.lookup(descriptor.getUuid()) + " " + if (gattDebugEnabled) { + aapsLogger.warn(LTag.PUMPBTCOMM, "onDescriptorWrite " + GattAttributes.lookup(descriptor.getUuid()) + " " + getGattStatusMessage(status) + " written: " + ByteUtil.getHex(descriptor.getValue())); } mCurrentOperation.gattOperationCompletionCallback(descriptor.getUuid(), descriptor.getValue()); @@ -178,8 +180,8 @@ public class RileyLinkBLE { public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorRead(gatt, descriptor, status); mCurrentOperation.gattOperationCompletionCallback(descriptor.getUuid(), descriptor.getValue()); - if (gattDebugEnabled && isLogEnabled()) { - LOG.warn("onDescriptorRead " + getGattStatusMessage(status) + " status " + descriptor); + if (gattDebugEnabled) { + aapsLogger.warn(LTag.PUMPBTCOMM, "onDescriptorRead " + getGattStatusMessage(status) + " status " + descriptor); } } @@ -187,8 +189,8 @@ public class RileyLinkBLE { @Override public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { super.onMtuChanged(gatt, mtu, status); - if (gattDebugEnabled && isLogEnabled()) { - LOG.warn("onMtuChanged " + mtu + " status " + status); + if (gattDebugEnabled) { + aapsLogger.warn(LTag.PUMPBTCOMM, "onMtuChanged " + mtu + " status " + status); } } @@ -196,8 +198,8 @@ public class RileyLinkBLE { @Override public void onReadRemoteRssi(final BluetoothGatt gatt, int rssi, int status) { super.onReadRemoteRssi(gatt, rssi, status); - if (gattDebugEnabled && isLogEnabled()) { - LOG.warn("onReadRemoteRssi " + getGattStatusMessage(status) + ": " + rssi); + if (gattDebugEnabled) { + aapsLogger.warn(LTag.PUMPBTCOMM, "onReadRemoteRssi " + getGattStatusMessage(status) + ": " + rssi); } } @@ -205,8 +207,8 @@ public class RileyLinkBLE { @Override public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { super.onReliableWriteCompleted(gatt, status); - if (gattDebugEnabled && isLogEnabled()) { - LOG.warn("onReliableWriteCompleted status " + status); + if (gattDebugEnabled) { + aapsLogger.warn(LTag.PUMPBTCOMM, "onReliableWriteCompleted status " + status); } } @@ -232,27 +234,26 @@ public class RileyLinkBLE { } } - if (gattDebugEnabled && isLogEnabled()) { - LOG.warn("onServicesDiscovered " + getGattStatusMessage(status)); + if (gattDebugEnabled) { + aapsLogger.warn(LTag.PUMPBTCOMM, "onServicesDiscovered " + getGattStatusMessage(status)); } - LOG.info("Gatt device is RileyLink device: " + rileyLinkFound); + aapsLogger.info(LTag.PUMPBTCOMM, "Gatt device is RileyLink device: " + rileyLinkFound); if (rileyLinkFound) { mIsConnected = true; - RileyLinkUtil.getInstance().sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkReady); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkReady); // RileyLinkUtil.sendNotification(new // ServiceNotification(RileyLinkConst.Intents.RileyLinkReady), null); } else { mIsConnected = false; - RileyLinkUtil.getInstance().setServiceState(RileyLinkServiceState.RileyLinkError, + rileyLinkServiceData.setServiceState(RileyLinkServiceState.RileyLinkError, RileyLinkError.DeviceIsNotRileyLink); } } else { - if (isLogEnabled()) - LOG.debug("onServicesDiscovered " + getGattStatusMessage(status)); - RileyLinkUtil.getInstance().sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkGattFailed); + aapsLogger.debug(LTag.PUMPBTCOMM, "onServicesDiscovered " + getGattStatusMessage(status)); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkGattFailed); } } }; @@ -261,9 +262,7 @@ public class RileyLinkBLE { private boolean isAnyRileyLinkServiceFound(BluetoothGattService service) { - boolean found = false; - - found = GattAttributes.isRileyLink(service.getUuid()); + boolean found = GattAttributes.isRileyLink(service.getUuid()); if (found) { return true; @@ -313,7 +312,7 @@ public class RileyLinkBLE { stringBuilder.append("\n\n"); - LOG.warn(stringBuilder.toString()); + aapsLogger.warn(LTag.PUMPBTCOMM, stringBuilder.toString()); List includedServices = service.getIncludedServices(); @@ -324,7 +323,7 @@ public class RileyLinkBLE { } - public void registerRadioResponseCountNotification(Runnable notifier) { + void registerRadioResponseCountNotification(Runnable notifier) { radioResponseCountNotified = notifier; } @@ -342,11 +341,10 @@ public class RileyLinkBLE { } if (bluetoothConnectionGatt.discoverServices()) { - if (isLogEnabled()) - LOG.warn("Starting to discover GATT Services."); + aapsLogger.warn(LTag.PUMPBTCOMM, "Starting to discover GATT Services."); return true; } else { - LOG.error("Cannot discover GATT Services."); + aapsLogger.error(LTag.PUMPBTCOMM, "Cannot discover GATT Services."); return false; } } @@ -356,7 +354,7 @@ public class RileyLinkBLE { BLECommOperationResult result = setNotification_blocking(UUID.fromString(GattAttributes.SERVICE_RADIO), // UUID.fromString(GattAttributes.CHARA_RADIO_RESPONSE_COUNT)); if (result.resultCode != BLECommOperationResult.RESULT_SUCCESS) { - LOG.error("Error setting response count notification"); + aapsLogger.error(LTag.PUMPBTCOMM, "Error setting response count notification"); return false; } return true; @@ -364,36 +362,34 @@ public class RileyLinkBLE { public void findRileyLink(String RileyLinkAddress) { - if (isLogEnabled()) - LOG.debug("RileyLink address: " + RileyLinkAddress); + aapsLogger.debug(LTag.PUMPBTCOMM, "RileyLink address: " + RileyLinkAddress); // Must verify that this is a valid MAC, or crash. rileyLinkDevice = bluetoothAdapter.getRemoteDevice(RileyLinkAddress); // if this succeeds, we get a connection state change callback? - if (rileyLinkDevice!=null) { + if (rileyLinkDevice != null) { connectGatt(); } else { - LOG.error("RileyLink device not found with address: " + RileyLinkAddress); + aapsLogger.error(LTag.PUMPBTCOMM, "RileyLink device not found with address: " + RileyLinkAddress); } } // This function must be run on UI thread. public void connectGatt() { - if (this.rileyLinkDevice==null) { - LOG.error("RileyLink device is null, can't do connectGatt."); + if (this.rileyLinkDevice == null) { + aapsLogger.error(LTag.PUMPBTCOMM, "RileyLink device is null, can't do connectGatt."); return; } bluetoothConnectionGatt = rileyLinkDevice.connectGatt(context, true, bluetoothGattCallback); // , BluetoothDevice.TRANSPORT_LE if (bluetoothConnectionGatt == null) { - LOG.error("Failed to connect to Bluetooth Low Energy device at " + bluetoothAdapter.getAddress()); + aapsLogger.error(LTag.PUMPBTCOMM, "Failed to connect to Bluetooth Low Energy device at " + bluetoothAdapter.getAddress()); } else { if (gattDebugEnabled) { - if (isLogEnabled()) - LOG.debug("Gatt Connected."); + aapsLogger.debug(LTag.PUMPBTCOMM, "Gatt Connected."); } } } @@ -401,8 +397,7 @@ public class RileyLinkBLE { public void disconnect() { mIsConnected = false; - if (isLogEnabled()) - LOG.warn("Closing GATT connection"); + aapsLogger.warn(LTag.PUMPBTCOMM, "Closing GATT connection"); // Close old conenction if (bluetoothConnectionGatt != null) { // Not sure if to disconnect or to close first.. @@ -420,7 +415,7 @@ public class RileyLinkBLE { } - public BLECommOperationResult setNotification_blocking(UUID serviceUUID, UUID charaUUID) { + private BLECommOperationResult setNotification_blocking(UUID serviceUUID, UUID charaUUID) { BLECommOperationResult rval = new BLECommOperationResult(); if (bluetoothConnectionGatt != null) { @@ -428,7 +423,7 @@ public class RileyLinkBLE { gattOperationSema.acquire(); SystemClock.sleep(1); // attempting to yield thread, to make sequence of events easier to follow } catch (InterruptedException e) { - LOG.error("setNotification_blocking: interrupted waiting for gattOperationSema"); + aapsLogger.error(LTag.PUMPBTCOMM, "setNotification_blocking: interrupted waiting for gattOperationSema"); return rval; } if (mCurrentOperation != null) { @@ -437,7 +432,7 @@ public class RileyLinkBLE { if (bluetoothConnectionGatt.getService(serviceUUID) == null) { // Catch if the service is not supported by the BLE device rval.resultCode = BLECommOperationResult.RESULT_NONE; - LOG.error("BT Device not supported"); + aapsLogger.error(LTag.PUMPBTCOMM, "BT Device not supported"); // TODO: 11/07/2016 UI update for user } else { BluetoothGattCharacteristic chara = bluetoothConnectionGatt.getService(serviceUUID) @@ -447,8 +442,7 @@ public class RileyLinkBLE { List list = chara.getDescriptors(); if (gattDebugEnabled) { for (int i = 0; i < list.size(); i++) { - if (isLogEnabled()) - LOG.debug("Found descriptor: " + list.get(i).toString()); + aapsLogger.debug(LTag.PUMPBTCOMM, "Found descriptor: " + list.get(i).toString()); } } BluetoothGattDescriptor descr = list.get(0); @@ -468,7 +462,7 @@ public class RileyLinkBLE { gattOperationSema.release(); } } else { - LOG.error("setNotification_blocking: not configured!"); + aapsLogger.error(LTag.PUMPBTCOMM, "setNotification_blocking: not configured!"); rval.resultCode = BLECommOperationResult.RESULT_NOT_CONFIGURED; } return rval; @@ -476,7 +470,7 @@ public class RileyLinkBLE { // call from main - public BLECommOperationResult writeCharacteristic_blocking(UUID serviceUUID, UUID charaUUID, byte[] value) { + BLECommOperationResult writeCharacteristic_blocking(UUID serviceUUID, UUID charaUUID, byte[] value) { BLECommOperationResult rval = new BLECommOperationResult(); if (bluetoothConnectionGatt != null) { rval.value = value; @@ -484,7 +478,7 @@ public class RileyLinkBLE { gattOperationSema.acquire(); SystemClock.sleep(1); // attempting to yield thread, to make sequence of events easier to follow } catch (InterruptedException e) { - LOG.error("writeCharacteristic_blocking: interrupted waiting for gattOperationSema"); + aapsLogger.error(LTag.PUMPBTCOMM, "writeCharacteristic_blocking: interrupted waiting for gattOperationSema"); return rval; } @@ -497,7 +491,7 @@ public class RileyLinkBLE { // app that created the bluetoothConnectionGatt has been destroyed/created, // e.g. when the user switches from portrait to landscape. rval.resultCode = BLECommOperationResult.RESULT_NONE; - LOG.error("BT Device not supported"); + aapsLogger.error(LTag.PUMPBTCOMM, "BT Device not supported"); // TODO: 11/07/2016 UI update for user } else { BluetoothGattCharacteristic chara = bluetoothConnectionGatt.getService(serviceUUID) @@ -516,21 +510,21 @@ public class RileyLinkBLE { gattOperationSema.release(); } } else { - LOG.error("writeCharacteristic_blocking: not configured!"); + aapsLogger.error(LTag.PUMPBTCOMM, "writeCharacteristic_blocking: not configured!"); rval.resultCode = BLECommOperationResult.RESULT_NOT_CONFIGURED; } return rval; } - public BLECommOperationResult readCharacteristic_blocking(UUID serviceUUID, UUID charaUUID) { + BLECommOperationResult readCharacteristic_blocking(UUID serviceUUID, UUID charaUUID) { BLECommOperationResult rval = new BLECommOperationResult(); if (bluetoothConnectionGatt != null) { try { gattOperationSema.acquire(); SystemClock.sleep(1); // attempting to yield thread, to make sequence of events easier to follow } catch (InterruptedException e) { - LOG.error("readCharacteristic_blocking: Interrupted waiting for gattOperationSema"); + aapsLogger.error(LTag.PUMPBTCOMM, "readCharacteristic_blocking: Interrupted waiting for gattOperationSema"); return rval; } if (mCurrentOperation != null) { @@ -539,7 +533,7 @@ public class RileyLinkBLE { if (bluetoothConnectionGatt.getService(serviceUUID) == null) { // Catch if the service is not supported by the BLE device rval.resultCode = BLECommOperationResult.RESULT_NONE; - LOG.error("BT Device not supported"); + aapsLogger.error(LTag.PUMPBTCOMM, "BT Device not supported"); // TODO: 11/07/2016 UI update for user } else { BluetoothGattCharacteristic chara = bluetoothConnectionGatt.getService(serviceUUID).getCharacteristic( @@ -559,7 +553,7 @@ public class RileyLinkBLE { mCurrentOperation = null; gattOperationSema.release(); } else { - LOG.error("readCharacteristic_blocking: not configured!"); + aapsLogger.error(LTag.PUMPBTCOMM, "readCharacteristic_blocking: not configured!"); rval.resultCode = BLECommOperationResult.RESULT_NOT_CONFIGURED; } return rval; @@ -582,9 +576,4 @@ public class RileyLinkBLE { return statusMessage; } - - private boolean isLogEnabled() { - return L.isEnabled(L.PUMPBTCOMM); - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RFSpyResponse.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RFSpyResponse.java index cf5362427c..a766e92266 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RFSpyResponse.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RFSpyResponse.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data; +import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLinkCommunicationException; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.RileyLinkCommand; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RFSpyRLResponse; @@ -48,12 +49,12 @@ public class RFSpyResponse { } - public RadioResponse getRadioResponse() throws RileyLinkCommunicationException { + public RadioResponse getRadioResponse(HasAndroidInjector injector) throws RileyLinkCommunicationException { if (looksLikeRadioPacket()) { - radioResponse = new RadioResponse(command); + radioResponse = new RadioResponse(injector, command); radioResponse.init(raw); } else { - radioResponse = new RadioResponse(); + radioResponse = new RadioResponse(injector); } return radioResponse; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioResponse.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioResponse.java index 3caf7872c0..343e32da67 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioResponse.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioResponse.java @@ -1,16 +1,19 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data; import org.apache.commons.lang3.NotImplementedException; -import org.slf4j.Logger; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import javax.inject.Inject; + +import dagger.android.HasAndroidInjector; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLinkCommunicationException; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.RileyLinkCommand; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkBLEError; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkCommandType; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.common.utils.CRC; @@ -19,26 +22,24 @@ import info.nightscout.androidaps.plugins.pump.common.utils.CRC; */ public class RadioResponse { - private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPBTCOMM); - public boolean decodedOK = false; + @Inject AAPSLogger aapsLogger; + @Inject RileyLinkServiceData rileyLinkServiceData; + + private boolean decodedOK = false; public int rssi; - public int responseNumber; - public byte[] decodedPayload = new byte[0]; - public byte receivedCRC; + private int responseNumber; + private byte[] decodedPayload = new byte[0]; + private byte receivedCRC; private RileyLinkCommand command; - public RadioResponse() { - + public RadioResponse(HasAndroidInjector injector) { + injector.androidInjector().inject(this); } - - // public RadioResponse(byte[] rxData) { - // init(rxData); - // } - - public RadioResponse(RileyLinkCommand command /* , byte[] raw */) { + public RadioResponse(HasAndroidInjector injector, RileyLinkCommand command /* , byte[] raw */) { + this(injector); this.command = command; // init(raw); } @@ -74,7 +75,7 @@ public class RadioResponse { } byte[] encodedPayload; - if (RileyLinkFirmwareVersion.isSameVersion(RileyLinkUtil.getInstance().getRileyLinkServiceData().versionCC110, + if (RileyLinkFirmwareVersion.isSameVersion(rileyLinkServiceData.versionCC110, RileyLinkFirmwareVersion.Version2)) { encodedPayload = ByteUtil.substring(rxData, 3, rxData.length - 3); rssi = rxData[1]; @@ -116,7 +117,7 @@ public class RadioResponse { receivedCRC = decodeThis[decodeThis.length - 1]; byte calculatedCRC = CRC.crc8(decodedPayload); if (receivedCRC != calculatedCRC) { - LOG.error(String.format("RadioResponse: CRC mismatch, calculated 0x%02x, received 0x%02x", + aapsLogger.error(LTag.PUMPCOMM, String.format("RadioResponse: CRC mismatch, calculated 0x%02x, received 0x%02x", calculatedCRC, receivedCRC)); } } else { @@ -131,7 +132,7 @@ public class RadioResponse { } } catch (NumberFormatException e) { decodedOK = false; - LOG.error("Failed to decode radio data: " + ByteUtil.shortHexString(encodedPayload)); + aapsLogger.error(LTag.PUMPBTCOMM, "Failed to decode radio data: " + ByteUtil.shortHexString(encodedPayload)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java index 09af88b437..4fc0069194 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java @@ -27,12 +27,12 @@ public class RileyLinkStatusActivity extends NoSplashAppCompatActivity { @Inject ResourceHelper resourceHelper; @Inject RileyLinkUtil rileyLinkUtil; + @Inject RileyLinkServiceData rileyLinkServiceData; TextView connectionStatus; TextView configuredAddress; TextView connectedDevice; TextView connectionError; - RileyLinkServiceData rileyLinkServiceData; private SectionsPagerAdapter mSectionsPagerAdapter; private FloatingActionButton floatingActionButton; @@ -77,8 +77,6 @@ public class RileyLinkStatusActivity extends NoSplashAppCompatActivity { this.connectedDevice = findViewById(R.id.rls_t1_connected_device); this.connectionError = findViewById(R.id.rls_t1_connection_error); - rileyLinkServiceData = rileyLinkUtil.getRileyLinkServiceData(); - // // 7-12 // int[] ids = {R.id.rls_t1_tv02, R.id.rls_t1_tv03, R.id.rls_t1_tv04, R.id.rls_t1_tv05, R.id.rls_t1_tv07, // // R.id.rls_t1_tv08, R.id.rls_t1_tv09, R.id.rls_t1_tv10, R.id.rls_t1_tv11, R.id.rls_t1_tv12}; @@ -98,7 +96,7 @@ public class RileyLinkStatusActivity extends NoSplashAppCompatActivity { public void refreshData(int position) { if (position == 0) { // FIXME i18n - this.connectionStatus.setText(rileyLinkServiceData.serviceState.name()); + this.connectionStatus.setText(rileyLinkServiceData.rileyLinkServiceState.name()); this.configuredAddress.setText(rileyLinkServiceData.rileylinkAddress); // FIXME this.connectedDevice.setText("???"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java index 4365c52757..ccf5bd0bcb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java @@ -17,7 +17,6 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.pump.common.dialog.RefreshableInterface; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion; -import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil; @@ -37,6 +36,7 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re @Inject MedtronicPumpStatus medtronicPumpStatus; @Inject ResourceHelper resourceHelper; @Inject MedtronicPumpPlugin medtronicPumpPlugin; + @Inject RileyLinkServiceData rileyLinkServiceData; TextView connectionStatus; TextView configuredAddress; @@ -50,8 +50,6 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re TextView lastDeviceContact; TextView firmwareVersion; - RileyLinkServiceData rileyLinkServiceData; - boolean first = false; @@ -66,7 +64,6 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re @Override public void onStart() { super.onStart(); - rileyLinkServiceData = rileyLinkUtil.getRileyLinkServiceData(); this.connectionStatus = getActivity().findViewById(R.id.rls_t1_connection_status); this.configuredAddress = getActivity().findViewById(R.id.rls_t1_configured_address); @@ -101,12 +98,9 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re public void refreshData() { - RileyLinkTargetDevice targetDevice = rileyLinkUtil.getTargetDevice(); + RileyLinkTargetDevice targetDevice = rileyLinkServiceData.targetDevice; - if (rileyLinkUtil.getServiceState() == null) - this.connectionStatus.setText(resourceHelper.gs(RileyLinkServiceState.NotStarted.getResourceId(targetDevice))); - else - this.connectionStatus.setText(resourceHelper.gs(rileyLinkUtil.getServiceState().getResourceId(targetDevice))); + this.connectionStatus.setText(resourceHelper.gs(rileyLinkServiceData.rileyLinkServiceState.getResourceId(targetDevice))); if (rileyLinkServiceData != null) { this.configuredAddress.setText(rileyLinkServiceData.rileylinkAddress); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkBroadcastReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkBroadcastReceiver.java index 2af0eccfdb..6dd29285f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkBroadcastReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkBroadcastReceiver.java @@ -46,18 +46,16 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver { @Inject HasAndroidInjector injector; @Inject RileyLinkUtil rileyLinkUtil; @Inject SP sp; + @Inject RileyLinkServiceData rileyLinkServiceData; private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM); RileyLinkService serviceInstance; protected Map> broadcastIdentifiers = null; String deviceSpecificPrefix; - Context context; - - public RileyLinkBroadcastReceiver(RileyLinkService serviceInstance, Context context) { + public RileyLinkBroadcastReceiver(RileyLinkService serviceInstance) { this.serviceInstance = serviceInstance; - this.context = context; createBroadcastIdentifiers(); } @@ -120,7 +118,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver { } - public void registerBroadcasts() { + public void registerBroadcasts(Context context) { IntentFilter intentFilter = new IntentFilter(); @@ -143,9 +141,9 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver { if (action.equals(RileyLinkConst.Intents.RileyLinkDisconnected)) { if (BluetoothAdapter.getDefaultAdapter().isEnabled()) { - rileyLinkUtil.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.RileyLinkUnreachable); + rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.RileyLinkUnreachable); } else { - rileyLinkUtil.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.BluetoothDisabled); + rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.BluetoothDisabled); } return true; @@ -155,25 +153,22 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver { LOG.warn("MedtronicConst.Intents.RileyLinkReady"); // sendIPCNotification(RT2Const.IPC.MSG_note_WakingPump); - if (this.serviceInstance.rileyLinkBLE == null) - return false; + serviceInstance.rileyLinkBLE.enableNotifications(); + serviceInstance.rfspy.startReader(); // call startReader from outside? - this.serviceInstance.rileyLinkBLE.enableNotifications(); - this.serviceInstance.rfspy.startReader(); // call startReader from outside? - - this.serviceInstance.rfspy.initializeRileyLink(); - String bleVersion = this.serviceInstance.rfspy.getBLEVersionCached(); - RileyLinkFirmwareVersion rlVersion = this.serviceInstance.rfspy.getRLVersionCached(); + serviceInstance.rfspy.initializeRileyLink(); + String bleVersion = serviceInstance.rfspy.getBLEVersionCached(); + RileyLinkFirmwareVersion rlVersion = serviceInstance.rfspy.getRLVersionCached(); // if (isLoggingEnabled()) LOG.debug("RfSpy version (BLE113): " + bleVersion); - this.serviceInstance.rileyLinkServiceData.versionBLE113 = bleVersion; + serviceInstance.rileyLinkServiceData.versionBLE113 = bleVersion; // if (isLoggingEnabled()) LOG.debug("RfSpy Radio version (CC110): " + rlVersion.name()); - this.serviceInstance.rileyLinkServiceData.versionCC110 = rlVersion; + serviceInstance.rileyLinkServiceData.versionCC110 = rlVersion; - ServiceTask task = new InitializePumpManagerTask(injector, rileyLinkUtil.getTargetDevice()); + ServiceTask task = new InitializePumpManagerTask(injector); ServiceTaskExecutor.startTask(task); if (isLoggingEnabled()) LOG.info("Announcing RileyLink open For business"); @@ -186,13 +181,13 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver { } else { // showBusy("Configuring Service", 50); // rileyLinkBLE.findRileyLink(RileylinkBLEAddress); - this.serviceInstance.reconfigureRileyLink(RileylinkBLEAddress); + serviceInstance.reconfigureRileyLink(RileylinkBLEAddress); // MainApp.getServiceClientConnection().setThisRileylink(RileylinkBLEAddress); } return true; } else if (action.equals(RileyLinkConst.Intents.RileyLinkDisconnect)) { - this.serviceInstance.disconnectRileyLink(); + serviceInstance.disconnectRileyLink(); return true; } else { @@ -247,7 +242,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver { } if (action.startsWith(this.deviceSpecificPrefix)) { - return this.serviceInstance.handleDeviceSpecificBroadcasts(intent); + return serviceInstance.handleDeviceSpecificBroadcasts(intent); } else return false; } @@ -262,7 +257,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver { return (L.isEnabled(L.PUMPCOMM)); } - public void unregisterBroadcasts() { + public void unregisterBroadcasts(Context context) { LocalBroadcastManager.getInstance(context).unregisterReceiver(this); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java index 282595a193..371a74b576 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java @@ -12,6 +12,7 @@ import javax.inject.Inject; import dagger.android.DaggerService; import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; +import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkCommunicationManager; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; @@ -36,17 +37,18 @@ public abstract class RileyLinkService extends DaggerService { @Inject protected AAPSLogger aapsLogger; @Inject protected SP sp; @Inject protected Context context; + @Inject protected RxBusWrapper rxBus; @Inject protected RileyLinkUtil rileyLinkUtil; @Inject protected MedtronicUtil medtronicUtil; // TODO should be avoided here as it's MDT - + @Inject protected RileyLinkServiceData rileyLinkServiceData; @NotNull protected RileyLinkBLE rileyLinkBLE; // android-bluetooth management, must be set in initRileyLinkServiceData protected BluetoothAdapter bluetoothAdapter; protected RFSpy rfspy; // interface for RL xxx Mhz radio. protected RileyLinkBroadcastReceiver mBroadcastReceiver; - protected RileyLinkServiceData rileyLinkServiceData; protected RileyLinkBluetoothStateReceiver bluetoothStateReceiver; + @Override public void onCreate() { super.onCreate(); @@ -54,8 +56,8 @@ public abstract class RileyLinkService extends DaggerService { rileyLinkUtil.setEncoding(getEncoding()); initRileyLinkServiceData(); - mBroadcastReceiver = new RileyLinkBroadcastReceiver(this, this.context); - mBroadcastReceiver.registerBroadcasts(); + mBroadcastReceiver = new RileyLinkBroadcastReceiver(this); + mBroadcastReceiver.registerBroadcasts(this); bluetoothStateReceiver = new RileyLinkBluetoothStateReceiver(); @@ -96,7 +98,7 @@ public abstract class RileyLinkService extends DaggerService { rileyLinkBLE.disconnect(); // dispose of Gatt (disconnect and close) if (mBroadcastReceiver != null) { - mBroadcastReceiver.unregisterBroadcasts(); + mBroadcastReceiver.unregisterBroadcasts(this); } if (bluetoothStateReceiver != null) { @@ -133,20 +135,20 @@ public abstract class RileyLinkService extends DaggerService { public boolean bluetoothInit() { aapsLogger.debug(LTag.PUMPCOMM, "bluetoothInit: attempting to get an adapter"); - rileyLinkUtil.setServiceState(RileyLinkServiceState.BluetoothInitializing); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.BluetoothInitializing); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { aapsLogger.error("Unable to obtain a BluetoothAdapter."); - rileyLinkUtil.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.NoBluetoothAdapter); + rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.NoBluetoothAdapter); } else { if (!bluetoothAdapter.isEnabled()) { aapsLogger.error("Bluetooth is not enabled."); - rileyLinkUtil.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.BluetoothDisabled); + rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.BluetoothDisabled); } else { - rileyLinkUtil.setServiceState(RileyLinkServiceState.BluetoothReady); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.BluetoothReady); return true; } } @@ -158,7 +160,7 @@ public abstract class RileyLinkService extends DaggerService { // returns true if our Rileylink configuration changed public boolean reconfigureRileyLink(String deviceAddress) { - rileyLinkUtil.setServiceState(RileyLinkServiceState.RileyLinkInitializing); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkInitializing); if (rileyLinkBLE.isConnected()) { if (deviceAddress.equals(rileyLinkServiceData.rileylinkAddress)) { @@ -179,7 +181,7 @@ public abstract class RileyLinkService extends DaggerService { } else { aapsLogger.debug(LTag.PUMPCOMM, "Using RL " + deviceAddress); - if (rileyLinkUtil.getServiceState() == RileyLinkServiceState.NotStarted) { + if (rileyLinkServiceData.getRileyLinkServiceState() == RileyLinkServiceState.NotStarted) { if (!bluetoothInit()) { aapsLogger.error("RileyLink can't get activated, Bluetooth is not functioning correctly. {}", getError() != null ? getError().name() : "Unknown error (null)"); @@ -201,7 +203,7 @@ public abstract class RileyLinkService extends DaggerService { // FIXME: This needs to be run in a session so that is interruptable, has a separate thread, etc. public void doTuneUpDevice() { - rileyLinkUtil.setServiceState(RileyLinkServiceState.TuneUpDevice); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.TuneUpDevice); medtronicUtil.setPumpDeviceState(PumpDeviceState.Sleeping); double lastGoodFrequency = 0.0d; @@ -226,10 +228,10 @@ public abstract class RileyLinkService extends DaggerService { if (newFrequency == 0.0d) { // error tuning pump, pump not present ?? - rileyLinkUtil.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.TuneUpOfDeviceFailed); + rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.TuneUpOfDeviceFailed); } else { getDeviceCommunicationManager().clearNotConnectedCount(); - rileyLinkUtil.setServiceState(RileyLinkServiceState.PumpConnectorReady); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady); } } @@ -241,7 +243,7 @@ public abstract class RileyLinkService extends DaggerService { rileyLinkServiceData.rileylinkAddress = null; } - rileyLinkUtil.setServiceState(RileyLinkServiceState.BluetoothReady); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.BluetoothReady); } @NotNull public RileyLinkBLE getRileyLinkBLE() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java index dd66255da4..9a2779806d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java @@ -1,21 +1,35 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service; +import javax.inject.Inject; +import javax.inject.Singleton; + +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; +import info.nightscout.androidaps.plugins.bus.RxBusWrapper; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.RLHistoryItem; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice; +import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicDeviceStatusChange; /** * Created by andy on 16/05/2018. */ +@Singleton public class RileyLinkServiceData { - public boolean tuneUpDone = false; + @Inject AAPSLogger aapsLogger; + @Inject RileyLinkUtil rileyLinkUtil; + @Inject RxBusWrapper rxBus; + + boolean tuneUpDone = false; public RileyLinkError errorCode; - public RileyLinkServiceState serviceState = RileyLinkServiceState.NotStarted; + public RileyLinkServiceState rileyLinkServiceState = RileyLinkServiceState.NotStarted; public String rileylinkAddress; - public long lastTuneUpTime = 0L; + long lastTuneUpTime = 0L; public Double lastGoodFrequency; // bt version @@ -29,15 +43,45 @@ public class RileyLinkServiceData { public String pumpID; public byte[] pumpIDBytes; - - public RileyLinkServiceData(RileyLinkTargetDevice targetDevice) { - this.targetDevice = targetDevice; - } - + @Inject + public RileyLinkServiceData() {} public void setPumpID(String pumpId, byte[] pumpIdBytes) { this.pumpID = pumpId; this.pumpIDBytes = pumpIdBytes; } + public void setRileyLinkServiceState(RileyLinkServiceState newState) { + setServiceState(newState, null); + } + + public RileyLinkServiceState getRileyLinkServiceState() { + return workWithServiceState(null, null, false); + } + + + public void setServiceState(RileyLinkServiceState newState, RileyLinkError errorCode) { + workWithServiceState(newState, errorCode, true); + } + + + private synchronized RileyLinkServiceState workWithServiceState(RileyLinkServiceState newState, RileyLinkError errorCode, boolean set) { + + if (set) { + + rileyLinkServiceState = newState; + this.errorCode = errorCode; + + aapsLogger.info(LTag.PUMP, "RileyLink State Changed: {} {}", newState, errorCode == null ? "" : " - Error State: " + errorCode.name()); + + rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItem(rileyLinkServiceState, errorCode, targetDevice)); + rxBus.send(new EventMedtronicDeviceStatusChange(newState, errorCode)); + return null; + + } else { + return rileyLinkServiceState; + } + + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.java index 64f7c8343b..4f79eb0561 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.java @@ -1,22 +1,20 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks; -import org.slf4j.Logger; - import javax.inject.Inject; import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.interfaces.ActivePluginProvider; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.pump.common.PumpPluginAbstract; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkCommunicationManager; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; -import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport; -import info.nightscout.androidaps.utils.SP; +import info.nightscout.androidaps.utils.sharedPreferences.SP; /** * Created by geoff on 7/9/16. @@ -25,66 +23,62 @@ import info.nightscout.androidaps.utils.SP; */ public class InitializePumpManagerTask extends ServiceTask { + @Inject AAPSLogger aapsLogger; @Inject ActivePluginProvider activePlugin; + @Inject SP sp; + @Inject RileyLinkServiceData rileyLinkServiceData; + @Inject RileyLinkUtil rileyLinkUtil; - private static final String TAG = "InitPumpManagerTask"; - private RileyLinkTargetDevice targetDevice; - private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM); - - public InitializePumpManagerTask(HasAndroidInjector injector, RileyLinkTargetDevice targetDevice) { + public InitializePumpManagerTask(HasAndroidInjector injector) { super(injector); - this.targetDevice = targetDevice; } - public InitializePumpManagerTask(HasAndroidInjector injector, ServiceTransport transport) { super(injector, transport); } - @Override public void run() { - double lastGoodFrequency = 0.0d; + double lastGoodFrequency; - if (RileyLinkUtil.getInstance().getRileyLinkServiceData().lastGoodFrequency == null) { + if (rileyLinkServiceData.lastGoodFrequency == null) { - lastGoodFrequency = SP.getDouble(RileyLinkConst.Prefs.LastGoodDeviceFrequency, 0.0d); + lastGoodFrequency = sp.getDouble(RileyLinkConst.Prefs.LastGoodDeviceFrequency, 0.0d); lastGoodFrequency = Math.round(lastGoodFrequency * 1000d) / 1000d; - RileyLinkUtil.getInstance().getRileyLinkServiceData().lastGoodFrequency = lastGoodFrequency; + rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency; // if (RileyLinkUtil.getRileyLinkTargetFrequency() == null) { // String pumpFrequency = SP.getString(MedtronicConst.Prefs.PumpFrequency, null); // } } else { - lastGoodFrequency = RileyLinkUtil.getInstance().getRileyLinkServiceData().lastGoodFrequency; + lastGoodFrequency = rileyLinkServiceData.lastGoodFrequency; } - RileyLinkCommunicationManager rileyLinkCommunicationManager = ((PumpPluginAbstract)activePlugin.getActivePump()).getRileyLinkService().getDeviceCommunicationManager(); + RileyLinkCommunicationManager rileyLinkCommunicationManager = ((PumpPluginAbstract) activePlugin.getActivePump()).getRileyLinkService().getDeviceCommunicationManager(); if ((lastGoodFrequency > 0.0d) && rileyLinkCommunicationManager.isValidFrequency(lastGoodFrequency)) { - RileyLinkUtil.getInstance().setServiceState(RileyLinkServiceState.RileyLinkReady); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkReady); - if (L.isEnabled(L.PUMPCOMM)) - LOG.info("Setting radio frequency to {} MHz", lastGoodFrequency); + aapsLogger.info(LTag.PUMPBTCOMM, "Setting radio frequency to {} MHz", lastGoodFrequency); rileyLinkCommunicationManager.setRadioFrequencyForPump(lastGoodFrequency); boolean foundThePump = rileyLinkCommunicationManager.tryToConnectToDevice(); if (foundThePump) { - RileyLinkUtil.getInstance().setServiceState(RileyLinkServiceState.PumpConnectorReady); + rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady); } else { - RileyLinkUtil.getInstance().setServiceState(RileyLinkServiceState.PumpConnectorError, + rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.NoContactWithDevice); - RileyLinkUtil.getInstance().sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump); } } else { - RileyLinkUtil.getInstance().sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTask.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTask.java index 178c5185f1..7fff8e225a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTask.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTask.java @@ -8,7 +8,6 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data. */ public class ServiceTask implements Runnable { - private static final String TAG = "ServiceTask(base)"; public boolean completed = false; protected ServiceTransport mTransport; protected HasAndroidInjector injector; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt index b4e2a0739e..947691a528 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt @@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLin import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.dialog.RileyLinkStatusActivity +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData import info.nightscout.androidaps.plugins.pump.medtronic.defs.BatteryType import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicCommandType import info.nightscout.androidaps.plugins.pump.medtronic.defs.PumpDeviceState @@ -60,6 +61,7 @@ class MedtronicFragment : DaggerFragment() { @Inject lateinit var rileyLinkUtil: RileyLinkUtil @Inject lateinit var medtronicUtil: MedtronicUtil @Inject lateinit var medtronicPumpStatus: MedtronicPumpStatus + @Inject lateinit var rileyLinkServiceData: RileyLinkServiceData private var disposable: CompositeDisposable = CompositeDisposable() @@ -174,17 +176,14 @@ class MedtronicFragment : DaggerFragment() { @Synchronized private fun setDeviceStatus() { - medtronicPumpStatus.rileyLinkServiceState = checkStatusSet(medtronicPumpStatus.rileyLinkServiceState, - rileyLinkUtil.getServiceState()) as RileyLinkServiceState? - - val resourceId = medtronicPumpStatus.rileyLinkServiceState.getResourceId(RileyLinkTargetDevice.MedtronicPump) + val resourceId = rileyLinkServiceData.rileyLinkServiceState.getResourceId(RileyLinkTargetDevice.MedtronicPump) val rileyLinkError = medtronicPumpPlugin.rileyLinkService?.error medtronic_rl_status.text = when { - medtronicPumpStatus.rileyLinkServiceState == RileyLinkServiceState.NotStarted -> resourceHelper.gs(resourceId) - medtronicPumpStatus.rileyLinkServiceState.isConnecting -> "{fa-bluetooth-b spin} " + resourceHelper.gs(resourceId) - medtronicPumpStatus.rileyLinkServiceState.isError && rileyLinkError == null -> "{fa-bluetooth-b} " + resourceHelper.gs(resourceId) - medtronicPumpStatus.rileyLinkServiceState.isError && rileyLinkError != null -> "{fa-bluetooth-b} " + resourceHelper.gs(rileyLinkError.getResourceId(RileyLinkTargetDevice.MedtronicPump)) + rileyLinkServiceData.rileyLinkServiceState == RileyLinkServiceState.NotStarted -> resourceHelper.gs(resourceId) + rileyLinkServiceData.rileyLinkServiceState.isConnecting -> "{fa-bluetooth-b spin} " + resourceHelper.gs(resourceId) + rileyLinkServiceData.rileyLinkServiceState.isError && rileyLinkError == null -> "{fa-bluetooth-b} " + resourceHelper.gs(resourceId) + rileyLinkServiceData.rileyLinkServiceState.isError && rileyLinkError != null -> "{fa-bluetooth-b} " + resourceHelper.gs(rileyLinkError.getResourceId(RileyLinkTargetDevice.MedtronicPump)) else -> "{fa-bluetooth-b} " + resourceHelper.gs(resourceId) } medtronic_rl_status.setTextColor(if (rileyLinkError != null) Color.RED else Color.WHITE) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index baa03a3ef1..56c5c272d8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -56,6 +56,7 @@ import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.ResetRileyLinkConfigurationTask; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.ServiceTaskExecutor; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.WakeAndTuneTask; @@ -99,6 +100,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter private final MedtronicUtil medtronicUtil; private final MedtronicPumpStatus medtronicPumpStatus; private final MedtronicHistoryData medtronicHistoryData; + private final RileyLinkServiceData rileyLinkServiceData; protected static MedtronicPumpPlugin plugin = null; private RileyLinkMedtronicService rileyLinkMedtronicService; @@ -129,7 +131,8 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter RileyLinkUtil rileyLinkUtil, MedtronicUtil medtronicUtil, MedtronicPumpStatus medtronicPumpStatus, - MedtronicHistoryData medtronicHistoryData + MedtronicHistoryData medtronicHistoryData, + RileyLinkServiceData rileyLinkServiceData ) { super(new PluginDescription() // @@ -149,6 +152,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter this.sp = sp; this.medtronicPumpStatus = medtronicPumpStatus; this.medtronicHistoryData = medtronicHistoryData; + this.rileyLinkServiceData = rileyLinkServiceData; displayConnectionMessages = false; @@ -408,7 +412,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter private boolean isPumpNotReachable() { - RileyLinkServiceState rileyLinkServiceState = rileyLinkUtil.getServiceState(); + RileyLinkServiceState rileyLinkServiceState = rileyLinkServiceData.rileyLinkServiceState; if (rileyLinkServiceState == null) { aapsLogger.debug(LTag.PUMP, "RileyLink unreachable. RileyLinkServiceState is null."); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java index c2fb02dee0..f0888b84a0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java @@ -24,6 +24,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RLMe import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RadioPacket; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RadioResponse; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RLMessageType; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.ServiceTaskExecutor; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.WakeAndTuneTask; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; @@ -66,6 +67,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager @Inject MedtronicConverter medtronicConverter; @Inject MedtronicUtil medtronicUtil; @Inject MedtronicPumpHistoryDecoder medtronicPumpHistoryDecoder; + @Inject RileyLinkServiceData rileyLinkServiceData; private final int MAX_COMMAND_TRIES = 3; private final int DEFAULT_TIMEOUT = 2000; @@ -154,7 +156,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager if (rfSpyResponse.wasTimeout()) { aapsLogger.error(LTag.PUMPBTCOMM, "isDeviceReachable. Failed to find pump (timeout)."); } else if (rfSpyResponse.looksLikeRadioPacket()) { - RadioResponse radioResponse = new RadioResponse(); + RadioResponse radioResponse = new RadioResponse(injector); try { @@ -470,11 +472,11 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager public byte[] createPumpMessageContent(RLMessageType type) { switch (type) { case PowerOn: - return medtronicUtil.buildCommandPayload(MedtronicCommandType.RFPowerOn, // + return medtronicUtil.buildCommandPayload(rileyLinkServiceData, MedtronicCommandType.RFPowerOn, // new byte[]{2, 1, (byte) receiverDeviceAwakeForMinutes}); // maybe this is better FIXME case ReadSimpleData: - return medtronicUtil.buildCommandPayload(MedtronicCommandType.PumpModel, null); + return medtronicUtil.buildCommandPayload(rileyLinkServiceData, MedtronicCommandType.PumpModel, null); } return new byte[0]; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java index e722e44818..a6e154cf1f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java @@ -14,7 +14,6 @@ import javax.inject.Singleton; import info.nightscout.androidaps.plugins.pump.common.data.PumpStatus; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError; -import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; import info.nightscout.androidaps.plugins.pump.medtronic.defs.BasalProfileStatus; import info.nightscout.androidaps.plugins.pump.medtronic.defs.BatteryType; import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicDeviceType; @@ -40,7 +39,6 @@ public class MedtronicPumpStatus extends PumpStatus { public Double maxBasal; // statuses - public RileyLinkServiceState rileyLinkServiceState = RileyLinkServiceState.NotStarted; public RileyLinkError rileyLinkError; public PumpDeviceState pumpDeviceState = PumpDeviceState.NeverContacted; public MedtronicDeviceType medtronicDeviceType = null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java index de6395d908..d29bebb7b1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java @@ -20,7 +20,6 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.Rile import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkService; -import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; import info.nightscout.androidaps.plugins.pump.medtronic.comm.MedtronicCommunicationManager; @@ -46,7 +45,6 @@ public class RileyLinkMedtronicService extends RileyLinkService { @Inject MedtronicUIPostprocessor medtronicUIPostprocessor; @Inject MedtronicPumpStatus medtronicPumpStatus; - private MedtronicUIComm medtronicUIComm; private MedtronicCommunicationManager medtronicCommunicationManager; private IBinder mBinder = new LocalBinder(); @@ -94,17 +92,14 @@ public class RileyLinkMedtronicService extends RileyLinkService { frequencies[0] = resourceHelper.gs(R.string.key_medtronic_pump_frequency_us_ca); frequencies[1] = resourceHelper.gs(R.string.key_medtronic_pump_frequency_worldwide); - rileyLinkServiceData = new RileyLinkServiceData(RileyLinkTargetDevice.MedtronicPump); - - rileyLinkUtil.setRileyLinkServiceData(rileyLinkServiceData); - rileyLinkUtil.setTargetDevice(RileyLinkTargetDevice.MedtronicPump); + rileyLinkServiceData.targetDevice = RileyLinkTargetDevice.MedtronicPump; setPumpIDString(sp.getString(MedtronicConst.Prefs.PumpSerial, "000000")); // get most recently used RileyLink address rileyLinkServiceData.rileylinkAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, ""); - rileyLinkBLE = new RileyLinkBLE(this.context); // or this + rileyLinkBLE = new RileyLinkBLE(injector, this); // or this rfspy = new RFSpy(rileyLinkBLE); rfspy.startReader(); @@ -185,7 +180,7 @@ public class RileyLinkMedtronicService extends RileyLinkService { // PumpInterface - REMOVE public boolean isInitialized() { - return RileyLinkServiceState.isReady(rileyLinkUtil.getRileyLinkServiceData().serviceState); + return RileyLinkServiceState.isReady(rileyLinkServiceData.rileyLinkServiceState); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java index 5ea62acdb2..dabf8ff0a8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java @@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.general.overview.notifications.Notific import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.RLHistoryItem; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.ClockDTO; import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.PumpSettingDTO; @@ -55,17 +56,19 @@ public class MedtronicUtil { private final AAPSLogger aapsLogger; private final RxBusWrapper rxBus; private final RileyLinkUtil rileyLinkUtil; + private final RileyLinkServiceData rileyLinkServiceData; @Inject public MedtronicUtil( AAPSLogger aapsLogger, RxBusWrapper rxBus, - RileyLinkUtil rileyLinkUtil - + RileyLinkUtil rileyLinkUtil, + RileyLinkServiceData rileyLinkServiceData ) { this.aapsLogger = aapsLogger; this.rxBus = rxBus; this.rileyLinkUtil = rileyLinkUtil; + this.rileyLinkServiceData = rileyLinkServiceData; } public LocalTime getTimeFrom30MinInterval(int interval) { @@ -241,12 +244,12 @@ public class MedtronicUtil { // } - public byte[] buildCommandPayload(MedtronicCommandType commandType, byte[] parameters) { - return buildCommandPayload((byte) commandType.commandCode, parameters); + public byte[] buildCommandPayload(RileyLinkServiceData rileyLinkServiceData, MedtronicCommandType commandType, byte[] parameters) { + return buildCommandPayload(rileyLinkServiceData, (byte) commandType.commandCode, parameters); } - public byte[] buildCommandPayload(byte commandType, byte[] parameters) { + public byte[] buildCommandPayload(RileyLinkServiceData rileyLinkServiceData, byte commandType, byte[] parameters) { // A7 31 65 51 C0 00 52 byte commandLength = (byte) (parameters == null ? 2 : 2 + parameters.length); @@ -254,7 +257,7 @@ public class MedtronicUtil { ByteBuffer sendPayloadBuffer = ByteBuffer.allocate(ENVELOPE_SIZE + commandLength); // + CRC_SIZE sendPayloadBuffer.order(ByteOrder.BIG_ENDIAN); - byte[] serialNumberBCD = rileyLinkUtil.getRileyLinkServiceData().pumpIDBytes; + byte[] serialNumberBCD = rileyLinkServiceData.pumpIDBytes; sendPayloadBuffer.put((byte) 0xA7); sendPayloadBuffer.put(serialNumberBCD[0]); @@ -398,7 +401,7 @@ public class MedtronicUtil { public void setPumpDeviceState(PumpDeviceState pumpDeviceState) { this.pumpDeviceState = pumpDeviceState; - rileyLinkUtil.historyRileyLink.add(new RLHistoryItem(pumpDeviceState, RileyLinkTargetDevice.MedtronicPump)); + rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItem(pumpDeviceState, RileyLinkTargetDevice.MedtronicPump)); rxBus.send(new EventMedtronicDeviceStatusChange(pumpDeviceState)); } @@ -426,7 +429,7 @@ public class MedtronicUtil { this.currentCommand = currentCommand; if (currentCommand != null) - rileyLinkUtil.historyRileyLink.add(new RLHistoryItem(currentCommand)); + rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItem(currentCommand)); }