108 lines
4.9 KiB
Java
108 lines
4.9 KiB
Java
package com.shiyi.internet.config;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.TypeReference;
|
|
import com.shiyi.internet.constants.FaultConstant;
|
|
import com.shiyi.internet.constants.RabbitContants;
|
|
import com.shiyi.internet.constants.VehicleConstant;
|
|
import com.shiyi.internet.domain.Car;
|
|
import com.shiyi.internet.domain.Fencing;
|
|
import com.shiyi.internet.domain.VehicleMessage;
|
|
import com.shiyi.internet.domain.fault.FaultCode;
|
|
import com.shiyi.internet.handler.LocationHandler;
|
|
import com.shiyi.internet.handler.VehicleData;
|
|
import com.fate.vehicle.feign.LogFeign;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.awt.geom.Point2D;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
/**
|
|
* @Description : RabbitMQ收到围栏处理消息
|
|
* @Author : YangHaoYu
|
|
* @Date: 2023-11-30 14:37
|
|
*/
|
|
@Log4j2
|
|
@Component
|
|
public class RabbitConfig {
|
|
@Autowired
|
|
private LogFeign logFeign;
|
|
|
|
@Autowired
|
|
private RedisTemplate redisTemplate;
|
|
|
|
private final ExecutorService executorService = Executors.newFixedThreadPool(20);
|
|
|
|
@RabbitListener(queues = RabbitContants.FENCE_HANDLER_QUEUE)
|
|
public void fenceHandler(String jsonMessage){
|
|
|
|
//将JSON消息转换成对象
|
|
VehicleMessage message = JSONObject.parseObject(jsonMessage, new TypeReference<VehicleMessage>() {
|
|
});
|
|
//从Redis中获取所有车辆数据
|
|
Car carInfo = (Car) redisTemplate.opsForHash().get(VehicleConstant.VEHICLE_INFO_MAP, message.getVin());
|
|
//判断车辆是否存在围栏 Id
|
|
if (StringUtils.isNotEmpty(carInfo.getFenceId())){
|
|
//获取围栏信息
|
|
Fencing fencingInfo = VehicleData.fencingInfoMap.get(carInfo.getFenceId());
|
|
String[] split = fencingInfo.getFencingLat().split(";");
|
|
ArrayList<Point2D.Double> locations = new ArrayList<>();
|
|
for (String location : split) {
|
|
String[] temp = location.split(",");
|
|
locations.add(new Point2D.Double(Double.parseDouble(temp[0]), Double.parseDouble(temp[1])));
|
|
}
|
|
boolean ptInPoly = LocationHandler.isPtInPoly(new Point2D.Double(Double.parseDouble(message.getLatitude()), Double.parseDouble(message.getLongitude())), locations);
|
|
if(fencingInfo.getFencingTypeId()==0 && !ptInPoly){
|
|
log.info("vin:{};驶出电子围栏告警",carInfo.getCarVin());
|
|
FaultCode log = (FaultCode) redisTemplate.opsForValue().get(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin() + "OUT");
|
|
if (log == null){
|
|
FaultCode faultCode = new FaultCode();
|
|
faultCode.setFaultContent("OUT");
|
|
faultCode.setVin(message.getVin());
|
|
faultCode.setWarningTime(new Date());
|
|
redisTemplate.opsForValue().set(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin() + ":OUT", faultCode);
|
|
logFeign.faultInsert(faultCode);
|
|
}
|
|
}
|
|
if (fencingInfo.getFencingTypeId()==0 && ptInPoly){
|
|
FaultCode log = (FaultCode) redisTemplate.opsForValue().get(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin() + "OUT");
|
|
if (log != null){
|
|
log.setNormalTime(new Date());
|
|
logFeign.faultUpdate(log);
|
|
redisTemplate.delete(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin() + ":OUT");
|
|
}
|
|
}
|
|
if (fencingInfo.getFencingTypeId()==1 && ptInPoly){
|
|
log.info("vin:{};驶入非法区域告警", carInfo.getCarVin());
|
|
FaultCode log = (FaultCode) redisTemplate.opsForValue().get(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin() + "IN");
|
|
if (log == null){
|
|
FaultCode faultCode = new FaultCode();
|
|
faultCode.setFaultCode("IN");
|
|
faultCode.setVin(message.getVin());
|
|
faultCode.setWarningTime(new Date());
|
|
redisTemplate.opsForValue().set(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin()+ ":IN", faultCode);
|
|
}
|
|
}
|
|
if (fencingInfo.getFencingTypeId()==1 &&!ptInPoly){
|
|
FaultCode log = (FaultCode) redisTemplate.opsForValue().get(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin() + "IN");
|
|
if (log != null){
|
|
log.setNormalTime(new Date());
|
|
logFeign.faultUpdate(log);
|
|
redisTemplate.delete(FaultConstant.FAULT_INFO_PREFIX + carInfo.getCarVin() + ":IN");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|