合并大唐高鸿obu,增加debug入口,去掉了vrMode按钮入口

This commit is contained in:
tongchenfei
2020-12-16 18:27:02 +08:00
parent 111ac63e38
commit 4231f006c1
18 changed files with 665 additions and 39 deletions

View File

@@ -32,6 +32,7 @@ import com.mogo.utils.ProcessUtils;
import com.mogo.utils.UiThreadHandler;
import com.mogo.utils.logger.LogLevel;
import com.mogo.utils.logger.Logger;
import com.mogo.utils.storage.SharedPrefsMgr;
import com.squareup.leakcanary.LeakCanary;
import com.zhidao.boot.persistent.lib.PersistentManager;
import com.zhidao.mogo.module.left.panel.LeftPanelConst;
@@ -141,7 +142,7 @@ public class MogoApplication extends AbsMogoApplication {
DebugConfig.setNeedLoadGuideModule( BuildConfig.IS_NEED_LOAD_GUIDE_MODULE );
DebugConfig.setNeedHideAdasWhenShare( BuildConfig.IS_NEED_HIDE_ADAS_WHEN_SHARE );
DebugConfig.setNeedUploadCoordinatesInTime( BuildConfig.IS_NEED_UPLOAD_COORDINATES_IN_TIME );
// DebugConfig.setObuType( BuildConfig.OBU_TYPE );
DebugConfig.setObuType(SharedPrefsMgr.getInstance(this).getInt("OBU_TYPE", DebugConfig.OBU_TYPE_CIDI));
}
@Override

View File

