peng
2026-03-18 e59a0201057ba67cad425fed804c82ff4ba0c6f1
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
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<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;
        }
        //车流量检测
        JSONArray carFlows = extend.getJSONArray("carFlow");
        List<TrafficFlow> 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);
    }
}