ke
2024-05-13 659d3e60c6fe5e5bcd8c4cbb3dd8077e29f13a38
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
package cn.gistack.sm.hk.service;
 
import cn.gistack.sm.hk.constant.HkEventConstant;
import cn.gistack.sm.hk.entity.HkEvent;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
@Component
@Slf4j
public class AsyncHkEventService {
 
    @Autowired
    private HkEventService hkEventService;
 
    /**
     * 异步加载海康视频事件
     * @param jsonData
     */
    @Async
    public void saveHkEventInfo(String jsonData){
        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        String params = jsonObject.get("params").toString();
        JSONObject parseObject = JSONObject.parseObject(params);
        JSONArray eventsArray = parseObject.getJSONArray("events");
        Date sendTime = parseObject.getDate("sendTime");
        for (int i = 0; i < eventsArray.size(); i++) {
            HkEvent hkEvent = new HkEvent();
            hkEvent.setEventId(eventsArray.getJSONObject(i).getString("eventId"));
            hkEvent.setEventType(eventsArray.getJSONObject(i).getLong("eventType"));
            hkEvent.setSrcIndex(eventsArray.getJSONObject(i).getString("srcIndex"));
            hkEvent.setSrcName(eventsArray.getJSONObject(i).getString("srcName"));
            hkEvent.setSrcType(eventsArray.getJSONObject(i).getString("srcType"));
            hkEvent.setSrcParentIndex(eventsArray.getJSONObject(i).getString("srcParentIndex"));
            hkEvent.setTimeout(eventsArray.getJSONObject(i).getLong("timeout"));
            hkEvent.setHappenTime(eventsArray.getJSONObject(i).getDate("happenTime"));
            hkEvent.setStatus(eventsArray.getJSONObject(i).getInteger("status"));
            hkEvent.setEventLvl(eventsArray.getJSONObject(i).getInteger("eventLvl"));
            hkEvent.setCreateTime(new Date());
            hkEvent.setSendTime(sendTime);
            hkEvent.setImageUrl(setImageUrl(eventsArray,i));
            // 保存
            hkEventService.save(hkEvent);
        }
    }
 
    /**
     * 设置图片路径
     * @param eventsArray
     * @param i
     * @return
     */
    private String setImageUrl(JSONArray eventsArray, int i) {
        String eventType = eventsArray.getJSONObject(i).getLong("eventType").toString();
        // 如果是非算法事件处理
        if (HkEventConstant.videoList.contains(eventType)){
            String columns = HkEventConstant.videoMap.get(eventType);
            if (!Strings.isBlank(columns)){
                String data = eventsArray.getJSONObject(i).getString("data");
                JSONObject parseObject = JSONObject.parseObject(data);
                JSONArray columnsJSONArray = parseObject.getJSONArray(columns);
                if (null!= columnsJSONArray && columnsJSONArray.size()>0) {
//                    log.info("img:" + columnsJSONArray.getJSONObject(0).getString("imageUrl"));
                    return columnsJSONArray.getJSONObject(0).getString("imageUrl");
                }
            }
            return null;
        }
        // 算法事件处理
        if (HkEventConstant.artVideoList.contains(eventType)){
//            log.info("img:" + eventsArray.getJSONObject(i).getString("refrencePicUrl"));
            return eventsArray.getJSONObject(i).getString("refrencePicUrl");
        }
        return null;
    }
}