zxl
2026-03-20 ee8b4f41b25d510eda0b1c7ec553b73f6d7dd7e6
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
package com.tievd.jyz.handler.alg.common;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tievd.cube.modules.system.entity.SysDepart;
import com.tievd.jyz.cache.AlgorithmCache;
import com.tievd.jyz.cache.DepartCache;
import com.tievd.jyz.cache.DeviceCache;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.entity.*;
import com.tievd.jyz.handler.alg.AlgHandleInterface;
import com.tievd.jyz.mapper.CameraMapper;
import com.tievd.jyz.mapper.OilEventMapper;
import com.tievd.jyz.mapper.SpotcheckEventMapper;
import com.tievd.jyz.mqtt.command.MqttCommandReceiver;
import com.tievd.jyz.mqtt.dto.EventInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.sql.Timestamp;
import java.time.LocalDate;
 
/**
 * 通用异常算法处理类
 * @author timi
 */
@Slf4j
@Component
public class CommonAlarmAlgHandler implements AlgHandleInterface {
 
    /** 通用算法标识头 */
    private final String[] ALG_TAG = new String[]{
            "C10002",
            "C10003",
            "C10005",
            "C10006",
            "C20001",
            "C20002",
            "C30001",
            "C30002",
            "C30003",
            "C30004",
            "C20003",
            "C20004",
            "C20005",
            "C20006",
            "C20007",
            "C20008",
            "C20009",
            "C20010",
            "C20011",
            "S10001",
            "S10002"
    };
 
    @Autowired
    private OilEventMapper oilEventMapper;
    
    @Autowired
    private SpotcheckEventMapper spotcheckEventMapper;
 
    @Autowired
    private CameraMapper cameraMapper;
 
    @Autowired
    private MqttCommandReceiver mqttCommandReceiver;
    @Value("${init.sendCommandTopic:/ty/aibox/command/}")
    private String sendCommandTopic;
 
    @Override
    public String[] getAlgTag(){
        return ALG_TAG;
    }
 
    /**
     * 处理入口
     * @param eventInfoDTO
     * @param sn
     * @param time
     */
    @Override
    public void handle(EventInfoDTO<JSONObject> eventInfoDTO, String sn,String time) {
        //当前事件唯一标识
        String eventCode = eventInfoDTO.getEventCode();;
        //检测算法编码
        String algCode = eventInfoDTO.getAlgCode();
        //终端编码
        String cameraCode = eventInfoDTO.getCameraCode();
        //扩展属性
        JSONObject extend = new JSONObject();
        Object extendObj = eventInfoDTO.getExtend();
        if(extendObj instanceof String){
            extend = JSONObject.parseObject(extendObj.toString());
        }else if(extendObj instanceof JSONObject){
            extend = (JSONObject)extendObj;
        }
        log.info("通用异常事件 eventCode:{},extend:{}",eventCode,extendObj);
        Device device = DeviceCache.getDeviceBySn(sn);
        if(ObjectUtil.isNull(device)){
            log.error("网关设备不存在,sn:{}",sn);
            return;
        }
        alarmHandle(eventCode,algCode,cameraCode,time,extend,device);
    }
 
 
    /**
     * 异常告警处理
     * @param eventCode
     * @param algCode
     * @param cameraCode
     * @param time
     * @param extend
     * @param device
     */
    private void alarmHandle(String eventCode,String algCode, String cameraCode,String time,JSONObject extend,Device device){
        JSONArray carArr = extend.getJSONArray("cars");
        if(carArr.size() == 0){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("number","");
            carArr.add(jsonObject);
        }
        for(Object obj: carArr){
            JSONObject infoObj = (JSONObject) obj;
            String plateNumber = infoObj.getString("number");
            OilEvent oilEvent = oilEventGen(eventCode,algCode,cameraCode,time,device);
            if(ObjectUtil.isNotNull(oilEvent)){
                if (algCode.contains("S")) {
                    SpotcheckEvent spotcheckEvent = BeanUtil.toBean(oilEvent, SpotcheckEvent.class);
                    spotcheckEventMapper.insert(spotcheckEvent);
                } else {
                    oilEvent.setLicenseNum(plateNumber);
                    oilEventMapper.insert(oilEvent);
                }
                
            }
        }
        //默认自动请求
        //请求图片和短视频
//        log.info("请求图片和短视频,eventCode:{}",eventCode);
//        EventResourceDTO eventResourceDTO = new EventResourceDTO(SystemConstant.ALL_SOURCE_TYPE,eventCode,cameraCode);
//        EventResourceCommand eventResourceCommand = new EventResourceCommand(mqttCommandReceiver,eventResourceDTO,sendCommandTopic,device.getSn());
//        eventResourceCommand.init();
//        if(eventResourceCommand.execute() == SystemConstant.DEAL_FAIL){
//            log.error("事件图片视频指令发送失败");
//        }
    }
 
    /**
     * 异常时间对象生成
     * @param eventCode
     * @param algCode
     * @param cameraCode
     * @param time
     * @param device
     * @return
     */
    private OilEvent oilEventGen(String eventCode,String algCode,String cameraCode,String time,Device device){
        Camera camera = cameraMapper.selectOne(new QueryWrapper<Camera>().eq("code",cameraCode).eq("device_id",device.getId()));
        if(ObjectUtil.isNull(camera)){
            log.error("平台终端不存在,code:{}",cameraCode);
            return null;
        }
        OilEvent oilEvent = new OilEvent();
        oilEvent.setAlgorithmCode(algCode);
        SysAlgorithmItem sysAlgorithmItem = AlgorithmCache.getAlgorithmMap(algCode);
        if(ObjectUtil.isNotNull(sysAlgorithmItem)){
            oilEvent.setAlgorithmName(sysAlgorithmItem.getAlgorithmName());
        }
        oilEvent.setEventAddress(camera.getInstallAddress());
        oilEvent.setCameraCode(camera.getCode());
        oilEvent.setCameraName(camera.getName());
        oilEvent.setDeviceId(device.getId());
        oilEvent.setDeviceName(device.getName());
        oilEvent.setOrgCode(device.getOrgCode());
        oilEvent.setEventDay(LocalDate.now().format(SystemConstant.DATE_DAY_FORMATTER));
        SysDepart sysDepart = DepartCache.getDepartByOrgCode(device.getOrgCode());
        if(ObjectUtil.isNotNull(sysDepart)){
            oilEvent.setOrgName(sysDepart.getDepartName());
        }
        oilEvent.setEventTime(new Timestamp(Long.parseLong(time)));
        oilEvent.setImgUid(eventCode);
        oilEvent.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return oilEvent;
    }
}