增加无限循环ping,增加相对应的链接状态

This commit is contained in:
xinfengkun
2022-03-01 16:12:08 +08:00
parent 76c857783d
commit de1f3e4f61
10 changed files with 94 additions and 33 deletions

View File

@@ -184,7 +184,7 @@ class MoGoAutopilotProvider :
*/
override fun resetIpAddress(autoPilotIp: String) {
// 关闭通信
AdasManager.getInstance().disConnect()
AdasManager.getInstance().disconnect()
// 延时执行连接指定IP地址
ThreadUtils.executeBySingleWithDelay(object : ThreadUtils.SimpleTask<String>() {
@RequiresPermission(permission.INTERNET)

View File

@@ -33,10 +33,6 @@ class MoGoAdasMsgConnectStatusListenerImpl : OnAdasConnectStatusListener,
private var mCurrentAutopilotSpeed = 0f
override fun onAddressDisabled() {
}
override fun onConnectionIPCStatus(ipcConnectionStatus: Int, reason: String?) {
if (ipcConnectionStatus == Constants.IPC_CONNECTION_STATUS.CONNECTED) {
Logger.d(TAG, "webSocket 连接成功")
@@ -57,6 +53,12 @@ class MoGoAdasMsgConnectStatusListenerImpl : OnAdasConnectStatusListener,
AdasManager.getInstance().adasConfig.address
CallerAutoPilotStatusListenerManager.getAutoPilotStatusInfo().connectStatus = false
CallerAutoPilotStatusListenerManager.invokeAutoPilotStatus()
} else if (ipcConnectionStatus == Constants.IPC_CONNECTION_STATUS.CONNECTING) {
Logger.d(TAG, "webSocket 正在连接")
} else if (ipcConnectionStatus == Constants.IPC_CONNECTION_STATUS.SEARCH_ADDRESS) {
Logger.d(TAG, "webSocket 正在搜索工控机IP")
} else if (ipcConnectionStatus == Constants.IPC_CONNECTION_STATUS.NOT_FOUND_ADDRESS) {
Logger.d(TAG, "webSocket 找不到可用IP 传入的IP不可用或固定IP列表中所有IP不可用")
}
}

View File

@@ -49,7 +49,6 @@ import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import mogo.webproto.WebsocketHeader;
import okhttp3.WebSocket;
import okio.ByteString;
/**
@@ -118,6 +117,14 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
this.onMultiDeviceListener = onMultiDeviceListener;
}
@Override
public void onStartFindIP() {
if (adasConnectStatusListener != null) {
ipcConnectionStatus = Constants.IPC_CONNECTION_STATUS.SEARCH_ADDRESS;
adasConnectStatusListener.onConnectionIPCStatus(ipcConnectionStatus, null);
}
}
/**
* 可连通的IP
*
@@ -192,10 +199,6 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
clientPkFileName = "sn")
private void connectSocket(String address) {
if (mSocket != null) {
ipcConnectionStatus = Constants.IPC_CONNECTION_STATUS.CONNECTING;
if (adasConnectStatusListener != null) {
adasConnectStatusListener.onConnectionIPCStatus(ipcConnectionStatus, null);
}
mSocket.connectWebSocket(address);
}
}
@@ -431,7 +434,7 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
* 断开与工控机的链接
*/
@Override
public void disConnect() {
public void disconnect() {
if (!adasOptions.isClient()) {
// ipcConnectionStatus = Constants.IPC_CONNECTION_STATUS.DISCONNECTED;
// if (adasConnectStatusListener != null) {
@@ -460,6 +463,14 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
}
@Override
public void onConnecting() {
ipcConnectionStatus = Constants.IPC_CONNECTION_STATUS.CONNECTING;
if (adasConnectStatusListener != null) {
adasConnectStatusListener.onConnectionIPCStatus(ipcConnectionStatus, null);
}
}
@Override
public void onWebSocketConnectSuccess() {
ipcConnectionStatus = Constants.IPC_CONNECTION_STATUS.CONNECTED;
@@ -512,8 +523,7 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
mSocket.setIsReconnect(false);
}
if (adasConnectStatusListener != null) {
ipcConnectionStatus = Constants.IPC_CONNECTION_STATUS.DISCONNECTED;
adasConnectStatusListener.onAddressDisabled();
ipcConnectionStatus = Constants.IPC_CONNECTION_STATUS.NOT_FOUND_ADDRESS;
adasConnectStatusListener.onConnectionIPCStatus(ipcConnectionStatus, "找不到可用的IP");
}
return;

View File

@@ -100,7 +100,7 @@ public class AdasManager implements IAdasNetCommApi {
public synchronized void destory() {
if (mChannel != null) {
mChannel.onDisconnectRtp();
mChannel.disConnect();
mChannel.disconnect();
mChannel = null;
}
}
@@ -114,9 +114,9 @@ public class AdasManager implements IAdasNetCommApi {
}
@Override
public void disConnect() {
public void disconnect() {
if (mChannel != null) {
mChannel.disConnect();
mChannel.disconnect();
}
}

View File

@@ -17,7 +17,7 @@ public interface IAdasNetCommApi {
/**
* 与工控机断开连接
*/
void disConnect();
void disconnect();
/**
* 控制自动驾驶车辆 自动模式 自动驾驶

View File

@@ -14,7 +14,7 @@ public interface OnAdasConnectStatusListener {
* 指定IP或固定IP轮询ping集合中的IP 找不到可用的IP时会被调用
* 规则: TextUtils.isEmpty()
*/
void onAddressDisabled();
// void onAddressDisabled();
/**
* 与工控机链接状态变化

View File

@@ -65,6 +65,10 @@ public class Constants {
/**
* 正在搜索IP
*/
int SEARCH_ADDRESS = 0x04;
int SEARCH_ADDRESS = 0x03;
/**
* 找不到可用IP 传入的IP不可用或固定IP列表中所有IP不可用
*/
int NOT_FOUND_ADDRESS = 0x04;
}
}

View File

@@ -10,7 +10,8 @@ public final class Define {
@IntDef(flag = true, value = {Constants.IPC_CONNECTION_STATUS.CONNECTED,
Constants.IPC_CONNECTION_STATUS.DISCONNECTED,
Constants.IPC_CONNECTION_STATUS.CONNECTING,
Constants.IPC_CONNECTION_STATUS.SEARCH_ADDRESS})
Constants.IPC_CONNECTION_STATUS.SEARCH_ADDRESS,
Constants.IPC_CONNECTION_STATUS.NOT_FOUND_ADDRESS})
@Retention(RetentionPolicy.SOURCE)
public @interface IPCConnectionStatus {
}

View File

@@ -7,6 +7,8 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Future;
/**
@@ -19,6 +21,7 @@ public class IPCFixationIPHelper {
private volatile boolean isCallListener = false;//是否已经调用了回调
private volatile int unavailableCount = 0;//不可用IP个数
private volatile int queryCount;//要查询的IP个数
private Timer retryTimer;
public IPCFixationIPHelper(IIPCFixationIPListener listener) {
if (listener == null)
@@ -28,7 +31,12 @@ public class IPCFixationIPHelper {
public interface IIPCFixationIPListener {
/**
* 可连通的IP
* 开始查找可用IP
*/
void onStartFindIP();
/**
* 轮询结果可连通的IP
*
* @param ip 返回可用IP 当为null时表示无可用IP
*/
@@ -49,7 +57,12 @@ public class IPCFixationIPHelper {
/**
* 开始根据固定IP列表惊醒PING 方式查找可用工控机
*/
public void start(HashSet<String> ips) {
public void start(final HashSet<String> ips) {
listener.onStartFindIP();
if (retryTimer != null) {
retryTimer.cancel();
retryTimer = null;
}
if (ips == null || ips.isEmpty()) {
listener.onAvailableIP(null);
} else {
@@ -71,7 +84,7 @@ public class IPCFixationIPHelper {
interrupted();
}
} else {
allNotAvailable();
allNotAvailable(ips);
}
}
};
@@ -81,7 +94,7 @@ public class IPCFixationIPHelper {
}
}
private synchronized void allNotAvailable() {
private synchronized void allNotAvailable(final HashSet<String> ips) {
unavailableCount++;
CupidLogUtils.i(TAG, "unavailableCount=" + unavailableCount);
if (queryCount == unavailableCount) {
@@ -89,6 +102,16 @@ public class IPCFixationIPHelper {
isCallListener = true;
listener.onAvailableIP(null);
CupidLogUtils.i(TAG, "所有IP均不可用");
//当配置中的所有IP都ping不通时需要继续ping
if (retryTimer == null) {
retryTimer = new Timer();
retryTimer.schedule(new TimerTask() {
@Override
public void run() {
start(ips);
}
}, 5 * 1000L);//延时
}
}
}
}

View File

@@ -94,6 +94,8 @@ public class FpgaSocket implements IWebSocket {
private void connect() {
if (client != null && mWebSocket == null) {
if (mWebSocketConnectListener != null)
mWebSocketConnectListener.onConnecting();
if (request == null)
request = new Request.Builder()
.url(wsHost)
@@ -167,6 +169,11 @@ public class FpgaSocket implements IWebSocket {
public interface IWebSocketConnectListener {
/**
* 正在连接
*/
void onConnecting();
void onWebSocketConnectSuccess();
void onWebSocketConnectFailed(String t);
@@ -250,27 +257,41 @@ public class FpgaSocket implements IWebSocket {
public void onClosing(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
super.onClosing(webSocket, code, reason);
CupidLogUtils.e(TAG, "WebSocket onClosing= " + reason);
mWebSocket = null;
if (mWebSocketConnectListener != null)
mWebSocketConnectListener.onWebSocketConnectFailed(reason);
reconnect();
if (mWebSocket != null) {
mWebSocket.close(1000, null);
mWebSocket.cancel();
mWebSocket = null;
} else {
if (mWebSocketConnectListener != null)
mWebSocketConnectListener.onWebSocketConnectFailed(reason);
reconnect();
}
}
@Override
public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
super.onClosed(webSocket, code, reason);
CupidLogUtils.e(TAG, "WebSocket onClosed= " + reason);
mWebSocket = null;
if (mWebSocketConnectListener != null)
mWebSocketConnectListener.onWebSocketConnectFailed(reason);
reconnect();
if (mWebSocket != null) {
mWebSocket.close(1000, null);
mWebSocket.cancel();
mWebSocket = null;
} else {
if (mWebSocketConnectListener != null)
mWebSocketConnectListener.onWebSocketConnectFailed(reason);
reconnect();
}
}
@Override
public void onFailure(@NonNull WebSocket webSocket, @NonNull Throwable t, Response response) {
super.onFailure(webSocket, t, response);
CupidLogUtils.e(TAG, "WebSocket onFailure= " + t);
mWebSocket = null;
if (mWebSocket != null) {
mWebSocket.close(1000, null);
mWebSocket.cancel();
mWebSocket = null;
}
if (mWebSocketConnectListener != null)
mWebSocketConnectListener.onWebSocketConnectFailed(t.toString());
reconnect();