From 80f5b3cf4bb9e6f312b64406c15a6d8e376cecde Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Sun, 10 May 2020 14:08:56 +0100 Subject: [PATCH] - changes on service starting (moved from cnst to onStart method) - change RileyLinkBLE to stop reader - added log for start and stop of service --- .../pump/common/PumpPluginAbstract.java | 7 +- .../pump/common/hw/rileylink/ble/RFSpy.java | 13 +- .../common/hw/rileylink/ble/RFSpyReader.java | 13 +- .../rileylink/service/RileyLinkService.java | 1 + .../pump/medtronic/MedtronicPumpPlugin.java | 13 +- .../service/RileyLinkMedtronicService.java | 15 -- .../pump/omnipod/OmnipodPumpPlugin.java | 142 ++++++++++-------- .../service/RileyLinkOmnipodService.java | 6 - .../omnipod_dash/OmnipodDashPumpPlugin.java | 7 + 9 files changed, 112 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java index 126b017b5e..6cdf1f80e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java @@ -114,10 +114,13 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI public abstract void initPumpStatusData(); + @Override protected void onStart() { super.onStart(); - // TODO: moved from constructor .... test if it works + + aapsLogger.debug(LTag.PUMP, this.deviceID() + " onStart()"); + initPumpStatusData(); Intent intent = new Intent(context, getServiceClass()); @@ -136,6 +139,8 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI @Override protected void onStop() { + aapsLogger.debug(LTag.PUMP, this.deviceID() + " onStop()"); + context.unbindService(serviceConnection); serviceRunning = false; 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 50c6a01710..9e8b5296f4 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 @@ -56,7 +56,7 @@ public class RFSpy { 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 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 private Double currentFrequencyMHz; @@ -421,14 +421,7 @@ public class RFSpy { } - public RFSpyResponse resetRileyLink() { - RFSpyResponse resp = null; - try { - resp = writeToData(new Reset(), EXPECTED_MAX_BLUETOOTH_LATENCY_MS); - aapsLogger.debug(LTag.PUMPBTCOMM, "Reset command send, response: {}", resp); - } catch (Exception e) { - e.toString(); - } - return resp; + public void stopReader() { + reader.stop(); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java index 5a3d459154..4ede9f70fb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java @@ -29,11 +29,12 @@ public class RFSpyReader { private int acquireCount = 0; private int releaseCount = 0; private boolean stopAtNull = true; + private static boolean isRunning = false; RFSpyReader(AAPSLogger aapsLogger, RileyLinkBLE rileyLinkBle) { - this.rileyLinkBle = rileyLinkBle; this.aapsLogger = aapsLogger; + setRileyLinkBle(rileyLinkBle); } @@ -87,6 +88,8 @@ public class RFSpyReader { public void start() { + isRunning = true; + readerTask = new AsyncTask() { @Override @@ -94,7 +97,7 @@ public class RFSpyReader { UUID serviceUUID = UUID.fromString(GattAttributes.SERVICE_RADIO); UUID radioDataUUID = UUID.fromString(GattAttributes.CHARA_RADIO_DATA); BLECommOperationResult result; - while (true) { + while (isRunning) { try { acquireCount++; waitForRadioData.acquire(); @@ -129,7 +132,13 @@ public class RFSpyReader { aapsLogger.error(LTag.PUMPBTCOMM, "Interrupted while waiting for data"); } } + return null; } }.execute(); } + + public void stop() { + isRunning = false; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java index 7ef3380a0d..62b8808243 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java @@ -101,6 +101,7 @@ public abstract class RileyLinkService extends DaggerService { super.onDestroy(); //LOG.error("I die! I die!"); + rfspy.stopReader(); rileyLinkBLE.disconnect(); // dispose of Gatt (disconnect and close) if (mBroadcastReceiver != null) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index a01c59771d..6865cc0165 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -160,6 +160,14 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter this.serviceTaskExecutor = serviceTaskExecutor; displayConnectionMessages = false; + } + + + @Override + protected void onStart() { + super.onStart(); + + aapsLogger.debug(LTag.PUMP, this.deviceID() + " started."); serviceConnection = new ServiceConnection() { @@ -186,12 +194,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter }).start(); } }; - } - - @Override - protected void onStart() { - super.onStart(); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java index 7c6a55d092..fa64469b79 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java @@ -187,21 +187,6 @@ public class RileyLinkMedtronicService extends RileyLinkService { } - @Override - public String getDeviceSpecificBroadcastsIdentifierPrefix() { - return null; - } - - - public boolean handleDeviceSpecificBroadcasts(Intent intent) { - return false; - } - - - @Override - public void registerDeviceSpecificBroadcasts(IntentFilter intentFilter) { - } - public boolean verifyConfiguration() { try { String regexSN = "[0-9]{6}"; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java index b3a66927d3..7e67a1fc20 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java @@ -118,9 +118,10 @@ public class OmnipodPumpPlugin extends PumpPluginAbstract implements OmnipodPump private Profile currentProfile; - boolean omnipodServiceRunning = false; + //boolean omnipodServiceRunning = false; private long nextPodCheck = 0L; + protected boolean isOmnipodEros = true; //OmnipodDriverState driverState = OmnipodDriverState.NotInitalized; @Inject @@ -157,71 +158,7 @@ public class OmnipodPumpPlugin extends PumpPluginAbstract implements OmnipodPump this.omnipodUtil = omnipodUtil; this.omnipodPumpStatus = omnipodPumpStatus; - //OmnipodUtil.setDriverState(); - -// TODO loop -// if (OmnipodUtil.isOmnipodEros()) { -// OmnipodUtil.setPlugin(this); -// OmnipodUtil.setOmnipodPodType(OmnipodPodType.Eros); -// OmnipodUtil.setPumpType(PumpType.Insulet_Omnipod); -// } - -// // TODO ccc - - - serviceConnection = new ServiceConnection() { - - @Override - public void onServiceDisconnected(ComponentName name) { - - aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is disconnected"); - rileyLinkOmnipodService = null; - } - - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - - aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is connected"); - RileyLinkOmnipodService.LocalBinder mLocalBinder = (RileyLinkOmnipodService.LocalBinder) service; - rileyLinkOmnipodService = mLocalBinder.getServiceInstance(); - - new Thread(() -> { - - for (int i = 0; i < 20; i++) { - SystemClock.sleep(5000); - - aapsLogger.debug(LTag.PUMP, "Starting Omnipod-RileyLink service"); - if (rileyLinkOmnipodService.setNotInPreInit()) { - break; - } - } - - -// if (OmnipodPumpPlugin.this.omnipodPumpStatus != null) { -// -// aapsLogger.debug(LTag.PUMP, "Starting OmniPod-RileyLink service"); -// if (omnipodService.setNotInPreInit()) { -// if (omnipodCommunicationManager == null) { -// omnipodCommunicationManager = AapsOmnipodManager.getInstance(); -// omnipodCommunicationManager.setPumpStatus(OmnipodPumpPlugin.this.omnipodPumpStatus); -// omnipodServiceRunning = true; -// } else { -// omnipodCommunicationManager.setPumpStatus(OmnipodPumpPlugin.this.omnipodPumpStatus); -// } -// -// omnipodUtil.setOmnipodPodType(OmnipodPodType.Eros); -// //omnipodUtil.setPlugin(OmnipodPumpPlugin.this); -// -// omnipodUIComm = new OmnipodUIComm(omnipodCommunicationManager, plugin, OmnipodPumpPlugin.this.omnipodPumpStatus); -// break; -// } -// } -// -// SystemClock.sleep(5000); - //} - }).start(); - } - }; + this.isOmnipodEros = true; } protected OmnipodPumpPlugin(PluginDescription pluginDescription, PumpType pumpType, @@ -248,6 +185,76 @@ public class OmnipodPumpPlugin extends PumpPluginAbstract implements OmnipodPump @Override protected void onStart() { super.onStart(); + + //OmnipodUtil.setDriverState(); + +// TODO loop +// if (OmnipodUtil.isOmnipodEros()) { +// OmnipodUtil.setPlugin(this); +// OmnipodUtil.setOmnipodPodType(OmnipodPodType.Eros); +// OmnipodUtil.setPumpType(PumpType.Insulet_Omnipod); +// } + +// // TODO ccc + + if (isOmnipodEros) { + + serviceConnection = new ServiceConnection() { + + @Override + public void onServiceDisconnected(ComponentName name) { + + aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is disconnected"); + rileyLinkOmnipodService = null; + } + + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + + aapsLogger.debug(LTag.PUMP, "RileyLinkOmnipodService is connected"); + RileyLinkOmnipodService.LocalBinder mLocalBinder = (RileyLinkOmnipodService.LocalBinder) service; + rileyLinkOmnipodService = mLocalBinder.getServiceInstance(); + + new Thread(() -> { + + for (int i = 0; i < 20; i++) { + SystemClock.sleep(5000); + + aapsLogger.debug(LTag.PUMP, "Starting Omnipod-RileyLink service"); + if (rileyLinkOmnipodService.setNotInPreInit()) { + break; + } + } + + +// if (OmnipodPumpPlugin.this.omnipodPumpStatus != null) { +// +// aapsLogger.debug(LTag.PUMP, "Starting OmniPod-RileyLink service"); +// if (omnipodService.setNotInPreInit()) { +// if (omnipodCommunicationManager == null) { +// omnipodCommunicationManager = AapsOmnipodManager.getInstance(); +// omnipodCommunicationManager.setPumpStatus(OmnipodPumpPlugin.this.omnipodPumpStatus); +// omnipodServiceRunning = true; +// } else { +// omnipodCommunicationManager.setPumpStatus(OmnipodPumpPlugin.this.omnipodPumpStatus); +// } +// +// omnipodUtil.setOmnipodPodType(OmnipodPodType.Eros); +// //omnipodUtil.setPlugin(OmnipodPumpPlugin.this); +// +// omnipodUIComm = new OmnipodUIComm(omnipodCommunicationManager, plugin, OmnipodPumpPlugin.this.omnipodPumpStatus); +// break; +// } +// } +// +// SystemClock.sleep(5000); + //} + }).start(); + } + }; + } + + disposable.add(rxBus .toObservable(EventPreferenceChange.class) .observeOn(Schedulers.io()) @@ -261,6 +268,9 @@ public class OmnipodPumpPlugin extends PumpPluginAbstract implements OmnipodPump rileyLinkOmnipodService.verifyConfiguration(); }, fabricPrivacy::logException) ); + + + //rileyLinkOmnipodService.verifyConfiguration(); //initPumpStatusData(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/service/RileyLinkOmnipodService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/service/RileyLinkOmnipodService.java index d283b658da..aecbd29cfd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/service/RileyLinkOmnipodService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/service/RileyLinkOmnipodService.java @@ -116,12 +116,6 @@ public class RileyLinkOmnipodService extends RileyLinkService { if (sp.contains(OmnipodConst.Prefs.PodState) && omnipodUtil.getPodSessionState() == null) { try { omnipodUtil.loadSessionState(); -// Gson gson = omnipodUtil.getGsonInstance(); -// String storedPodState = sp.getString(OmnipodConst.Prefs.PodState, ""); -// aapsLogger.info(LTag.PUMPCOMM, "PodSessionState-SP: loaded from SharedPreferences: " + storedPodState); -// podState = gson.fromJson(storedPodState, PodSessionState.class); -// podState.injectDaggerClass(injector); -// omnipodUtil.setPodSessionState(podState); } catch (Exception ex) { aapsLogger.error(LTag.PUMPCOMM, "Could not deserialize Pod state", ex); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod_dash/OmnipodDashPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod_dash/OmnipodDashPumpPlugin.java index 700724f11b..0a1ce1b009 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod_dash/OmnipodDashPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod_dash/OmnipodDashPumpPlugin.java @@ -152,6 +152,13 @@ public class OmnipodDashPumpPlugin extends OmnipodPumpPlugin implements OmnipodP } + @Override + public void onStart() { + this.isOmnipodEros = false; + super.onStart(); + } + + @Override public String deviceID() { return "Omnipod Dash";