TRX 1 rok temu
rodzic
commit
e6a87bc209

+ 17 - 11
src/main/java/com/zswl/dataservice/service/mqtt/DevicePingInfoService.java

@@ -141,19 +141,27 @@ public class DevicePingInfoService extends SuperService {
             list.parallelStream().forEach(deviceInfo -> {
                 // 默认离线
                 OnLineState onLineState = OnLineState.OffLine;
-                List<DevicePingInfo> _list = devicePingInfoDao.findTop5ByDeviceIdOrderByCreateTimeDesc(
-                        deviceInfo.getDeviceId());
-                if (_list != null && _list.size() > 2) {
-                    Long firstTime = _list.get(0).getCreateTime();
-                    Long lastTime = _list.get(_list.size() - 1).getCreateTime();
-                    // 2 次心跳间隔时间
-                    long avgTime = ((firstTime - lastTime) / (_list.size() - 1)) * 2;
-                    if (avgTime > maxPingTime) {
+                List<DevicePingInfo> _list = devicePingInfoDao.findTop5ByDeviceIdOrderByCreateTimeDesc(deviceInfo.getDeviceId());
+                if (_list != null) {
+                    Long firstTime = null;
+                    long avgTime = 0;
+                    if (_list.size() == 1) {
+                        firstTime = _list.get(0).getCreateTime();
                         avgTime = maxPingTime;
+                    } else if (_list.size() > 2) {
+                        firstTime = _list.get(0).getCreateTime();
+                        Long lastTime = _list.get(_list.size() - 1).getCreateTime();
+                        // 2 次心跳间隔时间
+                        avgTime = ((firstTime - lastTime) / (_list.size() - 1)) * 2;
+                        if (avgTime > maxPingTime) {
+                            avgTime = maxPingTime;
+                        }
                     }
                     // 根据ping数据判断是否在线
                     if ((time - firstTime) > avgTime) {
                         onLineState = OnLineState.OffLine;
+                    } else {
+                        onLineState = OnLineState.OnLine;
                     }
                 }
                 log.info("{} {}", deviceInfo.getDeviceId(), onLineState);
@@ -178,9 +186,7 @@ public class DevicePingInfoService extends SuperService {
             long time = System.currentTimeMillis();
             list.parallelStream().forEach(gateWayInfo -> {
                 OnLineState onLineState = OnLineState.OnLine;
-                List<DevicePingInfo> _list = devicePingInfoDao.findTop5ByGateWayIdAndPingTypeOrderByCreateTimeDesc(
-                        gateWayInfo.getGateWayId(), DeviceType.GateWay.name()
-                );
+                List<DevicePingInfo> _list = devicePingInfoDao.findTop5ByGateWayIdAndPingTypeOrderByCreateTimeDesc(gateWayInfo.getGateWayId(), DeviceType.GateWay.name());
                 if (_list != null && _list.size() > 2) {
                     Long firstTime = _list.get(0).getCreateTime();
                     Long lastTime = _list.get(_list.size() - 1).getCreateTime();