@@ -90,6 +90,11 @@ public class DebugConfig {
*/
public static final int OBU_TYPE_HUALI = 2;
/**
* 大唐高鸿obu 11.11国展
*/
public static final int OBU_TYPE_GOHIGH = 3;
/**
* 获取网络环境类型
*

View File

@@ -4,15 +4,18 @@ import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
@@ -207,6 +210,8 @@ public class EntranceFragment extends MvpFragment<EntranceView, EntrancePresente
private TextView seekHelpNum;
private View debugPanel;
private AdasNoticeHelper adasNoticeHelper = new AdasNoticeHelper();
private CameraLiveNoticeHelper mCameraLiveNoticeHelper = new CameraLiveNoticeHelper();
@@ -234,6 +239,7 @@ public class EntranceFragment extends MvpFragment<EntranceView, EntrancePresente
seekHelpGroup = findViewById(R.id.module_ext_id_seek_help_notice_group);
seekHelpNum = findViewById(R.id.module_ext_id_seek_help_notice_number);
debugPanel = findViewById(R.id.debugPanel);
NoMapTopViewShaderHelper.getInstance().initShaderView(findViewById(R.id.module_ext_id_top_container_shader));
@@ -472,20 +478,41 @@ public class EntranceFragment extends MvpFragment<EntranceView, EntrancePresente
}
});
debugPanel.setOnClickListener(view -> {
if (SystemClock.elapsedRealtime() - lastDebugPanelClickTime > 1000) {
debugPanelClickCount = 1;
}else{
debugPanelClickCount++;
}
lastDebugPanelClickTime = SystemClock.elapsedRealtime();
if (debugPanelClickCount == 10) {
// show panel
debugPanelGroup.setVisibility(View.VISIBLE);
}
});
initDebugPanel();
}
private int debugPanelClickCount = 0;
private long lastDebugPanelClickTime = 0;
private EditText etTimes;
private Group groupFix;
private void enterVrMode(){
tvEnterVrMode.setVisibility(View.GONE);
// tvEnterVrMode.setVisibility(View.GONE);
mMove2CurrentLocation.setVisibility(View.GONE);
mUploadRoadCondition.setVisibility(View.GONE);
mWeatherContainer.setVisibility(View.GONE);
mMsgContainer.setVisibility(View.GONE);
groupUserHead.setVisibility(View.GONE);
tvExitVrMode.setVisibility(View.VISIBLE);
// tvExitVrMode.setVisibility(View.VISIBLE);
TopViewAnimHelper.getInstance().enterVrMode();
TopViewNoLinkageAnimHelper.getInstance().enterVrMode();
mNaviInfo = vrModeNavInfoView;
@@ -497,14 +524,14 @@ public class EntranceFragment extends MvpFragment<EntranceView, EntrancePresente
private void exitVrMode(){
EntranceViewHolder.getInstance().forceHideNoticeView();
tvEnterVrMode.setVisibility(View.VISIBLE);
// tvEnterVrMode.setVisibility(View.VISIBLE);
mMove2CurrentLocation.setVisibility(View.VISIBLE);
mUploadRoadCondition.setVisibility(View.VISIBLE);
groupUserHead.setVisibility(View.VISIBLE);
// mWeatherContainer.setVisibility(View.VISIBLE);
// mMsgContainer.setVisibility(View.VISIBLE);
tvExitVrMode.setVisibility(View.GONE);
// tvExitVrMode.setVisibility(View.GONE);
TopViewAnimHelper.getInstance().exitVrMode();
TopViewNoLinkageAnimHelper.getInstance().exitVrMode();
mNaviInfo = animNavInfoView;
@@ -1219,4 +1246,68 @@ public class EntranceFragment extends MvpFragment<EntranceView, EntrancePresente
public void onLocationChanged(MogoLocation location) {
vrModeNavInfoView.refreshCurrentSpeed((int) (location.getSpeed() * 3.6F));
}
private Group debugPanelGroup;
private ImageButton ibDebugPanelClose;
private Button btnOpenLog,btnCloseLog,btnOpenV2XPanel;
private RadioButton rbCidi,rbHuali, rbGohigh;
private void initDebugPanel() {
debugPanelGroup = findViewById(R.id.groupDebugPanel);
ibDebugPanelClose = findViewById(R.id.ibDebugPanelClose);
btnOpenLog = findViewById(R.id.btnOpenLog);
btnCloseLog = findViewById(R.id.btnCloseLog);
btnOpenV2XPanel = findViewById(R.id.btnOpenV2xPanel);
rbCidi = findViewById(R.id.rbCidiObu);
rbHuali = findViewById(R.id.rbHualiObu);
rbGohigh = findViewById(R.id.rbGoHighObu);
ibDebugPanelClose.setOnClickListener(v -> debugPanelGroup.setVisibility(View.GONE));
btnOpenLog.setOnClickListener(v->{
Intent intent = new Intent("com.mogo.ACTION");
intent.putExtra("oper", 1);
getContext().sendBroadcast(intent);
debugPanelGroup.setVisibility(View.GONE);
});
btnCloseLog.setOnClickListener(v -> {
Intent intent = new Intent("com.mogo.ACTION");
intent.putExtra("oper", 2);
getContext().sendBroadcast(intent);
debugPanelGroup.setVisibility(View.GONE);
});
btnOpenV2XPanel.setOnClickListener(v -> {
Intent intent = new Intent("com.v2x.test_panel_control");
intent.putExtra("TextPanelOpenStatus", true);
getContext().sendBroadcast(intent);
debugPanelGroup.setVisibility(View.GONE);
});
switch (DebugConfig.getObuType()) {
case DebugConfig.OBU_TYPE_CIDI:
rbCidi.setChecked(true);
break;
case DebugConfig.OBU_TYPE_HUALI:
rbHuali.setChecked(true);
break;
default:
rbGohigh.setChecked(true);
break;
}
rbCidi.setOnClickListener(v -> exchangeObuType(DebugConfig.OBU_TYPE_CIDI));
rbHuali.setOnClickListener(v -> exchangeObuType(DebugConfig.OBU_TYPE_HUALI));
rbGohigh.setOnClickListener(v -> exchangeObuType(DebugConfig.OBU_TYPE_GOHIGH));
}
private void exchangeObuType(int obuType) {
SharedPrefsMgr.getInstance(getContext()).putInt("OBU_TYPE", obuType);
DebugConfig.setObuType(obuType);
Intent intent = new Intent("com.mogo.launcher.v2x.action.EXCHANGE_OBU_TYPE");
intent.putExtra("obuType", obuType);
getContext().sendBroadcast(intent);
}
}

View File

@@ -0,0 +1,140 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<View
android:id="@+id/debugPanelBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88000000" />
<ImageButton
android:id="@+id/ibDebugPanelClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/module_common_close_selector"
app:layout_constraintRight_toRightOf="@id/debugPanelBg"
app:layout_constraintTop_toTopOf="@id/debugPanelBg" />
<TextView
android:id="@+id/tvLogSetTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="LOG SET"
android:textColor="#fff"
android:textSize="30sp"
app:layout_constraintLeft_toLeftOf="@id/debugPanelBg"
app:layout_constraintTop_toTopOf="@id/debugPanelBg" />
<Button
android:id="@+id/btnOpenLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="打开日志"
android:textColor="#fff"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="@id/tvLogSetTitle"
app:layout_constraintTop_toBottomOf="@id/tvLogSetTitle" />
<Button
android:id="@+id/btnCloseLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="关闭日志"
android:textColor="#fff"
android:textSize="20sp"
app:layout_constraintLeft_toRightOf="@id/btnOpenLog"
app:layout_constraintTop_toBottomOf="@id/tvLogSetTitle" />
<TextView
android:id="@+id/tvV2xSetTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="V2X SET"
android:textColor="#fff"
android:textSize="30sp"
app:layout_constraintLeft_toLeftOf="@id/tvLogSetTitle"
app:layout_constraintTop_toBottomOf="@id/btnOpenLog" />
<Button
android:id="@+id/btnOpenV2xPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="打开V2X面板"
android:textColor="#fff"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="@id/tvV2xSetTitle"
app:layout_constraintTop_toBottomOf="@id/tvV2xSetTitle" />
<TextView
android:id="@+id/tvObuSetTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="OBU SET (设置完成后需要重启)"
android:textColor="#fff"
app:layout_constraintLeft_toLeftOf="@id/btnOpenV2xPanel"
app:layout_constraintTop_toBottomOf="@id/btnOpenV2xPanel"
android:textSize="30sp" />
<RadioGroup
android:id="@+id/rgObuSet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="@id/tvObuSetTitle"
app:layout_constraintTop_toBottomOf="@id/tvObuSetTitle">
<RadioButton
android:id="@+id/rbCidiObu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Cidi\n(北小营)"
android:textColor="#fff"
android:textSize="20sp" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rbHualiObu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="华砺智行\n(苏州)"
android:textColor="#fff"
android:textSize="20sp" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rbGoHighObu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大唐高鸿\n(11.11国展)"
android:textColor="#fff"
android:textSize="20sp" />
</RadioGroup>
<androidx.constraintlayout.widget.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/groupDebugPanel"
android:visibility="gone"
tools:visibility="visible"
app:constraint_referenced_ids="btnCloseLog,btnOpenLog,btnOpenV2xPanel,tvObuSetTitle,tvLogSetTitle,tvV2xSetTitle,rgObuSet,ibDebugPanelClose,debugPanelBg" />
</merge>

View File

@@ -219,6 +219,7 @@
android:layout_marginBottom="@dimen/module_ext_enter_vr_mode_margin_bottom"
android:background="@drawable/module_ext_dw_upload_road_condition_bkg"
android:gravity="center"
android:visibility="gone"
android:text="VR"
android:textColor="#fff"
android:textSize="@dimen/module_ext_enter_vr_mode_text_size"
@@ -352,4 +353,9 @@
android:visibility="gone"
app:constraint_referenced_ids="etTimes,btnFix" />
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/include_debug_panel" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -295,4 +295,8 @@
android:visibility="gone"
app:constraint_referenced_ids="btnShowDrawableTipNoSize,btnShowDrawableTip,btnShowTextTip,btnDebugCtrlNaviView,btnDebugCtrlSubView,btnDebugCtrlTopView,btnDebugAddBottomLayerView" />
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/include_debug_panel" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -8,15 +8,15 @@
android:id="@+id/module_ext_id_weather_container"
android:layout_width="wrap_content"
android:layout_height="@dimen/module_ext_height"
android:layout_marginStart="@dimen/module_ext_weather_margin_start"
android:background="@drawable/module_ext_shadow_bkg"
android:gravity="center"
android:paddingStart="@dimen/module_ext_weather_container_paddingLeft"
android:paddingEnd="@dimen/module_ext_weather_container_paddingRight"
android:visibility="invisible"
app:layout_goneMarginLeft="@dimen/module_ext_notice_margin_start"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/module_ext_id_msg"
android:layout_marginStart="@dimen/module_ext_weather_margin_start"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginLeft="@dimen/module_ext_notice_margin_start"
tools:visibility="visible">
<ImageView
@@ -61,9 +61,9 @@
android:id="@+id/module_ext_id_msg_counter"
android:layout_width="@dimen/module_ext_msg_counter_width"
android:layout_height="@dimen/module_ext_msg_counter_height"
android:layout_alignParentRight="true"
android:background="@drawable/module_ext_drawable_msg_bkg"
android:gravity="center"
android:layout_alignParentRight="true"
android:textColor="#FFFFFF"
android:textSize="@dimen/module_ext_msg_counter_textSize"
tools:text="···" />
@@ -73,26 +73,35 @@
android:id="@+id/ivUserHeadBoard"
android:layout_width="@dimen/module_ext_user_avator_board_size"
android:layout_height="@dimen/module_ext_user_avator_board_size"
android:background="@drawable/model_ext_default_user_head_board"
android:layout_marginRight="@dimen/module_common_shadow_width_pos"
android:layout_marginTop="@dimen/module_common_shadow_width_pos"
android:layout_marginRight="@dimen/module_common_shadow_width_pos"
android:background="@drawable/model_ext_default_user_head_board"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivUserHeadImg"
android:layout_width="@dimen/module_ext_user_avator_size"
android:layout_height="@dimen/module_ext_user_avator_size"
android:src="@drawable/model_ext_default_user_head"
tools:visibility="visible"
app:layout_constraintBottom_toBottomOf="@id/ivUserHeadBoard"
app:layout_constraintLeft_toLeftOf="@id/ivUserHeadBoard"
app:layout_constraintTop_toTopOf="@id/ivUserHeadBoard"
app:layout_constraintRight_toRightOf="@id/ivUserHeadBoard"
app:layout_constraintBottom_toBottomOf="@id/ivUserHeadBoard"/>
app:layout_constraintTop_toTopOf="@id/ivUserHeadBoard"
tools:visibility="visible" />
<androidx.constraintlayout.widget.Group
android:id="@+id/groupUserHead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/groupUserHead"
app:constraint_referenced_ids="ivUserHeadBoard,ivUserHeadImg" />
<View
android:id="@+id/debugPanel"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginTop="@dimen/dp_200"
android:background="#00000000"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</merge>

View File

@@ -20,18 +20,22 @@ class ObuManager {
private lateinit var obu: IObu
private lateinit var context: Context
private var listener:IObuCallback? = null
fun init(context: Context) {
Logger.d(TAG, "init=======")
Logger.d(TAG, "init=======${DebugConfig.getObuType()}")
this.context = context
obu = if (DebugConfig.getObuType() == DebugConfig.OBU_TYPE_CIDI) {
CidiObu()
} else {
HualiObu()
obu = when (DebugConfig.getObuType()) {
DebugConfig.OBU_TYPE_CIDI -> CidiObu()
DebugConfig.OBU_TYPE_HUALI -> HualiObu()
else -> NetCarObu()
}
obu.init()
obu.init(context)
}
fun registerObuDataChangedListener(listener: IObuCallback?) {
this.listener = listener
if (listener != null) {
obu.registerObuCallback(listener)
if(DebugConfig.isUseMockObuData() ) {
@@ -41,5 +45,9 @@ class ObuManager {
}
}
fun resetObuType(obuType: Int) {
Logger.d(TAG, "resetObuType: $obuType")
obu.release()
}
}

View File

@@ -1,11 +1,16 @@
package com.zhidao.mogo.module.obu.obu
import android.content.Context
abstract class BaseObu : IObu {
var callback: IObuCallback? = null
override fun init() {
override fun init(context: Context) {
}
override fun registerObuCallback(callback: IObuCallback) {
this.callback = callback
}
override fun release() {
}
}

View File

@@ -1,5 +1,6 @@
package com.zhidao.mogo.module.obu.obu
import android.content.Context
import android.os.Handler
import android.os.Message
import com.mogo.commons.debug.DebugConfig
@@ -26,8 +27,8 @@ class CidiObu : BaseObu(), Handler.Callback, OnMessageReceiveListener {
}
private val handler = Handler(this)
override fun init() {
super.init()
override fun init(context: Context) {
super.init(context)
// 初始化sdk注册数据回调等信息
V2xController.getInstance().setMessageReceiveListener(this)
V2xController.getInstance().init()
@@ -120,4 +121,11 @@ class CidiObu : BaseObu(), Handler.Callback, OnMessageReceiveListener {
}
return false
}
override fun release() {
super.release()
handler.removeMessages(MSG_MONITOR_OBU_STATUS)
V2xController.getInstance().setMessageReceiveListener(null)
V2xController.getInstance().release()
}
}

View File

@@ -1,5 +1,6 @@
package com.zhidao.mogo.module.obu.obu
import android.content.Context
import android.os.Handler
import android.os.SystemClock
import android.util.ArrayMap
@@ -62,8 +63,8 @@ class HualiObu : BaseObu(), IUdpSocketCallback {
private val handler = Handler()
override fun init() {
super.init()
override fun init(context: Context) {
super.init(context)
socketManager.connect(IP_ADDRESS, PORT)
Logger.d(TAG, "init")
}
@@ -308,4 +309,9 @@ class HualiObu : BaseObu(), IUdpSocketCallback {
private fun convertTwoUnSignInt(byteArray: ByteArray): Int =
(byteArray[0].toInt() shl 24) or (byteArray[1].toInt() and 0xFF) or (byteArray[2].toInt() shl 8) or (byteArray[3].toInt() and 0xFF)
override fun release() {
super.release()
socketManager.release()
}
}

View File

@@ -1,5 +1,7 @@
package com.zhidao.mogo.module.obu.obu
import android.content.Context
/**
* Obu基本方法
* @author tongchenfei
@@ -8,11 +10,15 @@ interface IObu {
/**
* 初始化
*/
fun init()
fun init(context: Context)
/**
* 注册数据回调
*/
fun registerObuCallback(callback: IObuCallback)
/**
* release
*/
fun release()
}

