‘liusuyi’
2024-04-13 55a4112533c958492078a5a94c90667e6cf5969c
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
package com.ruoyi.device.uav.service;
 
import com.alibaba.fastjson.JSON;
import com.dtflys.forest.exceptions.ForestNetworkException;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.ConfigUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.device.uav.websocketclient.UAVWebsocketClient;
import com.ruoyi.sy.controller.ArdSyCarController;
import com.ruoyi.utils.forest.UavClient;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.map.HashedMap;
import org.java_websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
 
@Service
@Slf4j(topic = "uav")
public class UavService {
    /*uav:
      host: http://112.98.126.2:6100/
      username: kaifabailu
      password: ardkj@2014
      salt: 0123456789012345
      */
    private String username;//用户名
 
    private String password;//密码
 
    private String salt; //盐
 
    private String host;//uav服务器11
 
 
    private Map uavUser;//登录的用户信息
    private ObjectMapper om = new ObjectMapper();
 
    private Map<String, UAVWebsocketClient> UAVWebsocketClientMap = new HashedMap();
 
    private String token;
 
    private String workspaceId;
 
    @Autowired
    private UavClient uavClient;
    @Autowired
    private RedisCache redisCache;
 
    @Value("${uav.enabled}")
    private Boolean uavEnabled;
 
    @PostConstruct
    public void created() {
        if(!uavEnabled){//无人机加入开关
            return;
        }
 
        getUavConfig();
        this.login();
        //创建无人机Websocket客户端
        createWebsocketCients();
    }
 
    private void getUavConfig() {
        this.host = ConfigUtils.getConfigValue("uav_host");
        this.username = ConfigUtils.getConfigValue("uav_username");
        this.password = ConfigUtils.getConfigValue("uav_password");
        this.salt = ConfigUtils.getConfigValue("uav_salt");
        /*this.host = "http://112.98.126.2:6100/";
        this.username = "znstest";
        this.password = "znstest";
        this.salt = "0123456789012345";*/
        if (StringUtils.isEmpty(this.host) || StringUtils.isEmpty(this.username) || StringUtils.isEmpty(this.password) || StringUtils.isEmpty(this.salt)) {
            throw new RuntimeException("无人机参数配置缺失:");
        }
    }
 
