aijinhui
2023-11-01 9dbd1b03adca3920a249b9079153d5f53d01a47d
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
/**
 * <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.ArdAccessFence;
import com.ruoyi.sy.domain.ArdAccessNum;
import com.ruoyi.sy.domain.ArdOilWall;
import com.ruoyi.sy.domain.ArdSyCarLock;
import com.ruoyi.sy.service.ArdAccessFenceService;
import com.ruoyi.sy.service.ArdOilWallService;
import com.ruoyi.sy.service.impl.*;
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.text.SimpleDateFormat;
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);//查询车辆及部门
            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");//获取部门名称
                //定义2维空间
                Point2D.Double point = new Point2D.Double(Double.parseDouble(lng), Double.parseDouble(lat));
                List<Point2D.Double> pts = new ArrayList<Point2D.Double>();
                //查询定位油车表中是否有该车辆
                ArdAccessFence ardAccessFence = SpringUtils.getBean(ArdAccessFenceService.class).queryAccess(carId);
                //查询所有装油点围栏
                List<ArdOilWall> ardOilWalls = SpringUtils.getBean(ArdOilWallService.class).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());
                            ((ArdAccessFenceServiceImpl)SpringUtils.getBean("ArdAccessFenceServiceImpl")).access(ardAccessFence1);
                            break;
                        }
                    }
                }else {
                    //如果车辆有数据判断是否有进入围栏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);
                                break;
                            }
                        }
                        ((ArdAccessFenceServiceImpl)SpringUtils.getBean("ArdAccessFenceServiceImpl")).updAccess(ardAccessFence);
                    }else {
                        //如果有进入围栏ID,判断是否开出装油点
                        String out = ardAccessFence.getOutShipmentId();
                        if(out == null){
                            //车辆未开出装油点
                            String into = ardAccessFence.getIntoShipmentId();
                            ArdOilWall ardOilWall = SpringUtils.getBean(ArdOilWallService.class).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);
                            ((ArdAccessFenceServiceImpl) SpringUtils.getBean("ArdAccessFenceServiceImpl")).updAccess(ardAccessFence);
                        }else {
                            //车辆已经开出装油点,判断是否进入卸油点
                            String unload = ardAccessFence.getIntoUnloadId();
                            //如果unload为空,判断是否进入卸油点
                            if (unload == null) {
                                //车辆未进入卸油点
                                List<ArdOilWall> ardOilWallUpload = SpringUtils.getBean(ArdOilWallService.class).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);
                                        ((ArdAccessFenceServiceImpl) SpringUtils.getBean("ArdAccessFenceServiceImpl")).updAccess(ardAccessFence);
                                        //已经进入卸油点,不在路上不需要停车报警
                                        isRoad = false;
                                        break;
                                    }
                                }
                                if(isRoad){
                                    //如果在路上判断坐标是否与上次一样
                                    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;
                                        List<ArdAccessNum> list = ((ArdAccessNumServiceImpl) SpringUtils.getBean("ArdAccessNumServiceImpl")).queryNum();
                                        if(list.size()>0){
                                            int alarm = list.get(0).getAlarm();
                                            if(diff>=alarm){
                                                ardAccessFence.setIsWarning("1");
                                                ardAccessFence.setAlarmLat(lat);
                                                ardAccessFence.setAlarmLng(lng);
                                                ((ArdAccessFenceServiceImpl) SpringUtils.getBean("ArdAccessFenceServiceImpl")).updAccess(ardAccessFence);
                                            }
                                        }
                                    }else {
                                        //如果不一样更新坐标
                                        ardAccessFence.setLat(lat);
                                        ardAccessFence.setLng(lng);
                                        ardAccessFence.setLastTime(time);
                                        ((ArdAccessFenceServiceImpl) SpringUtils.getBean("ArdAccessFenceServiceImpl")).updAccess(ardAccessFence);
                                    }
                                }
                            }else {
                                //车辆已经进入卸油点,判断是否已经开出卸油点
                                ArdOilWall ardOilWall = ((ArdOilWallServiceImpl) SpringUtils.getBean("ArdOilWallServiceImpl")).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)) {
                                    //开出了卸油点围栏,删除该条车辆数据
                                    ((ArdAccessFenceServiceImpl) SpringUtils.getBean("ArdAccessFenceServiceImpl")).delAccess(carId);
                                }else {
                                    //未开出卸油点,更新经纬度保存
                                    ardAccessFence.setLng(lng);
                                    ardAccessFence.setLat(lat);
                                    ardAccessFence.setLastTime(time);
                                    ((ArdAccessFenceServiceImpl)SpringUtils.getBean("ArdAccessFenceServiceImpl")).updAccess(ardAccessFence);
                                }
                            }
                        }
                    }
                }
                //清淤入栏结束------------
 
                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();
    }
}