Commit 258bc0aa by pye52

回滚旧版本,以发布生产包

parent 94dcef0e
...@@ -48,82 +48,54 @@ public class MainWebSocket extends WebSocketClient { ...@@ -48,82 +48,54 @@ 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) {
return; try {
} sDeviceSN = App.Companion.getDeviceSN();
sDeviceSN = App.Companion.getDeviceSN(); if (sDeviceSN.isEmpty()) return;
if (sDeviceSN.isEmpty()) {
LogUtils.file(TAG, "device id is empty"); EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING));
return; sInstance = new MainWebSocket(new URI(AppConfig.INSTANCE.getMainWebSocketServerUrl(sDeviceSN, BuildConfig.VERSION_NAME)));
} sInstance.setConnectionLostTimeout(10);
sInstance.connect();
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING)); TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
String host = AppConfig.INSTANCE.getMainWebSocketServerUrl(sDeviceSN, BuildConfig.VERSION_NAME); @Override
LogUtil.i(TAG, "try connecting to host: " + host); public void run(long id, boolean isLastTime) {
try { if (sInstance.isClosed()) {
sInstance = new MainWebSocket(new URI(host)); if (sReconnectTimes < 2) {
} catch (URISyntaxException e) { sReconnectTimes++;
LogUtil.i(TAG, "invalidate host: " + host, e); sInstance.reconnect();
} EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING));
sInstance.setConnectionLostTimeout(10); } else {
sInstance.connect(); sReconnectTimes = 0;
// 保证只有一个loop在运行 NetworkManager.INSTANCE.switchNetwork();
if (lastLoopId != -1) { EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CHANGE_NETWORK));
TimerHelper.INSTANCE.cancel(lastLoopId); }
} sAutoCheckCount = 0;
lastLoopId = TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() { } else {
@Override // 每10分钟发一次心跳包给后端
public void run(long id, boolean isLastTime) { sAutoCheckCount -= 2;
try { if (sAutoCheckCount < 0) {
if (sInstance == null) { sAutoCheckCount = 10 * 60;
LogUtils.file(TAG, "instance of socket could not be null!!!"); try {
initialize(); JSONObject data = new JSONObject();
return; data.put(FieldEnum.equipmentId.name(), sDeviceSN);
} action(ActionEnum.AUTO_CHECK_DEVICE.name(), data, null);
if (sInstance.isClosed()) { } catch (Exception e) {
doReconnect(); LogUtil.e(TAG, "action error: " + e.getMessage(), e);
} 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() {
// 每10分钟发一次心跳包给后端
sAutoCheckCount -= 2;
if (sAutoCheckCount < 0) {
sAutoCheckCount = 10 * 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