eliminate multiple resends in nsclient

This commit is contained in:
Milos Kozak 2017-05-29 10:03:55 +02:00
parent 60a41b7dce
commit e91acf3fed
3 changed files with 11 additions and 56 deletions

View file

@ -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);

View file

@ -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.

View file

@ -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<DbRequest> iterator = null;
try {
iterator = MainApp.getDbHelper().getDaoDbRequest().closeableIterator();