View File

@@ -0,0 +1,236 @@
package com.zhidao.mogo.module.obu.obu
import android.content.Context
import android.os.Handler
import android.os.Message
import com.mogo.utils.logger.Logger
import com.mogo.utils.network.utils.GsonUtil
import com.zhidao.mogo.module.obu.ObuConstant.TYPE_OPTIMAL_SPEED_ADVISORY
import com.zhidao.mogo.module.obu.ObuConstant.TYPE_ROAD_USER_COLLISION_WARNING
import com.zhidao.mogo.module.obu.obu.bean.MogoObuEventInfo
import com.zhidao.mogo.module.obu.obu.bean.MogoObuTrafficLightInfo
import com.zhidao.mogo.module.obu.socket.IUdpSocketStatusCallback
import com.zhidao.mogo.module.obu.socket.UdpSocketManager
import org.json.JSONObject
/**
* 大唐高鸿obu
*
* @author tongchenfei
*/
class NetCarObu : BaseObu(), IUdpSocketStatusCallback, Handler.Callback {
companion object {
const val TAG = "NetCarObu"
const val MSG_SEND_ACTIVATION_MSG = 1001
const val MSG_RECONNECT = 1002
const val SEND_ACTIVATION_MSG_DELAY = 4 * 1000L
const val RECONNECT_DELAY = 10_000L
const val TARGET_IP = "192.168.118.104"
const val TARGET_PORT = 50500
}
private lateinit var socketManager: UdpSocketManager
private val handler = Handler(this)
override fun init(context: Context) {
super.init(context)
Logger.d(TAG, "NetCar obu init")
socketManager = UdpSocketManager(context = context, useDefaultAddress = true, callback = this)
}
override fun isReady() {
Logger.d(TAG, "udp is ready")
// 1. 向obu注册
sendRegisterMsg()
}
private var unique: String = ""
override fun onMessageReceived(msg: ByteArray) {
val response = String(msg)
val json = JSONObject(response)
Logger.d(TAG, "收到消息: $json")
when (json.optInt("tag")) {
1002 -> {
// 查询状态成功,准备注册
socketManager.defaultAddress?.let {
socketManager.sendMsgTo(GsonUtil.jsonFromObject(Request(2001, NcsRegisteredData(it.ip, it.port))), TARGET_IP, TARGET_PORT)
startReconnectTiming()
}
}
2002 -> {
// 响应注册
val rsp = json.optInt("rsp", -1)
if (rsp == 0) {
// 注册成功, 等待激活
unique = json.optString("unique", "")
startActivationTiming()
}
}
2004 -> {
// 响应解除注册
val rsp = json.optInt("rsp", -1)
if (rsp == 0) {
// 解除注册成功
socketManager.release()
}
}
2006 -> {
// 响应激活
val rsp = json.optInt("rsp", -1)
if (rsp != 0) {
// 激活失败,需要重新激活
sendActivationMsg()
} else {
// 激活成功,重新计时
startActivationTiming()
}
}
2104 -> {
// 红绿灯倒数秒数上报(车速引导)
val dataJson = json.optJSONObject("data")
if (dataJson != null) {
// val lightInfo = dataJson.optJSONObject("phase")
val lightInfoArray = dataJson.optJSONArray("phase")
if (lightInfoArray != null && lightInfoArray.length() > 0) {
Logger.d(TAG, "lightInfo: $lightInfoArray")
val lightInfo = lightInfoArray.getJSONObject(0)
val trafficLightInfo = MogoObuTrafficLightInfo()
val color = lightInfo.optInt("color")
if (color in 1..3) {
// 有效颜色数据
trafficLightInfo.lightStatus = when (color) {
2 -> "R"
3 -> "Y"
else -> "G"
}
trafficLightInfo.surplusTime = lightInfo.optInt("time", 0).toString()
callback?.onTrafficLightInfoCallback(trafficLightInfo)
// val end = lightInfo.optInt("guide_speed_max", -1)
// val start = lightInfo.optInt("guide_speed_min", -1)
// if (start != -1 && end != -1) {
// val eventInfo = MogoObuEventInfo()
// eventInfo.mogoEventId = TYPE_OPTIMAL_SPEED_ADVISORY
// eventInfo.describe = "前方路口,建议车速 $start 到 $end km/h"
// callback?.let {
// handler.post {
// it.onEventInfoCallback(eventInfo)
// }
// }
// }
}
} else {
Logger.e(TAG, "红绿灯数据为空")
}
} else {
Logger.e(TAG, "红绿灯数据为data空")
}
}
2105 -> {
// 事件上报
val event = json.optJSONObject("data")
if (event != null) {
val type = event.optInt("type", 0) % 100000
Logger.d(TAG, "事件id取模后: $type")
if (type == 4012) {
// 前方行人预警
val eventInfo = MogoObuEventInfo()
eventInfo.mogoEventId = TYPE_ROAD_USER_COLLISION_WARNING
handler.post {
callback?.onEventInfoCallback(eventInfo)
}
} else if (type == 3005) {
// 红绿灯变灯提醒
val eventInfo = MogoObuEventInfo()
eventInfo.type = "vip变灯提醒"
eventInfo.typeCode = "vip变灯提醒"
eventInfo.describe = "将为您变灯vip车辆可优先通行"
handler.post {
callback?.onEventInfoCallback(eventInfo)
}
}
} else {
Logger.e(TAG, "事件数据为空")
}
}
}
}
private fun sendRegisterMsg() {
socketManager.defaultAddress?.let {
socketManager.sendMsgTo(GsonUtil.jsonFromObject(Request<NcsRegisteredData>(2001, NcsRegisteredData(it.ip, it.port))), TARGET_IP, TARGET_PORT)
startReconnectTiming()
}
}
/**
* 开启激活计时
*/
private fun startActivationTiming() {
handler.removeMessages(MSG_SEND_ACTIVATION_MSG)
handler.removeMessages(MSG_RECONNECT)
handler.sendEmptyMessageDelayed(MSG_SEND_ACTIVATION_MSG, SEND_ACTIVATION_MSG_DELAY)
}
private fun startReconnectTiming() {
handler.removeMessages(MSG_RECONNECT)
handler.sendEmptyMessageDelayed(MSG_RECONNECT, RECONNECT_DELAY)
}
/**
* 发送激活数据
*/
private fun sendActivationMsg() {
Logger.d(TAG, "准备发送激活消息====")
socketManager.sendMsgTo(GsonUtil.jsonFromObject(NcsActivationRequest(unique = unique)), TARGET_IP, TARGET_PORT)
startReconnectTiming()
}
override fun handleMessage(msg: Message): Boolean {
return when (msg.what) {
MSG_SEND_ACTIVATION_MSG -> {
sendActivationMsg()
true
}
MSG_RECONNECT -> {
if (!handler.hasMessages(MSG_SEND_ACTIVATION_MSG)) {
// 没有等待激活,就进行重试
Logger.d(TAG, "准备重连===")
sendRegisterMsg()
}
true
}
else -> false
}
}
val firstMsg = "{\"tag\":1001}"
override fun release() {
super.release()
// 解除注册
socketManager.sendMsgTo(GsonUtil.jsonFromObject(NcsUnRegisteredRequest(unique = unique)), TARGET_IP, TARGET_PORT)
}
data class Request<T>(val tag: Int, val data: T)
/**
* 注册请求data
*/
data class NcsRegisteredData(val ip: String, val port: Int)
/**
* 解注册请求request
*/
data class NcsUnRegisteredRequest(val tag: Int = 2003, val unique: String)
/**
* 激活请求
*/
data class NcsActivationRequest(val tag: Int = 2005, val unique: String)
}

