From 9c472e4e0536c5c385e471f3c43338961a281a41 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 12 May 2020 12:58:59 +0200 Subject: [PATCH] simplify RL code --- .../RileyLinkCommunicationManager.java | 18 +++++++++--------- .../comm/MedtronicCommunicationManager.java | 10 +++++----- .../comm/OmnipodCommunicationManager.java | 11 ++++------- 3 files changed, 18 insertions(+), 21 deletions(-) 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 d352250071..12a0f0f4d0 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 @@ -59,23 +59,23 @@ public abstract class RileyLinkCommunicationManager { // All pump communications go through this function. - protected E sendAndListen(RLMessage msg, int timeout_ms, Class clazz) + protected RLMessage sendAndListen(RLMessage msg, int timeout_ms) throws RileyLinkCommunicationException { - return sendAndListen(msg, timeout_ms, null, clazz); + return sendAndListen(msg, timeout_ms, null); } - private E sendAndListen(RLMessage msg, int timeout_ms, Integer extendPreamble_ms, Class clazz) + private RLMessage sendAndListen(RLMessage msg, int timeout_ms, Integer extendPreamble_ms) throws RileyLinkCommunicationException { - return sendAndListen(msg, timeout_ms, 0, extendPreamble_ms, clazz); + return sendAndListen(msg, timeout_ms, 0, extendPreamble_ms); } // For backward compatibility - private E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, Integer extendPreamble_ms, Class clazz) + private RLMessage sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, Integer extendPreamble_ms) throws RileyLinkCommunicationException { - return sendAndListen(msg, timeout_ms, repeatCount, 0, extendPreamble_ms, clazz); + return sendAndListen(msg, timeout_ms, repeatCount, 0, extendPreamble_ms); } - protected E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, int retryCount, Integer extendPreamble_ms, Class clazz) + protected RLMessage sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, int retryCount, Integer extendPreamble_ms) throws RileyLinkCommunicationException { // internal flag @@ -88,7 +88,7 @@ public abstract class RileyLinkCommunicationManager { (byte) 0, (byte) repeatCount, (byte) 0, (byte) 0, timeout_ms, (byte) retryCount, extendPreamble_ms); RadioResponse radioResponse = rfSpyResponse.getRadioResponse(injector); - E response = createResponseMessage(radioResponse.getPayload(), clazz); + RLMessage response = createResponseMessage(radioResponse.getPayload()); if (response.isValid()) { // Mark this as the last time we heard from the pump. @@ -124,7 +124,7 @@ public abstract class RileyLinkCommunicationManager { } - public abstract E createResponseMessage(byte[] payload, Class clazz); + public abstract RLMessage createResponseMessage(byte[] payload); public abstract void setPumpDeviceState(PumpDeviceState pumpDeviceState); 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 086dc92cd7..83ed08ddd0 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 @@ -87,9 +87,9 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager } @Override - public E createResponseMessage(byte[] payload, Class clazz) { + public RLMessage createResponseMessage(byte[] payload) { PumpMessage pumpMessage = new PumpMessage(aapsLogger, payload); - return (E) pumpMessage; + return pumpMessage; } @Override @@ -170,7 +170,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager if (radioResponse.isValid()) { - PumpMessage pumpResponse = createResponseMessage(radioResponse.getPayload(), PumpMessage.class); + PumpMessage pumpResponse = (PumpMessage) createResponseMessage(radioResponse.getPayload()); if (!pumpResponse.isValid()) { 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. - private PumpMessage sendAndListen(RLMessage msg, int timeout_ms) throws RileyLinkCommunicationException { - return sendAndListen(msg, timeout_ms, PumpMessage.class); + protected PumpMessage sendAndListen(RLMessage msg, int timeout_ms) throws RileyLinkCommunicationException { + return sendAndListen(msg, timeout_ms); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationManager.java index e77befd258..89b6a43559 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/OmnipodCommunicationManager.java @@ -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.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.utils.ByteUtil; 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.exception.CommunicationException; import info.nightscout.androidaps.plugins.pump.omnipod.comm.exception.IllegalMessageAddressException; @@ -94,8 +91,8 @@ public class OmnipodCommunicationManager extends RileyLinkCommunicationManager { } @Override - public E createResponseMessage(byte[] payload, Class clazz) { - return (E) new OmnipodPacket(payload); + public RLMessage createResponseMessage(byte[] payload) { + return new OmnipodPacket(payload); } @Override @@ -301,7 +298,7 @@ public class OmnipodCommunicationManager extends RileyLinkCommunicationManager { OmnipodPacket ack = createAckPacket(podState, packetAddress, messageAddress); boolean quiet = false; while (!quiet) try { - sendAndListen(ack, 300, 1, 0, 40, OmnipodPacket.class); + sendAndListen(ack, 300, 1, 0, 40); } catch (RileyLinkCommunicationException ex) { if (RileyLinkBLEError.Timeout.equals(ex.getErrorCode())) { quiet = true; @@ -333,7 +330,7 @@ public class OmnipodCommunicationManager extends RileyLinkCommunicationManager { while (System.currentTimeMillis() < timeoutTime) { OmnipodPacket response = null; try { - response = sendAndListen(packet, responseTimeoutMilliseconds, repeatCount, 9, preambleExtensionMilliseconds, OmnipodPacket.class); + response = (OmnipodPacket) sendAndListen(packet, responseTimeoutMilliseconds, repeatCount, 9, preambleExtensionMilliseconds); } catch (RileyLinkCommunicationException | OmnipodException ex) { aapsLogger.debug(LTag.PUMPBTCOMM, "Ignoring exception in exchangePackets: " + ex.getClass().getSimpleName() + ": " + ex.getMessage()); continue;