[change] 分发线程业务优化,新增可配置分发线程
This commit is contained in:
@@ -13,7 +13,6 @@ import static com.zhidao.support.adas.high.chain.AdasChain.CHAIN_LINK_LOG_CONNEC
|
||||
import static com.zhidao.support.adas.high.chain.AdasChain.CHAIN_LINK_LOG_WEB_SOCKET_AUTOPILOT;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -44,7 +43,9 @@ import com.zhidao.support.adas.high.thread.DispatchHandler;
|
||||
import com.zhjt.service.chain.ChainLog;
|
||||
import com.zhjt.service.chain.TracingConstants;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
@@ -67,13 +68,11 @@ import okio.ByteString;
|
||||
*/
|
||||
public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnectListener, IPCFixationIPHelper.IIPCFixationIPListener, DispatchHandler.OnDispatchHandlerListener, SubscribeInterface.OnSubscribeInterfaceListener {
|
||||
private static final String TAG = AdasChannel.class.getSimpleName();
|
||||
private static final String THREAD_NAME_DISPATCH_EVENT = "IPCEventDispatchHandler";//除点云单独拆分线程以外都是用此名称
|
||||
private static final String THREAD_NAME_DISPATCH_POINT_CLOUD = "IPCPointCloudDispatchHandler";
|
||||
private FpgaSocket mSocket;
|
||||
private RawUnpack rawUnpack;//业务数据拆包
|
||||
private RawPack rawPack;//数据打包
|
||||
private DispatchHandler dispatchHandler;//分发
|
||||
private DispatchHandler dispatchHandlerPointCloud;//原始的点云数据分发
|
||||
private DispatchHandler defaultDispatchHandler;//默认分发线程分发
|
||||
private final Map<MessagePad.MessageType, DispatchHandler> dispatchHandlers = new HashMap<>();//其他分发线程
|
||||
private Timer checkCompatibilityTimer;//检查版本兼容性定时器 连接成功后5秒内等待工控机发送配置信息
|
||||
/**
|
||||
* 与工控机链接状态
|
||||
@@ -166,8 +165,53 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
|
||||
//消息工厂
|
||||
myMessageFactory = new MyMessageFactory();
|
||||
//启用线程分发
|
||||
dispatchHandler = new DispatchHandler(THREAD_NAME_DISPATCH_EVENT, this);
|
||||
dispatchHandlerPointCloud = new DispatchHandler(THREAD_NAME_DISPATCH_POINT_CLOUD, this);
|
||||
defaultDispatchHandler = new DispatchHandler(MessagePad.MessageType.MsgTypeDefault, this);//默认分发线程 不要添加到Map中
|
||||
initOtherDispatchHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化其他分发线程
|
||||
*/
|
||||
private void initOtherDispatchHandler() {
|
||||
//点云透传
|
||||
dispatchHandlers.put(MessagePad.MessageType.MsgTypePointCloud, new DispatchHandler(MessagePad.MessageType.MsgTypePointCloud, this));
|
||||
//车前引导线
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeTrajectory, new DispatchHandler(MessagePad.MessageType.MsgTypeTrajectory, this));
|
||||
//障碍物信息
|
||||
dispatchHandlers.put(MessagePad.MessageType.MsgTypeTrackedObjects, new DispatchHandler(MessagePad.MessageType.MsgTypeTrackedObjects, this));
|
||||
//惯导信息
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeGnssInfo, new DispatchHandler(MessagePad.MessageType.MsgTypeGnssInfo, this));
|
||||
//底盘信息
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeVehicleState, new DispatchHandler(MessagePad.MessageType.MsgTypeVehicleState, this));
|
||||
//自动驾驶状态
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeAutopilotState, new DispatchHandler(MessagePad.MessageType.MsgTypeAutopilotState, this));
|
||||
//监控事件报告
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeReportMessage, new DispatchHandler(MessagePad.MessageType.MsgTypeReportMessage, this));
|
||||
//感知红绿灯
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypePerceptionTrafficLight, new DispatchHandler(MessagePad.MessageType.MsgTypePerceptionTrafficLight, this));
|
||||
//他车轨迹预测
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypePredictionObstacleTrajectory, new DispatchHandler(MessagePad.MessageType.MsgTypePredictionObstacleTrajectory, this));
|
||||
//Planning障碍物
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypePlanningObjects, new DispatchHandler(MessagePad.MessageType.MsgTypePlanningObjects, this));
|
||||
//自动驾驶设备基础信息请求
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeBasicInfoReq, new DispatchHandler(MessagePad.MessageType.MsgTypeBasicInfoReq, this));
|
||||
//车机基础信息应答
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeCarConfigResp, new DispatchHandler(MessagePad.MessageType.MsgTypeCarConfigResp, this));
|
||||
//数据采集结果
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeRecordResult, new DispatchHandler(MessagePad.MessageType.MsgTypeRecordResult, this));
|
||||
//自动驾驶路径应答
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeGlobalPathResp, new DispatchHandler(MessagePad.MessageType.MsgTypeGlobalPathResp, this));
|
||||
//预警数据
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeWarn, new DispatchHandler(MessagePad.MessageType.MsgTypeWarn, this));
|
||||
//到站提醒
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeArrivalNotification, new DispatchHandler(MessagePad.MessageType.MsgTypeArrivalNotification, this));
|
||||
//状态查询应答
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeStatusQueryResp, new DispatchHandler(MessagePad.MessageType.MsgTypeStatusQueryResp, this));
|
||||
//数据采集配置
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypeRecordDataConfigResp, new DispatchHandler(MessagePad.MessageType.MsgTypeRecordDataConfigResp, this));
|
||||
//Planning决策状态
|
||||
// dispatchHandlers.put(MessagePad.MessageType.MsgTypePlanningDecisionState, new DispatchHandler(MessagePad.MessageType.MsgTypePlanningDecisionState, this));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -292,14 +336,12 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
|
||||
if (!adasOptions.isClient())
|
||||
ReceiveTimeoutManager.getInstance().refreshLast(header.getTimestamp());
|
||||
// CupidLogUtils.w("--->websocket byte read header = " + messageType.toString());
|
||||
//判断是否是司机屏幕,是否切换分发线程
|
||||
if (header.getMsgType() == MessageType.TYPE_RECEIVE_POINT_CLOUD.typeCode) {
|
||||
if (dispatchHandlerPointCloud != null) {
|
||||
dispatchHandlerPointCloud.sendRawMessage(raw);
|
||||
}
|
||||
DispatchHandler handler = dispatchHandlers.get(header.getMsgType());
|
||||
if (handler != null) {
|
||||
handler.sendRawMessage(raw);
|
||||
} else {
|
||||
if (dispatchHandler != null)
|
||||
dispatchHandler.sendRawMessage(raw);
|
||||
if (defaultDispatchHandler != null)
|
||||
defaultDispatchHandler.sendRawMessage(raw);
|
||||
}
|
||||
} else {
|
||||
callError(raw.getProtocolStatus(), byteString.toByteArray());
|
||||
@@ -317,15 +359,15 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
|
||||
* @param raw
|
||||
*/
|
||||
@Override
|
||||
public void onDispatchRaw(String threadName, RawData raw) {
|
||||
public void onDispatchRaw(MessagePad.MessageType type, RawData raw) {
|
||||
// CupidLogUtils.i(TAG, "onDispatchRaw=" + Thread.currentThread().getName() + " TID=" + android.os.Process.myTid());
|
||||
try {
|
||||
//分发点云原始数据
|
||||
if (THREAD_NAME_DISPATCH_POINT_CLOUD.equals(threadName)) {
|
||||
if (type == MessagePad.MessageType.MsgTypePointCloud) {
|
||||
int len = raw.getPackageLengthValue() - raw.getOffsetValue();
|
||||
byte[] bytes = new byte[len];
|
||||
System.arraycopy(raw.originalData.toByteArray(), raw.getOffsetValue(), bytes, 0, len);
|
||||
mAdasListener.onPointCloud(bytes);
|
||||
CupidLogUtils.i(TAG, "收到点云数据" + raw.getPackageLengthValue());
|
||||
} else {
|
||||
if (rawUnpack != null) {
|
||||
if (raw.getProtocolStatus() == ProtocolStatus.SUCCEED) {
|
||||
@@ -527,11 +569,14 @@ public class AdasChannel implements IAdasNetCommApi, FpgaSocket.IWebSocketConnec
|
||||
|
||||
@Override
|
||||
public void stopDispatchHandler() {
|
||||
if (dispatchHandler != null) {
|
||||
dispatchHandler.stop();
|
||||
if (defaultDispatchHandler != null) {
|
||||
defaultDispatchHandler.stop();
|
||||
}
|
||||
if (dispatchHandlerPointCloud != null) {
|
||||
dispatchHandlerPointCloud.stop();
|
||||
for (Map.Entry<MessagePad.MessageType, DispatchHandler> entry : dispatchHandlers.entrySet()) {
|
||||
DispatchHandler dispatchHandler = entry.getValue();
|
||||
if (dispatchHandler != null) {
|
||||
dispatchHandler.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public class CupidLogUtils {
|
||||
* @param isEnableLog true开启 false关闭
|
||||
*/
|
||||
public static void setEnableLog(boolean isEnableLog) {
|
||||
// mIsEnableLog = isEnableLog; TODO 临时注释
|
||||
mIsEnableLog = isEnableLog;
|
||||
}
|
||||
|
||||
public static boolean isEnableLog() {
|
||||
|
||||
@@ -11,24 +11,25 @@ import com.zhidao.support.adas.high.protocol.RawData;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import mogo.telematics.pad.MessagePad;
|
||||
|
||||
/**
|
||||
* 将WebSocket线程分发到工作线程
|
||||
*/
|
||||
public class DispatchHandler {
|
||||
public static final int WHAT_DISPATCH_RAW = 0x01;//分发消息 what
|
||||
private final String name;
|
||||
private final MessagePad.MessageType messageType;
|
||||
private final OnDispatchHandlerListener listener;
|
||||
private HandlerThread mThread;
|
||||
private BaseHandler mBaseHandler;
|
||||
|
||||
public interface OnDispatchHandlerListener {
|
||||
void onDispatchRaw(String threadName, RawData raw);
|
||||
void onDispatchRaw(MessagePad.MessageType name, RawData raw);
|
||||
}
|
||||
|
||||
|
||||
public DispatchHandler(String name, @NonNull OnDispatchHandlerListener listener) {
|
||||
public DispatchHandler(MessagePad.MessageType messageType, @NonNull OnDispatchHandlerListener listener) {
|
||||
this.listener = listener;
|
||||
this.name = name;
|
||||
this.messageType = messageType;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
@@ -44,12 +45,12 @@ public class DispatchHandler {
|
||||
|
||||
public void sendRawMessage(RawData raw) {
|
||||
if (mThread == null) {
|
||||
String name = "Dispatch-" + messageType.name().replace("MsgType", "");
|
||||
mThread = new HandlerThread(name);
|
||||
mThread.start();
|
||||
initHandler(mThread.getLooper());
|
||||
}
|
||||
Message msg = Message.obtain();
|
||||
msg.what = WHAT_DISPATCH_RAW;
|
||||
msg.obj = raw;
|
||||
mBaseHandler.sendMessage(msg);
|
||||
}
|
||||
@@ -63,11 +64,7 @@ public class DispatchHandler {
|
||||
* @param msg
|
||||
*/
|
||||
protected void handleMessage(Message msg) {
|
||||
if (msg.what == WHAT_DISPATCH_RAW) {
|
||||
if (listener != null) {
|
||||
listener.onDispatchRaw(name, (RawData) msg.obj);
|
||||
}
|
||||
}
|
||||
listener.onDispatchRaw(messageType, (RawData) msg.obj);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user