MDT refactor pass 16

This commit is contained in:
Milos Kozak 2020-04-27 22:19:50 +02:00
parent a3278d53c3
commit 52e0e3ec51
8 changed files with 34 additions and 34 deletions

View file

@ -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<MainApp> {
fun injectRFSpy(rfSpy: RFSpy)
fun injectSendAndListen(sendAndListen: SendAndListen)
fun injectSetPreamble(setPreamble: SetPreamble)
fun injectRadioPacket(radioPacket: RadioPacket)
@Component.Builder
interface Builder {

View file

@ -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

View file

@ -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);

View file

@ -32,7 +32,6 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
public class RileyLinkUtil {
private List<RLHistoryItem> 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<UUID> uuids = new ArrayList<UUID>();
String name = null;

View file

@ -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;

View file

@ -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()));
}
}
}

View file

@ -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) {

View file

@ -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()));