NSClient watchdog

This commit is contained in:
Milos Kozak 2018-08-01 19:16:31 +02:00
parent 2859860d0d
commit eee187a71f
5 changed files with 55 additions and 15 deletions

View file

@ -131,9 +131,7 @@ public class NSClientFragment extends SubscriberFragment implements View.OnClick
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.nsclientinternal_paused:
SP.putBoolean(R.string.key_nsclientinternal_paused, isChecked);
NSClientPlugin.getPlugin().paused = isChecked;
MainApp.bus().post(new EventPreferenceChange(R.string.key_nsclientinternal_paused));
NSClientPlugin.getPlugin().pause(isChecked);
updateGUI();
FabricPrivacy.getInstance().logCustom(new CustomEvent("NSClientPause"));
break;
@ -154,19 +152,17 @@ public class NSClientFragment extends SubscriberFragment implements View.OnClick
protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
NSClientPlugin.getPlugin().updateLog();
logTextView.setText(NSClientPlugin.getPlugin().textLog);
if (NSClientPlugin.getPlugin().autoscroll) {
logScrollview.fullScroll(ScrollView.FOCUS_DOWN);
}
urlTextView.setText(NSClientPlugin.getPlugin().url());
Spanned queuetext = Html.fromHtml(MainApp.gs(R.string.queue) + " <b>" + UploadQueue.size() + "</b>");
queueTextView.setText(queuetext);
statusTextView.setText(NSClientPlugin.getPlugin().status);
activity.runOnUiThread(() -> {
NSClientPlugin.getPlugin().updateLog();
pausedCheckbox.setChecked(SP.getBoolean(R.string.key_nsclientinternal_paused, false));
logTextView.setText(NSClientPlugin.getPlugin().textLog);
if (NSClientPlugin.getPlugin().autoscroll) {
logScrollview.fullScroll(ScrollView.FOCUS_DOWN);
}
urlTextView.setText(NSClientPlugin.getPlugin().url());
Spanned queuetext = Html.fromHtml(MainApp.gs(R.string.queue) + " <b>" + UploadQueue.size() + "</b>");
queueTextView.setText(queuetext);
statusTextView.setText(NSClientPlugin.getPlugin().status);
});
}

View file

@ -209,6 +209,12 @@ public class NSClientPlugin extends PluginBase {
nsClientService.resend(reason);
}
public void pause(boolean newState) {
SP.putBoolean(R.string.key_nsclientinternal_paused, newState);
paused = newState;
MainApp.bus().post(new EventPreferenceChange(R.string.key_nsclientinternal_paused));
}
public UploadQueue queue() {
return NSClientService.uploadQueue;
}

View file

@ -8,6 +8,7 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.SystemClock;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
@ -22,6 +23,7 @@ import org.slf4j.LoggerFactory;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
@ -57,6 +59,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientRestart;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
@ -64,6 +67,7 @@ import info.nightscout.utils.DateUtil;
import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.JsonHelper;
import info.nightscout.utils.SP;
import info.nightscout.utils.T;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
@ -102,6 +106,11 @@ public class NSClientService extends Service {
public static UploadQueue uploadQueue = new UploadQueue();
private ArrayList<Long> reconnections = new ArrayList<>();
private int WATCHDOG_INTERVAL_MINUTES = 2;
private int WATCHDOG_RECONNECT_IN = 1;
private int WATCHDOG_MAXCONNECTIONS = 5;
public NSClientService() {
registerBus();
if (handler == null) {
@ -241,9 +250,36 @@ public class NSClientService extends Service {
connectCounter++;
MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "connect #" + connectCounter + " event. ID: " + mSocket.id()));
sendAuthMessage(new NSAuthAck());
watchdog();
}
};
void watchdog() {
synchronized (reconnections) {
long now = DateUtil.now();
reconnections.add(now);
for (int i = 0; i < reconnections.size(); i++) {
Long r = reconnections.get(i);
if (r < now - T.mins(WATCHDOG_INTERVAL_MINUTES).msecs()) {
reconnections.remove(r);
}
}
MainApp.bus().post(new EventNSClientNewLog("WATCHDOG", "connections in last " + WATCHDOG_INTERVAL_MINUTES + " mins: " + reconnections.size() + "/" + WATCHDOG_MAXCONNECTIONS));
if (reconnections.size() >= WATCHDOG_MAXCONNECTIONS) {
Notification n = new Notification(Notification.NSMALFUNCTION, MainApp.gs(R.string.nsmalfunction), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(n));
MainApp.bus().post(new EventNSClientNewLog("WATCHDOG", "pausing for " + WATCHDOG_RECONNECT_IN + " mins"));
NSClientPlugin.getPlugin().pause(true);
MainApp.bus().post(new EventNSClientUpdateGUI());
new Thread(() -> {
SystemClock.sleep(T.mins(WATCHDOG_RECONNECT_IN).msecs());
MainApp.bus().post(new EventNSClientNewLog("WATCHDOG", "reenabling NSClient"));
NSClientPlugin.getPlugin().pause(false);
}).start();
}
}
}
private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {

View file

@ -65,6 +65,7 @@ public class Notification {
public static final int PERMISSION_BATTERY = 37;
public static final int PERMISSION_SMS = 38;
public static final int MAXIMUM_BASAL_VALUE_REPLACED = 39;
public static final int NSMALFUNCTION = 40;
public int id;

View file

@ -1170,6 +1170,7 @@
<string name="openaps_noasdata">No autosens data available</string>
<string name="nav_logsettings">Log settings</string>
<string name="resettodefaults">Reset to defaults</string>
<string name="nsmalfunction">NSClient malfunction. Consider NS and NSClient restart.</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>