Commit 6cd22534 by pye52

1、避免loop重复生成

2、完善连接状态的日志输出

用于下一发版测试
parent 258bc0aa
...@@ -48,54 +48,83 @@ public class MainWebSocket extends WebSocketClient { ...@@ -48,54 +48,83 @@ public class MainWebSocket extends WebSocketClient {
public static final String CODE_FAIL = "-1"; public static final String CODE_FAIL = "-1";
private static MainWebSocket sInstance; private static MainWebSocket sInstance;
private static long lastLoopId = -1;
private static String sDeviceSN; private static String sDeviceSN;
private static int sReconnectTimes = 0; private static int sReconnectTimes = 0;
private static int sAutoCheckCount = 0; private static int sAutoCheckCount = 0;
public static void initialize() { public static void initialize() {
if (sInstance == null) { if (sInstance != null) {
try { return;
sDeviceSN = App.Companion.getDeviceSN(); }
if (sDeviceSN.isEmpty()) return; sDeviceSN = App.Companion.getDeviceSN();
if (sDeviceSN.isEmpty()) {
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING)); LogUtils.file(TAG, "device id is empty");
sInstance = new MainWebSocket(new URI(AppConfig.INSTANCE.getMainWebSocketServerUrl(sDeviceSN, BuildConfig.VERSION_NAME))); return;
sInstance.setConnectionLostTimeout(10); }
sInstance.connect();
TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() { EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING));
@Override String host = AppConfig.INSTANCE.getMainWebSocketServerUrl(sDeviceSN, BuildConfig.VERSION_NAME);
public void run(long id, boolean isLastTime) { LogUtil.i(TAG, "try connecting to host: " + host);
if (sInstance.isClosed()) { try {
if (sReconnectTimes < 2) { sInstance = new MainWebSocket(new URI(host));
sReconnectTimes++; } catch (URISyntaxException e) {
sInstance.reconnect(); LogUtil.i(TAG, "invalidate host: " + host, e);
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING)); }
} else { sInstance.setConnectionLostTimeout(10);
sReconnectTimes = 0; sInstance.connect();
NetworkManager.INSTANCE.switchNetwork(); // 保证只有一个loop在运行
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CHANGE_NETWORK)); if (lastLoopId != -1) {
} TimerHelper.INSTANCE.cancel(lastLoopId);
sAutoCheckCount = 0; }
} else { lastLoopId = TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
// 每10分钟发一次心跳包给后端 @Override
sAutoCheckCount -= 2; public void run(long id, boolean isLastTime) {
if (sAutoCheckCount < 0) { try {
sAutoCheckCount = 10 * 60; if (sInstance == null) {
try { LogUtils.file(TAG, "instance of socket could not be null!!!");
JSONObject data = new JSONObject(); initialize();
data.put(FieldEnum.equipmentId.name(), sDeviceSN); return;
action(ActionEnum.AUTO_CHECK_DEVICE.name(), data, null); }
} catch (Exception e) { if (sInstance.isClosed()) {
LogUtil.e(TAG, "action error: " + e.getMessage(), e); doReconnect();
} } else {
} doHeartbeat();
} }
} catch (Exception e) {
LogUtils.file(TAG, "fatal error: " + e.getMessage());
}
}
private void doReconnect() {
if (sReconnectTimes < 2) {
sReconnectTimes++;
sInstance.reconnect();
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING));
} else {
sReconnectTimes = 0;
NetworkManager.INSTANCE.switchNetwork();
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CHANGE_NETWORK));
}
sAutoCheckCount = 0;
}
private void doHeartbeat() {
// 每3分钟发一次心跳包给后端
// 原为10分钟一次,现改为3分钟一次以方便后台预警功能
sAutoCheckCount -= 2;
if (sAutoCheckCount < 0) {
sAutoCheckCount = 3 * 60;
try {
JSONObject data = new JSONObject();
data.put(FieldEnum.equipmentId.name(), sDeviceSN);
action(ActionEnum.AUTO_CHECK_DEVICE.name(), data, null);
} catch (Exception e) {
LogUtil.e(TAG, "action error: " + e.getMessage(), e);
} }
}, 2000, -1, 2000); }
} catch (URISyntaxException e) {
LogUtils.file(TAG, "fatal error: " + e.getMessage());
} }
} }, 2000, -1, 2000);
} }
public static boolean isInited() { public static boolean isInited() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment