TrafficDataConvertHandler.java 19.7 KB
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
package com.viontech.handler;

import com.alibaba.fastjson.JSONObject;
import com.viontech.constant.RedisConstants;
import com.viontech.scheduled.DataCacheScheduled;
import com.viontech.utils.DateUtil;
import com.viontech.utils.Gb1400GenerateIDUtil;
import com.viontech.vo.gb1400.*;
import com.viontech.vo.traffic.DeviceModel;
import com.viontech.vo.traffic.LaneModel;
import com.viontech.vo.traffic.TollgateModel;
import com.viontech.vo.traffic.TrafficMongoModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Slf4j
@Component
public class TrafficDataConvertHandler {
    @Value("${fanxing.getPicInfo.url:}")
    private String getPicInfoUrl;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Gb1400GenerateIDUtil gb1400GenerateIDUtil;
    @Autowired
    private Properties wflxProperties;
    @Autowired
    private Properties csysProperties;
    @Autowired
    private Properties cllxProperties;
    @Autowired
    private Properties clppProperties;
    @Autowired
    private Properties hpzlProperties;
    @Autowired
    private Properties hpysProperties;

    public SubscribeNotificationsObj trafficIllegalToSubscribeNotifications(List<TrafficMongoModel> trafficMongoModels) {
        if (CollectionUtils.isEmpty(trafficMongoModels)) {
            log.warn("trafficMongoModels isEmpty");
            return null;
        }
        SubscribeNotificationsObj result = new SubscribeNotificationsObj();
        SubscribeNotificationsObj.SubscribeNotificationListObject resultListObject = new SubscribeNotificationsObj.SubscribeNotificationListObject();
        List<SubscribeNotificationsObj.SubscribeNotificationObject> subNotifList = new ArrayList<>();

        SubscribeNotificationsObj.SubscribeNotificationObject subNotifObj = new SubscribeNotificationsObj.SubscribeNotificationObject();
        JSONObject subscribeInfo = getIllegalSubscribeInfo();
        if (StringUtils.isBlank(subscribeInfo.getString("SubscribeID"))) {
            log.warn("未查询到订阅信息!");
            return null;
        }
        subNotifObj.setNotificationID(gb1400GenerateIDUtil.generateNotificationID());
        subNotifObj.setSubscribeID(subscribeInfo.getString("SubscribeID"));
        subNotifObj.setTitle(subscribeInfo.getString("Title"));
        subNotifObj.setTriggerTime(DateUtil.format(new Date(), DateUtil.DATE_TIME_FORMAT_NO_DELIMITER));

        MotorVehicleObj vehicleObj = new MotorVehicleObj();
        List<MotorVehicleObj.MotorVehicleObject> vehicleList = new ArrayList<>();
        StringBuilder infoIds = new StringBuilder();
        //设置违法数据
        for (TrafficMongoModel trafficMongoModel : trafficMongoModels) {
            MotorVehicleObj.MotorVehicleObject vehicle = tranferToMotorVehicleObject(trafficMongoModel);
            if (vehicle == null) {
                continue;
            }

            vehicleList.add(vehicle);
            if ("".equals(infoIds.toString())) {
                infoIds.append(vehicle.getMotorVehicleID());
            } else {
                infoIds.append("," + vehicle.getMotorVehicleID());
            }
        }
        if (CollectionUtils.isEmpty(vehicleList)) {
            return null;
        }

        vehicleObj.setMotorVehicleObject(vehicleList);
        subNotifObj.setInfoIDs(infoIds.toString());
        subNotifObj.setMotorVehicleObjectList(vehicleObj);

        subNotifList.add(subNotifObj);
        resultListObject.setSubscribeNotificationObjects(subNotifList);
        result.setSubscribeNotificationListObject(resultListObject);
        return result;
    }

