From 52e0e3ec5141219b873477c306051cbd580b1a5d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 27 Apr 2020 22:19:50 +0200 Subject: [PATCH] MDT refactor pass 16 --- .../dependencyInjection/AppComponent.kt | 2 ++ .../dependencyInjection/AppModule.kt | 2 ++ .../RileyLinkCommunicationManager.java | 8 +++---- .../common/hw/rileylink/RileyLinkUtil.java | 12 ----------- .../pump/common/hw/rileylink/ble/RFSpy.java | 14 +++++++------ .../hw/rileylink/ble/data/RadioPacket.java | 21 ++++++++++++------- .../hw/rileylink/ble/data/RadioResponse.java | 7 ++++--- .../comm/MedtronicCommunicationManager.java | 2 +- 8 files changed, 34 insertions(+), 34 deletions(-) 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 0cf7856ec0..7d9dcac987 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt @@ -34,6 +34,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RFSpy import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLinkBLE import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.SendAndListen import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.SetPreamble +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.service.tasks.* import info.nightscout.androidaps.plugins.pump.medtronic.comm.MedtronicCommunicationManager @@ -209,6 +210,7 @@ interface AppComponent : AndroidInjector { fun injectRFSpy(rfSpy: RFSpy) fun injectSendAndListen(sendAndListen: SendAndListen) fun injectSetPreamble(setPreamble: SetPreamble) + fun injectRadioPacket(radioPacket: RadioPacket) @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 ee351fd9ef..6d334aa3d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -51,6 +51,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RFSpy import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLinkBLE import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.SendAndListen import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.SetPreamble +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.service.tasks.* import info.nightscout.androidaps.plugins.pump.medtronic.comm.MedtronicCommunicationManager @@ -302,6 +303,7 @@ open class AppModule { @ContributesAndroidInjector fun rfSpyProvider(): RFSpy @ContributesAndroidInjector fun sendAndListenProvider(): SendAndListen @ContributesAndroidInjector fun setPreambleProvider(): SetPreamble + @ContributesAndroidInjector fun radioPacketProvider(): RadioPacket @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 3963316f37..f89e820671 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 @@ -86,7 +86,7 @@ public abstract class RileyLinkCommunicationManager { aapsLogger.info(LTag.PUMPBTCOMM, "Sent:" + ByteUtil.shortHexString(msg.getTxData())); } - RFSpyResponse rfSpyResponse = rfspy.transmitThenReceive(new RadioPacket(msg.getTxData()), + RFSpyResponse rfSpyResponse = rfspy.transmitThenReceive(new RadioPacket(injector, msg.getTxData()), (byte) 0, (byte) repeatCount, (byte) 0, (byte) 0, timeout_ms, (byte) retryCount, extendPreamble_ms); RadioResponse radioResponse = rfSpyResponse.getRadioResponse(injector); @@ -159,7 +159,7 @@ public abstract class RileyLinkCommunicationManager { aapsLogger.info(LTag.PUMPBTCOMM, "Waking pump..."); byte[] pumpMsgContent = createPumpMessageContent(RLMessageType.ReadSimpleData); // simple - RFSpyResponse resp = rfspy.transmitThenReceive(new RadioPacket(pumpMsgContent), (byte) 0, (byte) 200, + RFSpyResponse resp = rfspy.transmitThenReceive(new RadioPacket(injector, pumpMsgContent), (byte) 0, (byte) 200, (byte) 0, (byte) 0, 25000, (byte) 0); aapsLogger.info(LTag.PUMPBTCOMM, "wakeup: raw response is " + ByteUtil.shortHexString(resp.getRaw())); @@ -238,7 +238,7 @@ public abstract class RileyLinkCommunicationManager { for (int j = 0; j < tries; j++) { byte[] pumpMsgContent = createPumpMessageContent(RLMessageType.ReadSimpleData); - RFSpyResponse resp = rfspy.transmitThenReceive(new RadioPacket(pumpMsgContent), (byte) 0, (byte) 0, + RFSpyResponse resp = rfspy.transmitThenReceive(new RadioPacket(injector, pumpMsgContent), (byte) 0, (byte) 0, (byte) 0, (byte) 0, 1250, (byte) 0); if (resp.wasTimeout()) { aapsLogger.error(LTag.PUMPBTCOMM, "scanForPump: Failed to find pump at frequency {}", frequencies[i]); @@ -326,7 +326,7 @@ public abstract class RileyLinkCommunicationManager { rfspy.setBaseFrequency(freqMHz); // RLMessage msg = makeRLMessage(RLMessageType.ReadSimpleData); byte[] pumpMsgContent = createPumpMessageContent(RLMessageType.ReadSimpleData); - RadioPacket pkt = new RadioPacket(pumpMsgContent); + RadioPacket pkt = new RadioPacket(injector, pumpMsgContent); RFSpyResponse resp = rfspy.transmitThenReceive(pkt, (byte) 0, (byte) 0, (byte) 0, (byte) 0, SCAN_TIMEOUT, (byte) 0); if (resp.wasTimeout()) { aapsLogger.warn(LTag.PUMPBTCOMM, "tune_tryFrequency: no pump response at frequency {}", freqMHz); 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 184094eb92..8fc900aa90 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 @@ -32,7 +32,6 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks public class RileyLinkUtil { private List historyRileyLink = new ArrayList<>(); - public RileyLinkCommunicationManager rileyLinkCommunicationManager; private ServiceTask currentTask; private RileyLinkEncodingType encoding; @@ -40,16 +39,6 @@ public class RileyLinkUtil { @Inject public RileyLinkUtil() { - instance = this; - } - - private static RileyLinkUtil instance; - - // TODO: replace by injection - @Deprecated - public static RileyLinkUtil getInstance() { - if (instance == null) throw new IllegalStateException("RileyLinkUtil not initialized"); - return instance; } public RileyLinkEncodingType getEncoding() { @@ -113,7 +102,6 @@ public class RileyLinkUtil { } - @Deprecated public static BleAdvertisedData parseAdertisedData(byte[] advertisedData) { List uuids = new ArrayList(); String name = null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java index ace5db3987..7f933c1082 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java @@ -32,7 +32,7 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil; import info.nightscout.androidaps.plugins.pump.common.utils.ThreadUtil; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicConst; -import info.nightscout.androidaps.utils.SP; +import info.nightscout.androidaps.utils.sharedPreferences.SP; import info.nightscout.androidaps.utils.resources.ResourceHelper; /** @@ -42,12 +42,14 @@ public class RFSpy { @Inject AAPSLogger aapsLogger; @Inject ResourceHelper resourceHelper; + @Inject SP sp; @Inject RileyLinkServiceData rileyLinkServiceData; + @Inject RileyLinkUtil rileyLinkUtil; private final HasAndroidInjector injector; - public static final long RILEYLINK_FREQ_XTAL = 24000000; - public static final int EXPECTED_MAX_BLUETOOTH_LATENCY_MS = 7500; // 1500 + private static final long RILEYLINK_FREQ_XTAL = 24000000; + private static final int EXPECTED_MAX_BLUETOOTH_LATENCY_MS = 7500; // 1500 public int notConnectedCount = 0; private RileyLinkBLE rileyLinkBle; private RFSpyReader reader; @@ -56,7 +58,7 @@ public class RFSpy { private UUID radioVersionUUID = UUID.fromString(GattAttributes.CHARA_RADIO_VERSION); private UUID responseCountUUID = UUID.fromString(GattAttributes.CHARA_RADIO_RESPONSE_COUNT); private String bleVersion; // We don't use it so no need of sofisticated logic - Double currentFrequencyMHz; + private Double currentFrequencyMHz; public RFSpy(HasAndroidInjector injector, RileyLinkBLE rileyLinkBle) { @@ -360,7 +362,7 @@ public class RFSpy { RileyLinkEncodingType encoding = RileyLinkEncodingType.FourByteSixByteLocal; if (RileyLinkFirmwareVersion.isSameVersion(rileyLinkServiceData.firmwareVersion, RileyLinkFirmwareVersion.Version2AndHigher)) { - if (SP.getString(MedtronicConst.Prefs.Encoding, "None").equals(resourceHelper.gs(R.string.key_medtronic_pump_encoding_4b6b_rileylink))) { + if (sp.getString(MedtronicConst.Prefs.Encoding, "None").equals(resourceHelper.gs(R.string.key_medtronic_pump_encoding_4b6b_rileylink))) { encoding = RileyLinkEncodingType.FourByteSixByteRileyLink; } } @@ -387,7 +389,7 @@ public class RFSpy { if (resp.isOK()) { reader.setRileyLinkEncodingType(encoding); - RileyLinkUtil.getInstance().setEncoding(encoding); + rileyLinkUtil.setEncoding(encoding); } return resp; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioPacket.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioPacket.java index 35fc0d54ff..cff7c1b9d1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioPacket.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/RadioPacket.java @@ -2,6 +2,9 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data; import org.apache.commons.lang3.NotImplementedException; +import javax.inject.Inject; + +import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.common.utils.CRC; @@ -12,10 +15,13 @@ import info.nightscout.androidaps.plugins.pump.common.utils.CRC; public class RadioPacket { - protected byte[] pkt; + @Inject RileyLinkUtil rileyLinkUtil; + + private byte[] pkt; - public RadioPacket(byte[] pkt) { + public RadioPacket(HasAndroidInjector injector, byte[] pkt) { + injector.androidInjector().inject(this); this.pkt = pkt; } @@ -25,7 +31,7 @@ public class RadioPacket { } - public byte[] getWithCRC() { + private byte[] getWithCRC() { byte[] withCRC = ByteUtil.concat(pkt, CRC.crc8(pkt)); return withCRC; } @@ -33,7 +39,7 @@ public class RadioPacket { public byte[] getEncoded() { - switch (RileyLinkUtil.getInstance().getEncoding()) { + switch (rileyLinkUtil.getEncoding()) { case Manchester: { // We have this encoding in RL firmware return pkt; } @@ -41,8 +47,8 @@ public class RadioPacket { case FourByteSixByteLocal: { byte[] withCRC = getWithCRC(); - byte[] encoded = RileyLinkUtil.getInstance().getEncoding4b6b().encode4b6b(withCRC); - return ByteUtil.concat(encoded, (byte)0); + byte[] encoded = rileyLinkUtil.getEncoding4b6b().encode4b6b(withCRC); + return ByteUtil.concat(encoded, (byte) 0); } case FourByteSixByteRileyLink: { @@ -50,8 +56,7 @@ public class RadioPacket { } default: - throw new NotImplementedException(("Encoding not supported: " + RileyLinkUtil.getInstance().getEncoding().toString())); + throw new NotImplementedException(("Encoding not supported: " + rileyLinkUtil.getEncoding().toString())); } } - } 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 343e32da67..785691e4c2 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 @@ -25,6 +25,7 @@ public class RadioResponse { @Inject AAPSLogger aapsLogger; @Inject RileyLinkServiceData rileyLinkServiceData; + @Inject RileyLinkUtil rileyLinkUtil; private boolean decodedOK = false; public int rssi; @@ -98,7 +99,7 @@ public class RadioResponse { return; } - switch (RileyLinkUtil.getInstance().getEncoding()) { + switch (rileyLinkUtil.getEncoding()) { case Manchester: case FourByteSixByteRileyLink: { @@ -108,7 +109,7 @@ public class RadioResponse { break; case FourByteSixByteLocal: { - byte[] decodeThis = RileyLinkUtil.getInstance().getEncoding4b6b().decode4b6b(encodedPayload); + byte[] decodeThis = rileyLinkUtil.getEncoding4b6b().decode4b6b(encodedPayload); if (decodeThis != null && decodeThis.length > 2) { decodedOK = true; @@ -127,7 +128,7 @@ public class RadioResponse { break; default: - throw new NotImplementedException("this {" + RileyLinkUtil.getInstance().getEncoding().toString() + throw new NotImplementedException("this {" + rileyLinkUtil.getEncoding().toString() + "} encoding is not supported"); } } catch (NumberFormatException e) { 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 0fb88af515..37de1fa7f6 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 @@ -150,7 +150,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager PumpDeviceState state = medtronicPumpStatus.getPumpDeviceState(); byte[] pumpMsgContent = createPumpMessageContent(RLMessageType.ReadSimpleData); // simple - RFSpyResponse rfSpyResponse = rfspy.transmitThenReceive(new RadioPacket(pumpMsgContent), (byte) 0, (byte) 200, + RFSpyResponse rfSpyResponse = rfspy.transmitThenReceive(new RadioPacket(injector, pumpMsgContent), (byte) 0, (byte) 200, (byte) 0, (byte) 0, 25000, (byte) 0); aapsLogger.info(LTag.PUMPBTCOMM, "wakeup: raw response is " + ByteUtil.shortHexString(rfSpyResponse.getRaw()));