From 163612b45d704abb71604af267aec6273f68182d Mon Sep 17 00:00:00 2001 From: xinfengkun Date: Thu, 29 Sep 2022 16:38:53 +0800 Subject: [PATCH] =?UTF-8?q?[change]=20=E5=88=86=E5=8F=91=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E4=BC=98=E5=8C=96=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE=E5=88=86=E5=8F=91=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhidao/support/adas/high/AdasChannel.java | 87 ++++++++++++++----- .../adas/high/common/CupidLogUtils.java | 2 +- .../adas/high/thread/DispatchHandler.java | 19 ++-- 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/AdasChannel.java b/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/AdasChannel.java index 023d43b64a..36c75f03ed 100644 --- a/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/AdasChannel.java +++ b/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/AdasChannel.java @@ -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 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 entry : dispatchHandlers.entrySet()) { + DispatchHandler dispatchHandler = entry.getValue(); + if (dispatchHandler != null) { + dispatchHandler.stop(); + } } } diff --git a/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/common/CupidLogUtils.java b/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/common/CupidLogUtils.java index 2252539d46..36f5e83d71 100644 --- a/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/common/CupidLogUtils.java +++ b/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/common/CupidLogUtils.java @@ -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() { diff --git a/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/thread/DispatchHandler.java b/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/thread/DispatchHandler.java index b2f03d3177..5badcbe4ab 100644 --- a/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/thread/DispatchHandler.java +++ b/libraries/mogo-adas/src/main/java/com/zhidao/support/adas/high/thread/DispatchHandler.java @@ -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); } /**