/**
|
* <p>Description: </p>
|
* <p>Copyright: Copyright (c) 2020</p>
|
* <p>Company: www.31gps.net</p>
|
* @author chencq
|
* @version 1.0
|
*/
|
package com.ruoyi.sy.gps31;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.gps31.push.netty.PushClient;
|
import com.gps31.push.netty.PushMsg;
|
import com.gps31.push.netty.client.TcpClient;
|
import com.gps31.push.util.MapUtil;
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
import com.ruoyi.sy.domain.ArdOilWall;
|
import com.ruoyi.sy.service.impl.ArdOilWallServiceImpl;
|
import com.ruoyi.sy.service.impl.ArdSyCarDayServiceImpl;
|
import com.ruoyi.sy.service.impl.ArdSyCarServiceImpl;
|
import com.ruoyi.utils.gps.GeoTools;
|
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.LogFactory;
|
|
import java.awt.geom.Point2D;
|
import java.util.*;
|
|
/**
|
* <p>Description: </p>
|
* <p>Copyright: Copyright (c) 2020</p>
|
* <p>Company: www.31gps.net</p>
|
* @author chencq
|
* @version 1.0
|
*/
|
public class PushClientImplPosition extends PushClient implements Runnable {
|
|
private static final Log log = LogFactory.getLog(PushClientImplPosition.class);
|
|
private String ip;
|
|
private String userId;
|
|
private String password;
|
|
public PushClientImplPosition(String ip, String userId, String password) {
|
this.ip = ip;
|
this.userId = userId;
|
this.password = password;
|
}
|
|
public PushClientImplPosition() {
|
|
}
|
|
@Override
|
public void messageReceived(TcpClient tcpClient, PushMsg pushMsg)
|
throws Exception {
|
if("8001".equals(pushMsg.getCmd())) {//登录应答
|
String rspResult = MapUtil.getStrVal(pushMsg.getJsonMap(),"rspResult","1");
|
if("0".equals(rspResult)) {//登录成功,定阅动态消息
|
Map<String,Object> map = new HashMap<String,Object>();
|
map.put("seq", "1");
|
map.put("action", "add");
|
map.put("msgIds", JSON.toJSONString(getSubCmdSet()));
|
PushMsg subMsg = getInstance("0003",map);
|
sendMsg(subMsg);
|
}
|
}else if("8002".equals(pushMsg.getCmd())){//心跳应答
|
|
}else if("8003".equals(pushMsg.getCmd())){//订阅动态消息应答
|
String rspResult = MapUtil.getStrVal(pushMsg.getJsonMap(),"rspResult","1");
|
//log.error(String.format(" 订阅应答:%s", "0".equals(rspResult)?"成功":"失败"));
|
}else if("0200".equals(pushMsg.getCmd())) {//定位信息
|
Map<String,Object> gpsMap = pushMsg.getJsonMap();
|
/*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)));
|
String carId = (String)gpsMap.get("carId");
|
((ArdSyCarDayServiceImpl)SpringUtils.getBean("ardSyCarDayServiceImpl")).insertArdSyCarDay(carId);//新增车辆上报时间
|
Map<String,Object> result = ((ArdSyCarServiceImpl)SpringUtils.getBean("ardSyCarServiceImpl")).getArdSyCarAndDeptByCarId(carId);//查询车辆及部门
|
// log.error("---------------------"+result);
|
if(result != null){
|
//清淤定位
|
String lng = (String) gpsMap.get("lng");//获取经度
|
String lat = (String) gpsMap.get("lat");//获取纬度
|
String carName = (String) gpsMap.get("carName");//获取车牌号
|
String time = (String) gpsMap.get("result");//定位时间
|
String deptNum = (String) result.get("deptId");//获取部门ID
|
String deptName = (String) result.get("deptName");//获取部门名称
|
//定义2维空间
|
Point2D.Double point = new Point2D.Double(Double.parseDouble(lng), Double.parseDouble(lat));
|
List<Point2D.Double> pts = new ArrayList<Point2D.Double>();
|
//匹配全部围栏
|
List<ArdOilWall> ardOilWalls = ((ArdOilWallServiceImpl)SpringUtils.getBean("ArdOilWallServiceImpl")).all();
|
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)){
|
//进入了围栏入库
|
|
}
|
}
|
gpsMap.putAll(result);
|
String ancestors = (String) gpsMap.get("ancestors");//获取父级部门
|
String[] ancestorsArray = ancestors.split(",");
|
if(ancestorsArray.length == 1){//最高级部门
|
String deptId = String.valueOf(gpsMap.get("deptId"));
|
Map<String,Map<String,Object>> deptMap = PositionContainer.getDeptPositionMap().get(deptId);
|
if(deptMap == null){
|
deptMap = new HashMap();
|
deptMap.put((String)gpsMap.get("carId"),gpsMap);
|
PositionContainer.getDeptPositionMap().put(deptId,deptMap);
|
}else{
|
deptMap.put((String)gpsMap.get("carId"),gpsMap);
|
}
|
}else{//其他部门
|
//加入本部门容器
|
String deptId = String.valueOf(gpsMap.get("deptId"));
|
Map<String,Map<String,Object>> deptMap = PositionContainer.getDeptPositionMap().get(deptId);
|
if(deptMap == null){
|
deptMap = new HashMap();
|
deptMap.put((String)gpsMap.get("carId"),gpsMap);
|
PositionContainer.getDeptPositionMap().put(deptId,deptMap);
|
}else{
|
deptMap.put((String)gpsMap.get("carId"),gpsMap);
|
}
|
//加入父级部门容器
|
for(int i = 1;i <= ancestorsArray.length - 1;i++){
|
String deptIdp = ancestorsArray[i];
|
Map<String,Map<String,Object>> deptMapp = PositionContainer.getDeptPositionMap().get(deptIdp);
|
if(deptMapp == null){
|
deptMapp = new HashMap();
|
deptMapp.put((String)gpsMap.get("carId"),gpsMap);
|
PositionContainer.getDeptPositionMap().put(deptIdp,deptMapp);
|
}else{
|
deptMapp.put((String)gpsMap.get("carId"),gpsMap);
|
}
|
}
|
}
|
//存入用户容器
|
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)){
|
PositionContainer.getUserPositionMap().get(userId).put(carId,gpsMap);
|
}
|
}
|
}
|
}
|
}
|
/**
|
*
|
* 请加入三方集成方的业务逻辑
|
*
|
*/
|
}else if("0300".equals(pushMsg.getCmd())) {//报警消息
|
Map<String,Object> alarmMap = pushMsg.getJsonMap();
|
String carName = MapUtil.getStrVal(alarmMap, "carName","");//获取车牌号
|
//log.error(String.format(" ---->收到报警数据:%s",JSON.toJSONString(alarmMap)));
|
/**
|
*
|
* 请加入三方集成方的业务逻辑
|
*
|
*/
|
}else if("0401".equals(pushMsg.getCmd())) {//透传消息
|
Map<String,Object> dataMap = pushMsg.getJsonMap();
|
String carName = MapUtil.getStrVal(dataMap, "carName","");//获取车牌号
|
//log.error(String.format(" ---->收到透传数据:%s",JSON.toJSONString(dataMap)));
|
/**
|
*
|
* 请加入三方集成方的业务逻辑
|
*
|
*/
|
}
|
}
|
|
public void sendMassage() {
|
try {
|
PushClientImplPosition client = new PushClientImplPosition();
|
client.setLog(false);//是否打印明文
|
client.setHost(this.ip);//服务器IP
|
client.setPort(10100);//服务器端口
|
client.setUserName(this.userId);//系统用户名
|
client.setPwd(this.password);//系统用户密码
|
client.setSubMsgIds("0200");//订阅的动态消息,多个动态消息使用|辟分,当前示例是订阅 定位消息(0x0200)和报警消息(0x0300)
|
client.setDesc("测试客户端");//客户端的描述,
|
client.start();
|
while(true) {
|
Thread.sleep(1000);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void run() {
|
sendMassage();
|
}
|
}
|