Xiajijian
2025-11-26 d85eddf40bc7d5db0c8f4ec6cd74ade775ff2c57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.ard.work.domain.cache;
 
import com.ard.work.domain.dto.state.UavState;
import com.ard.work.domain.dto.telemetry.TelemetryData;
 
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 无人机遥测数据缓存
 *
 * @author 刘苏义
 * @date 2024/10/24 8:55
 */
public class UavTelemetryDataCache {
 
    // 使用 ConcurrentHashMap 来缓存无人机遥测数据,key 是 boxSn(无人机序列号)
    private static final ConcurrentHashMap<String, TelemetryData> cache = new ConcurrentHashMap<>();
 
    // 存储无人机遥测数据到缓存
    public static void put(TelemetryData telemetryData) {
        cache.put(telemetryData.getBoxSn(), telemetryData);
    }
 
    // 从缓存中获取无人机遥测数据
    public static TelemetryData get(String boxSn) {
        return cache.get(boxSn);
    }
 
    // 删除缓存中的无人机遥测数据
    public static void remove(String boxSn) {
        cache.remove(boxSn);
    }
 
    // 定时任务使用:判断状遥测数据是否变化,但不更新缓存
    public static boolean hasChanged(String boxSn, TelemetryData telemetryData) {
        TelemetryData oldTelemetryData= cache.get(boxSn);
        if (oldTelemetryData == null) {
            return true; // 没有缓存,算作变化
        }
        return !oldTelemetryData.equals(telemetryData); // true = 有变化
    }
}