From e91acf3fed1a6172c19fa34f7f11486d4c4b00f0 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 29 May 2017 10:03:55 +0200 Subject: [PATCH] eliminate multiple resends in nsclient --- .../NSClientInternalPlugin.java | 6 -- .../plugins/NSClientInternal/UploadQueue.java | 5 -- .../services/NSClientService.java | 56 ++++--------------- 3 files changed, 11 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java index 606858c5d9..07c34cda8c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java @@ -24,7 +24,6 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventAppExit; -import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; @@ -150,11 +149,6 @@ public class NSClientInternalPlugin implements PluginBase { MainApp.instance().getApplicationContext().unbindService(mConnection); } - @Subscribe - public void onStatusEvent(final EventPreferenceChange s) { - //TODO - } - @Subscribe public void onStatusEvent(final EventNSClientNewLog ev) { addToLog(ev); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java index b318d8026f..d2696dce11 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java @@ -5,22 +5,17 @@ import android.content.Intent; import com.j256.ormlite.dao.CloseableIterator; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.sql.SQLException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.DbRequest; import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; -import info.nightscout.utils.SP; /** * Created by mike on 21.02.2016. diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java index b1eaae1d0e..edf799ccf0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java @@ -29,16 +29,15 @@ import java.util.Date; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.DbRequest; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventConfigBuilderChange; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.plugins.PumpDanaR.Services.ExecutionService; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue; import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSAddAck; import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSAuthAck; -import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSPingAck; import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSUpdateAck; import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastCals; import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastDeviceStatus; @@ -47,7 +46,6 @@ import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastP import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastSgvs; import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastStatus; import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastTreatment; -import info.nightscout.androidaps.db.DbRequest; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSCal; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; @@ -59,6 +57,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientS import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.PumpDanaR.Services.ExecutionService; import info.nightscout.utils.DateUtil; import info.nightscout.utils.SP; import io.socket.client.IO; @@ -92,9 +91,7 @@ public class NSClientService extends Service { private String nsDevice = ""; private Integer nsHours = 24; - private final Integer timeToWaitForResponseInMs = 30000; - private boolean uploading = false; - public Date lastReception = new Date(); + public long lastResendTime = 0; public long latestDateInReceivedData = 0; @@ -293,7 +290,6 @@ public class NSClientService extends Service { } else { MainApp.bus().post(new EventDismissNotification(Notification.NSCLIENT_NO_WRITE_PERMISSION)); } - lastReception = new Date(); } public void readPreferences() { @@ -316,7 +312,6 @@ public class NSClientService extends Service { private Emitter.Listener onDataUpdate = new Emitter.Listener() { @Override public void call(final Object... args) { - lastReception = new Date(); NSClientService.handler.post(new Runnable() { @Override public void run() { @@ -593,40 +588,6 @@ public class NSClientService extends Service { } } - public void doPing() { - if (!isConnected || !hasWriteAuth) return; - MainApp.bus().post(new EventNSClientNewLog("PING", "Sending")); - uploading = true; - JSONObject message = new JSONObject(); - try { - message.put("mills", new Date().getTime()); - } catch (JSONException e) { - e.printStackTrace(); - } - NSPingAck ack = new NSPingAck(); - mSocket.emit("nsping", message, ack); - synchronized (ack) { - try { - ack.wait(timeToWaitForResponseInMs); - } catch (InterruptedException e) { - } - } - if (ack.received) { - String connectionStatus = "Pong received"; - if (ack.auth_received) { - connectionStatus += ": "; - if (ack.read) connectionStatus += "R"; - if (ack.write) connectionStatus += "W"; - if (ack.write_treatment) connectionStatus += "T"; - } - if (!ack.read) sendAuthMessage(new NSAuthAck()); - MainApp.bus().post(new EventNSClientNewLog("AUTH ", connectionStatus)); - } else { - MainApp.bus().post(new EventNSClientNewLog("PING", "Ping lost")); - } - uploading = false; - } - private boolean isCurrent(NSTreatment treatment) { long now = (new Date()).getTime(); long minPast = now - nsHours * 60L * 60 * 1000; @@ -644,14 +605,19 @@ public class NSClientService extends Service { if (!isConnected || !hasWriteAuth) return; - MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend started: " + reason)); - handler.post(new Runnable() { @Override public void run() { - Logger log = LoggerFactory.getLogger(UploadQueue.class); if (mSocket == null || !mSocket.connected()) return; + if (lastResendTime > new Date().getTime() - 30 * 1000L) { + log.debug("Skipping resend by lastResendTime: " + ((new Date().getTime() - lastResendTime) / 1000L) + " sec"); + return; + } + lastResendTime = new Date().getTime(); + + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend started: " + reason)); + CloseableIterator iterator = null; try { iterator = MainApp.getDbHelper().getDaoDbRequest().closeableIterator();