|
@@ -35,6 +35,7 @@ import java.io.OutputStream; |
|
@@ -35,6 +35,7 @@ import java.io.OutputStream; |
|
35
|
import java.net.Socket;
|
35
|
import java.net.Socket;
|
|
36
|
import java.util.Arrays;
|
36
|
import java.util.Arrays;
|
|
37
|
import java.util.concurrent.atomic.AtomicBoolean;
|
37
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
38
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
38
|
|
39
|
|
|
39
|
import static com.qnbar.smc.service.SocketService.JUST_OPEN_DOOR;
|
40
|
import static com.qnbar.smc.service.SocketService.JUST_OPEN_DOOR;
|
|
40
|
import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR;
|
41
|
import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR;
|
|
@@ -109,6 +110,7 @@ public class SocketService1 extends BaseService { |
|
@@ -109,6 +110,7 @@ public class SocketService1 extends BaseService { |
|
109
|
private Handler mHandler = new Handler();
|
110
|
private Handler mHandler = new Handler();
|
|
110
|
|
111
|
|
|
111
|
AtomicBoolean sendRegister = new AtomicBoolean(false);
|
112
|
AtomicBoolean sendRegister = new AtomicBoolean(false);
|
|
|
|
113
|
+ AtomicInteger heartBeatErrorCount = new AtomicInteger(0);
|
|
112
|
|
114
|
|
|
113
|
private Runnable heartBeatRunnable = new Runnable() {
|
115
|
private Runnable heartBeatRunnable = new Runnable() {
|
|
114
|
@Override
|
116
|
@Override
|
|
@@ -123,7 +125,6 @@ public class SocketService1 extends BaseService { |
|
@@ -123,7 +125,6 @@ public class SocketService1 extends BaseService { |
|
123
|
}
|
125
|
}
|
|
124
|
}
|
126
|
}
|
|
125
|
};
|
127
|
};
|
|
126
|
-
|
|
|
|
127
|
int mainChargeCount = 0;
|
128
|
int mainChargeCount = 0;
|
|
128
|
private Runnable startMainRunnable = new Runnable() {
|
129
|
private Runnable startMainRunnable = new Runnable() {
|
|
129
|
@Override
|
130
|
@Override
|
|
@@ -226,6 +227,9 @@ public class SocketService1 extends BaseService { |
|
@@ -226,6 +227,9 @@ public class SocketService1 extends BaseService { |
|
226
|
if (!msg.contains("20025")) {
|
227
|
if (!msg.contains("20025")) {
|
|
227
|
LogUtils.d(TAG, "send msg:" + msg);
|
228
|
LogUtils.d(TAG, "send msg:" + msg);
|
|
228
|
} else {
|
229
|
} else {
|
|
|
|
230
|
+ if (heartBeatErrorCount.getAndDecrement() > 3) {
|
|
|
|
231
|
+ clearConnect();
|
|
|
|
232
|
+ }
|
|
229
|
LogUtils.d(TAG, "send heart beat msg:" + msg);
|
233
|
LogUtils.d(TAG, "send heart beat msg:" + msg);
|
|
230
|
}
|
234
|
}
|
|
231
|
Socket soc = socket;
|
235
|
Socket soc = socket;
|
|
@@ -297,6 +301,7 @@ public class SocketService1 extends BaseService { |
|
@@ -297,6 +301,7 @@ public class SocketService1 extends BaseService { |
|
297
|
LogUtils.d(TAG, "clearConnect");
|
301
|
LogUtils.d(TAG, "clearConnect");
|
|
298
|
mHandler.removeCallbacks(heartBeatRunnable);
|
302
|
mHandler.removeCallbacks(heartBeatRunnable);
|
|
299
|
sendRegister.set(false);
|
303
|
sendRegister.set(false);
|
|
|
|
304
|
+ heartBeatErrorCount.set(0);
|
|
300
|
releaseLastSocket();
|
305
|
releaseLastSocket();
|
|
301
|
}
|
306
|
}
|
|
302
|
///end socket operation
|
307
|
///end socket operation
|
|
@@ -366,6 +371,7 @@ public class SocketService1 extends BaseService { |
|
@@ -366,6 +371,7 @@ public class SocketService1 extends BaseService { |
|
366
|
|
371
|
|
|
367
|
switch (socketResponse.getCode()) {
|
372
|
switch (socketResponse.getCode()) {
|
|
368
|
case SUCCESS_MESSAGE:
|
373
|
case SUCCESS_MESSAGE:
|
|
|
|
374
|
+ heartBeatErrorCount.set(0);
|
|
369
|
Log.d(TAG, "SUCCESS_MESSAGE");
|
375
|
Log.d(TAG, "SUCCESS_MESSAGE");
|
|
370
|
break;
|
376
|
break;
|
|
371
|
case VERIFY_SUCCESS:
|
377
|
case VERIFY_SUCCESS:
|
|
@@ -374,6 +380,7 @@ public class SocketService1 extends BaseService { |
|
@@ -374,6 +380,7 @@ public class SocketService1 extends BaseService { |
|
374
|
LogUtils.d(TAG, "verify success start heart beat");
|
380
|
LogUtils.d(TAG, "verify success start heart beat");
|
|
375
|
break;
|
381
|
break;
|
|
376
|
case HEART_BEAT_SUCCESS:
|
382
|
case HEART_BEAT_SUCCESS:
|
|
|
|
383
|
+ heartBeatErrorCount.set(0);
|
|
377
|
//每成功5次心跳判定是否启动main activity
|
384
|
//每成功5次心跳判定是否启动main activity
|
|
378
|
if (++mainChargeCount == 5) {
|
385
|
if (++mainChargeCount == 5) {
|
|
379
|
mainChargeCount = 0;
|
386
|
mainChargeCount = 0;
|
|
@@ -385,7 +392,6 @@ public class SocketService1 extends BaseService { |
|
@@ -385,7 +392,6 @@ public class SocketService1 extends BaseService { |
|
385
|
boolean serviceLaunched = ActivityCollector.getActivity(MainActivity.class) != null;
|
392
|
boolean serviceLaunched = ActivityCollector.getActivity(MainActivity.class) != null;
|
|
386
|
Log.d(TAG, "HEART_BEAT_SUCCESS,MainActivity Launched:" + serviceLaunched);
|
393
|
Log.d(TAG, "HEART_BEAT_SUCCESS,MainActivity Launched:" + serviceLaunched);
|
|
387
|
break;
|
394
|
break;
|
|
388
|
-
|
|
|
|
389
|
case HEART_BEAT_ERROR_SYMBOL:
|
395
|
case HEART_BEAT_ERROR_SYMBOL:
|
|
390
|
case ROOM_SN_CONNECTED:
|
396
|
case ROOM_SN_CONNECTED:
|
|
391
|
String msg1 = socketResponse.getCode() == HEART_BEAT_ERROR_SYMBOL ? "HEART_BEAT_ERROR_SYMBOL" : "ROOM_SN_CONNECTED";
|
397
|
String msg1 = socketResponse.getCode() == HEART_BEAT_ERROR_SYMBOL ? "HEART_BEAT_ERROR_SYMBOL" : "ROOM_SN_CONNECTED";
|
|
@@ -402,6 +408,7 @@ public class SocketService1 extends BaseService { |
|
@@ -402,6 +408,7 @@ public class SocketService1 extends BaseService { |
|
402
|
}
|
408
|
}
|
|
403
|
break;
|
409
|
break;
|
|
404
|
case CONTAIN_MESSAGE:
|
410
|
case CONTAIN_MESSAGE:
|
|
|
|
411
|
+ heartBeatErrorCount.set(0);
|
|
405
|
LogUtils.d(TAG, "CONTAIN_MESSAGE");
|
412
|
LogUtils.d(TAG, "CONTAIN_MESSAGE");
|
|
406
|
if (socketResponse.getData() != null) {
|
413
|
if (socketResponse.getData() != null) {
|
|
407
|
if (socketResponse.getCmd() == OPEN_DOOR) {
|
414
|
if (socketResponse.getCmd() == OPEN_DOOR) {
|