    public SubscribeNotificationsObj deviceToSubscribeNotifications(List<DeviceModel> deviceModels) {
        if (CollectionUtils.isEmpty(deviceModels)) {
            log.warn("deviceModels isEmpty");
            return null;
        }
        SubscribeNotificationsObj result = new SubscribeNotificationsObj();
        SubscribeNotificationsObj.SubscribeNotificationListObject resultListObject = new SubscribeNotificationsObj.SubscribeNotificationListObject();
        List<SubscribeNotificationsObj.SubscribeNotificationObject> subNotifList = new ArrayList<>();

        SubscribeNotificationsObj.SubscribeNotificationObject subNotifObj = new SubscribeNotificationsObj.SubscribeNotificationObject();
        JSONObject subscribeInfo = getSubscribeInfo("3");
        if (StringUtils.isBlank(subscribeInfo.getString("SubscribeID"))) {
            log.warn("未查询到设备订阅信息!");
            return null;
        }
        subNotifObj.setNotificationID(gb1400GenerateIDUtil.generateNotificationID());
        subNotifObj.setSubscribeID(subscribeInfo.getString("SubscribeID"));
        subNotifObj.setTitle(subscribeInfo.getString("Title"));
        subNotifObj.setTriggerTime(DateUtil.format(new Date(), DateUtil.DATE_TIME_FORMAT_NO_DELIMITER));

        APEList apeListObj = new APEList();
        List<APEList.APE> apeList = new ArrayList<>();
        StringBuilder infoIds = new StringBuilder();
        //设置设备数据
        for (DeviceModel deviceModel : deviceModels) {
            APEList.APE ape = new APEList.APE();
            BeanUtils.copyProperties(deviceModel, ape);

            apeList.add(ape);
            if ("".equals(infoIds.toString())) {
                infoIds.append(ape.getApeID());
            } else {
                infoIds.append("," + ape.getApeID());
            }
        }

        apeListObj.setAPEObject(apeList);
        subNotifObj.setInfoIDs(infoIds.toString());
        subNotifObj.setDeviceList(apeListObj);

        subNotifList.add(subNotifObj);
        resultListObject.setSubscribeNotificationObjects(subNotifList);
        result.setSubscribeNotificationListObject(resultListObject);
        return result;
    }

    public SubscribeNotificationsObj tollgateToSubscribeNotifications(List<TollgateModel> tollgateModels) {
        if (CollectionUtils.isEmpty(tollgateModels)) {
            log.warn("tollgateModels isEmpty");
            return null;
        }
        SubscribeNotificationsObj result = new SubscribeNotificationsObj();
        SubscribeNotificationsObj.SubscribeNotificationListObject resultListObject = new SubscribeNotificationsObj.SubscribeNotificationListObject();
        List<SubscribeNotificationsObj.SubscribeNotificationObject> subNotifList = new ArrayList<>();

        SubscribeNotificationsObj.SubscribeNotificationObject subNotifObj = new SubscribeNotificationsObj.SubscribeNotificationObject();
        JSONObject subscribeInfo = getSubscribeInfo("7");
        if (StringUtils.isBlank(subscribeInfo.getString("SubscribeID"))) {
            log.warn("未查询到卡口订阅信息!");
            return null;
        }
        subNotifObj.setNotificationID(gb1400GenerateIDUtil.generateNotificationID());
        subNotifObj.setSubscribeID(subscribeInfo.getString("SubscribeID"));
        subNotifObj.setTitle(subscribeInfo.getString("Title"));
        subNotifObj.setTriggerTime(DateUtil.format(new Date(), DateUtil.DATE_TIME_FORMAT_NO_DELIMITER));

        TollgateList tollgateListObj = new TollgateList();
        List<TollgateList.Tollgate> tollgateList = new ArrayList<>();
        StringBuilder infoIds = new StringBuilder();
        //设置卡口数据
        for (TollgateModel tollgateModel : tollgateModels) {
            TollgateList.Tollgate tollgate = new TollgateList.Tollgate();
            BeanUtils.copyProperties(tollgateModel, tollgate);

            tollgateList.add(tollgate);
            if ("".equals(infoIds.toString())) {
                infoIds.append(tollgate.getTollgateID());
            } else {
                infoIds.append("," + tollgate.getTollgateID());
            }
        }

        tollgateListObj.setTollgateObject(tollgateList);
        subNotifObj.setInfoIDs(infoIds.toString());
        subNotifObj.setTollgateObjectList(tollgateListObj);

        subNotifList.add(subNotifObj);
        resultListObject.setSubscribeNotificationObjects(subNotifList);
        result.setSubscribeNotificationListObject(resultListObject);
        return result;
    }

