MDT refactor pass 13

This commit is contained in:
Milos Kozak 2020-04-27 20:22:00 +02:00
parent e21b1e4d3d
commit dbb7570de7
14 changed files with 118 additions and 156 deletions

View file

@ -30,7 +30,10 @@ 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.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.RadioResponse
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.*
import info.nightscout.androidaps.plugins.pump.medtronic.comm.MedtronicCommunicationManager
@ -203,6 +206,9 @@ interface AppComponent : AndroidInjector<MainApp> {
fun injectWakeAndTuneTask(wakeAndTuneTask: WakeAndTuneTask)
fun injectRadioResponse(radioResponse: RadioResponse)
fun injectRileyLinkBLE(rileyLinkBLE: RileyLinkBLE)
fun injectRFSpy(rfSpy: RFSpy)
fun injectSendAndListen(sendAndListen: SendAndListen)
fun injectSetPreamble(setPreamble: SetPreamble)
@Component.Builder
interface Builder {

View file

@ -47,7 +47,10 @@ 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.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.RadioResponse
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.*
import info.nightscout.androidaps.plugins.pump.medtronic.comm.MedtronicCommunicationManager
@ -296,6 +299,9 @@ open class AppModule {
@ContributesAndroidInjector fun wakeAndTuneTaskProvider(): WakeAndTuneTask
@ContributesAndroidInjector fun radioResponseProvider(): RadioResponse
@ContributesAndroidInjector fun rileyLinkBLEProvider(): RileyLinkBLE
@ContributesAndroidInjector fun rfSpyProvider(): RFSpy
@ContributesAndroidInjector fun sendAndListenProvider(): SendAndListen
@ContributesAndroidInjector fun setPreambleProvider(): SetPreamble
@Binds
fun bindActivePluginProvider(pluginStore: PluginStore): ActivePluginProvider

View file

@ -48,9 +48,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.GattAttributes;
import info.nightscout.androidaps.plugins.pump.common.utils.LocationHelper;
import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin;
import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpStatus;
import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicPumpConfigurationChanged;
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import info.nightscout.androidaps.utils.sharedPreferences.SP;
@ -62,7 +60,7 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity {
@Inject RxBusWrapper rxBus;
@Inject ResourceHelper resourceHelper;
@Inject RileyLinkUtil rileyLinkUtil;
@Inject MedtronicUtil medtronicUtil;
// TODO change this. Currently verifyConfiguration uses MDT data not only RL
@Inject MedtronicPumpPlugin medtronicPumpPlugin;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 30241; // arbitrary.
@ -192,7 +190,7 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity {
}
// disable currently selected RL, so that we can discover it
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, this);
}

View file

@ -16,6 +16,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.Radi
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.ble.defs.RileyLinkBLEError;
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;
@ -34,7 +35,7 @@ public abstract class RileyLinkCommunicationManager {
@Inject protected SP sp;
@Inject MedtronicPumpStatus medtronicPumpStatus;
@Inject RileyLinkUtil rileyLinkUtil;
@Inject RileyLinkServiceData rileyLinkServiceData;
private final int SCAN_TIMEOUT = 1500;
@ -59,23 +60,23 @@ public abstract class RileyLinkCommunicationManager {
// All pump communications go through this function.
public <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, Class<E> clazz)
protected <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, Class<E> clazz)
throws RileyLinkCommunicationException {
return sendAndListen(msg, timeout_ms, null, clazz);
}
public <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, Integer extendPreamble_ms, Class<E> clazz)
private <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, Integer extendPreamble_ms, Class<E> clazz)
throws RileyLinkCommunicationException {
return sendAndListen(msg, timeout_ms, 0, extendPreamble_ms, clazz);
}
// For backward compatibility
public <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, Integer extendPreamble_ms, Class<E> clazz)
private <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, Integer extendPreamble_ms, Class<E> clazz)
throws RileyLinkCommunicationException {
return sendAndListen(msg, timeout_ms, repeatCount, 0, extendPreamble_ms, clazz);
}
public <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, int retryCount, Integer extendPreamble_ms, Class<E> clazz)
private <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, int retryCount, Integer extendPreamble_ms, Class<E> clazz)
throws RileyLinkCommunicationException {
// internal flag
@ -189,7 +190,7 @@ public abstract class RileyLinkCommunicationManager {
public double tuneForDevice() {
return scanForDevice(rileyLinkUtil.getRileyLinkTargetFrequency().getScanFrequencies());
return scanForDevice(rileyLinkServiceData.rileyLinkTargetFrequency.getScanFrequencies());
}
@ -203,7 +204,7 @@ public abstract class RileyLinkCommunicationManager {
*/
public boolean isValidFrequency(double frequency) {
double[] scanFrequencies = rileyLinkUtil.getRileyLinkTargetFrequency().getScanFrequencies();
double[] scanFrequencies = rileyLinkServiceData.rileyLinkTargetFrequency.getScanFrequencies();
if (scanFrequencies.length == 1) {
return RileyLinkUtil.isSame(scanFrequencies[0], frequency);

View file

@ -5,8 +5,6 @@ import android.content.Intent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
@ -17,27 +15,15 @@ import java.util.UUID;
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.ble.RileyLinkBLE;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.encoding.Encoding4b6b;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.encoding.Encoding4b6bGeoff;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkEncodingType;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkTargetFrequency;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.BleAdvertisedData;
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.common.hw.rileylink.service.RileyLinkServiceData;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceNotification;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceResult;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.ServiceTask;
import info.nightscout.androidaps.plugins.pump.common.ui.RileyLinkSelectPreference;
import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicDeviceStatusChange;
/**
* Created by andy on 17/05/2018.
@ -50,28 +36,13 @@ public class RileyLinkUtil {
public RileyLinkCommunicationManager rileyLinkCommunicationManager;
@Deprecated
static ServiceTask currentTask;
private RileyLinkTargetFrequency rileyLinkTargetFrequency;
private RileyLinkEncodingType encoding;
private RileyLinkSelectPreference rileyLinkSelectPreference;
private Encoding4b6b encoding4b6b;
private RileyLinkFirmwareVersion firmwareVersion;
@NotNull private final Context context;
@NotNull private final AAPSLogger aapsLogger;
@NotNull private final RxBusWrapper rxBus;
@Inject
public RileyLinkUtil(
Context context,
AAPSLogger aapsLogger,
RxBusWrapper rxBus
) {
this.context = context;
this.aapsLogger = aapsLogger;
this.rxBus = rxBus;
public RileyLinkUtil() {
instance = this;
}
@ -98,7 +69,7 @@ public class RileyLinkUtil {
}
public void sendBroadcastMessage(String message) {
public void sendBroadcastMessage(String message, Context context) {
Intent intent = new Intent(message);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
@ -129,7 +100,7 @@ public class RileyLinkUtil {
}
public static void sendServiceTransportResponse(ServiceTransport transport, ServiceResult serviceResult) {
private static void sendServiceTransportResponse(ServiceTransport transport, ServiceResult serviceResult) {
// get the key (hashcode) of the client who requested this
Integer clientHashcode = transport.getSenderHashcode();
// make a new bundle to send as the message data
@ -140,16 +111,6 @@ public class RileyLinkUtil {
}
public RileyLinkTargetFrequency getRileyLinkTargetFrequency() {
return rileyLinkTargetFrequency;
}
public void setRileyLinkTargetFrequency(RileyLinkTargetFrequency rileyLinkTargetFrequency) {
this.rileyLinkTargetFrequency = rileyLinkTargetFrequency;
}
public static boolean isSame(Double d1, Double d2) {
double diff = d1 - d2;
@ -218,12 +179,4 @@ public class RileyLinkUtil {
public Encoding4b6b getEncoding4b6b() {
return encoding4b6b;
}
public void setFirmwareVersion(RileyLinkFirmwareVersion firmwareVersion) {
this.firmwareVersion = firmwareVersion;
}
public RileyLinkFirmwareVersion getFirmwareVersion() {
return firmwareVersion;
}
}

View file

@ -2,15 +2,14 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble;
import android.os.SystemClock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import info.nightscout.androidaps.MainApp;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.R;
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.hw.rileylink.RileyLinkUtil;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.Reset;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.RileyLinkCommand;
@ -28,45 +27,45 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.Rile
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkTargetFrequency;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.operations.BLECommOperationResult;
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.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.resources.ResourceHelper;
/**
* Created by geoff on 5/26/16.
*/
public class RFSpy {
@Inject AAPSLogger aapsLogger;
@Inject ResourceHelper resourceHelper;
@Inject RileyLinkServiceData rileyLinkServiceData;
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 Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPBTCOMM);
public int notConnectedCount = 0;
private RileyLinkBLE rileyLinkBle;
private RFSpyReader reader;
private RileyLinkTargetFrequency selectedTargetFrequency;
private UUID radioServiceUUID = UUID.fromString(GattAttributes.SERVICE_RADIO);
private UUID radioDataUUID = UUID.fromString(GattAttributes.CHARA_RADIO_DATA);
private UUID radioVersionUUID = UUID.fromString(GattAttributes.CHARA_RADIO_VERSION);
private UUID responseCountUUID = UUID.fromString(GattAttributes.CHARA_RADIO_RESPONSE_COUNT);
private RileyLinkFirmwareVersion firmwareVersion;
private String bleVersion; // We don't use it so no need of sofisticated logic
Double currentFrequencyMHz;
public RFSpy(RileyLinkBLE rileyLinkBle) {
public RFSpy(HasAndroidInjector injector, RileyLinkBLE rileyLinkBle) {
injector.androidInjector().inject(this);
this.injector = injector;
this.rileyLinkBle = rileyLinkBle;
reader = new RFSpyReader(rileyLinkBle);
}
public RileyLinkFirmwareVersion getRLVersionCached() {
return firmwareVersion;
}
public String getBLEVersionCached() {
return bleVersion;
}
@ -75,13 +74,7 @@ public class RFSpy {
// Call this after the RL services are discovered.
// Starts an async task to read when data is available
public void startReader() {
rileyLinkBle.registerRadioResponseCountNotification(new Runnable() {
@Override
public void run() {
newDataIsAvailable();
}
});
rileyLinkBle.registerRadioResponseCountNotification(this::newDataIsAvailable);
reader.start();
}
@ -90,13 +83,12 @@ public class RFSpy {
// firmware version
public void initializeRileyLink() {
bleVersion = getVersion();
firmwareVersion = getFirmwareVersion();
RileyLinkUtil.getInstance().setFirmwareVersion(firmwareVersion);
rileyLinkServiceData.firmwareVersion = getFirmwareVersion();
}
// Call this from the "response count" notification handler.
public void newDataIsAvailable() {
private void newDataIsAvailable() {
// pass the message to the reader (which should be internal to RFSpy)
reader.newDataIsAvailable();
}
@ -108,11 +100,10 @@ public class RFSpy {
BLECommOperationResult result = rileyLinkBle.readCharacteristic_blocking(radioServiceUUID, radioVersionUUID);
if (result.resultCode == BLECommOperationResult.RESULT_SUCCESS) {
String version = StringUtil.fromBytes(result.value);
if (isLogEnabled())
LOG.debug("BLE Version: " + version);
aapsLogger.debug(LTag.PUMPBTCOMM, "BLE Version: " + version);
return version;
} else {
LOG.error("getVersion failed with code: " + result.resultCode);
aapsLogger.error(LTag.PUMPBTCOMM, "getVersion failed with code: " + result.resultCode);
return "(null)";
}
}
@ -120,14 +111,13 @@ public class RFSpy {
public boolean isRileyLinkStillAvailable() {
RileyLinkFirmwareVersion firmwareVersion = getFirmwareVersion();
return (firmwareVersion!= RileyLinkFirmwareVersion.UnknownVersion);
return (firmwareVersion != RileyLinkFirmwareVersion.UnknownVersion);
}
public RileyLinkFirmwareVersion getFirmwareVersion() {
private RileyLinkFirmwareVersion getFirmwareVersion() {
if (isLogEnabled())
LOG.debug("Firmware Version. Get Version - Start");
aapsLogger.debug(LTag.PUMPBTCOMM, "Firmware Version. Get Version - Start");
for (int i = 0; i < 5; i++) {
// We have to call raw version of communication to get firmware version
@ -136,8 +126,7 @@ public class RFSpy {
byte[] getVersionRaw = getByteArray(RileyLinkCommandType.GetVersion.code);
byte[] response = writeToDataRaw(getVersionRaw, 5000);
if (isLogEnabled())
LOG.debug("Firmware Version. GetVersion [response={}]", ByteUtil.shortHexString(response));
aapsLogger.debug(LTag.PUMPBTCOMM, "Firmware Version. GetVersion [response={}]", ByteUtil.shortHexString(response));
if (response != null) { // && response[0] == (byte) 0xDD) {
@ -146,8 +135,7 @@ public class RFSpy {
RileyLinkFirmwareVersion version = RileyLinkFirmwareVersion.getByVersionString(StringUtil
.fromBytes(response));
if (isLogEnabled())
LOG.trace("Firmware Version string: {}, resolved to {}.", versionString, version);
aapsLogger.debug(LTag.PUMPBTCOMM, "Firmware Version string: {}, resolved to {}.", versionString, version);
if (version != RileyLinkFirmwareVersion.UnknownVersion)
return version;
@ -156,7 +144,7 @@ public class RFSpy {
}
}
LOG.error("Firmware Version can't be determined. Checking with BLE Version [{}].", bleVersion);
aapsLogger.error(LTag.PUMPBTCOMM, "Firmware Version can't be determined. Checking with BLE Version [{}].", bleVersion);
if (bleVersion.contains(" 2.")) {
return RileyLinkFirmwareVersion.Version_2_0;
@ -172,7 +160,7 @@ public class RFSpy {
byte[] junkInBuffer = reader.poll(0);
while (junkInBuffer != null) {
LOG.warn(ThreadUtil.sig() + "writeToData: draining read queue, found this: "
aapsLogger.warn(LTag.PUMPBTCOMM, ThreadUtil.sig() + "writeToData: draining read queue, found this: "
+ ByteUtil.shortHexString(junkInBuffer));
junkInBuffer = reader.poll(0);
}
@ -180,12 +168,12 @@ public class RFSpy {
// prepend length, and send it.
byte[] prepended = ByteUtil.concat(new byte[]{(byte) (bytes.length)}, bytes);
LOG.debug("writeToData (raw={})", ByteUtil.shortHexString(prepended));
aapsLogger.debug(LTag.PUMPBTCOMM, "writeToData (raw={})", ByteUtil.shortHexString(prepended));
BLECommOperationResult writeCheck = rileyLinkBle.writeCharacteristic_blocking(radioServiceUUID, radioDataUUID,
prepended);
if (writeCheck.resultCode != BLECommOperationResult.RESULT_SUCCESS) {
LOG.error("BLE Write operation failed, code=" + writeCheck.resultCode);
aapsLogger.error(LTag.PUMPBTCOMM, "BLE Write operation failed, code=" + writeCheck.resultCode);
return null; // will be a null (invalid) response
}
SystemClock.sleep(100);
@ -204,23 +192,22 @@ public class RFSpy {
RFSpyResponse resp = new RFSpyResponse(command, rawResponse);
if (rawResponse == null) {
LOG.error("writeToData: No response from RileyLink");
aapsLogger.error(LTag.PUMPBTCOMM, "writeToData: No response from RileyLink");
notConnectedCount++;
} else {
if (resp.wasInterrupted()) {
LOG.error("writeToData: RileyLink was interrupted");
aapsLogger.error(LTag.PUMPBTCOMM, "writeToData: RileyLink was interrupted");
} else if (resp.wasTimeout()) {
LOG.error("writeToData: RileyLink reports timeout");
aapsLogger.error(LTag.PUMPBTCOMM, "writeToData: RileyLink reports timeout");
notConnectedCount++;
} else if (resp.isOK()) {
LOG.warn("writeToData: RileyLink reports OK");
aapsLogger.warn(LTag.PUMPBTCOMM, "writeToData: RileyLink reports OK");
resetNotConnectedCount();
} else {
if (resp.looksLikeRadioPacket()) {
// RadioResponse radioResp = resp.getRadioResponse();
// byte[] responsePayload = radioResp.getPayload();
if (isLogEnabled())
LOG.trace("writeToData: received radio response. Will decode at upper level");
aapsLogger.debug(LTag.PUMPBTCOMM, "writeToData: received radio response. Will decode at upper level");
resetNotConnectedCount();
}
// Log.i(TAG, "writeToData: raw response is " + ByteUtil.shortHexString(rawResponse));
@ -274,14 +261,14 @@ public class RFSpy {
int sendDelay = repeatCount * delay_ms;
int receiveDelay = timeout_ms * (retryCount + 1);
SendAndListen command = new SendAndListen(sendChannel, repeatCount, delay_ms, listenChannel, timeout_ms,
SendAndListen command = new SendAndListen(injector, sendChannel, repeatCount, delay_ms, listenChannel, timeout_ms,
retryCount, extendPreamble_ms, pkt);
return writeToData(command, sendDelay + receiveDelay + EXPECTED_MAX_BLUETOOTH_LATENCY_MS);
}
public RFSpyResponse updateRegister(CC111XRegister reg, int val) {
private RFSpyResponse updateRegister(CC111XRegister reg, int val) {
RFSpyResponse resp = writeToData(new UpdateRegister(reg, (byte) val), EXPECTED_MAX_BLUETOOTH_LATENCY_MS);
return resp;
}
@ -292,11 +279,11 @@ public class RFSpy {
updateRegister(CC111XRegister.freq0, (byte) (value & 0xff));
updateRegister(CC111XRegister.freq1, (byte) ((value >> 8) & 0xff));
updateRegister(CC111XRegister.freq2, (byte) ((value >> 16) & 0xff));
LOG.info("Set frequency to {} MHz", freqMHz);
aapsLogger.info(LTag.PUMPBTCOMM, "Set frequency to {} MHz", freqMHz);
this.currentFrequencyMHz = freqMHz;
configureRadioForRegion(RileyLinkUtil.getInstance().getRileyLinkTargetFrequency());
configureRadioForRegion(rileyLinkServiceData.rileyLinkTargetFrequency);
}
@ -362,35 +349,32 @@ public class RFSpy {
}
break;
default:
LOG.warn("No region configuration for RfSpy and {}", frequency.name());
aapsLogger.warn(LTag.PUMPBTCOMM, "No region configuration for RfSpy and {}", frequency.name());
break;
}
this.selectedTargetFrequency = frequency;
}
private void setMedtronicEncoding() {
RileyLinkEncodingType encoding = RileyLinkEncodingType.FourByteSixByteLocal;
if (RileyLinkFirmwareVersion.isSameVersion(this.firmwareVersion, RileyLinkFirmwareVersion.Version2AndHigher)) {
if (SP.getString(MedtronicConst.Prefs.Encoding, "None").equals(MainApp.gs(R.string.key_medtronic_pump_encoding_4b6b_rileylink))) {
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))) {
encoding = RileyLinkEncodingType.FourByteSixByteRileyLink;
}
}
setRileyLinkEncoding(encoding);
if (isLogEnabled())
LOG.debug("Set Encoding for Medtronic: " + encoding.name());
aapsLogger.debug(LTag.PUMPBTCOMM, "Set Encoding for Medtronic: " + encoding.name());
}
private RFSpyResponse setPreamble(int preamble) {
RFSpyResponse resp = null;
try {
resp = writeToData(new SetPreamble(preamble), EXPECTED_MAX_BLUETOOTH_LATENCY_MS);
resp = writeToData(new SetPreamble(injector, preamble), EXPECTED_MAX_BLUETOOTH_LATENCY_MS);
} catch (Exception e) {
e.toString();
}
@ -431,16 +415,10 @@ public class RFSpy {
RFSpyResponse resp = null;
try {
resp = writeToData(new Reset(), EXPECTED_MAX_BLUETOOTH_LATENCY_MS);
if (isLogEnabled())
LOG.debug("Reset command send, response: {}", resp);
aapsLogger.debug(LTag.PUMPBTCOMM, "Reset command send, response: {}", resp);
} catch (Exception e) {
e.toString();
}
return resp;
}
private boolean isLogEnabled() {
return L.isEnabled(L.PUMPBTCOMM);
}
}

View file

@ -145,7 +145,7 @@ public class RileyLinkBLE {
if (newState == BluetoothProfile.STATE_CONNECTED) {
if (status == BluetoothGatt.GATT_SUCCESS) {
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.BluetoothConnected);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.BluetoothConnected, context);
} else {
aapsLogger.debug(LTag.PUMPBTCOMM, "BT State connected, GATT status {} ({})", status, getGattStatusMessage(status));
}
@ -155,7 +155,7 @@ public class RileyLinkBLE {
// aapsLogger.debug(LTag.PUMPBTCOMM,"We are in {} state.", status == BluetoothProfile.STATE_CONNECTING ? "Connecting" :
// "Disconnecting");
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnected);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnected, context);
if (manualDisconnect)
close();
aapsLogger.warn(LTag.PUMPBTCOMM, "RileyLink Disconnected.");
@ -242,7 +242,7 @@ public class RileyLinkBLE {
if (rileyLinkFound) {
mIsConnected = true;
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkReady);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkReady, context);
// RileyLinkUtil.sendNotification(new
// ServiceNotification(RileyLinkConst.Intents.RileyLinkReady), null);
} else {
@ -253,7 +253,7 @@ public class RileyLinkBLE {
} else {
aapsLogger.debug(LTag.PUMPBTCOMM, "onServicesDiscovered " + getGattStatusMessage(status));
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkGattFailed);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkGattFailed, context);
}
}
};

View file

@ -3,14 +3,20 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command;
import java.nio.ByteBuffer;
import java.util.ArrayList;
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.hw.rileylink.ble.data.RadioPacket;
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;
public class SendAndListen extends RileyLinkCommand {
@Inject RileyLinkServiceData rileyLinkServiceData;
private byte sendChannel;
private byte repeatCount;
private int delayBetweenPackets_ms;
@ -21,20 +27,21 @@ public class SendAndListen extends RileyLinkCommand {
private RadioPacket packetToSend;
public SendAndListen(byte sendChannel, byte repeatCount, byte delayBetweenPackets_ms, byte listenChannel,
public SendAndListen(HasAndroidInjector injector, byte sendChannel, byte repeatCount, byte delayBetweenPackets_ms, byte listenChannel,
int timeout_ms, byte retryCount, RadioPacket packetToSend
) {
this(sendChannel, repeatCount, delayBetweenPackets_ms, listenChannel, timeout_ms, retryCount, null,
this(injector, sendChannel, repeatCount, delayBetweenPackets_ms, listenChannel, timeout_ms, retryCount, null,
packetToSend);
}
public SendAndListen(byte sendChannel, byte repeatCount, int delayBetweenPackets_ms, byte listenChannel,
int timeout_ms, byte retryCount, Integer preambleExtension_ms, RadioPacket packetToSend
public SendAndListen(HasAndroidInjector injector, byte sendChannel, byte repeatCount, int delayBetweenPackets_ms, byte listenChannel,
int timeout_ms, byte retryCount, Integer preambleExtension_ms, RadioPacket packetToSend
) {
super();
injector.androidInjector().inject(this);
this.sendChannel = sendChannel;
this.repeatCount = repeatCount;
this.delayBetweenPackets_ms = delayBetweenPackets_ms;
@ -57,8 +64,8 @@ public class SendAndListen extends RileyLinkCommand {
// If firmware version is not set (error reading version from device, shouldn't happen),
// we will default to version 2
boolean isPacketV2 = RileyLinkUtil.getInstance().getFirmwareVersion() != null ? RileyLinkUtil.getInstance().getFirmwareVersion()
.isSameVersion(RileyLinkFirmwareVersion.Version2AndHigher) : true;
boolean isPacketV2 = rileyLinkServiceData.firmwareVersion == null || rileyLinkServiceData.firmwareVersion
.isSameVersion(RileyLinkFirmwareVersion.Version2AndHigher);
ArrayList<Byte> bytes = new ArrayList<Byte>();
bytes.add(this.getCommandType().code);

View file

@ -4,20 +4,26 @@ import java.nio.ByteBuffer;
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.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;
public class SetPreamble extends RileyLinkCommand {
@Inject RileyLinkServiceData rileyLinkServiceData;
private int preamble;
public SetPreamble(int preamble) throws Exception {
public SetPreamble(HasAndroidInjector injector, int preamble) throws Exception {
super();
// this command was not supported before 2.0
if (!RileyLinkUtil.getInstance().getFirmwareVersion().isSameVersion(RileyLinkFirmwareVersion.Version2AndHigher)) {
if (!rileyLinkServiceData.firmwareVersion.isSameVersion(RileyLinkFirmwareVersion.Version2AndHigher)) {
throw new NotImplementedException("Old firmware does not support SetPreamble command");
}

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
@ -28,7 +27,7 @@ class RileyLinkBluetoothStateReceiver : DaggerBroadcastReceiver() {
BluetoothAdapter.STATE_ON -> {
aapsLogger.debug("RileyLinkBluetoothStateReceiver: Bluetooth back on. Sending broadcast to RileyLink Framework")
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.BluetoothReconnected)
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.BluetoothReconnected, context)
}
}
}

View file

@ -106,7 +106,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
LOG.debug("Received Broadcast: " + action);
if (!processBluetoothBroadcasts(action) && //
!processRileyLinkBroadcasts(action) && //
!processRileyLinkBroadcasts(action, context) && //
!processTuneUpBroadcasts(action) && //
!processDeviceSpecificBroadcasts(action, intent) && //
!processApplicationSpecificBroadcasts(action, intent) //
@ -137,7 +137,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
}
private boolean processRileyLinkBroadcasts(String action) {
private boolean processRileyLinkBroadcasts(String action, Context context) {
if (action.equals(RileyLinkConst.Intents.RileyLinkDisconnected)) {
if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
@ -158,7 +158,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
serviceInstance.rfspy.initializeRileyLink();
String bleVersion = serviceInstance.rfspy.getBLEVersionCached();
RileyLinkFirmwareVersion rlVersion = serviceInstance.rfspy.getRLVersionCached();
RileyLinkFirmwareVersion rlVersion = rileyLinkServiceData.firmwareVersion;
// if (isLoggingEnabled())
LOG.debug("RfSpy version (BLE113): " + bleVersion);
@ -168,7 +168,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
LOG.debug("RfSpy Radio version (CC110): " + rlVersion.name());
serviceInstance.rileyLinkServiceData.versionCC110 = rlVersion;
ServiceTask task = new InitializePumpManagerTask(injector);
ServiceTask task = new InitializePumpManagerTask(injector, context);
ServiceTaskExecutor.startTask(task);
if (isLoggingEnabled())
LOG.info("Announcing RileyLink open For business");

View file

@ -8,6 +8,7 @@ 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.ble.defs.RileyLinkTargetFrequency;
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;
@ -28,6 +29,9 @@ public class RileyLinkServiceData {
boolean tuneUpDone = false;
public RileyLinkError rileyLinkError;
public RileyLinkServiceState rileyLinkServiceState = RileyLinkServiceState.NotStarted;
public RileyLinkFirmwareVersion firmwareVersion;
public RileyLinkTargetFrequency rileyLinkTargetFrequency;
public String rileylinkAddress;
long lastTuneUpTime = 0L;
public Double lastGoodFrequency;

View file

@ -1,5 +1,7 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import android.content.Context;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
@ -29,12 +31,16 @@ public class InitializePumpManagerTask extends ServiceTask {
@Inject RileyLinkServiceData rileyLinkServiceData;
@Inject RileyLinkUtil rileyLinkUtil;
public InitializePumpManagerTask(HasAndroidInjector injector) {
private final Context context;
public InitializePumpManagerTask(HasAndroidInjector injector, Context context) {
super(injector);
this.context = context;
}
public InitializePumpManagerTask(HasAndroidInjector injector, ServiceTransport transport) {
public InitializePumpManagerTask(HasAndroidInjector injector, Context context, ServiceTransport transport) {
super(injector, transport);
this.context = context;
}
@Override
@ -74,11 +80,11 @@ public class InitializePumpManagerTask extends ServiceTask {
} else {
rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError,
RileyLinkError.NoContactWithDevice);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context);
}
} else {
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context);
}
}
}

View file

@ -51,7 +51,6 @@ public class RileyLinkMedtronicService extends RileyLinkService {
private boolean serialChanged = false;
private String[] frequencies;
private RileyLinkTargetFrequency targetFrequency;
private String rileyLinkAddress = null;
private boolean rileyLinkAddressChanged = false;
private RileyLinkEncodingType encodingType;
@ -100,7 +99,7 @@ public class RileyLinkMedtronicService extends RileyLinkService {
rileyLinkServiceData.rileylinkAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, "");
rileyLinkBLE = new RileyLinkBLE(injector, this); // or this
rfspy = new RFSpy(rileyLinkBLE);
rfspy = new RFSpy(injector, rileyLinkBLE);
rfspy.startReader();
// init rileyLinkCommunicationManager
@ -263,9 +262,8 @@ public class RileyLinkMedtronicService extends RileyLinkService {
RileyLinkTargetFrequency.Medtronic_US
: RileyLinkTargetFrequency.Medtronic_WorldWide;
if (targetFrequency != newTargetFrequency) {
rileyLinkUtil.setRileyLinkTargetFrequency(newTargetFrequency);
targetFrequency = newTargetFrequency;
if (rileyLinkServiceData.rileyLinkTargetFrequency != newTargetFrequency) {
rileyLinkServiceData.rileyLinkTargetFrequency = newTargetFrequency;
}
}
@ -359,7 +357,7 @@ public class RileyLinkMedtronicService extends RileyLinkService {
}
if (rileyLinkAddressChanged) {
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this);
rileyLinkAddressChanged = false;
}