添加obu tcp通信支持
This commit is contained in:
@@ -6,6 +6,7 @@ import com.mogo.utils.logger.Logger
|
||||
import com.zhidao.mogo.module.obu.ObuConstant
|
||||
import com.zhidao.mogo.module.obu.obu.bean.MogoObuEventInfo
|
||||
import com.zhidao.mogo.module.obu.socket.IUdpSocketCallback
|
||||
import com.zhidao.mogo.module.obu.socket.SimpleSocketManager
|
||||
import com.zhidao.mogo.module.obu.socket.UdpSocketManager
|
||||
import org.json.JSONObject
|
||||
import java.lang.StringBuilder
|
||||
@@ -19,11 +20,13 @@ import kotlin.experimental.xor
|
||||
* @author tongchenfei
|
||||
*/
|
||||
class HualiObu : BaseObu(), IUdpSocketCallback {
|
||||
private val socketManager = UdpSocketManager(this)
|
||||
// private val socketManager = UdpSocketManager(this)
|
||||
private val socketManager = SimpleSocketManager(this)
|
||||
|
||||
companion object {
|
||||
const val TAG = "HualiObu"
|
||||
const val IP_ADDRESS = "172.30.31.44"
|
||||
const val IP_ADDRESS = "192.168.8.10"
|
||||
// const val IP_ADDRESS = "192.168.43.206"
|
||||
const val PORT = 10005
|
||||
}
|
||||
|
||||
@@ -31,7 +34,7 @@ class HualiObu : BaseObu(), IUdpSocketCallback {
|
||||
|
||||
override fun init() {
|
||||
super.init()
|
||||
socketManager.receiveMsgFrom(IP_ADDRESS, PORT)
|
||||
socketManager.connect(IP_ADDRESS, PORT)
|
||||
Logger.d(TAG, "init")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.zhidao.mogo.module.obu.socket
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.HandlerThread
|
||||
import com.mogo.utils.logger.Logger
|
||||
import java.io.*
|
||||
import java.lang.Exception
|
||||
import java.net.InetAddress
|
||||
import java.net.Socket
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* socket连接简单实现,仅支持连接一路socket,只接受数据
|
||||
*/
|
||||
class SimpleSocketManager(private val callback: IUdpSocketCallback? = null) {
|
||||
companion object {
|
||||
const val TAG = "SimpleSocketManager"
|
||||
}
|
||||
|
||||
private val dataCacheThread = HandlerThread("obu-data-cache")
|
||||
private val dataCacheHandler: Handler
|
||||
|
||||
init {
|
||||
dataCacheThread.start()
|
||||
dataCacheHandler = Handler(dataCacheThread.looper)
|
||||
}
|
||||
|
||||
private var socket: Socket? = null
|
||||
private val socketExecutor = Executors.newCachedThreadPool()
|
||||
fun connect(ip: String, port: Int) {
|
||||
socketExecutor.execute {
|
||||
realConnect(ip, port)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun realConnect(ip: String, port: Int) {
|
||||
try {
|
||||
Logger.d(TAG, "socket connect===ip: $ip, port: $port ====++++")
|
||||
val address = InetAddress.getByName(ip)
|
||||
Logger.d(TAG, "address: $address")
|
||||
socket = Socket(address, port)
|
||||
socket?.let {
|
||||
Logger.d(TAG, "socket连接基本成功,准备接受数据")
|
||||
// 超时时间60秒
|
||||
// it.soTimeout = 60_000
|
||||
val buffer = ByteArray(2048)
|
||||
var result: Int
|
||||
do {
|
||||
result = it.getInputStream().read(buffer)
|
||||
if (result != -1) {
|
||||
Logger.d(TAG, "收到obu数据")
|
||||
val msg = ByteArray(buffer.size)
|
||||
System.arraycopy(buffer, 0, msg, 0, buffer.size)
|
||||
dataCacheHandler.post {
|
||||
callback?.onMessageReceived(msg)
|
||||
}
|
||||
}
|
||||
} while (result != -1)
|
||||
|
||||
}
|
||||
}catch (e:Exception){
|
||||
e.printStackTrace()
|
||||
Logger.e(TAG, e, "udp连接异常")
|
||||
Thread.sleep(10_000)
|
||||
connect(ip,port)
|
||||
}
|
||||
}
|
||||
|
||||
fun sendMessage(msg: String) {
|
||||
Logger.d(TAG, "暂不支持发送消息")
|
||||
}
|
||||
}
|
||||
@@ -300,7 +300,9 @@ public class V2XObuEventScenario extends AbsV2XScenario<V2XObuEventEntity> imple
|
||||
private void sendBroadcastToAdas(String status) {
|
||||
if (status.equals(CALL_ADAS_SHOW_OPTIMAL)) {
|
||||
// 给adas发送显示绿波的广播,同时自己也要开始划线
|
||||
drawLine();
|
||||
if(DebugConfig.getObuType() == DebugConfig.OBU_TYPE_CIDI) {
|
||||
drawLine();
|
||||
}
|
||||
} else {
|
||||
// 给adas发送隐藏绿波的广播,同时自己也需要结束划线动画
|
||||
hideLine();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/dp_210"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:background="@drawable/v2x_bg_simple_obu">
|
||||
android:background="@drawable/module_v2x_shadow_bkg">
|
||||
|
||||
<!-- app:roundLayoutRadius="@dimen/dp_20"-->
|
||||
|
||||
|
||||
Reference in New Issue
Block a user