فهرست منبع

离线判断优化

zengy 3 روز پیش
والد
کامیت
f17bfda4c7

+ 7 - 4
zk-api-service/src/main/java/com/zksy/api/mapper/CollectionLastReceiptMapper.java

@@ -11,7 +11,10 @@ import java.util.List;
 
 public interface CollectionLastReceiptMapper {
     /**
-     * 仅恢复本类采集表里有记录且台账仍在线的设备;表名和列名均为固定白名单。
+     * 恢复本类采集表里有记录的设备最后采集时间;表名和列名均为固定白名单。
+     *
+     * <p>不能只查在线设备:设备被误判离线后,定时任务仍需根据历史采集时间
+     * 判断是否可以自动恢复在线。</p>
      */
     @Select({"<script>",
             "SELECT r.device_code, MAX(r.received_at) AS last_receive_time FROM (",
@@ -27,12 +30,12 @@ public interface CollectionLastReceiptMapper {
             "<otherwise>SELECT CAST(NULL AS varchar) AS device_code, CAST(NULL AS timestamp) AS received_at WHERE 1=0</otherwise>",
             "</choose>",
             ") r WHERE r.received_at IS NOT NULL AND EXISTS (",
-            "SELECT 1 FROM app_user.equipment_base b JOIN app_user.equipment_status s ON s.equipment_id=b.equipment_id",
-            "WHERE b.equipment_code=r.device_code AND s.online_status=1 AND COALESCE(b.del_flag,'0')='0' AND COALESCE(s.del_flag,'0')='0'",
+            "SELECT 1 FROM app_user.equipment_base b",
+            "WHERE b.equipment_code=r.device_code AND COALESCE(b.del_flag,'0')='0'",
             ") GROUP BY r.device_code",
             "</script>"})
     @Results({@Result(column = "device_code", property = "deviceCode"),
             @Result(column = "last_receive_time", property = "lastReceiveTime")})
     @Options(timeout = 15)
-    List<CollectionLastReceipt> selectOnlineLastReceipts(@Param("collector") String collector);
+    List<CollectionLastReceipt> selectLastReceipts(@Param("collector") String collector);
 }

+ 16 - 2
zk-api-service/src/main/java/com/zksy/api/scheduling/RefreshableOfflineCheckTask.java

@@ -139,7 +139,7 @@ public abstract class RefreshableOfflineCheckTask implements EnvironmentAware, A
         int timeoutMinutes = effectiveOfflineTimeoutMinutes();
         log.info("离线巡检开始: collector={}, timeoutMinutes={}, memoryDevices={}", collector(), timeoutMinutes, deviceLastReceiveTimeMap.size());
         if (lastReceiptService != null) {
-            lastReceiptService.restore(collector(), deviceLastReceiveTimeMap, offlineDeviceSet);
+            lastReceiptService.restore(collector(), deviceLastReceiveTimeMap);
         }
         Date now = new Date();
         long timeoutMs = (long) timeoutMinutes * 60 * 1000;
@@ -147,7 +147,21 @@ public abstract class RefreshableOfflineCheckTask implements EnvironmentAware, A
             synchronized (deviceLock(entry.getKey())) {
                 // 与在线恢复串行,且重新读取时间,避免旧快照把刚收到上报的设备置离线。
                 Date latest = deviceLastReceiveTimeMap.get(entry.getKey());
-                if (latest == null || now.getTime() - latest.getTime() <= timeoutMs) continue;
+                if (latest == null) {
+                    continue;
+                }
+                if (now.getTime() - latest.getTime() <= timeoutMs) {
+                    // 离线设备若最后采集时间仍在超时窗口内(如超时参数调大),自动恢复在线。
+                    if (offlineDeviceSet.remove(entry.getKey())) {
+                        if (updateDeviceOnlineStatus(entry.getKey(), 1)) {
+                            onlineDeviceSet.add(entry.getKey());
+                            log.info("设备 {} 已恢复在线(最后采集时间在{}分钟超时窗口内)", entry.getKey(), timeoutMinutes);
+                        } else {
+                            offlineDeviceSet.add(entry.getKey());
+                        }
+                    }
+                    continue;
+                }
                 if (offlineDeviceSet.add(entry.getKey())) {
                     if (!updateDeviceOnlineStatus(entry.getKey(), 0)) {
                         offlineDeviceSet.remove(entry.getKey());

+ 2 - 6
zk-api-service/src/main/java/com/zksy/api/service/CollectionLastReceiptService.java

@@ -7,7 +7,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import java.util.Date;
 import java.util.List;
-import java.util.Set;
 import java.util.concurrent.ConcurrentMap;
 
 @Slf4j
@@ -16,18 +15,15 @@ public class CollectionLastReceiptService {
     @Autowired private CollectionLastReceiptMapper mapper;
 
     /** 每轮重查数据库,补齐重启丢失的设备,并合并其他实例已保存的更新上报。 */
-    public void restore(String collector, ConcurrentMap<String, Date> received,
-                        Set<String> offlineDevices) {
+    public void restore(String collector, ConcurrentMap<String, Date> received) {
         try {
-            List<CollectionLastReceipt> rows = mapper.selectOnlineLastReceipts(collector);
+            List<CollectionLastReceipt> rows = mapper.selectLastReceipts(collector);
             if (rows == null) return;
             for (CollectionLastReceipt row : rows) {
                 if (row == null || row.getDeviceCode() == null || row.getDeviceCode().trim().isEmpty()
                         || row.getLastReceiveTime() == null) continue;
                 received.merge(row.getDeviceCode(), row.getLastReceiveTime(), (local, saved) ->
                         local.after(saved) ? local : saved);
-                // 数据库仍是在线时不能仅因本地已处理过离线而永久跳过。
-                offlineDevices.remove(row.getDeviceCode());
             }
         } catch (Exception e) {
             log.error("恢复离线检测名单失败,本轮继续检查本地已知设备,下轮重试: collector={}", collector, e);