peng
2026-03-24 ca41db25ab3da9ddd509b79fd783b60d2e66056f
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.tievd.jyz.controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tievd.cube.commons.annotations.DictApi;
import com.tievd.cube.commons.base.Result;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.mqtt.MqttMsgReceiver;
import com.tievd.jyz.mqtt.dto.SystemStatusDTO;
import com.tievd.jyz.util.MultiMinioUtil;
import com.tievd.jyz.util.StringCampareUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
@Slf4j
@DictApi
@RestController
@RequestMapping("/third")
public class ThirdController {
  
  @Autowired
  MqttMsgReceiver mqttMsgReceiver;
  
  @Value("${third.aibox.url:http://25.30.13.30}")
  String thirdBoxUrl;
  
  @Value("${third.aibox.url:/api/v1/notify/config}")
  String aiboxConfig;
  
  @Autowired
  MultiMinioUtil s3Util;
  
  @Autowired
  ApplicationContext context;
  
  /**
   * 第三方卸油 算法code map
   */
  static Map<Integer, String> algCodeMap = new HashMap<>();
  
  static {
    algCodeMap.put(23, "X10001");
    algCodeMap.put(24, "X10002");
    algCodeMap.put(25, "X10004");
    algCodeMap.put(26, "X10003");
    algCodeMap.put(27, "X10006");
    algCodeMap.put(28, "X10005");
    algCodeMap.put(29, "X10007");
    algCodeMap.put(30, "X10008");
  }
  
  //http://25.30.14.164:8080/cube/third/deviceHeart
  @PostMapping("/deviceHeart")
  public void deviceHeart(@RequestBody JSONObject param, HttpServletRequest request) {
    log.info("=====>接收到卸油aibox心跳", param.toJSONString());
    param.put("type", MqttMsgReceiver.REGISTER);
    param.put("time", System.currentTimeMillis());
    JSONObject data = new JSONObject();
    data.put("deviceType", "AIbox");
    data.put("version", param.get("soft_version"));
    data.put("name", "AIbox卸油设备");
    data.put("ip", request.getRemoteHost());
    data.put("port", 80);
    param.put("data", data);
  
    mqttMsgReceiver.dealCommandMsg(param.toJSONString());
  }
  
  @SneakyThrows
  @PostMapping("/oilOutEvent")
  public void oilOutEvent(@RequestBody JSONObject thirdData) {
    log.info("=====>接收到卸油aibox事件上报");
    JSONArray events = thirdData.getJSONArray("oil_results");
    if (CollectionUtils.isEmpty(events)) {
      log.info("=====>接收到卸油aibox事件异常--未包含完整信息");
      return;
    }
    JSONObject msgJson = new JSONObject();
    msgJson.put("time", System.currentTimeMillis());
    
    List eventList = new ArrayList();
    List cameraList = new ArrayList();
    for (int i = 0; i < events.size(); i++) {
      JSONObject event = events.getJSONObject(i);
      msgJson.putIfAbsent("sn", event.getString("serial_number"));
      try {
        //数据
        JSONObject convertEvent = converEvent(event);
        log.info("=====>转换卸油事件信息: {}", convertEvent.toJSONString());
        eventList.add(convertEvent);
        JSONObject camera = convertCamera(event);
        cameraList.add(camera);
      } catch (Exception e) {
        log.error("第三方数据转换异常", e);
      }
    }
    
    //上报摄像头数据--------
    msgJson.put("type", MqttMsgReceiver.BASE_UPDATE);
    msgJson.put("data", generateCameraMsg(cameraList));
    mqttMsgReceiver.dealCommandMsg(msgJson.toJSONString());
    Thread.sleep(5000);
    //上报摄像头状态--------
    msgJson.put("type", MqttMsgReceiver.HEARTBEAT);
    msgJson.put("data", new JSONObject(){{
      put("camera", cameraList);
      put("system", new SystemStatusDTO());
    }});
    mqttMsgReceiver.dealCommandMsg(msgJson.toJSONString());
    Thread.sleep(1000);
    //上报事件数据----------
    msgJson.put("type", MqttMsgReceiver.EVENT_INFO);
    msgJson.put("data", eventList);
    mqttMsgReceiver.dealEventMsg(msgJson.toJSONString());
  
  }
    
    /**
     * 摄像头数据
     * @param cameraList
     * @return
     */
  JSONObject generateCameraMsg(List cameraList) {
    JSONObject cameraMsg = new JSONObject();
    cameraMsg.put("mode", "inc");
    cameraMsg.put("exist", new JSONObject(){{
      put("cameras", cameraList);
    }});
    return cameraMsg;
  }
  