    public String doUavRequest(HttpMethod method, String url, String data) {
 
        getUavConfig();
        String res = null;
        Method requestMethod = null;
        //获取method
        try {
            requestMethod = UavClient.class.getMethod(method.name(), String.class, String.class, String.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        //执行method
       // this.getToken();//获取token验证,验证token有效性
        try {
            String token = this.getToken();
            res = (String) requestMethod.invoke(this.uavClient, this.host + url, token, data);
        } catch (IllegalAccessException e) {
            System.out.println("doUavRequest 访问失败");
            System.out.println(url);
            log.error("doUavRequest 访问失败" + e.getMessage());
        } catch (InvocationTargetException e) {
            System.out.println("doUavRequest 执行失败");
            System.out.println(url);
            log.error("doUavRequest 执行失败" + e.getMessage());
            e.printStackTrace();
        }
 
        return res;
    }
 
    public Map getUavUser() {
        return redisCache.getCacheMap("uav:uavUser");
    }
 
    public String getToken() {
        //获取uav用户信息
        Map uavUser = redisCache.getCacheMap("uav:uavUser");
        String token = (String) uavUser.get("access_token");
        //验证token有效性
        try {
            String res = uavClient.GET(this.host+"manage/api/v1/devices", token, "{}");
            res+="";
            //System.out.println(res);
 
            //token赋值
            this.token = token;
            this.workspaceId = (String) uavUser.get("workspace_id");
        } catch (ForestNetworkException fe) {
            if (fe.getStatusCode() == 401) {//token失效,重新登录
                this.login();
                //再次获取token
                uavUser = redisCache.getCacheMap("uav:uavUser");
                token = (String) uavUser.get("access_token");
                //token赋值
                this.token = token;
                this.workspaceId = (String) uavUser.get("workspace_id");
                return token;
            }
        }
        return token;
    }
 
    public void login() {
 
        getUavConfig();
        log.debug("登录无人机外部接口");
        String codedPassword = this.Encrypt(password, salt);
        String body = "{\"username\":\"" + username + "\",\"password\":\"" + codedPassword + "\"}";
        log.debug("body:" + body);
        try {
            //uavClient.GET("logout", null, "{}");
            String res = uavClient.POST(this.host+"login", null, body);
            Map resMap = om.readValue(res, Map.class);
            if (resMap != null&& resMap.get("data") instanceof Map) {
                redisCache.setCacheMap("uav:uavUser", (Map) resMap.get("data"));
            }
        } catch (ForestRuntimeException e) {
            log.error("无人机连接超时" + e.getMessage());
        } catch (JsonMappingException e) {
        } catch (JsonProcessingException e) {
        } catch (IOException e) {
        }
    }
 
    public void createWebsocketCients(){
        /*String ip = this.host.replace("http://", "");
        if(this.UAVWebsocketClientMap.get("status") != null && this.UAVWebsocketClientMap.get("control") != null){
            //无人机状态灭活
            UAVWebsocketClient uavStatusWebsocketClient = this.UAVWebsocketClientMap.get("status");
            uavStatusWebsocketClient.getHeartBeatTimer().cancel();
            uavStatusWebsocketClient.close();
            //无人机控制灭活
            UAVWebsocketClient uavControlWebsocketClient = this.UAVWebsocketClientMap.get("control");
            uavControlWebsocketClient.getHeartBeatTimer().cancel();
            uavControlWebsocketClient.close();
 
            this.UAVWebsocketClientMap.remove("status");
            this.UAVWebsocketClientMap.remove("control");
        }
 
        String uavStatusWsURL = "ws://" + ip + "api/v1/ws?x-auth-token=" + token;
        System.out.println(uavStatusWsURL);
        UAVWebsocketClient uavStatusWebsocketClient;
        try {
            uavStatusWebsocketClient = new UAVWebsocketClient(new URI(uavStatusWsURL));
            uavStatusWebsocketClient.connect();
            System.out.println("连接中。。。");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(!uavStatusWebsocketClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
                uavStatusWebsocketClient.close();
                return;
            }
            Date date = new Date();
            Timer heartBeatTimer = new Timer();
            TimerTask heartBeatTask = new TimerTask(){
                public void run(){
                    Map<String,Object> para = new HashMap();
                    para.put("biz_code", "user_cmd");
                    para.put("version", "1.0");
                    Date date = new Date();
                    para.put("time_stamp", String.valueOf(date.getTime()));
                    Map<String,String> payload = new HashMap();
                    payload.put("timeStamp", String.valueOf(date.getTime()));
                    Map<String,Object> data = new HashMap();
                    data.put("dataHead", "0x7479");
                    data.put("cmdCode", "0x02");
                    data.put("payload", payload);
                    para.put("data", data);
                    try{
                        uavStatusWebsocketClient.send(JSON.toJSONString(para));
                        System.out.println("无人机状态心跳发送");
                    }catch(Exception e){
                        e.printStackTrace();
                        System.out.println(uavStatusWebsocketClient.getServerUri());
                    }
                }
            };
            uavStatusWebsocketClient.setHeartBeatTimer(heartBeatTimer);
            heartBeatTimer.scheduleAtFixedRate(heartBeatTask,date,10*1000);
            this.UAVWebsocketClientMap.put("status", uavStatusWebsocketClient);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        String uavControlWsURL = "ws://" + ip + "api/v1/ws/cmd?x-auth-token=" + token;
        System.out.println(uavControlWsURL);
        UAVWebsocketClient uavControlWebsocketClient;
        try {
            uavControlWebsocketClient = new UAVWebsocketClient(new URI(uavControlWsURL));
            uavControlWebsocketClient.connect();
            System.out.println("连接中。。。");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(!uavControlWebsocketClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
                //无人机状态灭活
                UAVWebsocketClient uavStatusWebsocketClient0 =  this.UAVWebsocketClientMap.get("status");
                uavStatusWebsocketClient0.getHeartBeatTimer().cancel();
                uavStatusWebsocketClient0.close();
 
                uavControlWebsocketClient.close();
                return;
            }
            Date date = new Date();
            Timer heartBeatTimer = new Timer();
            TimerTask heartBeatTask = new TimerTask(){
                public void run(){
                    Map<String,Object> para = new HashMap();
                    para.put("biz_code", "user_cmd");
                    para.put("version", "1.0");
                    Date date = new Date();
                    para.put("time_stamp", String.valueOf(date.getTime()));
                    Map<String,String> payload = new HashMap();
                    payload.put("timeStamp", String.valueOf(date.getTime()));
                    Map<String,Object> data = new HashMap();
                    data.put("dataHead", "0x7479");
                    data.put("cmdCode", "0x02");
                    data.put("payload", payload);
                    para.put("data", data);
                    try{
                        uavControlWebsocketClient.send(JSON.toJSONString(para));
                        System.out.println("无人机控制心跳发送");
                    }catch(Exception e){
                        e.printStackTrace();
                        System.out.println(uavControlWebsocketClient.getServerUri());
                    }
                }
            };
            uavControlWebsocketClient.setHeartBeatTimer(heartBeatTimer);
            heartBeatTimer.scheduleAtFixedRate(heartBeatTask,date,10*1000);
            this.UAVWebsocketClientMap.put("control", uavControlWebsocketClient);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        String ip = this.host.replace("http://", "");
        Date date = new Date();
        Timer heartBeatTimer = new Timer();
        TimerTask heartBeatTask = new TimerTask(){
            public void run(){
                if(token != null){//判断是否为null
                    if(UAVWebsocketClientMap.get("status") != null && UAVWebsocketClientMap.get("control") != null){
                        String oldURL = UAVWebsocketClientMap.get("status").getServerUri().toString();
                        String oldToken = oldURL.split("x-auth-token=")[1];
                        if(!oldToken.equals(token)){//token更新从新创建客户端
                            //原客户端灭活
                            UAVWebsocketClientMap.get("status").close();//无人机状态灭活
                            UAVWebsocketClientMap.get("control").close();//无人机控制灭活
                            UAVWebsocketClientMap.remove("status");
                            UAVWebsocketClientMap.remove("control");
 
                            String uavStatusWsURL = "ws://" + ip + "api/v1/ws?x-auth-token=" + token;
                            UAVWebsocketClient uavStatusWebsocketClient = null;
                            try {
                                uavStatusWebsocketClient = new UAVWebsocketClient(new URI(uavStatusWsURL));
                                uavStatusWebsocketClient.connect();
                                System.out.println("连接中。。。");
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                if(!uavStatusWebsocketClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
                                    uavStatusWebsocketClient.close();
                                    uavStatusWebsocketClient = null;
                                }
                            } catch (NumberFormatException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (URISyntaxException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
 
                            String uavControlWsURL = "ws://" + ip + "api/v1/ws/cmd?x-auth-token=" + token;
                            UAVWebsocketClient uavControlWebsocketClient = null;
                            try {
                                uavControlWebsocketClient = new UAVWebsocketClient(new URI(uavControlWsURL));
                                uavControlWebsocketClient.connect();
                                System.out.println("连接中。。。");
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                if(!uavControlWebsocketClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
                                    //无人机状态灭活
                                    UAVWebsocketClient uavStatusWebsocketClient0 =  UAVWebsocketClientMap.get("status");
                                    uavStatusWebsocketClient0.close();
 
                                    uavControlWebsocketClient.close();
                                    uavStatusWebsocketClient0 = null;
                                    uavControlWebsocketClient = null;
                                    //return;
                                }
                            } catch (NumberFormatException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (URISyntaxException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            if(uavStatusWebsocketClient != null || uavControlWebsocketClient != null){
                                UAVWebsocketClientMap.put("status", uavStatusWebsocketClient);
                                UAVWebsocketClientMap.put("control", uavControlWebsocketClient);
 
                                Map<String,Object> para = new HashMap();
                                para.put("biz_code", "user_cmd");
                                para.put("version", "1.0");
                                Date date = new Date();
                                para.put("time_stamp", String.valueOf(date.getTime()));
                                Map<String,String> payload = new HashMap();
                                payload.put("timeStamp", String.valueOf(date.getTime()));
                                Map<String,Object> data = new HashMap();
                                data.put("dataHead", "0x7479");
                                data.put("cmdCode", "0x02");
                                data.put("payload", payload);
                                para.put("data", data);
                                try{
                                    uavStatusWebsocketClient.send(JSON.toJSONString(para));
                                    uavControlWebsocketClient.send(JSON.toJSONString(para));
                                    //System.out.println("无人机心跳发送");
                                }catch(Exception e){
                                    e.printStackTrace();
                                    System.out.println(uavStatusWebsocketClient.getServerUri());
                                    System.out.println(uavControlWebsocketClient.getServerUri());
                                    //无人机状态灭活
                                    uavStatusWebsocketClient = UAVWebsocketClientMap.get("status");
                                    uavStatusWebsocketClient.close();
                                    //无人机控制灭活
                                    uavControlWebsocketClient = UAVWebsocketClientMap.get("control");
                                    uavControlWebsocketClient.close();
 
                                    UAVWebsocketClientMap.remove("status");
                                    UAVWebsocketClientMap.remove("control");
                                }
                            }
                        }else{
                            Map<String,Object> para = new HashMap();
                            para.put("biz_code", "user_cmd");
                            para.put("version", "1.0");
                            Date date = new Date();
                            para.put("time_stamp", String.valueOf(date.getTime()));
                            Map<String,String> payload = new HashMap();
                            payload.put("timeStamp", String.valueOf(date.getTime()));
                            Map<String,Object> data = new HashMap();
                            data.put("dataHead", "0x7479");
                            data.put("cmdCode", "0x02");
                            data.put("payload", payload);
                            para.put("data", data);
                            UAVWebsocketClient uavStatusWebsocketClient = UAVWebsocketClientMap.get("status");
                            UAVWebsocketClient uavControlWebsocketClient = UAVWebsocketClientMap.get("control");
                            try{
                                uavStatusWebsocketClient.send(JSON.toJSONString(para));
                                uavControlWebsocketClient.send(JSON.toJSONString(para));
                                //System.out.println("无人机心跳发送");
                            }catch(Exception e){
                                e.printStackTrace();
                                System.out.println(uavStatusWebsocketClient.getServerUri());
                                System.out.println(uavControlWebsocketClient.getServerUri());
                                //无人机状态灭活
                                uavStatusWebsocketClient = UAVWebsocketClientMap.get("status");
                                uavStatusWebsocketClient.close();
                                //无人机控制灭活
                                uavControlWebsocketClient = UAVWebsocketClientMap.get("control");
                                uavControlWebsocketClient.close();
 
                                UAVWebsocketClientMap.remove("status");
                                UAVWebsocketClientMap.remove("control");
                            }
                        }
                    }else{
                        String uavStatusWsURL = "ws://" + ip + "api/v1/ws?x-auth-token=" + token;
                        System.out.println(uavStatusWsURL);
                        UAVWebsocketClient uavStatusWebsocketClient = null;
                        try {
                            uavStatusWebsocketClient = new UAVWebsocketClient(new URI(uavStatusWsURL));
                            uavStatusWebsocketClient.connect();
                            System.out.println("连接中。。。");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            if(!uavStatusWebsocketClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
                                uavStatusWebsocketClient.close();
                                uavStatusWebsocketClient = null;
                            }
                        } catch (NumberFormatException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (URISyntaxException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
 
                        String uavControlWsURL = "ws://" + ip + "api/v1/ws/cmd?x-auth-token=" + token;
                        System.out.println(uavControlWsURL);
                        UAVWebsocketClient uavControlWebsocketClient = null;
                        try {
                            uavControlWebsocketClient = new UAVWebsocketClient(new URI(uavControlWsURL));
                            uavControlWebsocketClient.connect();
                            System.out.println("连接中。。。");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            if(!uavControlWebsocketClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
                                //无人机状态灭活
                                UAVWebsocketClient uavStatusWebsocketClient0 =  UAVWebsocketClientMap.get("status");
                                uavStatusWebsocketClient0.close();
 
                                uavControlWebsocketClient.close();
                                uavStatusWebsocketClient0 = null;
                                uavControlWebsocketClient = null;
                            }
                        } catch (NumberFormatException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (URISyntaxException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        if(uavStatusWebsocketClient != null || uavControlWebsocketClient != null){
                            UAVWebsocketClientMap.put("status", uavStatusWebsocketClient);
                            UAVWebsocketClientMap.put("control", uavControlWebsocketClient);
 
                            Map<String,Object> para = new HashMap();
                            para.put("biz_code", "user_cmd");
                            para.put("version", "1.0");
                            Date date = new Date();
                            para.put("time_stamp", String.valueOf(date.getTime()));
                            Map<String,String> payload = new HashMap();
                            payload.put("timeStamp", String.valueOf(date.getTime()));
                            Map<String,Object> data = new HashMap();
                            data.put("dataHead", "0x7479");
                            data.put("cmdCode", "0x02");
                            data.put("payload", payload);
                            para.put("data", data);
                            try{
                                uavStatusWebsocketClient.send(JSON.toJSONString(para));
                                uavControlWebsocketClient.send(JSON.toJSONString(para));
                                //System.out.println("无人机心跳发送");
                            }catch(Exception e){
                                e.printStackTrace();
                                System.out.println(uavStatusWebsocketClient.getServerUri());
                                System.out.println(uavControlWebsocketClient.getServerUri());
                                //无人机状态灭活
                                uavStatusWebsocketClient = UAVWebsocketClientMap.get("status");
                                uavStatusWebsocketClient.close();
                                //无人机控制灭活
                                uavControlWebsocketClient = UAVWebsocketClientMap.get("control");
                                uavControlWebsocketClient.close();
 
                                UAVWebsocketClientMap.remove("status");
                                UAVWebsocketClientMap.remove("control");
                            }
                        }
                    }
                }
            }
        };
        heartBeatTimer.scheduleAtFixedRate(heartBeatTask,date,10*1000);
    }
 
 
    public String Encrypt(String sSrc, String sKey) {
        if (sKey == null) {
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            return null;
        }
        byte[] encrypted = null;
        try {
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
    }
 
    public Object checkPointValid(String url, Map<String,Map<String,Object>> data) {
        String token = this.getToken();
        url = this.host + url;
        Map<String,Object> resultMap = checkPointValid(url,token, data);
        return resultMap;
    }
 
    public Map<String,Object> checkPointValid(String url,String access_token,Map<String,Map<String,Object>> map){
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
 
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(map));
 
        Request request = new Request.Builder().url(url).post(body).addHeader("x-auth-token",access_token).build();
 
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        ResponseBody responseBody = response.body();
 
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map0 = (Map<String, Object>) JSON.parse(message);
            return map0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
 
    public Object checkLineValid(String url, Map<String,List<Map<String,String>>> data) {
        String token = this.getToken();
        url = this.host + url;
        Map<String,Object> resultMap = checkLineValid(url,token, data);
        return resultMap;
    }
 
    public Map<String,Object> checkLineValid(String url,String access_token,Map<String,List<Map<String,String>>> map){
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
 
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(map));
 
        Request request = new Request.Builder().url(url).post(body).addHeader("x-auth-token",access_token).build();
 
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        ResponseBody responseBody = response.body();
 
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map0 = (Map<String, Object>) JSON.parse(message);
            return map0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
 
    public String getWorkspaceId(){
        String token = this.getToken();
        return this.workspaceId;
    }
 
    public Object addWaylinesPlanning(String url, Map<String,Object> data) {
        String token = this.getToken();
        url = this.host + url;
        Map<String,Object> resultMap = addWaylinesPlanning(url,token, data);
        return resultMap;
    }
 
    public Map<String,Object> addWaylinesPlanning(String url,String access_token,Map<String,Object> map){
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
 
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(map));
 
        Request request = new Request.Builder().url(url).post(body).addHeader("x-auth-token",access_token).build();
 
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        ResponseBody responseBody = response.body();
 
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map0 = (Map<String, Object>) JSON.parse(message);
            return map0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
 
    public Object deleteWaylinesPlanning(String url, Map<String,String> data) {
        String token = this.getToken();
        String waylineId = data.get("waylineId");
        url = this.host + url + waylineId;
        Map<String,Object> resultMap = deleteWaylinesPlanning(url,token);
        return resultMap;
    }
 
    public static Map<String,Object> deleteWaylinesPlanning(String url,String access_token){
        OkHttpClient okHttpClient = new OkHttpClient();
 
        Request request = new Request.Builder().url(url).delete().addHeader("x-auth-token",access_token).build();
 
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        ResponseBody responseBody = response.body();
 
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map0 = (Map<String, Object>) JSON.parse(message);
            return map0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
 
    public Object updateWaylinesPlanning(String url, Map<String,Object> data) {
        String token = this.getToken();
        url = this.host + url;
        Map<String,Object> resultMap = updateWaylinesPlanning(url,token, data);
        return resultMap;
    }
 
    public Map<String,Object> updateWaylinesPlanning(String url,String access_token,Map<String,Object> map){
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
 
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(map));
 
        Request request = new Request.Builder().url(url).post(body).addHeader("x-auth-token",access_token).build();
 
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        ResponseBody responseBody = response.body();
 
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map0 = (Map<String, Object>) JSON.parse(message);
            return map0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
 
    public Object addAlarm(String url, Map<String,Object> data) {
        String token = this.getToken();
        url = this.host + url;
        data.put("userName",this.username);
        Map<String,Object> resultMap = addAlarm(url,token, data);
        return resultMap;
    }
 
    public Map<String,Object> addAlarm(String url,String access_token,Map<String,Object> map){
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
 
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(map));
 
        Request request = new Request.Builder().url(url).post(body).addHeader("x-auth-token",access_token).build();
 
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        ResponseBody responseBody = response.body();
 
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map0 = (Map<String, Object>) JSON.parse(message);
            return map0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
 
    public Object saveCurrentGroup(String url, Map<String,Object> data) {
        String token = this.getToken();
        url = this.host + url;
        Map<String,Object> resultMap = addWaylinesPlanning(url,token, data);
        return resultMap;
    }
 
    public Map<String,Object> saveCurrentGroup(String url,String access_token,Map<String,Object> map){
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
 
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(map));
 
        Request request = new Request.Builder().url(url).post(body).addHeader("x-auth-token",access_token).build();
 
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        ResponseBody responseBody = response.body();
 
        try {
            String message = responseBody.string();// 响应体
            Map<String,Object> map0 = (Map<String, Object>) JSON.parse(message);
            return map0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new HashMap();
        }
    }
}