重构obu模块,新增udp通信
This commit is contained in:
@@ -26,12 +26,12 @@ import kotlin.random.Random
|
||||
*/
|
||||
object MockUtil {
|
||||
const val TAG = "MockUtil"
|
||||
private lateinit var dataCallback : OnMessageReceiveListener
|
||||
private var dataCallback : OnMessageReceiveListener?=null
|
||||
|
||||
private lateinit var intervalObs:Disposable
|
||||
private lateinit var intervalObs2:Disposable
|
||||
|
||||
fun init(context: Context, callback: OnMessageReceiveListener){
|
||||
fun init(context: Context, callback: OnMessageReceiveListener? = null){
|
||||
Logger.d(TAG, "使用模拟obu数据===")
|
||||
dataCallback = callback
|
||||
val api = ARouter.getInstance().build(MogoServicePaths.PATH_SERVICE_APIS).navigation(context) as IMogoServiceApis
|
||||
@@ -45,7 +45,7 @@ object MockUtil {
|
||||
0 -> "已更改前方红绿灯状态,可优先通行"
|
||||
else -> "已增加前方绿灯时间,可优先通行"
|
||||
}
|
||||
dataCallback.showCarEventInfo(carEventInfo)
|
||||
dataCallback?.showCarEventInfo(carEventInfo)
|
||||
}
|
||||
|
||||
view.findViewById<Button>(R.id.btnRoadCondition).setOnClickListener {
|
||||
@@ -53,7 +53,7 @@ object MockUtil {
|
||||
carEventInfo.type = "行人碰撞预警"
|
||||
carEventInfo.typeCode = "39"
|
||||
carEventInfo.describe = "这个应该是随便写,反正也不用"
|
||||
dataCallback.showCarEventInfo(carEventInfo)
|
||||
dataCallback?.showCarEventInfo(carEventInfo)
|
||||
}
|
||||
api.windowManagerApi.addView(view, 500, 300, false)
|
||||
intervalMockData()
|
||||
@@ -92,21 +92,21 @@ object MockUtil {
|
||||
1 -> "建议50km/h车速行驶"
|
||||
else -> "建议30km/h ~ 50km/h 车速行驶"
|
||||
}
|
||||
dataCallback.showCarEventInfo(carEventInfo)
|
||||
dataCallback?.showCarEventInfo(carEventInfo)
|
||||
}
|
||||
1->{
|
||||
// 前方急刹预警
|
||||
carEventInfo.type = "紧急制动预警"
|
||||
carEventInfo.typeCode = "06"
|
||||
carEventInfo.describe = "这个应该是随便写,反正也不用"
|
||||
dataCallback.showCarEventInfo(carEventInfo)
|
||||
dataCallback?.showCarEventInfo(carEventInfo)
|
||||
}
|
||||
2->{
|
||||
// 行人碰撞预警
|
||||
carEventInfo.type = "行人碰撞预警"
|
||||
carEventInfo.typeCode = "39"
|
||||
carEventInfo.describe = "这个应该是随便写,反正也不用"
|
||||
dataCallback.showCarEventInfo(carEventInfo)
|
||||
dataCallback?.showCarEventInfo(carEventInfo)
|
||||
}
|
||||
3 -> {
|
||||
// vip变灯提醒
|
||||
@@ -117,7 +117,7 @@ object MockUtil {
|
||||
0 -> "已更改前方红绿灯状态,可优先通行"
|
||||
else -> "已增加前方绿灯时间,可优先通行"
|
||||
}
|
||||
dataCallback.showCarEventInfo(carEventInfo)
|
||||
dataCallback?.showCarEventInfo(carEventInfo)
|
||||
}
|
||||
else->{
|
||||
// 其他,不处理
|
||||
@@ -135,7 +135,7 @@ object MockUtil {
|
||||
trafficLightInfo.lightStatus = "R"
|
||||
for (i in 0..5) {
|
||||
trafficLightInfo.surplusTime = (5-i).toString()
|
||||
dataCallback.showTrafficLightInfo(trafficLightInfo)
|
||||
dataCallback?.showTrafficLightInfo(trafficLightInfo)
|
||||
Thread.sleep(1000)
|
||||
}
|
||||
// 停5秒
|
||||
@@ -144,7 +144,7 @@ object MockUtil {
|
||||
trafficLightInfo.lightStatus = "Y"
|
||||
for (i in 0..5) {
|
||||
trafficLightInfo.surplusTime = (5-i).toString()
|
||||
dataCallback.showTrafficLightInfo(trafficLightInfo)
|
||||
dataCallback?.showTrafficLightInfo(trafficLightInfo)
|
||||
Thread.sleep(1000)
|
||||
}
|
||||
// 停5秒
|
||||
@@ -153,7 +153,7 @@ object MockUtil {
|
||||
trafficLightInfo.lightStatus = "G"
|
||||
for (i in 0..5) {
|
||||
trafficLightInfo.surplusTime = (5-i).toString()
|
||||
dataCallback.showTrafficLightInfo(trafficLightInfo)
|
||||
dataCallback?.showTrafficLightInfo(trafficLightInfo)
|
||||
Thread.sleep(1000)
|
||||
}
|
||||
Logger.d(TAG, "红绿灯模拟数据发送结束====")
|
||||
|
||||
@@ -1,136 +1,47 @@
|
||||
package com.zhidao.mogo.module.obu
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.Message
|
||||
import android.view.View
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.alibaba.android.arouter.facade.annotation.Route
|
||||
import com.mogo.commons.debug.DebugConfig
|
||||
import com.mogo.map.listener.IMogoMapListener
|
||||
import com.mogo.map.location.IMogoLocationListener
|
||||
import com.mogo.map.marker.IMogoMarkerClickListener
|
||||
import com.mogo.map.navi.IMogoNaviListener
|
||||
import com.mogo.service.MogoServicePaths
|
||||
import com.mogo.service.module.IMogoModuleLifecycle
|
||||
import com.mogo.service.obu.IMogoObuDataChangedListener
|
||||
import com.mogo.service.obu.IMogoObuManager
|
||||
import com.mogo.utils.logger.Logger
|
||||
import com.zhidao.mogo.module.obu.obu.CidiObu
|
||||
import com.zhidao.mogo.module.obu.obu.HualiObu
|
||||
import com.zhidao.mogo.module.obu.obu.IObu
|
||||
import com.zhidao.mogo.module.obu.obu.IObuCallback
|
||||
import com.zhidao.smartv2x.api.V2xController
|
||||
import com.zhidao.smartv2x.model.obu.CarEventInfo
|
||||
import com.zhidao.smartv2x.model.obu.CarLocationInfo
|
||||
import com.zhidao.smartv2x.model.obu.TrafficLightInfo
|
||||
|
||||
/**
|
||||
* obu provider
|
||||
*
|
||||
* @author tongchenfei
|
||||
*/
|
||||
@Route(path = MogoServicePaths.PATH_OBU)
|
||||
class ObuManager: IMogoObuManager,Handler.Callback {
|
||||
class ObuManager {
|
||||
companion object{
|
||||
const val TAG = "ObuManger"
|
||||
const val MSG_MONITOR_OBU_STATUS = 1001
|
||||
const val DEFAULT_MONITOR_TIME = 10_000L
|
||||
}
|
||||
private var dataChangedListener: IMogoObuDataChangedListener? = null
|
||||
|
||||
private var handler = Handler(Looper.getMainLooper(), this)
|
||||
// private var handler = Handler(Looper.getMainLooper(), this)
|
||||
|
||||
override fun init(context: Context) {
|
||||
private lateinit var obu:IObu
|
||||
|
||||
fun init(context: Context) {
|
||||
Logger.d(TAG,"init=======")
|
||||
if (DebugConfig.isUseMockObuData()) {
|
||||
MockUtil.init(context,this)
|
||||
MockUtil.init(context)
|
||||
} else {
|
||||
// 初始化sdk,注册数据回调等信息
|
||||
V2xController.getInstance().setMessageReceiveListener(this)
|
||||
V2xController.getInstance().init()
|
||||
// V2xController.getInstance().setMessageReceiveListener(this@ObuManager)
|
||||
handler.sendEmptyMessageDelayed(MSG_MONITOR_OBU_STATUS, DEFAULT_MONITOR_TIME)
|
||||
}
|
||||
}
|
||||
|
||||
override fun registerObuDataChangedListener(listener: IMogoObuDataChangedListener?) {
|
||||
this.dataChangedListener = listener
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆位置信息回调接口
|
||||
*/
|
||||
override fun showCarLocationInfo(info: CarLocationInfo?) {
|
||||
Logger.v(TAG, "thread: ${Thread.currentThread()} showCarLocationInfo: ${info?.carId}")
|
||||
if(!DebugConfig.isUseMockObuData()) {
|
||||
// 不使用模拟数据时候不做此处理
|
||||
// 由于obu会频繁回调此接口,故10秒收不到此数据,则认为obu连接不正常,就断开重连一下
|
||||
handler.removeMessages(MSG_MONITOR_OBU_STATUS)
|
||||
handler.sendEmptyMessageDelayed(MSG_MONITOR_OBU_STATUS, DEFAULT_MONITOR_TIME)
|
||||
}
|
||||
|
||||
handler.post{
|
||||
dataChangedListener?.showCarLocationInfo(info)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UDP 收发消息回调接口
|
||||
*/
|
||||
override fun showOtherInfo(info: String?) {
|
||||
Logger.d(TAG, "thread: ${Thread.currentThread()} showOtherInfo: $info")
|
||||
handler.post{
|
||||
dataChangedListener?.showOtherInfo(info)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 交通信号灯信息回调接口
|
||||
* 17号路口红绿灯倒计时出现了,如果[TrafficLightInfo.lightStatus]为0,或者说,不为G/Y/R时,此条红绿灯数据无效
|
||||
*/
|
||||
override fun showTrafficLightInfo(info: TrafficLightInfo?) {
|
||||
Logger.d(TAG, "thread: ${Thread.currentThread()} showTrafficLightInfo: $info")
|
||||
Logger.d(TAG, "isTrafficLightInfoValid: ${isTrafficLightInfoValid(info)}")
|
||||
if(isTrafficLightInfoValid(info)) {
|
||||
handler.post {
|
||||
dataChangedListener?.showTrafficLightInfo(info)
|
||||
obu = if(DebugConfig.getObuType() == DebugConfig.OBU_TYPE_CIDI){
|
||||
CidiObu()
|
||||
}else{
|
||||
HualiObu()
|
||||
}
|
||||
obu.init()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断红绿灯信息是否有效
|
||||
*
|
||||
* @return true 有效
|
||||
*/
|
||||
private fun isTrafficLightInfoValid(info:TrafficLightInfo?):Boolean{
|
||||
return info == null||(info.lightStatus=="G"||info.lightStatus=="Y"||info.lightStatus=="R")
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景触发事件信息回调接口
|
||||
*/
|
||||
override fun showCarEventInfo(info: CarEventInfo?) {
|
||||
Logger.d(TAG, "thread: ${Thread.currentThread()} showCarEventInfo: ${info?.type}")
|
||||
handler.post {
|
||||
dataChangedListener?.showCarEventInfo(info)
|
||||
fun registerObuDataChangedListener(listener: IObuCallback?) {
|
||||
if (listener != null) {
|
||||
obu.registerObuCallback(listener)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param msg A [Message][android.os.Message] object
|
||||
* @return True if no further handling is desired
|
||||
*/
|
||||
override fun handleMessage(msg: Message?): Boolean {
|
||||
if (msg?.what == MSG_MONITOR_OBU_STATUS) {
|
||||
// obu未正常连接,需要重新连接
|
||||
Logger.d(TAG,"obu未正常连接,需要重新连接")
|
||||
V2xController.getInstance().release()
|
||||
V2xController.getInstance().setMessageReceiveListener(this)
|
||||
V2xController.getInstance().init()
|
||||
handler.sendEmptyMessageDelayed(MSG_MONITOR_OBU_STATUS, DEFAULT_MONITOR_TIME)
|
||||
// V2xController.getInstance().release()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.zhidao.mogo.module.obu.obu
|
||||
|
||||
open class BaseObu : IObu {
|
||||
protected var callback: IObuCallback? = null
|
||||
override fun init() {
|
||||
}
|
||||
|
||||
override fun registerObuCallback(callback: IObuCallback) {
|
||||
this.callback = callback
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.zhidao.mogo.module.obu.obu
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Message
|
||||
import com.mogo.commons.debug.DebugConfig
|
||||
import com.mogo.utils.logger.Logger
|
||||
import com.zhidao.mogo.module.obu.obu.bean.MogoObuEventInfo
|
||||
import com.zhidao.mogo.module.obu.obu.bean.MogoObuLocationInfo
|
||||
import com.zhidao.mogo.module.obu.obu.bean.MogoObuTrafficLightInfo
|
||||
import com.zhidao.smartv2x.api.V2xController
|
||||
import com.zhidao.smartv2x.listener.OnMessageReceiveListener
|
||||
import com.zhidao.smartv2x.model.obu.CarEventInfo
|
||||
import com.zhidao.smartv2x.model.obu.CarLocationInfo
|
||||
import com.zhidao.smartv2x.model.obu.TrafficLightInfo
|
||||
|
||||
/**
|
||||
* 西迪obu
|
||||
*
|
||||
* @author tongchenfei
|
||||
*/
|
||||
class CidiObu : BaseObu(), Handler.Callback, OnMessageReceiveListener {
|
||||
companion object {
|
||||
const val TAG = "CidiObu"
|
||||
const val MSG_MONITOR_OBU_STATUS = 1001
|
||||
const val DEFAULT_MONITOR_TIME = 10_000L
|
||||
}
|
||||
|
||||
private val handler = Handler(this)
|
||||
override fun init() {
|
||||
super.init()
|
||||
// 初始化sdk,注册数据回调等信息
|
||||
V2xController.getInstance().setMessageReceiveListener(this)
|
||||
V2xController.getInstance().init()
|
||||
handler.sendEmptyMessageDelayed(MSG_MONITOR_OBU_STATUS, DEFAULT_MONITOR_TIME)
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆位置信息回调接口
|
||||
*/
|
||||
override fun showCarLocationInfo(info: CarLocationInfo?) {
|
||||
Logger.v(TAG, "thread: ${Thread.currentThread()} showCarLocationInfo: ${info?.carId}")
|
||||
if (!DebugConfig.isUseMockObuData()) {
|
||||
// 不使用模拟数据时候不做此处理
|
||||
// 由于obu会频繁回调此接口,故10秒收不到此数据,则认为obu连接不正常,就断开重连一下
|
||||
handler.removeMessages(MSG_MONITOR_OBU_STATUS)
|
||||
handler.sendEmptyMessageDelayed(MSG_MONITOR_OBU_STATUS, DEFAULT_MONITOR_TIME)
|
||||
}
|
||||
|
||||
handler.post {
|
||||
if (info == null) {
|
||||
callback?.onLocationInfoCallback(null)
|
||||
}else {
|
||||
callback?.onLocationInfoCallback(MogoObuLocationInfo(info))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UDP 收发消息回调接口
|
||||
*/
|
||||
override fun showOtherInfo(info: String?) {
|
||||
Logger.d(TAG, "thread: ${Thread.currentThread()} showOtherInfo: $info")
|
||||
}
|
||||
|
||||
/**
|
||||
* 交通信号灯信息回调接口
|
||||
* 17号路口红绿灯倒计时出现了,如果[TrafficLightInfo.lightStatus]为0,或者说,不为G/Y/R时,此条红绿灯数据无效
|
||||
*/
|
||||
override fun showTrafficLightInfo(info: TrafficLightInfo?) {
|
||||
Logger.d(TAG, "thread: ${Thread.currentThread()} showTrafficLightInfo: $info")
|
||||
Logger.d(TAG, "isTrafficLightInfoValid: ${isTrafficLightInfoValid(info)}")
|
||||
if (isTrafficLightInfoValid(info)) {
|
||||
handler.post {
|
||||
if (info == null) {
|
||||
callback?.onTrafficLightInfoCallback(null)
|
||||
}else{
|
||||
callback?.onTrafficLightInfoCallback(MogoObuTrafficLightInfo(info))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断红绿灯信息是否有效
|
||||
*
|
||||
* @return true 有效
|
||||
*/
|
||||
private fun isTrafficLightInfoValid(info: TrafficLightInfo?): Boolean {
|
||||
return info == null || (info.lightStatus == "G" || info.lightStatus == "Y" || info.lightStatus == "R")
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景触发事件信息回调接口
|
||||
*/
|
||||
override fun showCarEventInfo(info: CarEventInfo?) {
|
||||
Logger.d(TAG, "thread: ${Thread.currentThread()} showCarEventInfo: ${info?.type}")
|
||||
handler.post {
|
||||
if (info == null) {
|
||||
callback?.onEventInfoCallback(null)
|
||||
}else{
|
||||
callback?.onEventInfoCallback(MogoObuEventInfo(info))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param msg A [Message][android.os.Message] object
|
||||
* @return True if no further handling is desired
|
||||
*/
|
||||
override fun handleMessage(msg: Message?): Boolean {
|
||||
if (msg?.what == MSG_MONITOR_OBU_STATUS) {
|
||||
// obu未正常连接,需要重新连接
|
||||
Logger.d(TAG, "obu未正常连接,需要重新连接")
|
||||
V2xController.getInstance().release()
|
||||
V2xController.getInstance().setMessageReceiveListener(this)
|
||||
V2xController.getInstance().init()
|
||||
handler.sendEmptyMessageDelayed(MSG_MONITOR_OBU_STATUS, DEFAULT_MONITOR_TIME)
|
||||
// V2xController.getInstance().release()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.zhidao.mogo.module.obu.obu
|
||||
|
||||
import com.mogo.utils.logger.Logger
|
||||
import com.zhidao.mogo.module.obu.socket.IUdpSocketCallback
|
||||
import com.zhidao.mogo.module.obu.socket.UdpSocketManager
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
/**
|
||||
* 华砺智行obu 苏州演示项目用到的
|
||||
*
|
||||
* @author tongchenfei
|
||||
*/
|
||||
class HualiObu : BaseObu(),IUdpSocketCallback {
|
||||
private val socketManager = UdpSocketManager(this)
|
||||
companion object{
|
||||
const val TAG = "HualiObu"
|
||||
const val IP_ADDRESS = "172.30.33.38"
|
||||
const val PORT = 10005
|
||||
}
|
||||
override fun init() {
|
||||
super.init()
|
||||
socketManager.receiveMsgFrom(IP_ADDRESS, PORT)
|
||||
socketManager.sendMsgTo("Hello ssokit", IP_ADDRESS, PORT)
|
||||
Logger.d(TAG, "init")
|
||||
thread {
|
||||
var count = 0
|
||||
while (true) {
|
||||
Logger.d(TAG,"准备发送测试数据===")
|
||||
socketManager.sendMsgTo("Hello ssokit===$count", IP_ADDRESS, PORT)
|
||||
count++
|
||||
Thread.sleep(1000)
|
||||
if (count == 1000) {
|
||||
Logger.d(TAG, "测试数据结束")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMessageReceived(msg: ByteArray) {
|
||||
// todo 处理数据
|
||||
val m = String(msg)
|
||||
Logger.d(TAG, "onMessageReceived: $msg")
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.zhidao.mogo.module.obu.obu
|
||||
|
||||
/**
|
||||
* Obu基本方法
|
||||
* @author tongchenfei
|
||||
*/
|
||||
interface IObu {
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
fun init()
|
||||
|
||||
/**
|
||||
* 注册数据回调
|
||||
*/
|
||||
fun registerObuCallback(callback: IObuCallback)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.zhidao.mogo.module.obu.obu
|
||||
|
||||
import com.zhidao.mogo.module.obu.obu.bean.MogoObuEventInfo
|
||||
import com.zhidao.mogo.module.obu.obu.bean.MogoObuLocationInfo
|
||||
import com.zhidao.mogo.module.obu.obu.bean.MogoObuTrafficLightInfo
|
||||
|
||||
/**
|
||||
* obu数据回调,目前是根据西迪的数据进行的封装
|
||||
*
|
||||
* @author tongchenfei
|
||||
*/
|
||||
interface IObuCallback {
|
||||
/**
|
||||
* obu事件回调
|
||||
* @param eventInfo 事件信息
|
||||
*/
|
||||
fun onEventInfoCallback(eventInfo:MogoObuEventInfo?)
|
||||
|
||||
/**
|
||||
* obu定位信息回调
|
||||
* @param locationInfo 定位信息
|
||||
*/
|
||||
fun onLocationInfoCallback(locationInfo: MogoObuLocationInfo?)
|
||||
|
||||
/**
|
||||
* obu红绿灯信息回调
|
||||
* @param trafficLightInfo 红绿灯信息
|
||||
*/
|
||||
fun onTrafficLightInfoCallback(trafficLightInfo: MogoObuTrafficLightInfo?)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.zhidao.mogo.module.obu.obu.bean
|
||||
|
||||
import com.zhidao.smartv2x.model.obu.CarEventInfo
|
||||
|
||||
/**
|
||||
* obu事件数据封装,根据西迪obu数据进行整理
|
||||
*
|
||||
* {"typeCode":"01","type":"前碰撞预警","describe":"等级:1"}
|
||||
*/
|
||||
class MogoObuEventInfo(){
|
||||
var typeCode:String? = null
|
||||
var type:String? = null
|
||||
var describe:String? = null
|
||||
override fun toString(): String {
|
||||
return "MogoObuEventInfo(typeCode=$typeCode, type=$type, describe=$describe)"
|
||||
}
|
||||
|
||||
constructor(info:CarEventInfo):this(){
|
||||
this.typeCode = info.typeCode
|
||||
this.type = info.type
|
||||
this.describe = info.describe
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.zhidao.mogo.module.obu.obu.bean
|
||||
|
||||
import com.zhidao.smartv2x.model.obu.CarLocationInfo
|
||||
|
||||
/**
|
||||
* obu定位信息,根据西迪obu进行封装
|
||||
* {"carId":"1952999795","lat":28.089524299999997,"lng":112.9908293,"speed":"14"," direction":"10112","sateNum":"24","signal":"0"}
|
||||
* @author tongchenfei
|
||||
*/
|
||||
class MogoObuLocationInfo() {
|
||||
var carId: String? = null
|
||||
var lat: Double? = 0.0
|
||||
var lon: Double? = 0.0
|
||||
var speed: String? = null
|
||||
var direction: String? = null
|
||||
var sateNum: String? = null
|
||||
var signal: String? = null
|
||||
override fun toString(): String {
|
||||
return "MogoObuLocationInfo(lat=$lat)"
|
||||
}
|
||||
|
||||
constructor(info:CarLocationInfo?) : this() {
|
||||
this.carId = info?.carId
|
||||
this.lat = info?.lat
|
||||
this.lon = info?.lng
|
||||
this.speed = info?.speed
|
||||
this.direction = info?.direction
|
||||
this.sateNum = info?.sateNum
|
||||
this.signal = info?.signal
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.zhidao.mogo.module.obu.obu.bean
|
||||
|
||||
import com.zhidao.smartv2x.model.obu.TrafficLightInfo
|
||||
|
||||
/**
|
||||
* obu信号灯数据,根据西迪obu进行封装
|
||||
* lightStatus:信号灯的当前状态 G/R/Y 分别对应 绿/红/黄
|
||||
* surplusTime:当前信号灯剩余时间 十进制 单位:秒
|
||||
* lightPriority:保留字段,暂无说明
|
||||
*
|
||||
* @author tongchenfei
|
||||
*/
|
||||
class MogoObuTrafficLightInfo(){
|
||||
var id:String? =null
|
||||
var lightStatus:String? =null
|
||||
var surplusTime:String? =null
|
||||
var lightPriority:String? =null
|
||||
override fun toString(): String {
|
||||
return "MogoObuTrafficLightInfo(id=$id, lightStatus=$lightStatus, surplusTime=$surplusTime, lightPriority=$lightPriority)"
|
||||
}
|
||||
|
||||
constructor(info:TrafficLightInfo):this(){
|
||||
this.id = info.id
|
||||
this.lightStatus = info.lightStatus
|
||||
this.surplusTime = info.surplusTime
|
||||
this.lightPriority = info.lightPriority
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.zhidao.mogo.module.obu.socket
|
||||
|
||||
/**
|
||||
* udp 数据回调
|
||||
*/
|
||||
interface IUdpSocketCallback {
|
||||
/**
|
||||
* udp过来的字节码数据,目前已知数据类型就是ByteArray
|
||||
*
|
||||
* @param msg udp发过来的数据
|
||||
*/
|
||||
fun onMessageReceived(msg: ByteArray)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.zhidao.mogo.module.obu.socket
|
||||
|
||||
class SimpleAddress(val ip: String, val port: Int) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return when (other) {
|
||||
is SimpleAddress -> other.ip == ip && port == port
|
||||
null -> false
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return ip.hashCode() + port.hashCode()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "SimpleAddress(ip='$ip', port=$port)"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.zhidao.mogo.module.obu.socket
|
||||
|
||||
import android.util.ArrayMap
|
||||
import com.mogo.utils.logger.Logger
|
||||
import java.net.DatagramPacket
|
||||
import java.net.DatagramSocket
|
||||
import java.net.InetAddress
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* udp socket 管理类,统一管理udp socket数据接收和发送
|
||||
*/
|
||||
class UdpSocketManager(private val callback: IUdpSocketCallback? = null) {
|
||||
companion object{
|
||||
const val TAG = "Mogo-UdpSocketManager"
|
||||
}
|
||||
private val socketMap = ArrayMap<SimpleAddress, DatagramSocket>()
|
||||
private val socketExecutor = Executors.newCachedThreadPool()
|
||||
|
||||
@Volatile private var isConnected = false
|
||||
|
||||
fun sendMsgTo(msg:String,ip: String, port: Int) {
|
||||
isConnected = true
|
||||
val address = SimpleAddress(ip, port)
|
||||
socketExecutor.execute(UdpSenderRunnable(address, msg))
|
||||
}
|
||||
|
||||
fun receiveMsgFrom(ip: String, port: Int) {
|
||||
isConnected = true
|
||||
val address = SimpleAddress(ip, port)
|
||||
socketExecutor.execute(UdpReceiverRunnable(address))
|
||||
}
|
||||
|
||||
fun release(){
|
||||
isConnected = false
|
||||
socketMap.forEach {
|
||||
it.value.close()
|
||||
}
|
||||
}
|
||||
|
||||
inner class UdpSenderRunnable(private val address: SimpleAddress,private val msg:String):Runnable{
|
||||
override fun run() {
|
||||
Logger.d(TAG, "${address}准备发送消息: $msg")
|
||||
var socket = socketMap[address]
|
||||
val netAddress = InetAddress.getByName(address.ip)
|
||||
if (socket == null) {
|
||||
socket = DatagramSocket(address.port)
|
||||
socketMap[address] = socket
|
||||
}
|
||||
socket.send(DatagramPacket(msg.toByteArray(), msg.toByteArray().size, netAddress, address.port))
|
||||
}
|
||||
}
|
||||
inner class UdpReceiverRunnable(private val address:SimpleAddress):Runnable{
|
||||
override fun run() {
|
||||
var socket = socketMap[address]
|
||||
Logger.d(TAG, "init receiver: $address")
|
||||
|
||||
if (socket == null) {
|
||||
socket = DatagramSocket(address.port)
|
||||
socketMap[address] = socket
|
||||
}
|
||||
val buffer = ByteArray(1024)
|
||||
val packet = DatagramPacket(buffer, buffer.size)
|
||||
while (isConnected){
|
||||
if(socket.isClosed){
|
||||
break
|
||||
}
|
||||
Logger.d(TAG,"准备接受消息====$address")
|
||||
socket.receive(packet)
|
||||
val msg = ByteArray(buffer.size)
|
||||
System.arraycopy(buffer, 0, msg, 0, buffer.size)
|
||||
callback?.onMessageReceived(msg)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user