| | |
| | | package com.ruoyi.sy.gps31; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.gps31.push.netty.PushClient; |
| | | import com.gps31.push.netty.PushMsg; |
| | | import com.gps31.push.netty.client.TcpClient; |
| | |
| | | import com.ruoyi.sy.service.*; |
| | | import com.ruoyi.sy.service.impl.*; |
| | | import com.ruoyi.utils.gps.GeoTools; |
| | | import com.ruoyi.utils.mqtt.MqttConsumer; |
| | | import com.ruoyi.utils.mqtt.MqttOnce; |
| | | import com.ruoyi.utils.qymqtt.newM.EmqClient; |
| | | import com.ruoyi.utils.qymqtt.newM.QosEnum; |
| | | import com.ruoyi.utils.qymqtt.oldM.MqttCustomerClient; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.eclipse.paho.client.mqttv3.MqttException; |
| | |
| | | * @author chencq |
| | | * @version 1.0 |
| | | */ |
| | | @Slf4j(topic = "mqttCar") |
| | | public class PushClientImplPosition extends PushClient implements Runnable { |
| | | |
| | | private static final Log log = LogFactory.getLog(PushClientImplPosition.class); |
| | | // private static final Log log = LogFactory.getLog(PushClientImplPosition.class); |
| | | |
| | | private String ip; |
| | | |
| | |
| | | /*String carName = MapUtil.getStrVal(gpsMap, "carName","");//获取车牌号 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | System.out.println(sdf.format(new Date()));*/ |
| | | // log.error(String.format(" ---->收到定位数据:%s",JSON.toJSONString(gpsMap))); |
| | | log.debug(String.format(" ---->----收到定位数据如下:%s",JSON.toJSONString(gpsMap))); |
| | | String carId = (String)gpsMap.get("carId"); |
| | | ((ArdSyCarDayServiceImpl)SpringUtils.getBean("ardSyCarDayServiceImpl")).insertArdSyCarDay(carId);//新增车辆上报时间 |
| | | Map<String,Object> result = ((ArdSyCarServiceImpl)SpringUtils.getBean("ardSyCarServiceImpl")).getArdSyCarAndDeptByCarId(carId);//查询车辆及部门 |
| | | if(result != null){ |
| | | //清淤开始---------- |
| | | String lng = (String) gpsMap.get("lng").toString();//获取经度 |
| | | String lat = (String) gpsMap.get("lat").toString();//获取纬度 |
| | | String carName = (String) gpsMap.get("carName");//获取车牌号 |
| | | String time = (String) gpsMap.get("result");//定位时间 |
| | | String deptNum = (String) result.get("deptId").toString();//获取部门ID |
| | | String deptName = (String) result.get("deptName");//获取部门名称 |
| | | String carModel = (String) result.get("carModel");//获取车辆模型 |
| | | Integer drct = (Integer) gpsMap.get("drct");//获取车辆模型 |
| | | //定义2维空间 |
| | | Point2D.Double point = new Point2D.Double(Double.parseDouble(lng), Double.parseDouble(lat)); |
| | | List<Point2D.Double> pts = new ArrayList<Point2D.Double>(); |
| | | //注入带锁车辆Bean |
| | | ArdAccessFenceService ardAccessFenceService = SpringUtils.getBean(ArdAccessFenceService.class); |
| | | //注入油点围栏Bean |
| | | ArdOilWallService ardOilWallService = SpringUtils.getBean(ArdOilWallService.class); |
| | | //注入报警计数Bean |
| | | ArdAccessNumService accessNumService = SpringUtils.getBean(ArdAccessNumService.class); |
| | | //注入电子锁Bean |
| | | ArdSyCarRtuService carRtuService = SpringUtils.getBean(ArdSyCarRtuService.class); |
| | | //注入锁Bean |
| | | ArdSyCarLockService lockService = SpringUtils.getBean(ArdSyCarLockService.class); |
| | | //创建mqtt工具类对象 |
| | | MqttOnce mqttOnce = new MqttOnce(); |
| | | //查询定位油车表中是否有该车辆 |
| | | ArdAccessFence ardAccessFence = ardAccessFenceService.queryAccess(carId); |
| | | //查询所有装油点围栏 |
| | | List<ArdOilWall> ardOilWalls = ardOilWallService.accessAll(); |
| | | //如果没有说明车辆未进入装油点,需要匹配所有装油点围栏 |
| | | if(ardAccessFence==null){ |
| | | //车辆未进入装油点 |
| | | for (int i = 0; i < ardOilWalls.size(); i++) { |
| | | pts.clear(); |
| | | ArdOilWall ardOilWall = ardOilWalls.get(i); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j+1]))); |
| | | } |
| | | if(GeoTools.IsPtInPoly(point, pts)){ |
| | | //进入装油点入库 |
| | | ArdAccessFence ardAccessFence1 = new ArdAccessFence(); |
| | | ardAccessFence1.setId(String.valueOf(UUID.randomUUID())); |
| | | ardAccessFence1.setCarId(carId); |
| | | ardAccessFence1.setCarName(carName); |
| | | ardAccessFence1.setDeptId(deptNum); |
| | | ardAccessFence1.setDeptName(deptName); |
| | | ardAccessFence1.setIntoShipmentId(ardOilWall.getId()); |
| | | ardAccessFence1.setIntoShipmentTime(time); |
| | | ardAccessFence1.setLng(lng); |
| | | ardAccessFence1.setLat(lat); |
| | | ardAccessFence1.setLastTime(time); |
| | | ardAccessFence1.setNowWallName(ardOilWall.getName()); |
| | | ardAccessFence1.setCarModel(carModel); |
| | | ardAccessFence1.setIsWarning(0); |
| | | ardAccessFence1.setDrct(drct); |
| | | ardAccessFence1.setIsAlarm(0); |
| | | ardAccessFenceService.access(ardAccessFence1); |
| | | //mqtt发送既定Rtu状态 |
| | | feng(carId,"装油点",carRtuService,lockService,mqttOnce); |
| | | break; |
| | | } |
| | | try{ |
| | | String lng = (String) gpsMap.get("lng").toString();//获取经度 |
| | | String lat = (String) gpsMap.get("lat").toString();//获取纬度 |
| | | String carName = (String) gpsMap.get("carName");//获取车牌号 |
| | | String time = (String) gpsMap.get("time");//定位时间 |
| | | //时间状态 |
| | | Boolean timeType = true; |
| | | if(time==null || time == ""){ |
| | | timeType = false; |
| | | } |
| | | }else { |
| | | //带锁车辆有值优先判断是否为手动添加车辆 |
| | | if(ardAccessFence.getIsAlarm()==1){ |
| | | //1为手动添加车辆无需报警信息,更新位置即可 |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | }else if(ardAccessFence.getIsAlarm() == 2){ |
| | | //2为手动添加报警车辆有报警,所在围栏名称赋null,排查报警信息,只需处理路上情况 |
| | | ardAccessFence.setNowWallName(null); |
| | | //如果在路上判断坐标是否与上次一样 |
| | | if(ardAccessFence.getLng().equals(lng) && ardAccessFence.getLat().equals(lat)){ |
| | | //如果坐标一样时间相减 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date nowTime = sdf.parse(time); |
| | | Date lastTime = sdf.parse(ardAccessFence.getLastTime()); |
| | | Long diff = (nowTime.getTime() - lastTime.getTime())/1000; |
| | | ArdAccessNum list = accessNumService.queryNum(); |
| | | if(list==null){ |
| | | int alarm = list.getAlarm(); |
| | | if(diff>=alarm){ |
| | | //报警字段为1提示报警 |
| | | ardAccessFence.setIsWarning(1); |
| | | ardAccessFence.setAlarmLat(lat); |
| | | ardAccessFence.setAlarmLng(lng); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | String deptNum = (String) result.get("deptId").toString();//获取部门ID |
| | | String deptName = (String) result.get("deptName");//获取部门名称 |
| | | String carModel = (String) result.get("carModel");//获取车辆模型 |
| | | Integer drct = (Integer) gpsMap.get("drct");//获取车辆模型 |
| | | Integer speed = (Integer) gpsMap.get("speed");//车速 |
| | | String reserved = (String) result.get("reserved_1");//rtu关联 |
| | | //rtu标识 |
| | | boolean rtuType = true; |
| | | if(reserved==null || reserved==""){ |
| | | //无锁 |
| | | rtuType = false; |
| | | } |
| | | // log.debug("定位数据:车辆carId:"+carId+"经度lng:"+lng+"纬度lat:"+lat); |
| | | //定义2维空间 |
| | | Point2D.Double point = new Point2D.Double(Double.parseDouble(lng), Double.parseDouble(lat)); |
| | | List<Point2D.Double> pts = new ArrayList<Point2D.Double>(); |
| | | //注入带锁车辆Bean |
| | | ArdAccessFenceService ardAccessFenceService = SpringUtils.getBean(ArdAccessFenceService.class); |
| | | //注入油点围栏Bean |
| | | ArdOilWallService ardOilWallService = SpringUtils.getBean(ArdOilWallService.class); |
| | | //注入报警计数Bean |
| | | ArdAccessNumService accessNumService = SpringUtils.getBean(ArdAccessNumService.class); |
| | | //注入电子锁Bean |
| | | ArdSyCarRtuService carRtuService = SpringUtils.getBean(ArdSyCarRtuService.class); |
| | | //注入锁Bean |
| | | ArdSyCarLockService lockService = SpringUtils.getBean(ArdSyCarLockService.class); |
| | | //查询定位油车表中是否有该车辆 |
| | | ArdAccessFence ardAccessFence = ardAccessFenceService.queryAccess(carId); |
| | | //查询所有装油点围栏 |
| | | List<ArdOilWall> ardOilWalls = ardOilWallService.accessAll(); |
| | | //日期格式 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | //如果没有说明车辆未进入装油点,需要匹配所有装油点围栏 |
| | | if(ardAccessFence==null){ |
| | | //车辆未进入装油点 |
| | | for (int i = 0; i < ardOilWalls.size(); i++) { |
| | | pts.clear(); |
| | | ArdOilWall ardOilWall = ardOilWalls.get(i); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j+1]))); |
| | | } |
| | | }else { |
| | | //如果不一样更新坐标 |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | }else if(ardAccessFence.getIsAlarm() == 0 || ardAccessFence.getIsAlarm() == null){ |
| | | //车辆非手动添加数据,判断是否有进入围栏ID |
| | | if(ardAccessFence.getIntoShipmentId()==null){ |
| | | //没有进入装油点,赋值经纬度 |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | //判断是否进入装油点围栏 |
| | | for (int i = 0; i < ardOilWalls.size(); i++) { |
| | | pts.clear(); |
| | | ArdOilWall ardOilWall = ardOilWalls.get(i); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j+1]))); |
| | | } |
| | | if(GeoTools.IsPtInPoly(point, pts)){ |
| | | //进入装油点入库 |
| | | ardAccessFence.setIntoShipmentId(ardOilWall.getId()); |
| | | ardAccessFence.setIntoShipmentTime(time); |
| | | ardAccessFence.setNowWallName(ardOilWall.getName()); |
| | | ardAccessFence.setLastTime(time); |
| | | if(GeoTools.IsPtInPoly(point, pts)){ |
| | | //进入装油点入库 |
| | | ArdAccessFence ardAccessFence1 = new ArdAccessFence(); |
| | | ardAccessFence1.setId(String.valueOf(UUID.randomUUID())); |
| | | ardAccessFence1.setCarId(carId); |
| | | ardAccessFence1.setCarName(carName); |
| | | ardAccessFence1.setDeptId(deptNum); |
| | | ardAccessFence1.setDeptName(deptName); |
| | | ardAccessFence1.setIntoShipmentId(ardOilWall.getId()); |
| | | ardAccessFence1.setIntoShipmentTime(time); |
| | | ardAccessFence1.setLng(lng); |
| | | ardAccessFence1.setLat(lat); |
| | | ardAccessFence1.setLastTime(time); |
| | | ardAccessFence1.setNowWallName("进入装油点:"+ardOilWall.getName()); |
| | | ardAccessFence1.setCarModel(carModel); |
| | | ardAccessFence1.setIsWarning(0); |
| | | ardAccessFence1.setDrct(drct); |
| | | ardAccessFence1.setIsAlarm(0); |
| | | ardAccessFenceService.access(ardAccessFence1); |
| | | if(rtuType){ |
| | | //mqtt发送既定Rtu状态 |
| | | feng(carId,"装油点",carRtuService,lockService,mqttOnce); |
| | | feng(carId,"装油点",carRtuService,lockService); |
| | | break; |
| | | } |
| | | } |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | }else { |
| | | //如果有进入围栏ID,判断是否开出装油点 |
| | | String out = ardAccessFence.getOutShipmentId(); |
| | | //出装油点ID为空 |
| | | if(out == null){ |
| | | //车辆未开出装油点,判断是否开出装油点 |
| | | String into = ardAccessFence.getIntoShipmentId(); |
| | | ArdOilWall ardOilWall = ardOilWallService.one(into); |
| | | pts.clear(); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j + 1]))); |
| | | } |
| | | if (!GeoTools.IsPtInPoly(point, pts)) { |
| | | //车辆开出装油点 |
| | | ardAccessFence.setOutShipmentId(into); |
| | | ardAccessFence.setOutShipmentTime(time); |
| | | ardAccessFence.setNowWallName(ardOilWall.getName()); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | //开出装油点围栏,发送上锁及施封命令 |
| | | feng(carId,"施封",carRtuService,lockService,mqttOnce); |
| | | } |
| | | }else { |
| | | //车辆已经开出装油点,查看是否有卸油点ID |
| | | String unload = ardAccessFence.getIntoUnloadId(); |
| | | //如果unload为空,判断是否进入卸油点 |
| | | if (unload == null) { |
| | | //车辆未进入卸油点 |
| | | List<ArdOilWall> ardOilWallUpload = ardOilWallService.uploadAll(); |
| | | //是否在路上 |
| | | Boolean isRoad = true; |
| | | //判断是否进去卸油点 |
| | | for (int i = 0; i < ardOilWallUpload.size(); i++) { |
| | | pts.clear(); |
| | | ArdOilWall ardOilWall = ardOilWallUpload.get(i); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j + 1]))); |
| | | } |
| | | if (GeoTools.IsPtInPoly(point, pts)) { |
| | | //进入卸油点,存入卸油点围栏ID |
| | | ardAccessFence.setIntoUnloadId(ardOilWall.getId()); |
| | | ardAccessFence.setIntoUnloadTime(time); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setNowWallName(ardOilWall.getName()); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | //已经进入卸油点,不在路上不需要停车报警 |
| | | isRoad = false; |
| | | //mqtt发送既定Rtu状态 |
| | | feng(carId,"卸油点",carRtuService,lockService,mqttOnce); |
| | | break; |
| | | } |
| | | } |
| | | }else { |
| | | //车速标记 |
| | | Boolean stop = true; |
| | | //判断车速 |
| | | if(rtuType){ |
| | | if(speed>5){ |
| | | //mqtt发送既定Rtu状态 |
| | | feng(carId,"施封",carRtuService,lockService); |
| | | stop = false; |
| | | } |
| | | } |
| | | //车辆有值优先判断是否为手动添加车辆 |
| | | if(ardAccessFence.getIsAlarm() == 0 || ardAccessFence.getIsAlarm() == null){ |
| | | //车辆非手动添加数据,判断是否有进入围栏ID |
| | | if(ardAccessFence.getIntoShipmentId()==null){ |
| | | //没有进入装油点,赋值经纬度 |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | //判断是否进入装油点围栏 |
| | | for (int i = 0; i < ardOilWalls.size(); i++) { |
| | | pts.clear(); |
| | | ArdOilWall ardOilWall = ardOilWalls.get(i); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j+1]))); |
| | | } |
| | | //不在卸油点就是在路上 |
| | | if(isRoad){ |
| | | //所在围栏名称赋null |
| | | ardAccessFence.setNowWallName(null); |
| | | //如果在路上判断坐标是否与上次一样 |
| | | if(ardAccessFence.getLng().equals(lng) && ardAccessFence.getLat().equals(lat)){ |
| | | //如果坐标一样时间相减 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date nowTime = sdf.parse(time); |
| | | Date lastTime = sdf.parse(ardAccessFence.getLastTime()); |
| | | Long diff = (nowTime.getTime() - lastTime.getTime())/1000; |
| | | ArdAccessNum list = accessNumService.queryNum(); |
| | | if(list==null){ |
| | | int alarm = list.getAlarm(); |
| | | if(diff>=alarm){ |
| | | ardAccessFence.setIsWarning(1); |
| | | ardAccessFence.setAlarmLat(lat); |
| | | ardAccessFence.setAlarmLng(lng); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | if(GeoTools.IsPtInPoly(point, pts)){ |
| | | //进入装油点入库 |
| | | ardAccessFence.setIntoShipmentId(ardOilWall.getId()); |
| | | ardAccessFence.setIntoShipmentTime(time); |
| | | ardAccessFence.setNowWallName("进入装油点:"+ardOilWall.getName()); |
| | | ardAccessFence.setLastTime(time); |
| | | if(rtuType){ |
| | | if(stop){ |
| | | //mqtt发送既定Rtu状态 |
| | | feng(carId,"装油点",carRtuService,lockService); |
| | | } |
| | | }else { |
| | | //如果不一样更新坐标 |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | break; |
| | | } |
| | | }else { |
| | | //车辆已经进入卸油点,判断是否已经开出卸油点 |
| | | ArdOilWall ardOilWall = ardOilWallService.one(unload); |
| | | } |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | }else { |
| | | //如果有进入围栏ID,判断是否开出装油点 |
| | | String out = ardAccessFence.getOutShipmentId(); |
| | | //出装油点ID为空 |
| | | if(out == null){ |
| | | //车辆未开出装油点,判断是否开出装油点 |
| | | String into = ardAccessFence.getIntoShipmentId(); |
| | | ArdOilWall ardOilWall = ardOilWallService.one(into); |
| | | pts.clear(); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j + 1]))); |
| | | } |
| | | if (!GeoTools.IsPtInPoly(point, pts)) { |
| | | //开出了卸油点围栏,发送上锁及施解封指令 |
| | | feng(carId,"施封",carRtuService,lockService,mqttOnce); |
| | | //删除该条车辆数据 |
| | | ardAccessFenceService.delAccess(carId); |
| | | }else { |
| | | //未开出卸油点,更新经纬度保存 |
| | | //车辆开出装油点 |
| | | ardAccessFence.setOutShipmentId(into); |
| | | ardAccessFence.setOutShipmentTime(time); |
| | | ardAccessFence.setNowWallName("开出装油点:"+ardOilWall.getName()); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | if(rtuType){ |
| | | //开出装油点围栏,发送上锁及施封命令 |
| | | feng(carId,"施封",carRtuService,lockService); |
| | | } |
| | | } |
| | | }else { |
| | | //车辆已经开出装油点,查看是否有卸油点ID |
| | | String unload = ardAccessFence.getIntoUnloadId(); |
| | | //如果unload为空,判断是否进入卸油点 |
| | | if (unload == null) { |
| | | //车辆未进入卸油点 |
| | | List<ArdOilWall> ardOilWallUpload = ardOilWallService.uploadAll(); |
| | | //是否在路上 |
| | | Boolean isRoad = true; |
| | | //判断是否进去卸油点 |
| | | for (int i = 0; i < ardOilWallUpload.size(); i++) { |
| | | pts.clear(); |
| | | ArdOilWall ardOilWall = ardOilWallUpload.get(i); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j + 1]))); |
| | | } |
| | | if (GeoTools.IsPtInPoly(point, pts)) { |
| | | //进入卸油点,存入卸油点围栏ID |
| | | ardAccessFence.setIntoUnloadId(ardOilWall.getId()); |
| | | ardAccessFence.setIntoUnloadTime(time); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setNowWallName("进入卸油点:"+ardOilWall.getName()); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | //已经进入卸油点,不在路上不需要停车报警 |
| | | isRoad = false; |
| | | if(rtuType){ |
| | | if(stop){ |
| | | //mqtt发送既定Rtu状态 |
| | | feng(carId,"卸油点",carRtuService,lockService); |
| | | } |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | //不在卸油点就是在路上 |
| | | if(isRoad){ |
| | | //所在围栏名称赋null |
| | | ardAccessFence.setNowWallName("在路上"); |
| | | //如果在路上判断坐标是否与上次一样 |
| | | if(ardAccessFence.getLng().equals(lng) && ardAccessFence.getLat().equals(lat)){ |
| | | if(ardAccessFence.getLastTime()!=null){ |
| | | if(timeType){ |
| | | //如果坐标一样相减 |
| | | Date nowTime = sdf.parse(time); |
| | | Date lastTime = sdf.parse(ardAccessFence.getLastTime()); |
| | | Long diff = (nowTime.getTime() - lastTime.getTime())/1000; |
| | | ArdAccessNum list = accessNumService.queryNum(); |
| | | if(list!=null){ |
| | | int alarm = list.getAlarm(); |
| | | if(diff>=alarm){ |
| | | ardAccessFence.setIsWarning(1); |
| | | ardAccessFence.setAlarmLat(lat); |
| | | ardAccessFence.setAlarmLng(lng); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | } |
| | | }else { |
| | | log.debug(carId+"号车定位时间为空"); |
| | | } |
| | | }else { |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | }else { |
| | | //如果不一样更新坐标 |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | } |
| | | }else { |
| | | //车辆已经进入卸油点,判断是否已经开出卸油点 |
| | | ArdOilWall ardOilWall = ardOilWallService.one(unload); |
| | | pts.clear(); |
| | | String[] coord = ardOilWall.getCoord().split(","); |
| | | for (int j = 0; j < coord.length; j = j + 3) { |
| | | pts.add(new Point2D.Double(Double.parseDouble(coord[j]), Double.parseDouble(coord[j + 1]))); |
| | | } |
| | | if (!GeoTools.IsPtInPoly(point, pts)) { |
| | | if(rtuType){ |
| | | //开出了卸油点围栏,发送上锁及施解封指令 |
| | | feng(carId,"施封",carRtuService,lockService); |
| | | } |
| | | //删除该条车辆数据 |
| | | ardAccessFenceService.delAccess(carId); |
| | | }else { |
| | | //未开出卸油点,更新经纬度保存 |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }else |
| | | if(ardAccessFence.getIsAlarm()==1){ |
| | | //1为手动添加车辆无需报警信息,更新位置即可 |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | }else if(ardAccessFence.getIsAlarm() == 2){ |
| | | //2为手动添加报警车辆有报警,所在围栏名称赋null,排查报警信息,只需处理路上情况 |
| | | ardAccessFence.setNowWallName(null); |
| | | //如果在路上判断坐标是否与上次一样 |
| | | if(ardAccessFence.getLng().equals(lng) && ardAccessFence.getLat().equals(lat)){ |
| | | if(timeType){ |
| | | Date nowTime = sdf.parse(time); |
| | | Date lastTime = sdf.parse(ardAccessFence.getLastTime()); |
| | | Long diff = (nowTime.getTime() - lastTime.getTime())/1000; |
| | | ArdAccessNum list = accessNumService.queryNum(); |
| | | if(list!=null){ |
| | | int alarm = list.getAlarm(); |
| | | if(diff>=alarm){ |
| | | //报警字段为1提示报警 |
| | | ardAccessFence.setIsWarning(1); |
| | | ardAccessFence.setAlarmLat(lat); |
| | | ardAccessFence.setAlarmLng(lng); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | } |
| | | }else { |
| | | log.debug(carId+"号车定位时间为空"); |
| | | } |
| | | }else { |
| | | //如果不一样更新坐标 |
| | | ardAccessFence.setLat(lat); |
| | | ardAccessFence.setLng(lng); |
| | | ardAccessFence.setLastTime(time); |
| | | ardAccessFenceService.updAccess(ardAccessFence); |
| | | } |
| | | } |
| | | } |
| | | //清淤结束------------ |
| | | }catch(Exception e){ |
| | | |
| | | } |
| | | //清淤结束------------ |
| | | |
| | | gpsMap.putAll(result); |
| | | String ancestors = (String) gpsMap.get("ancestors");//获取父级部门 |
| | |
| | | } |
| | | } |
| | | } |
| | | //若有用户需要追踪车辆,则计算预估位置 |
| | | if(PositionContainer.getUserPositionMap().size() != 0){ |
| | | for(String userId : PositionContainer.getUserPositionMap().keySet()){ |
| | | Map<String,Map<String,Object>> carIdCarMap = PositionContainer.getUserPositionMap().get(userId); |
| | | if(carIdCarMap != null){ |
| | | if(carIdCarMap.keySet().contains(carId)){ |
| | | if(PositionContainer.getCarPositionMap().get(carId) == null){ |
| | | gpsMap.put("elng",gpsMap.get("lng")); |
| | | gpsMap.put("elat",gpsMap.get("lat")); |
| | | }else{ |
| | | Double velocityOld = ((Integer) PositionContainer.getCarPositionMap().get(carId).get("speed")).doubleValue(); |
| | | Double velocityNew = ((Integer) gpsMap.get("speed")).doubleValue(); |
| | | Double acceleration = Double.valueOf(1000*(velocityNew - velocityOld)/3600); |
| | | Double distance = getDistance(velocityNew, acceleration ,"1.5"); |
| | | Double[] eposition = azimuth_offset(((BigDecimal)gpsMap.get("lng")).doubleValue(),((BigDecimal)gpsMap.get("lat")).doubleValue(),(Integer)gpsMap.get("drct"),distance); |
| | | gpsMap.put("elng",eposition[0]); |
| | | gpsMap.put("elat",eposition[1]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | //存入实时位置容器 |
| | | PositionContainer.getCarPositionMap().put(carId,gpsMap); |
| | | //存入用户容器 |
| | | if(PositionContainer.getUserPositionMap().size() != 0){ |
| | | for(String userId : PositionContainer.getUserPositionMap().keySet()){ |
| | |
| | | sendMassage(); |
| | | } |
| | | |
| | | private void feng(String carId,String type,ArdSyCarRtuService carRtuService,ArdSyCarLockService lockService,MqttOnce mqttOnce) throws MqttException { |
| | | private void feng(String carId,String type,ArdSyCarRtuService carRtuService,ArdSyCarLockService lockService) throws MqttException { |
| | | //查询装油点既定Rtu状态 |
| | | ArdSyCarRtu ardSyCarRtu = carRtuService.one(carId); |
| | | Integer fengType = null; |
| | | if("装油点".equals(type)){ |
| | | fengType = ardSyCarRtu.getInstallType(); |
| | | }else if("卸油点".equals(type)){ |
| | | fengType = ardSyCarRtu.getUnloadType(); |
| | | }else if("施封".equals(type)){ |
| | | fengType = 0; |
| | | } |
| | | //1解封 |
| | | if(fengType==1){ |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("A01", 110000);//导通第一路继电器 |
| | | map.put("A02", 110000);//导通第二路继电器 |
| | | map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | mqttOnce.publishCar(carId,ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"解封"); |
| | | } |
| | | //0施封 |
| | | if(fengType==0){ |
| | | //锁开关状态 |
| | | int lockCloseNum = 0; |
| | | if(ardSyCarRtu!=null){ |
| | | //查询锁信息 |
| | | List<ArdSyCarLock> ardSyCarLocks = lockService.carLock(ardSyCarRtu.getId()); |
| | | for (int j = 0; j < ardSyCarLocks.size(); j++) { |
| | | ArdSyCarLock ardSyCarLock = ardSyCarLocks.get(j); |
| | | String currents = ardSyCarLock.getCurrents(); |
| | | Double currentInfo = ardSyCarLock.getCurrentInfo(); |
| | | //电流值为4.0是关锁状态 |
| | | if(currentInfo.equals(4.00)){ |
| | | lockCloseNum += 1; |
| | | }else { |
| | | String key = ""; |
| | | if(currents.equals("C01")){ |
| | | key = "D03"; |
| | | }else if(currents.equals("C02")){ |
| | | key = "D04"; |
| | | if(ardSyCarLocks.size()>0){ |
| | | Integer fengType = null; |
| | | if("装油点".equals(type)){ |
| | | fengType = ardSyCarRtu.getInstallType(); |
| | | }else if("卸油点".equals(type)){ |
| | | fengType = ardSyCarRtu.getUnloadType(); |
| | | }else if("施封".equals(type)){ |
| | | fengType = 0; |
| | | } |
| | | //1解封 |
| | | if(fengType==1){ |
| | | //如果不是解封封状态,则发送施封命令 |
| | | if(ardSyCarLocks.get(0).getRelayInfo()==0 || ardSyCarLocks.get(0).getRelayInfo()==null){ |
| | | // Map<String,Object> map = new HashMap<>(); |
| | | // map.put("A01", 110000);//导通第一路继电器 |
| | | // map.put("A02", 110000);//导通第二路继电器 |
| | | // map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | // String mapString = com.alibaba.fastjson.JSON.toJSONString(map); |
| | | String mapString = "{\"A01\":110000,\"A02\":110000,\"res\":\""+String.valueOf(System.currentTimeMillis())+"\"}"; |
| | | log.debug("自动解封:"+mapString); |
| | | EmqClient emqClient = new EmqClient(carId+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | log.debug("车辆"+carId+"解封命令发送成功!"); |
| | | // mqttOnce.publishCar(carId,ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"解封"); |
| | | } |
| | | Map<String,Object> map = new HashMap(); |
| | | map.put(key, 0.01); |
| | | map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | mqttOnce.publishCar("carId",ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"开锁:"+currents); |
| | | while(true){ |
| | | ArdSyCarLock ardSyCarLock1 = lockService.getCurrentInfo(ardSyCarRtu.getId(),currents); |
| | | Double currentInfo1 = ardSyCarLock1.getCurrentInfo(); |
| | | if(currentInfo1.equals(4.00)){ |
| | | lockCloseNum +=1; |
| | | break; |
| | | } |
| | | |
| | | //0施封 |
| | | if(fengType==0){ |
| | | if(ardSyCarLocks.get(0).getRelayInfo()==1 || ardSyCarLocks.get(0).getRelayInfo()==null){ |
| | | //锁开关状态 |
| | | int lockCloseNum = 0; |
| | | String mapString1 = "{\"A01\":100000,\"A02\":100000,\"res\":\""+String.valueOf(System.currentTimeMillis())+"\"}"; |
| | | System.out.println("自动施封:"+mapString1); |
| | | EmqClient emqClient1 = new EmqClient(carId+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient1.connect(); |
| | | emqClient1.publish(ardSyCarRtu.getTopicPublish(),mapString1, QosEnum.QoS2,false); |
| | | //如果不是施封状态,则发送施封命令 |
| | | if(ardSyCarLocks.get(0).getRelayInfo()!=null){ |
| | | if(ardSyCarLocks.get(0).getRelayInfo()==1){ |
| | | for (int j = 0; j < ardSyCarLocks.size(); j++) { |
| | | ArdSyCarLock ardSyCarLock = ardSyCarLocks.get(j); |
| | | String currents = ardSyCarLock.getCurrents(); |
| | | Double currentInfo = ardSyCarLock.getCurrentInfo(); |
| | | //电流值为4.0是关锁状态 |
| | | if(currentInfo.equals(4.00) || currentInfo.equals(4)){ |
| | | lockCloseNum += 1; |
| | | }else { |
| | | String key = ""; |
| | | if(currents.equals("C01")){ |
| | | key = "D03"; |
| | | }else if(currents.equals("C02")){ |
| | | key = "D04"; |
| | | } |
| | | // Map<String,Object> map = new HashMap(); |
| | | // map.put(key, 0.01); |
| | | // map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | // String mapString = com.alibaba.fastjson.JSON.toJSONString(map); |
| | | String mapString = "{\""+key+"\":0.01,\"res\":\""+String.valueOf(System.currentTimeMillis())+"\"}"; |
| | | log.debug("自动关锁:"+mapString); |
| | | EmqClient emqClient = new EmqClient(carId+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | // mqttOnce.publishCar("carId",ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"开锁:"+currents); |
| | | while(true){ |
| | | ArdSyCarLock ardSyCarLock1 = lockService.getCurrentInfo(ardSyCarRtu.getId(),currents); |
| | | Double currentInfo1 = ardSyCarLock1.getCurrentInfo(); |
| | | if(currentInfo1.equals(4.00) || currentInfo1.equals(4)){ |
| | | lockCloseNum +=1; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if(lockCloseNum == 2){ |
| | | // Map<String,Object> map = new HashMap<>(); |
| | | // map.put("A01", 100000);//断开第一路继电器 |
| | | // map.put("A02", 100000);//断开第二路继电器 |
| | | // map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | // String mapString = com.alibaba.fastjson.JSON.toJSONString(map); |
| | | log.debug("车辆"+carId+"施封命令发送成功!"); |
| | | // mqttOnce.publishCar(carId,ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"施封"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if(lockCloseNum == 2){ |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("A01", 100000);//断开第一路继电器 |
| | | map.put("A02", 100000);//断开第二路继电器 |
| | | map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | mqttOnce.publishCar(carId,ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"施封"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public Double[] azimuth_offset(double origin_lon, double origin_lat, Integer direction,double distance){ |
| | | Double[] lonlat = new Double[2]; |
| | | if(direction != null && distance > 0){ |
| | | lonlat[0] = origin_lon + distance * Math.sin(direction* Math.PI / 180) * 180 / ( Math.PI * 6371229 * Math.cos(origin_lat * Math.PI / 180)); |
| | | lonlat[1] = origin_lat + distance * Math.cos(direction* Math.PI / 180) / ( Math.PI * 6371229 / 180); |
| | | }else{ |
| | | lonlat[0] = origin_lon; |
| | | lonlat[1] = origin_lat; |
| | | } |
| | | return lonlat; |
| | | } |
| | | |
| | | public Double getDistance(Double velocity ,Double acceleration ,String time){ |
| | | Double distance = velocity*Double.parseDouble(time) + 0.5*acceleration*Double.parseDouble(time)*Double.parseDouble(time); |
| | | return distance; |
| | | } |
| | | |
| | | /*public Double getVelocity(Double velocity ,Double acceleration ,String time){ |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Long t = (long) 0; |
| | | try { |
| | | t = (new Date().getTime() - simpleDateFormat.parse(time).getTime())/1000; |
| | | } catch (Exception e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | Double velocityt = velocity + acceleration*t; |
| | | return velocityt; |
| | | }*/ |
| | | |
| | | } |