simplify RL code

This commit is contained in:
Milos Kozak 2020-05-12 12:58:59 +02:00
parent 66c567f0e7
commit 9c472e4e05
3 changed files with 18 additions and 21 deletions

View file

@ -59,23 +59,23 @@ public abstract class RileyLinkCommunicationManager {
// All pump communications go through this function. // All pump communications go through this function.
protected <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, Class<E> clazz) protected RLMessage sendAndListen(RLMessage msg, int timeout_ms)
throws RileyLinkCommunicationException { throws RileyLinkCommunicationException {
return sendAndListen(msg, timeout_ms, null, clazz); return sendAndListen(msg, timeout_ms, null);
} }
private <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, Integer extendPreamble_ms, Class<E> clazz) private RLMessage sendAndListen(RLMessage msg, int timeout_ms, Integer extendPreamble_ms)
throws RileyLinkCommunicationException { throws RileyLinkCommunicationException {
return sendAndListen(msg, timeout_ms, 0, extendPreamble_ms, clazz); return sendAndListen(msg, timeout_ms, 0, extendPreamble_ms);
} }
// For backward compatibility // For backward compatibility
private <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, Integer extendPreamble_ms, Class<E> clazz) private RLMessage sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, Integer extendPreamble_ms)
throws RileyLinkCommunicationException { throws RileyLinkCommunicationException {
return sendAndListen(msg, timeout_ms, repeatCount, 0, extendPreamble_ms, clazz); return sendAndListen(msg, timeout_ms, repeatCount, 0, extendPreamble_ms);
} }
protected <E extends RLMessage> E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, int retryCount, Integer extendPreamble_ms, Class<E> clazz) protected RLMessage sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, int retryCount, Integer extendPreamble_ms)
throws RileyLinkCommunicationException { throws RileyLinkCommunicationException {
// internal flag // internal flag
@ -88,7 +88,7 @@ public abstract class RileyLinkCommunicationManager {
(byte) 0, (byte) repeatCount, (byte) 0, (byte) 0, timeout_ms, (byte) retryCount, extendPreamble_ms); (byte) 0, (byte) repeatCount, (byte) 0, (byte) 0, timeout_ms, (byte) retryCount, extendPreamble_ms);
RadioResponse radioResponse = rfSpyResponse.getRadioResponse(injector); RadioResponse radioResponse = rfSpyResponse.getRadioResponse(injector);
E response = createResponseMessage(radioResponse.getPayload(), clazz); RLMessage response = createResponseMessage(radioResponse.getPayload());
if (response.isValid()) { if (response.isValid()) {
// Mark this as the last time we heard from the pump. // Mark this as the last time we heard from the pump.
@ -124,7 +124,7 @@ public abstract class RileyLinkCommunicationManager {
} }
public abstract <E extends RLMessage> E createResponseMessage(byte[] payload, Class<E> clazz); public abstract RLMessage createResponseMessage(byte[] payload);
public abstract void setPumpDeviceState(PumpDeviceState pumpDeviceState); public abstract void setPumpDeviceState(PumpDeviceState pumpDeviceState);

View file

@ -87,9 +87,9 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager
} }
@Override @Override
public <E extends RLMessage> E createResponseMessage(byte[] payload, Class<E> clazz) { public RLMessage createResponseMessage(byte[] payload) {
PumpMessage pumpMessage = new PumpMessage(aapsLogger, payload); PumpMessage pumpMessage = new PumpMessage(aapsLogger, payload);
return (E) pumpMessage; return pumpMessage;
} }
@Override @Override
@ -170,7 +170,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager
if (radioResponse.isValid()) { if (radioResponse.isValid()) {
PumpMessage pumpResponse = createResponseMessage(radioResponse.getPayload(), PumpMessage.class); PumpMessage pumpResponse = (PumpMessage) createResponseMessage(radioResponse.getPayload());
if (!pumpResponse.isValid()) { if (!pumpResponse.isValid()) {
aapsLogger.warn(LTag.PUMPCOMM, "Response is invalid ! [interrupted={}, timeout={}]", rfSpyResponse.wasInterrupted(), aapsLogger.warn(LTag.PUMPCOMM, "Response is invalid ! [interrupted={}, timeout={}]", rfSpyResponse.wasInterrupted(),
@ -551,8 +551,8 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager
// All pump communications go through this function. // All pump communications go through this function.
private PumpMessage sendAndListen(RLMessage msg, int timeout_ms) throws RileyLinkCommunicationException { protected PumpMessage sendAndListen(RLMessage msg, int timeout_ms) throws RileyLinkCommunicationException {
return sendAndListen(msg, timeout_ms, PumpMessage.class); return sendAndListen(msg, timeout_ms);
} }

View file

@ -16,11 +16,8 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLink
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RLMessage; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.RLMessage;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RLMessageType; 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.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.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil;
import info.nightscout.androidaps.plugins.pump.medtronic.defs.PumpDeviceState; import info.nightscout.androidaps.plugins.pump.medtronic.defs.PumpDeviceState;
import info.nightscout.androidaps.plugins.pump.omnipod.OmnipodPumpPlugin;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.OmnipodAction; import info.nightscout.androidaps.plugins.pump.omnipod.comm.action.OmnipodAction;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.exception.CommunicationException; import info.nightscout.androidaps.plugins.pump.omnipod.comm.exception.CommunicationException;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.exception.IllegalMessageAddressException; import info.nightscout.androidaps.plugins.pump.omnipod.comm.exception.IllegalMessageAddressException;
@ -94,8 +91,8 @@ public class OmnipodCommunicationManager extends RileyLinkCommunicationManager {
} }
@Override @Override
public <E extends RLMessage> E createResponseMessage(byte[] payload, Class<E> clazz) { public RLMessage createResponseMessage(byte[] payload) {
return (E) new OmnipodPacket(payload); return new OmnipodPacket(payload);
} }
@Override @Override
@ -301,7 +298,7 @@ public class OmnipodCommunicationManager extends RileyLinkCommunicationManager {
OmnipodPacket ack = createAckPacket(podState, packetAddress, messageAddress); OmnipodPacket ack = createAckPacket(podState, packetAddress, messageAddress);
boolean quiet = false; boolean quiet = false;
while (!quiet) try { while (!quiet) try {
sendAndListen(ack, 300, 1, 0, 40, OmnipodPacket.class); sendAndListen(ack, 300, 1, 0, 40);
} catch (RileyLinkCommunicationException ex) { } catch (RileyLinkCommunicationException ex) {
if (RileyLinkBLEError.Timeout.equals(ex.getErrorCode())) { if (RileyLinkBLEError.Timeout.equals(ex.getErrorCode())) {
quiet = true; quiet = true;
@ -333,7 +330,7 @@ public class OmnipodCommunicationManager extends RileyLinkCommunicationManager {
while (System.currentTimeMillis() < timeoutTime) { while (System.currentTimeMillis() < timeoutTime) {
OmnipodPacket response = null; OmnipodPacket response = null;
try { try {
response = sendAndListen(packet, responseTimeoutMilliseconds, repeatCount, 9, preambleExtensionMilliseconds, OmnipodPacket.class); response = (OmnipodPacket) sendAndListen(packet, responseTimeoutMilliseconds, repeatCount, 9, preambleExtensionMilliseconds);
} catch (RileyLinkCommunicationException | OmnipodException ex) { } catch (RileyLinkCommunicationException | OmnipodException ex) {
aapsLogger.debug(LTag.PUMPBTCOMM, "Ignoring exception in exchangePackets: " + ex.getClass().getSimpleName() + ": " + ex.getMessage()); aapsLogger.debug(LTag.PUMPBTCOMM, "Ignoring exception in exchangePackets: " + ex.getClass().getSimpleName() + ": " + ex.getMessage());
continue; continue;