xuhy
2026-04-22 746517aca9d0b7ae334c70b3041a9b1de795addb
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.xiaopeng.vehicleConvoy.controller;
 
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.xiaopeng.api.dto.vehicleConvoy.VehicleTemperatureDto;
import com.xiaopeng.api.vo.vehicleConvoy.VehicleStateVo;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.vehicleConvoy.model.dto.CarControlCallbackDTO;
import com.xiaopeng.vehicleConvoy.service.AsyncVehicleStateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
/**
 * 控车回调接口
 */
@Slf4j
@RestController
@RequestMapping("/logan/api/v1")
public class CarControlCallbackController {
 
    @Resource
    private AsyncVehicleStateService asyncVehicleStateService;
    private RedisService redisService;
 
    /**
     * 车控指令执行的回调接口
     *
     * @param request 车控指令请求
     * @return 车控指令响应
     */
    @PostMapping("/vehicles/command/callback")
    public R<String> callback(@RequestBody CarControlCallbackDTO request) {
        if (request == null) {
            throw new ServiceException("请求参数缺失");
        }
        log.info("接收到回调:{}", JSONUtil.toJsonStr(request));
 
        try {
            // 处理车辆状态回调, 19指令处理回调,其他暂不处理
            if (request.getMsgRef() != null && request.getMsgContent() != null && request.getServiceType() != null && request.getServiceType() == 19) {
                // 尝试解析车辆状态数据
                VehicleTemperatureDto stateData = parseVehicleStateFromCallback(request);
                if (stateData != null) {
                    // 通知异步服务处理回调
                    asyncVehicleStateService.handleStateCallback(request.getMsgRef(), stateData);
                }
                // 尝试解析车辆空调数据
                parseAirConditioningSwitchFromCallback(request);
            }
        } catch (Exception e) {
            log.error("处理车辆状态回调异常: msgId={}", request.getMsgRef(), e);
        }
 
        return R.ok(JSONUtil.toJsonStr(request));
    }
 
    /**
     * 从回调消息中解析车辆状态数据
     */
    private VehicleTemperatureDto parseVehicleStateFromCallback(CarControlCallbackDTO callback) {
        try {
            if (callback.getMsgContent() == null) {
                return null;
            }
 
            log.debug("解析车辆状态数据: msgId={}, content={}", callback.getMsgRef(), JSON.toJSONString(callback.getMsgContent()));
 
            // 解析msgContent为Map结构
            Map<String, Object> msgContent = (Map<String, Object>) callback.getMsgContent();
 
            // 检查返回码
            Integer code = (Integer) msgContent.get("code");
            if (code == null || code != 200) {
                log.warn("车辆状态回调返回错误码: msgId={}, code={}", callback.getMsgRef(), code);
                return null;
            }
 
            // 获取instant数据
            Map<String, Object> instant = (Map<String, Object>) msgContent.get("instant");
            if (instant == null) {
                log.warn("回调数据中缺少instant字段: msgId={}", callback.getMsgRef());
                return null;
            }
 
            // 创建VehicleTemperatureDto对象
            VehicleTemperatureDto stateData = new VehicleTemperatureDto();
            Map<String, VehicleStateVo> stateMap = new HashMap<>();
 
            // 解析door信息
            Map<String, Object> door = (Map<String, Object>) instant.get("door");
            if (door != null) {
                //存入缓存用于后续使用
                redisService.setCacheObject("DOOR:" + callback.getMsgId(), JSON.toJSONString(door), 30L, TimeUnit.SECONDS);
                // 获取trunk_ajar_st_new值
                Object trunkAjarStNewObj = door.get("trunk_ajar_st_new");
                if (trunkAjarStNewObj != null) {
                    VehicleStateVo trunkState = new VehicleStateVo();
                    trunkState.setValue(trunkAjarStNewObj.toString());
                    stateMap.put("trunkAjarCloseSt", trunkState); // 保持原有的key名称兼容
 
                    // 同时添加新的key名称
                    stateMap.put("trunk_ajar_st_new", trunkState);
 
                    log.info("解析到后备箱状态: msgId={}, trunk_ajar_st_new={}", callback.getMsgRef(), trunkAjarStNewObj);
                }
            }
            stateData.setStateMap(stateMap);
            return stateData;
 
        } catch (Exception e) {
            log.warn("解析车辆状态数据失败: msgId={}", callback.getMsgRef(), e);
            return null;
        }
    }
 
 
    /**
     * 从回调消息中解析车辆空调状态数据
     */
    private VehicleTemperatureDto parseAirConditioningSwitchFromCallback(CarControlCallbackDTO callback) {
        try {
            if (callback.getMsgContent() == null) {
                return null;
            }
 
            log.debug("解析空调状态数据: msgId={}, content={}", callback.getMsgRef(), JSON.toJSONString(callback.getMsgContent()));
 
            // 解析msgContent为Map结构
            Map<String, Object> msgContent = (Map<String, Object>) callback.getMsgContent();
 
            // 检查返回码
            Integer code = (Integer) msgContent.get("code");
            if (code == null || code != 200) {
                log.warn("空调状态回调返回错误码: msgId={}, code={}", callback.getMsgRef(), code);
                return null;
            }
 
            // 获取instant数据
            Map<String, Object> instant = (Map<String, Object>) msgContent.get("instant");
            if (instant == null) {
                log.warn("回调数据中缺少instant字段: msgId={}", callback.getMsgRef());
                return null;
            }
 
            // 创建VehicleTemperatureDto对象
            VehicleTemperatureDto stateData = new VehicleTemperatureDto();
            Map<String, VehicleStateVo> stateMap = new HashMap<>();
 
            // 解析hvac信息
            Map<String, Object> hvac = (Map<String, Object>) instant.get("hvac");
            if (hvac != null) {
                log.info("解析到空调: msgId={}, hvac_on={}", callback.getMsgRef(), JSON.toJSONString(hvac));
                //存入缓存用于后续使用
                redisService.setCacheObject("HVAC:" + callback.getMsgId(), JSON.toJSONString(hvac), 30L, TimeUnit.SECONDS);
            }
            stateData.setStateMap(stateMap);
            return stateData;
        } catch (Exception e) {
            log.warn("解析空调状态数据失败: msgId={}", callback.getMsgRef(), e);
            return null;
        }
    }
}