TRX пре 1 година
родитељ
комит
8503a778f6

+ 4 - 1
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/mqtt/GateWayInfoSearchParam.java

@@ -18,5 +18,8 @@ public class GateWayInfoSearchParam extends SuperSearchParam {
     private String gateWayName;
 
     @Schema(description = "设备在线状态")
-    OnLineState state;
+    private OnLineState onLineState;
+
+    @Schema(description = "名称")
+    private String name;
 }

+ 8 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/mqtt/impl/GateWayInfoDaoImpl.java

@@ -44,12 +44,20 @@ public class GateWayInfoDaoImpl extends BaseImpl implements GateWayInfoDaoExtend
             criteria.and("epId").is(param.getEpId());
         }
 
+        if (param.getOnLineState() != null) {
+            criteria.and("onLineState").is(param.getOnLineState());
+        }
+
         // 模糊搜索
         List<Criteria> criterias = new ArrayList<>();
         if (StringUtils.isNotEmpty(param.getGateWayName())) {
             Pattern pattern = Pattern.compile("^.*" + param.getGateWayName() + ".*$");
             criterias.add(Criteria.where("gateWayName").is(pattern));
         }
+        if (StringUtils.isNotEmpty(param.getName())) {
+            Pattern pattern = Pattern.compile("^.*" + param.getName() + ".*$");
+            criterias.add(Criteria.where("gateWayName").is(pattern));
+        }
         if (!CollectionUtils.isEmpty(criterias)) {
             criteria.andOperator(criterias.toArray(new Criteria[]{}));
         }

+ 27 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/mqtt/DevicePingInfoService.java

@@ -184,8 +184,34 @@ public class DevicePingInfoService extends SuperService {
         if (ObjectUtils.isNotEmpty(list)) {
             long time = System.currentTimeMillis();
             list.parallelStream().forEach(gateWayInfo -> {
-                OnLineState onLineState = OnLineState.OnLine;
                 List<DevicePingInfo> _list = devicePingInfoDao.findTop5ByGateWayIdAndPingTypeOrderByCreateTimeDesc(gateWayInfo.getGateWayId(), DeviceType.GateWay.name());
+                // 默认离线
+                OnLineState onLineState = OnLineState.OffLine;
+                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;
+                        }
+                    }
+                    if (firstTime != null) {
+                        // 根据ping数据判断是否在线
+                        if ((time - firstTime) > avgTime) {
+                            onLineState = OnLineState.OffLine;
+                        } else {
+                            onLineState = OnLineState.OnLine;
+                        }
+                    }
+                }
+
                 if (_list != null && _list.size() > 2) {
                     Long firstTime = _list.get(0).getCreateTime();
                     Long lastTime = _list.get(_list.size() - 1).getCreateTime();