    public SubscribeNotificationsObj laneToSubscribeNotifications(List<LaneModel> laneModels) {
        if (CollectionUtils.isEmpty(laneModels)) {
            log.warn("laneModels isEmpty");
            return null;
        }
        SubscribeNotificationsObj result = new SubscribeNotificationsObj();
        SubscribeNotificationsObj.SubscribeNotificationListObject resultListObject = new SubscribeNotificationsObj.SubscribeNotificationListObject();
        List<SubscribeNotificationsObj.SubscribeNotificationObject> subNotifList = new ArrayList<>();

        SubscribeNotificationsObj.SubscribeNotificationObject subNotifObj = new SubscribeNotificationsObj.SubscribeNotificationObject();
        JSONObject subscribeInfo = getSubscribeInfo("9");
        if (StringUtils.isBlank(subscribeInfo.getString("SubscribeID"))) {
            log.warn("未查询到车道订阅信息!");
            return null;
        }
        subNotifObj.setNotificationID(gb1400GenerateIDUtil.generateNotificationID());
        subNotifObj.setSubscribeID(subscribeInfo.getString("SubscribeID"));
        subNotifObj.setTitle(subscribeInfo.getString("Title"));
        subNotifObj.setTriggerTime(DateUtil.format(new Date(), DateUtil.DATE_TIME_FORMAT_NO_DELIMITER));

        LaneList laneListObj = new LaneList();
        List<LaneList.Lane> laneList = new ArrayList<>();
        StringBuilder infoIds = new StringBuilder();
        //设置车道数据
        for (LaneModel laneModel : laneModels) {
            LaneList.Lane lane = new LaneList.Lane();
            BeanUtils.copyProperties(laneModel, lane);
            laneList.add(lane);

            StringBuilder laneSb = new StringBuilder();
            laneSb.append(lane.getTollgateID()).append("_").append(lane.getLaneId());
            if ("".equals(infoIds.toString())) {
                infoIds.append(laneSb.toString());
            } else {
                infoIds.append("," + laneSb.toString());
            }
        }

        laneListObj.setLaneObject(laneList);
        subNotifObj.setInfoIDs(infoIds.toString());
        subNotifObj.setLaneObjectList(laneListObj);

        subNotifList.add(subNotifObj);
        resultListObject.setSubscribeNotificationObjects(subNotifList);
        result.setSubscribeNotificationListObject(resultListObject);
        return result;
    }

