[Update]支持连接指定ip的server,支持client向server请求车牌号

This commit is contained in:
chenfufeng
2022-04-02 19:38:18 +08:00
parent 12077d768f
commit f33d5e9263
8 changed files with 57 additions and 27 deletions

View File

@@ -20,6 +20,8 @@ import com.mogo.telematic.client.status.ConnectState;
import java.util.Arrays;
import io.netty.channel.Channel;
public class NettyClientActivity extends AppCompatActivity implements View.OnClickListener, NettyClientListener<MogoProtocolMsg> {
private static final String TAG = "NettyClientActivity";
@@ -107,14 +109,14 @@ public class NettyClientActivity extends AppCompatActivity implements View.OnCli
}
@Override
public void onMessageResponseClient(MogoProtocolMsg msg, String sign) {
public void onMessageResponseClient(MogoProtocolMsg msg, String sign, Channel channel) {
String result = msg.toString();
Log.e(TAG, "onMessageResponse:" + result);
logRece(result);
}
@Override
public void onClientStatusConnectChanged(final int statusCode, final String sign) {
public void onClientStatusConnectChanged(final int statusCode, final String sign, Channel channel) {
runOnUiThread(() -> {
if (statusCode == ConnectState.STATUS_CONNECT_SUCCESS) {
Log.e(TAG, "STATUS_CONNECT_SUCCESS:");

View File

@@ -36,26 +36,26 @@ PASSWORD=xintai2018
RELEASE=true
# AI CLOUD 云平台
# 工具类
MOGO_UTILS_VERSION=1.3.50
MOGO_UTILS_VERSION=1.3.51
# 网络请求
MOGO_NETWORK_VERSION=1.3.50
MOGO_NETWORK_VERSION=1.3.51
# 网络DNS
MOGO_HTTPDNS_VERSION=1.3.50
MOGO_HTTPDNS_VERSION=1.3.51
# 鉴权
MOGO_PASSPORT_VERSION=1.3.50
MOGO_PASSPORT_VERSION=1.3.51
# 常链接
MOGO_SOCKET_VERSION=1.3.50
MOGO_SOCKET_VERSION=1.3.51
# 数据采集
MOGO_REALTIME_VERSION=1.3.50
MOGO_REALTIME_VERSION=1.3.51
# 探路,道路事件发布,获取
MOGO_TANLU_VERSION=1.3.50
MOGO_TANLU_VERSION=1.3.51
# 直播推流
MOGO_LIVE_VERSION=1.3.50
MOGO_LIVE_VERSION=1.3.51
# 直播拉流
MOGO_TRAFFICLIVE_VERSION=1.3.50
MOGO_TRAFFICLIVE_VERSION=1.3.51
# 定位服务
MOGO_LOCATION_VERSION=1.3.50
MOGO_LOCATION_VERSION=1.3.51
# 远程通讯模块
MOGO_TELEMATIC_VERSION=1.3.50
MOGO_TELEMATIC_VERSION=1.3.51
# v2x
MOGO_V2X_VERSION=1.3.50
MOGO_V2X_VERSION=1.3.51

View File

@@ -11,6 +11,7 @@ public class MogoProtocolMsg {
public static final int IDENTITY_REGIST = 2;
// 同步美化模式状态
public static final int SYNC_MODE_STATUS = 3;
public static final int REQ_CAR_NUMBER = 4;
private int protocolType;
private int bodyLength;

View File

@@ -263,6 +263,23 @@ public class NSDNettyManager {
}
}
/**
* 连接指定ip服务端
* @param ip
*/
public void connectSpecifiedServer(String ip, int port, String uuid, NettyClientListener listener) {
if (mNsdClient != null) {
// 停止局域网内扫描
mNsdClient.stopServiceDiscovery();
}
if (mNettyTcpClient != null && (mNettyTcpClient.isConnecting() || mNettyTcpClient.getConnectStatus())) {
// 断开已连接上的
mNettyTcpClient.disconnect();
mNettyTcpClient = null;
}
connectNettyServer(ip, port, uuid, listener);
}
public static String bytesToHexFun(byte[] bytes, int length) {
StringBuilder buf = new StringBuilder(length * 2);
for (int i = 0; i < length; i++) {// 使用String的format方法进行转换

View File

@@ -219,16 +219,16 @@ public class NettyTcpClient {
private NettyClientListener mProxyListener = new NettyClientListener() {
@Override
public void onMessageResponseClient(Object msg, String sign) {
public void onMessageResponseClient(Object msg, String sign, Channel channel) {
if (listener != null) {
listener.onMessageResponseClient(msg, sign);
listener.onMessageResponseClient(msg, sign, channel);
}
}
@Override
public void onClientStatusConnectChanged(int statusCode, String sign) {
public void onClientStatusConnectChanged(int statusCode, String sign, Channel channel) {
if (listener != null) {
listener.onClientStatusConnectChanged(statusCode, sign);
listener.onClientStatusConnectChanged(statusCode, sign, channel);
}
if (statusCode == ConnectState.STATUS_CONNECT_SUCCESS && !TextUtils.isEmpty(mSign)) {
byte[] signByteArr = mSign.getBytes();

View File

@@ -26,7 +26,7 @@ public class NsdClient {
private final Context mContext;
private final String mServiceName;
private final IServerFound mIServerFound;
private boolean isRegisterDiscovery;
/**
* 用来存储解析后的网络对象列表,包含完整数据
*/
@@ -57,6 +57,7 @@ public class NsdClient {
mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
initializeDiscoveryListener();
mNsdManager.discoverServices(NSD_SERVER_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
isRegisterDiscovery = true;
}
/**
@@ -67,13 +68,13 @@ public class NsdClient {
mDiscoveryListener = new NsdManager.DiscoveryListener() {
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
mNsdManager.stopServiceDiscovery(this);
isRegisterDiscovery = false;
Logger.e(TAG, "onStartDiscoveryFailed()");
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
mNsdManager.stopServiceDiscovery(this);
isRegisterDiscovery = false;
Logger.e(TAG, "onStopDiscoveryFailed()");
}
@@ -85,6 +86,7 @@ public class NsdClient {
@Override
public void onDiscoveryStopped(String serviceType) {
Logger.e(TAG, "onDiscoveryStopped()");
isRegisterDiscovery = false;
}
/**
@@ -142,10 +144,12 @@ public class NsdClient {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Logger.e(TAG, "onResolveFailed()");
isRegisterDiscovery = false;
}
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
isRegisterDiscovery = false;
int port = serviceInfo.getPort();
InetAddress host = serviceInfo.getHost();
String serviceName = serviceInfo.getServiceName();
@@ -165,7 +169,11 @@ public class NsdClient {
public void stopServiceDiscovery() {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
// 必须要加isRegisterDiscovery判断
// 否则停止扫描的时候会去mListenerMap查找是否存在不存在的话会崩溃
if (mNsdManager != null && isRegisterDiscovery) {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
}
}
public interface IServerFound {

View File

@@ -72,7 +72,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
@Override
public void channelActive(ChannelHandlerContext ctx) {
Logger.d(TAG, "channelActive");
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_SUCCESS, mSign);
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_SUCCESS, mSign, ctx.channel());
}
/**
@@ -99,7 +99,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
NettyTcpClient.sSERVER_SN = sn;
} else {
if (listener != null) {
listener.onMessageResponseClient(msg, mSign);
listener.onMessageResponseClient(msg, mSign, ctx.channel());
}
}
}
@@ -111,7 +111,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<MogoProtocol
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Logger.e(TAG, "NettyClientHandler#exceptionCaught is:" + cause.getMessage());
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_ERROR, mSign);
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_ERROR, mSign, ctx.channel());
cause.printStackTrace();
ctx.close();
}

View File

@@ -1,6 +1,8 @@
package com.mogo.telematic.client.listener;
import io.netty.channel.Channel;
/**
* TCP状态变化监听
*/
@@ -11,12 +13,12 @@ public interface NettyClientListener<T> {
* @param msg 消息
* @param sign tcp 客户端的标识,因为一个应用程序可能有很多个长链接
*/
void onMessageResponseClient(T msg, String sign);
void onMessageResponseClient(T msg, String sign, Channel channel);
/**
* 当服务状态发生变化时触发
* @param statusCode 状态变化
* @param sign tcp 客户端的标识,因为一个应用程序可能有很多个长链接
*/
void onClientStatusConnectChanged(int statusCode, String sign);
void onClientStatusConnectChanged(int statusCode, String sign, Channel channel);
}