package com.tievd.jyz.handler.alg.common; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.tievd.jyz.cache.CarModelCache; import com.tievd.jyz.cache.DeviceCache; import com.tievd.jyz.entity.Device; import com.tievd.jyz.entity.TrafficFlow; import com.tievd.jyz.handler.alg.AlgHandleInterface; import com.tievd.jyz.mqtt.dto.EventInfoDTO; import com.tievd.jyz.service.ITrafficFlowService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.sql.Timestamp; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; /** * 车流量算法处理类 * @author timi */ @Slf4j @Component public class TrafficFlowAlgHandler implements AlgHandleInterface { /** 通用算法标识头 */ private final String ALG_TAG[] = new String[]{"C10004"}; @Autowired private ITrafficFlowService trafficFlowService; @Override public String[] getAlgTag(){ return ALG_TAG; } /** * 处理入口 * @param eventInfoDTO * @param sn * @param time */ @Override public void handle(EventInfoDTO 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; } //车流量检测 JSONArray carFlows = extend.getJSONArray("carFlow"); List flowList = new ArrayList<>(); for (Object o : carFlows) { JSONObject flow = (JSONObject) o; Integer carCount = flow.getIntValue("carCount"); String carModelCode = flow.getString("carModel"); if (CarModelCache.contains(carModelCode)){ log.info("包含不存在的车型:{}", carModelCode); continue; } TrafficFlow trafficFlow = new TrafficFlow() .setCameraCode(cameraCode) .setDeviceId(device.getId()) .setCarCount(carCount) .setModelCode(carModelCode) .setOrgCode(device.getOrgCode()) .setCaptureTime(new Timestamp(Long.parseLong(time))) .setCaptureDay(LocalDate.now().toString()) .generateId(); flowList.add(trafficFlow); } trafficFlowService.saveOrUpdateBatch(flowList); } }