    /**
     * 转成MotorVehicleObject对象
     * @param trafficMongoModel
     * @return
     */
    private MotorVehicleObj.MotorVehicleObject tranferToMotorVehicleObject(TrafficMongoModel trafficMongoModel) {
        String passTime = "";
        String picUrl = "";
        String illCode = "";
        String deviceID = "";
        Map eventData = trafficMongoModel.getEvent_data();
        List pics = trafficMongoModel.getPics();
        if (eventData == null || eventData.get("device") == null || eventData.get("illegal") == null || CollectionUtils.isEmpty(pics)) {
            log.warn("缺少必填数据!");
            return null;
        }

        try {
            String eventDateStr = trafficMongoModel.getEvent_dt();
            Date eventDateUtc = DateUtil.parse(eventDateStr, DateUtil.TIMESTAMP_FORMAT);
            passTime = DateUtil.format(DateUtil.addHours(eventDateUtc, 8), DateUtil.DATE_TIME_FORMAT_NO_DELIMITER);
            //图片
            Map picMap = (Map) pics.get(0);
            String pic_unid = (String) picMap.get("pic_unid");
            picUrl = getPicUrlByPicUnid(pic_unid);
//          Map object_rect = (Map) picMap.get("object_rect");
            //设备信息
            Map deviceMap = (Map) eventData.get("device");
            deviceID = (String) deviceMap.get("code");
            //违法代码
            Map illegalMap = (Map) eventData.get("illegal");
            illCode = (String) illegalMap.get("code");
        } catch (Exception e) {
            log.error("获取必须字段异常Exception", e);
        }

        if (StringUtils.isBlank(picUrl) || StringUtils.isBlank(illCode) || StringUtils.isBlank(deviceID) || StringUtils.isBlank(passTime)) {
            log.warn("获取必填数据失败!");
            return null;
        }

        Integer laneNo = 1;
        String hasPlate = "1";
        String plateNo = "-";
        String plateClass = "99";
        String plateColor = "99";
        String csys = "";
        String cllx = "";
        String clpp = "";
        try {
            //车道
            Map laneMap = (Map) eventData.get("lane");
            if (laneMap != null) {
                laneNo = (Integer) laneMap.get("number");
            }
            //车牌
            Map vehicleMap = (Map) eventData.get("vehicle");
            if (vehicleMap != null) {
                Map plateMap = (Map) vehicleMap.get("plate");
                if (plateMap != null) {
                    String text = (String) plateMap.get("text");
                    if (StringUtils.isBlank(text) || "无牌".equals(text)) {
                        hasPlate = "0";
                    } else {
                        plateNo = text;
                    }
                    plateColor = hpysProperties.getProperty((String) plateMap.getOrDefault("color_code", "9"), "99");
                    plateClass = hpzlProperties.getProperty((String) plateMap.getOrDefault("type_code", "99"), "99");
                }
                //车辆信息
                Map bodyMap = (Map) vehicleMap.get("body");
                if (bodyMap != null) {
                    csys = getCsysByVehicleBody(bodyMap, "Z");
                    cllx = getCllxByVehicleBody(bodyMap, "");
                    clpp = getClppByVehicleBody(bodyMap, "");
                }
            }
        } catch (Exception e) {
            log.error("获取车辆信息异常", e);
        }

        MotorVehicleObj.MotorVehicleObject vehicle = new MotorVehicleObj.MotorVehicleObject();
        //基本信息
        vehicle.setInfoKind(1);
        vehicle.setDeviceID(deviceID);
        vehicle.setPassTime(passTime);
        vehicle.setSourceID(gb1400GenerateIDUtil.generateSourceID(vehicle.getDeviceID(), "02", vehicle.getPassTime()));
        vehicle.setMotorVehicleID(gb1400GenerateIDUtil.generateMotorVehicleID(vehicle.getSourceID(), "02"));
        String tollgateID = "";
        if (DataCacheScheduled.deviceTollgateMap.containsKey(deviceID)) {
            tollgateID = DataCacheScheduled.deviceTollgateMap.get(deviceID);
        }
        vehicle.setTollgateID(tollgateID);

        vehicle.setBreakRuleMode(wflxProperties.getProperty(illCode, illCode));
        vehicle.setStorageUrl1(picUrl);
        vehicle.setLeftTopX(0);
        vehicle.setLeftTopY(0);
        vehicle.setRightBtmX(0);
        vehicle.setRightBtmY(0);
        vehicle.setLaneNo(laneNo);
        vehicle.setHasPlate(hasPlate);
        vehicle.setPlateClass(plateClass);
        vehicle.setPlateColor(plateColor);
        vehicle.setPlateNo(plateNo);
//        vehicle.setDirection("");
        vehicle.setVehicleClass(cllx);
        vehicle.setVehicleBrand(clpp);
//        vehicle.setVehicleStyles("");
        vehicle.setVehicleColor(csys);
        return vehicle;
    }