  public JSONObject convertCamera(JSONObject event) {
    JSONObject camera = new JSONObject();
    //主要数据
    camera.put("code", event.getString("IPC_serial_num"));
    camera.put("name", event.getString("channel_name"));
    camera.put("ip", event.getString("IPC_addr"));
    camera.put("rtsp", event.getString("IPC_addr"));
    camera.put("installAddress", SystemConstant.INSTALL_ADDRESS_OIL_OUT);
    camera.put("status", SystemConstant.DEVICE_ONLINE);
    camera.put("updateTime", new Date().getTime());
    return camera;
  }
    
    /**
     * 事件信息
     * @param event
     * @return
     */
  JSONObject converEvent(JSONObject event){
    JSONObject convertEvent = new JSONObject();
    //主要数据
    convertEvent.put("eventCode", event.getString("process_id") + event.getString("step_id"));
  
    //上传信息中无车牌号,暂时使用第三方的processId作为record id传入记录中
    convertEvent.put("id", event.getString("process_id").substring(2));
    convertEvent.put("eventType", event.getString("step_status"));
    convertEvent.put("algCode", algCodeMap.get(event.getIntValue("step_type")));
    convertEvent.put("cameraCode", event.getString("IPC_serial_num"));
    convertEvent.put("code", event.getString("IPC_serial_num"));
    convertEvent.put("cameraName", event.getString("channel_name"));
    convertEvent.put("cameraIp", event.getString("IPC_addr"));
    //采集时间,UTC 格式,单位s
    long time = event.getLong("timestamp") + 8 * 60 * 60;
    convertEvent.put("alarmTime", time);
    
    //转s3地址
    String imgType = event.getJSONObject("big_image").getString("ImageType");
    String imgBase = event.getJSONObject("big_image").getString("Image");
    byte[] imgByte = Base64.getDecoder().decode(imgBase);
    String parentPath = "oilout/";
    String key = s3Util.upload(imgByte, parentPath + convertEvent.getString("eventCode") + "." + imgType);
    String url = s3Util.getBucketName() + SystemConstant.s3_split_char + key;
    convertEvent.put("sourceType", imgType);
    convertEvent.put("imgPath", url);
    JSONObject extend = new JSONObject();
    extend.putAll(convertEvent);
    convertEvent.put("extend", extend);
    return convertEvent;
  
  }
  
  
  @SneakyThrows
  @PostMapping("/reportVideo")
  public void reportVideo(HttpServletRequest request) {
    log.info("=====>接收到卸油aibox视频上报");
//    String key = s3Util.upload(file.getBytes(), file.getName());
//    String url = s3Util.getBucketName() + SystemConstant.s3_split_char + key;
  
    JSONObject data = new JSONObject();
    data.put("type", MqttMsgReceiver.CUT_VIDEO);
    //todo 缺少事件基础数据
    
    
    
    mqttMsgReceiver.dealCommandMsg("");
  }
  
  
  @GetMapping("/aiboxConfig")
  Result<JSONObject> getAiboxConfig() {
    JSONObject config = new RestTemplate().getForObject(thirdBoxUrl + aiboxConfig, JSONObject.class);
    return Result.ok(config);
  }
  
  
  @PutMapping("/aiboxConfig")
  Result<JSONObject> putAiboxConfig(JSONObject putConfig) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.put(thirdBoxUrl + aiboxConfig, putConfig);
    JSONObject config = restTemplate.getForObject(thirdBoxUrl + aiboxConfig, JSONObject.class);
    return Result.ok(config);
  }
    
    @GetMapping("/grade/{grade}")
    Result<String> gradeSet(@PathVariable int grade) {
        StringCampareUtil.grade = grade;
        return Result.ok("成功");
    }
    
    //调试接口
    @GetMapping("/similar")
    Result<?> similarGet() {
        return Result.ok(StringCampareUtil.similarMap);
    }
    
    @GetMapping("/similar/put/{sameStr}")
    Result<?> similarPut(@PathVariable String sameStr) {
        char key = sameStr.charAt(0);
        StringCampareUtil.similarMap.put(key, sameStr);
        return Result.ok("成功");
    }
    
    @PutMapping("/similar")
    Result<?> similarPut(@RequestBody Map<Character, String> sameMap) {
        StringCampareUtil.similarMap.putAll(sameMap);
        return Result.ok(StringCampareUtil.similarMap);
    }
  
}