zxl
2026-03-25 0b39edb68acc67ed01fbfe5d31bfa776a1b17de1
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
package com.tievd.jyz.handler;
 
import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.dto.CaptureVehicleDTO;
import com.tievd.jyz.entity.Camera;
import com.tievd.jyz.entity.Device;
import com.tievd.jyz.mapper.OilEventMapper;
import com.tievd.jyz.mapper.OilPositionMapper;
import com.tievd.jyz.mapper.OilRecordMapper;
import com.tievd.jyz.mapper.SysCarModelMapper;
import com.tievd.jyz.mqtt.MqttMsgReceiver;
import com.tievd.jyz.mqtt.dto.EventInfoDTO;
import com.tievd.jyz.mqtt.dto.MqttParamDTO;
import com.tievd.jyz.service.ICameraService;
import com.tievd.jyz.service.ICarInfoService;
import com.tievd.jyz.service.IDeviceService;
import com.tievd.jyz.util.MultiMinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
 
/**
 * 事件接收处理类
 * @author timi
 */
@Slf4j
@Component
public class CameraIdentifyHandler {
    
    @Autowired
    private OilEventMapper oilEventMapper;
    @Autowired
    private ICameraService cameraService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private OilRecordMapper oilRecordMapper;
    @Autowired
    private ICarInfoService carInfoService;
    @Autowired
    private SysCarModelMapper sysCarModelMapper;
    @Autowired
    private OilPositionMapper oilPositionMapper;
    
    @Autowired
    MultiMinioUtil s3Util;
    
    @Autowired
    EventInfoHandler eventInfoHandler;
    
    public void handle(CaptureVehicleDTO param){
        log.info("====================>开始处理摄像头抓取数据");
        assert param.getSource() != null;
        assert param.getVehicles() != null;
        String cameraCode = param.getSource().getId();
        Camera camera = cameraService.getOne(new LambdaQueryWrapper<Camera>().eq(Camera::getCode, cameraCode), false);
        Device device = deviceService.getById(camera.getDeviceId());
        if (camera == null) return;
    
        List<JSONObject> cars = new ArrayList<>();
        for (CaptureVehicleDTO.Vehicle vehicle : param.getVehicles()) {
            if (StringUtils.isBlank(vehicle.getLicense()) || StringUtils.isBlank(vehicle.getImageData())) {
                continue;
            }
            JSONObject car = converEvent(vehicle);
            cars.add(car);
        }
        if (cars.size() == 0) {
            log.info("====================>摄像头抓取数据车辆信息有缺失, 结束处理");
            return;
        }
        
        log.info("====================>对摄像头抓取数据进行转换");
        //中间层
        EventInfoDTO eventInfoDTO = new EventInfoDTO();
        eventInfoDTO.setAlgCode("C10001");
        eventInfoDTO.setCameraCode(cameraCode);
        String eventCode = new Snowflake().nextIdStr();
        eventInfoDTO.setEventCode(eventCode);
        eventInfoDTO.setExtend(cars);
        List<EventInfoDTO> eventList = new ArrayList<>();
        eventList.add(eventInfoDTO);
        //最外层
        MqttParamDTO mqttDto = new MqttParamDTO();
        mqttDto.setSn(device.getSn());
        mqttDto.setTime(String.valueOf(param.getDate().getTime()));
        mqttDto.setData(eventList);
        mqttDto.setType(MqttMsgReceiver.EVENT_INFO);
        eventInfoHandler.event(mqttDto);
        log.info("====================>摄像头抓取数据载入主流程");
    }
    
    
    JSONObject converEvent(CaptureVehicleDTO.Vehicle vehicle){
        JSONObject convertEvent = new JSONObject();
        //主要数据
        convertEvent.put("number", vehicle.getLicense());
        convertEvent.put("score", "0.9");
        convertEvent.put("type", vehicle.getExtend().getModel());
        convertEvent.put("color", vehicle.getColor());
        
        //多余字段
        convertEvent.put("licenseColor", vehicle.getLicenseColor());
        convertEvent.put("licenseType", vehicle.getLicenseType());
        
        //转s3地址
        byte[] imgByte = Base64.getDecoder().decode(vehicle.getImageData());
        String parentPath = "cameraPhoto/" + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE) + "/";
        String key = s3Util.upload(imgByte, parentPath + convertEvent.getString("eventCode"));
        String url = s3Util.getBucketName() + SystemConstant.s3_split_char + key;
        convertEvent.put("sourceType", "jpg");
        convertEvent.put("imgPath", url);
        JSONObject extend = new JSONObject();
        extend.putAll(convertEvent);
        convertEvent.put("extend", extend);
        return convertEvent;
        
    }
    
    
    
}