    /**
     * 获取图片url
     * @param pic_unid
     * @return
     */
    private String getPicUrlByPicUnid(String pic_unid) {
        String geturl = getPicInfoUrl + pic_unid;
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(geturl, String.class);
        if (responseEntity.getStatusCodeValue() == 200) {
            String picget = responseEntity.getBody();
            log.info("图片服务查询[{}],响应videoget={}", pic_unid, picget);
            if (StringUtils.isNotBlank(picget)) {
                JSONObject picJson = JSONObject.parseObject(picget);
                if (picJson != null && StringUtils.isNotBlank(picJson.getString("pic_url"))) {
                    return picJson.getString("pic_url");
                }
            }
        } else {
            log.info("error-------------图片服务查询响应失败[{}]", pic_unid);
        }
        return "";
    }

    /**
     * 获取违法数据的订阅信息
     * @return
     */
    private JSONObject getIllegalSubscribeInfo() {
        List<Object> values = redisTemplate.opsForHash().values(RedisConstants.GB1400_SUBSCRIBES_HASH);
        if (!CollectionUtils.isEmpty(values)) {
            for (Object value : values) {
                String data = (String) value;
                JSONObject dataObj = JSONObject.parseObject(data);
                String subscribeDetail = dataObj.getString("SubscribeDetail");
                if (StringUtils.isBlank(subscribeDetail)) {
                    continue;
                }
                String[] subArr = subscribeDetail.split(",");
                List<String> subList = Arrays.asList(subArr);
                if (subList.contains("50")) {
                    return dataObj;
                }
            }
        }
        return new JSONObject();
    }

    /**
     * 获取指定的订阅信息
     * 订阅类别 : 3:设备,7:卡口,9:车道,50:违法数据
     * @return
     */
    private JSONObject getSubscribeInfo(String target) {
        List<Object> values = redisTemplate.opsForHash().values(RedisConstants.GB1400_SUBSCRIBES_HASH);
        if (!CollectionUtils.isEmpty(values)) {
            for (Object value : values) {
                String data = (String) value;
                JSONObject dataObj = JSONObject.parseObject(data);
                String subscribeDetail = dataObj.getString("SubscribeDetail");
                if (StringUtils.isBlank(subscribeDetail)) {
                    continue;
                }
                String[] subArr = subscribeDetail.split(",");
                List<String> subList = Arrays.asList(subArr);
                if (subList.contains(target)) {
                    return dataObj;
                }
            }
        }
        return new JSONObject();
    }

    private String getCsysByVehicleBody(Map bodyMap, String defaultValue) {
        try {
            Map colorMap = (Map) bodyMap.get("color");
            String csys = (String) colorMap.getOrDefault("code", "Z");
            return csysProperties.getProperty(csys, defaultValue);
        } catch (Exception e) {
            log.error("getCsysByVehicleBody.Exception:{}", e.getMessage());
        }
        return defaultValue;
    }

    private String getCllxByVehicleBody(Map bodyMap, String defaultValue) {
        try {
            Map typeMap = (Map) bodyMap.get("type");
            String cllx = (String) typeMap.getOrDefault("code", defaultValue);
            return cllxProperties.getProperty(cllx, defaultValue);
        } catch (Exception e) {
            log.error("getCllxByVehicleBody.Exception:{}", e.getMessage());
        }
        return defaultValue;
    }

    private String getClppByVehicleBody(Map bodyMap, String defaultValue) {
        try {
            Map logoMap= (Map) bodyMap.get("logo");
            String clpp = (String) logoMap.getOrDefault("code", defaultValue);
            return clppProperties.getProperty(clpp, defaultValue);
        } catch (Exception e) {
            log.error("getClppByVehicleBody.Exception:{}", e.getMessage());
        }
        return defaultValue;
    }

    public static void main(String[] args) {
        Integer laneNo = 1;
        String xxx = "";
        Map eventData = new HashMap();
        Map laneMap = new HashMap();
        laneNo = (Integer) laneMap.get("number");
        xxx = (String) laneMap.getOrDefault("xxx", "99");
        System.out.println(laneNo);
    }
}