From b3dbfe5ab42efc7e744df5a391a8568e1b7c2289 Mon Sep 17 00:00:00 2001
From: zhangnaisong <2434969829@qq.com>
Date: 星期五, 16 二月 2024 17:06:18 +0800
Subject: [PATCH] 无人机websocket客户端心跳保活提交
---
ard-work/pom.xml | 9 +
ard-work/src/main/java/com/ruoyi/device/uav/websocketclient/UAVWebsocketClient.java | 45 +++++
ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java | 386 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 438 insertions(+), 2 deletions(-)
diff --git a/ard-work/pom.xml b/ard-work/pom.xml
index 8a51372..d7e3021 100644
--- a/ard-work/pom.xml
+++ b/ard-work/pom.xml
@@ -196,7 +196,14 @@
<artifactId>orai18n</artifactId>
<version>12.1.0.2.0</version>
</dependency>
-<!-- <dependency>-->
+ <!-- WebSocket瀹㈡埛绔� -->
+ <dependency>
+ <groupId>org.java-websocket</groupId>
+ <artifactId>Java-WebSocket</artifactId>
+ <version>1.3.8</version>
+ </dependency>
+
+ <!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>1.2.15</version>-->
diff --git a/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java b/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java
index 2a009f9..3ce9f56 100644
--- a/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java
+++ b/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java
@@ -1,5 +1,6 @@
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;
@@ -8,9 +9,13 @@
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 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;
@@ -24,7 +29,9 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.util.Map;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.*;
@Service
@Slf4j(topic = "uav")
@@ -46,6 +53,11 @@
private Map uavUser;//鐧诲綍鐨勭敤鎴蜂俊鎭�
private ObjectMapper om = new ObjectMapper();
+
+ private Map<String, UAVWebsocketClient> UAVWebsocketClientMap = new HashedMap();
+
+ private String token;
+
@Autowired
private UavClient uavClient;
@Autowired
@@ -56,6 +68,8 @@
getUavConfig();
this.login();
+ //鍒涘缓鏃犱汉鏈篧ebsocket瀹㈡埛绔�
+ createWebsocketCients();
}
private void getUavConfig() {
@@ -85,8 +99,12 @@
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();
}
@@ -107,12 +125,17 @@
String res = uavClient.GET(this.host+"manage/api/v1/devices", token, "{}");
res+="";
//System.out.println(res);
+
+ //token璧嬪��
+ this.token = token;
} 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;
return token;
}
}
@@ -141,6 +164,367 @@
}
}
+ 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){//鍒ゆ柇鏄惁涓簄ull
+ 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) {
diff --git a/ard-work/src/main/java/com/ruoyi/device/uav/websocketclient/UAVWebsocketClient.java b/ard-work/src/main/java/com/ruoyi/device/uav/websocketclient/UAVWebsocketClient.java
new file mode 100644
index 0000000..4c0712b
--- /dev/null
+++ b/ard-work/src/main/java/com/ruoyi/device/uav/websocketclient/UAVWebsocketClient.java
@@ -0,0 +1,45 @@
+package com.ruoyi.device.uav.websocketclient;
+
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.handshake.ServerHandshake;
+
+import java.net.URI;
+import java.util.Timer;
+
+public class UAVWebsocketClient extends WebSocketClient {
+
+ private URI serverUri;
+
+ public UAVWebsocketClient(URI serverUri) {
+ super(serverUri);
+ this.serverUri = serverUri;
+ }
+
+ public URI getServerUri() {
+ return serverUri;
+ }
+
+ public void setServerUri(URI serverUri) {
+ this.serverUri = serverUri;
+ }
+
+ @Override
+ public void onOpen(ServerHandshake serverHandshake) {
+
+ }
+
+ @Override
+ public void onMessage(String s) {
+
+ }
+
+ @Override
+ public void onClose(int i, String s, boolean b) {
+
+ }
+
+ @Override
+ public void onError(Exception e) {
+
+ }
+}
--
Gitblit v1.9.3