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().eq(Camera::getCode, cameraCode), false); Device device = deviceService.getById(camera.getDeviceId()); if (camera == null) return; List 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 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; } }