View File

@@ -0,0 +1,13 @@
package com.zhidao.mogo.module.obu.socket
/**
* udp 数据回调
*/
interface ISocketMsgCallback {
/**
* udp过来的字节码数据目前已知数据类型就是ByteArray
*
* @param msg udp发过来的数据
*/
fun onMessageReceived(msg: ByteArray)
}

View File

@@ -0,0 +1,11 @@
package com.zhidao.mogo.module.obu.socket
/**
* udp状态回调
*/
interface IUdpSocketStatusCallback:ISocketMsgCallback {
/**
* udp连接准备完毕指的是准备好接收数据了
*/
fun isReady()
}

View File

@@ -72,4 +72,9 @@ class SimpleSocketManager(private val callback: IUdpSocketCallback? = null) {
fun sendMessage(msg: String) {
Logger.d(TAG, "暂不支持发送消息")
}
fun release(){
socket?.close()
dataCacheHandler.looper.quitSafely()
}
}

View File

@@ -1,9 +1,13 @@
package com.zhidao.mogo.module.obu.socket
import android.content.Context
import android.net.wifi.WifiManager
import android.os.Handler
import android.os.HandlerThread
import android.util.ArrayMap
import com.mogo.utils.NetworkUtils
import com.mogo.utils.logger.Logger
import java.lang.Thread.sleep
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
@@ -12,9 +16,11 @@ import java.util.concurrent.Executors
/**
* udp socket 管理类统一管理udp socket数据接收和发送
*/
class UdpSocketManager(private val callback: IUdpSocketCallback? = null) {
class UdpSocketManager(val context: Context? = null, useDefaultAddress: Boolean = false, private val callback: IUdpSocketStatusCallback? = null) {
companion object {
const val TAG = "Mogo-UdpSocketManager"
const val DEFAULT_PORT = 50501
const val DEFAULT_IP = "192.168.8.169"
}
private val socketMap = ArrayMap<SimpleAddress, DatagramSocket>()
@@ -23,20 +29,66 @@ class UdpSocketManager(private val callback: IUdpSocketCallback? = null) {
private val dataCacheThread = HandlerThread("obu-data-cache")
private val dataCacheHandler: Handler
var defaultAddress:SimpleAddress? = null
init {
dataCacheThread.start()
dataCacheHandler = Handler(dataCacheThread.looper)
if (useDefaultAddress) {
// 获取连接的wifi的ip地址
if (context == null) {
throw IllegalArgumentException("do not find context at constructor")
}else{
// defaultAddress = SimpleAddress(DEFAULT_IP, DEFAULT_PORT)
// receiveMsgFrom(DEFAULT_IP, DEFAULT_PORT)
if (NetworkUtils.isConnectedWifi(context)) {
// 已经判定连接到了wifi
generateDefaultAddress()
}else{
// 还没连接到wifi需要等待此处采用每隔一秒检查一次不用再注册相关监听广播
dataCacheHandler.post {
while (!NetworkUtils.isConnectedWifi(context)) {
sleep(1000)
}
// 已经连接到wifi
generateDefaultAddress()
}
}
}
}
}
@Volatile
private var isConnected = false
fun sendMsgTo(msg: String, ip: String, port: Int) {
Logger.d(TAG, "sendMsg: $msg to $ip:$port")
isConnected = true
val address = SimpleAddress(ip, port)
socketExecutor.execute(UdpSenderRunnable(address, msg))
}
/**
* 仅获取连接wifi后的ip地址使用默认端口进行数据接收
*/
private fun generateDefaultAddress(){
context?.let {
val wifiManager = it.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo = wifiManager.connectionInfo
val ip = intIp2StringIp(wifiInfo.ipAddress)
Logger.d(TAG, "获取到本机ip地址: $ip")
defaultAddress = SimpleAddress(ip, DEFAULT_PORT)
// 生成了默认地址,开始准备接受数据
receiveMsgFrom(ip, DEFAULT_PORT)
}
}
private fun intIp2StringIp(ip: Int):String {
return "${ip and 0xff}.${(ip shr 8) and 0xff}.${(ip shr 16) and 0xff}.${(ip shr 24) and 0xff}"
}
private var isReady = false
fun receiveMsgFrom(ip: String, port: Int) {
isConnected = true
val address = SimpleAddress(ip, port)
@@ -78,6 +130,10 @@ class UdpSocketManager(private val callback: IUdpSocketCallback? = null) {
if (socket.isClosed) {
break
}
if (!isReady) {
isReady = true
callback?.isReady()
}
Logger.d(TAG, "准备接受消息====$address")
socket.receive(packet)
val msg = ByteArray(buffer.size)
@@ -91,4 +147,5 @@ class UdpSocketManager(private val callback: IUdpSocketCallback? = null) {
}
}

View File

@@ -1,7 +1,9 @@
package com.mogo.module.v2x;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
@@ -67,19 +69,24 @@ public class V2XObuManager implements IObuCallback, Handler.Callback {
private static final int MSG_HIDE_TRAFFIC_LIGHT = 1001;
private static final long DEFAULT_HIDE_TRAFFIC_LIGHT_DELAY = 1500L;
private Handler handler = new Handler(this);
private final Handler handler = new Handler(this);
private final ObuTypeExchangeReceiver obuTypeExchangeReceiver = new ObuTypeExchangeReceiver();
private ObuManager obuManager;
public void init(Context context) {
Logger.d(MODULE_NAME, "obuManager初始化--");
ObuManager obuManager = new ObuManager();
obuManager = new ObuManager();
obuManager.init(context);
obuManager.registerObuDataChangedListener(this);
IntentFilter filter = new IntentFilter("com.mogo.launcher.v2x.action.EXCHANGE_OBU_TYPE");
context.registerReceiver(obuTypeExchangeReceiver, filter);
}
/**
* 用来处理30秒内不重复播报的情况
*/
private Map<String, Long> intervalMap = new ArrayMap<>();
private final Map<String, Long> intervalMap = new ArrayMap<>();
private int parseObuEvent(String type) {
switch (type) {
@@ -99,7 +106,7 @@ public class V2XObuManager implements IObuCallback, Handler.Callback {
}
}
private MogoLocation[] historyPath = new MogoLocation[2];
private final MogoLocation[] historyPath = new MogoLocation[2];
private float computeCarAngle(MogoLocation location) {
float angle = 0f;
@@ -177,7 +184,7 @@ public class V2XObuManager implements IObuCallback, Handler.Callback {
}
// int eventType = parseObuEvent(info.getTypeCode());
int eventType = info.getMogoEventId();
if (eventType == ObuConstant.TYPE_OPTIMAL_SPEED_ADVISORY&& DebugConfig.getObuType() == DebugConfig.OBU_TYPE_CIDI) {
if (eventType == ObuConstant.TYPE_OPTIMAL_SPEED_ADVISORY && DebugConfig.getObuType() == DebugConfig.OBU_TYPE_CIDI) {
// 加一个容错机制如果已经驶过绿波车速路口那么再收到绿波车速obu事件就不再上报
MogoLocation currentLocation = V2XLocationListener.getInstance().getLastCarLocation();
double eventAngle = DrivingDirectionUtils.getDegreeOfCar2Poi(
@@ -192,7 +199,7 @@ public class V2XObuManager implements IObuCallback, Handler.Callback {
return;
}
}
if (SystemClock.elapsedRealtime() - last > DEFAULT_INTERVAL_TIME||DebugConfig.getObuType() == DebugConfig.OBU_TYPE_HUALI) {
if (SystemClock.elapsedRealtime() - last > DEFAULT_INTERVAL_TIME || DebugConfig.getObuType() == DebugConfig.OBU_TYPE_HUALI) {
// 距离上次记录超过三十秒,继续相关逻辑,如果不超过三十秒,忽略此次事件
// 华砺智行obu暂时去掉此判断
intervalMap.put(info.getTypeCode(), SystemClock.elapsedRealtime());
@@ -254,17 +261,17 @@ public class V2XObuManager implements IObuCallback, Handler.Callback {
default:
break;
}
}else{
Logger.d(TAG,"未超过时限,不展示事件");
} else {
Logger.d(TAG, "未超过时限,不展示事件");
}
}
@Override
public void onLocationInfoCallback( MogoObuLocationInfo locationInfo) {
public void onLocationInfoCallback(MogoObuLocationInfo locationInfo) {
if (ObuConfig.useObuLocation) {
MogoLocation currentLocation = new MogoLocation();
double coor[] = CoordinateUtils.transformFromWGSToGCJ( locationInfo.getLat(), locationInfo.getLon() );
double coor[] = CoordinateUtils.transformFromWGSToGCJ(locationInfo.getLat(), locationInfo.getLon());
currentLocation.setLatitude(coor[0]);
currentLocation.setLongitude(coor[1]);
@@ -287,4 +294,12 @@ public class V2XObuManager implements IObuCallback, Handler.Callback {
sendTrafficLightStatusToAdas(CALL_ADAS_SHOW_TRAFFIC_LIGHT, trafficLightInfo);
}
}
class ObuTypeExchangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int obuType = intent.getIntExtra("obuType", DebugConfig.OBU_TYPE_CIDI);
obuManager.resetObuType(obuType);
}
}
}