Commit 28fd8c31 authored by p x's avatar p x
Browse files

增加车辆位姿接口

parent 6c7fe9df
......@@ -8,11 +8,11 @@ import org.springframework.web.bind.annotation.RestController
/**
* 推送配置控制接口
*/
@RestController
//@RestController
//@RequestMapping("/api/push")
class PushConfigController(
// 注入PushConfig组件(Spring自动管理单例)
private val pushConfig: PushConfig
// private val pushConfig: PushConfig
) {
......
package com.inzy.wsmock.bean
/***车辆位姿*/
class CarVeh {
/**业务状态 park call park_com call_com**/
var status = "park"
/***车辆行驶的点位**/
var carPos = 0
/***全局路径**/
var rs: List<MutableList<Double>>? = null
}
\ No newline at end of file
......@@ -4,10 +4,11 @@ import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import com.inzy.wsmock.ChannelManager
import com.inzy.wsmock.RequestParamHandler
import com.inzy.wsmock.bean.MRoutes
import com.inzy.wsmock.bean.CarVeh
import com.inzy.wsmock.bean.V2xStartBean
import com.inzy.wsmock.bean.VObject
import com.inzy.wsmock.utils.FileIoUtil
import com.inzy.wsmock.utils.MyMapUtils
import io.netty.channel.Channel
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame
import jakarta.annotation.PostConstruct
......@@ -33,13 +34,15 @@ class AvpPushTask(
private val channelManager = ChannelManager.instance
//车辆位姿,泊车
private var carDstPark = mutableListOf<String>()
// private var carDstPark = mutableListOf<String>()
private var carVehPark: CarVeh? = null
//车辆位姿,召车
private var carDstCall = mutableListOf<String>()
// private var carDstCall = mutableListOf<String>()
private var carVehCall: CarVeh? = null
//车辆行驶的位置
private var carPos = 0
private var carIndex = 0
//AVP状态 park call
private var status = ""
......@@ -58,10 +61,14 @@ class AvpPushTask(
// 读取文件内容(Kotlin简化写法)
CoroutineScope(Dispatchers.IO).launch {
//读取泊车数据
FileIoUtil.getMockToList(classpathResource.inputStream, carDstPark)
val parkStr = FileIoUtil.getFileDate(classpathResource.inputStream)
carVehPark = JSON.parseObject(parkStr, CarVeh::class.java)
//读取召车数据
classpathResource = resourceLoader.getResource("classpath:avpm/Car_fangzhen_call.txt")
FileIoUtil.getMockToList(classpathResource.inputStream, carDstCall)
val callStr = FileIoUtil.getFileDate(classpathResource.inputStream)
carVehCall = JSON.parseObject(callStr, CarVeh::class.java)
//读取感知物数据
classpathResource = resourceLoader.getResource("classpath:avpm/PerTarget.txt")
FileIoUtil.getMockToList(classpathResource.inputStream, preDst)
......@@ -71,8 +78,8 @@ class AvpPushTask(
@PreDestroy
fun destroy() {
// logger.info("销毁 AdasPushTask 实例,ID: $instanceId")
carDstPark.clear()
carDstCall.clear()
// carDstPark.clear()
// carDstCall.clear()
preDst.clear()
}
......@@ -103,43 +110,75 @@ class AvpPushTask(
*/
private fun pushCarPos(channel: Channel) {
CoroutineScope(Dispatchers.Default).launch {
delay(8000)
delay(10000)
status = "park"
//开始推送泊车
carDstPark.forEachIndexed { i, str ->
val mRoutes = JSON.parseObject(str, MRoutes::class.java)
mRoutes.status = status
mRoutes.carPos = i
sendMsg(channel, JSON.toJSONString(mRoutes))
if (carVehPark != null) {
carVehPark!!.status = status
val total = carVehPark!!.rs?.count()
for (i in 0 until total!!) {
carIndex = i
carVehPark?.carPos = i
if (i > 0) {
var p1 = carVehPark?.rs!!.get(i - 1)
var pc = carVehPark?.rs!!.get(i)
var head = MyMapUtils.calculateBearing(
p1[0], p1[1],
pc[0], pc[1]
)
carVehPark!!.rs!!.get(i).add(head)
}
sendMsg(channel, JSON.toJSONString(carVehPark))
delay(300)
}
}
//泊车完成
val parkTemp=MRoutes().apply {
status = "park_com"
carVehPark?.apply {
this.status = this@AvpPushTask.status
carPos = 0
rs = listOf()
}
sendMsg(channel, JSON.toJSONString(parkTemp))
sendMsg(channel, JSON.toJSONString(carVehPark))
delay(3000)
status = "call"
//开始推送召车
carDstCall.forEachIndexed { i, str ->
val mRoutes = JSON.parseObject(str, MRoutes::class.java)
mRoutes.status = status
mRoutes.carPos = i
sendMsg(channel, JSON.toJSONString(mRoutes))
delay(200)
status = "call"
if (carVehCall != null) {
carVehCall!!.status = status
val total = carVehCall!!.rs?.count()
for (i in 0 until total!!) {
carIndex = i
carVehCall?.carPos = i
if (i > 0) {
var p1 = carVehCall?.rs!!.get(i - 1)
var pc = carVehCall?.rs!!.get(i)
var head = MyMapUtils.calculateBearing(
p1[0], p1[1],
pc[0], pc[1]
)
carVehCall!!.rs!!.get(i).add(head)
}
sendMsg(channel, JSON.toJSONString(carVehCall))
delay(300)
}
}
//召车完成
val callTemp=MRoutes().apply {
status = "call_com"
carVehCall?.apply {
status = this@AvpPushTask.status
carPos = 0
rs = listOf()
}
sendMsg(channel, JSON.toJSONString(callTemp))
sendMsg(channel, JSON.toJSONString(carVehCall))
//全部完成
val temp=MRoutes().apply {
carIndex = 0
status = ""
carVehCall?.apply {
this.status = this@AvpPushTask.status
carPos = 0
rs = listOf()
}
sendMsg(channel, JSON.toJSONString(temp))
status = ""
sendMsg(channel, JSON.toJSONString(carVehCall))
}
}
......@@ -161,50 +200,53 @@ class AvpPushTask(
private fun pushV2x(channel: Channel) {
CoroutineScope(Dispatchers.Default).launch {
while (isActive) {
carPos = 8
// carPos = 0
// println("-------------carIndex = ${carIndex}")
if (carIndex == 0 || status.equals(""))
continue
if (status.equals("park")) {
when (carPos) {
when (carIndex) {
8 -> {//紧急制动预警
val v2x = V2xStartBean().apply {
type = 6
objects =
listOf(VObject(""), VObject("f117fdfa-feff-0100-85dc-35850000acb0"))
}
pushV2xAtomic(channel, v2x)
// val v2x = V2xStartBean().apply {
// type = 6
// objects =
// listOf(VObject(""), VObject("f117fdfa-feff-0100-85dc-35850000acb0"))
// }
pushV2xAtomic(channel, 6,"f117fdfa-feff-0100-85dc-35850000acb0")
}
30 -> {//前向碰撞
val v2x = V2xStartBean().apply {
/* val v2x = V2xStartBean().apply {
type = 1
objects =
listOf(VObject(""), VObject("f117fdfa-feff-0100-85dc-35850000acb0"))
}
pushV2xAtomic(channel, v2x)
}*/
pushV2xAtomic(channel, 1,"f117fdfa-feff-0100-85dc-35850000acb0")
}
60 -> {//弱势交通参与者
val v2x = V2xStartBean().apply {
/* val v2x = V2xStartBean().apply {
type = 12
objects =
listOf(VObject(""), VObject("50332456-3030-3030-3530-303334533955"))
}
pushV2xAtomic(channel, v2x)
}*/
pushV2xAtomic(channel, 12,"50332456-3030-3030-3530-303334533955")
}
}
} else if (status.equals("call")) {
when (carPos) {
when (carIndex) {
70 -> {//盲区预警
val v2x = V2xStartBean().apply {
/* val v2x = V2xStartBean().apply {
type = 4
objects =
listOf(VObject(""), VObject("2435"))
}*/
pushV2xAtomic(channel, 4,"2345")
}
pushV2xAtomic(channel, v2x)
}
}
}
delay(300)
delay(200)
}
}
}
......@@ -221,6 +263,21 @@ class AvpPushTask(
}
}
private fun pushV2xAtomic(channel: Channel, type: Int, id: String, change: Boolean = true) {
if (isV2x.compareAndSet(false, true)) {
val v2x = V2xStartBean().apply {
this.type = type
objects =
listOf(VObject(""), VObject(id))
}
val str = JSONObject.toJSONString(v2x)
sendMsg(channel, str)
if (change) {
isV2x.set(false)
}
}
}
private fun sendMsg(channel: Channel, msg: String) {
if (!channel.isActive()) {
......
......@@ -15,5 +15,17 @@ object FileIoUtil {
}
}
/***一次读取全部内容**/
fun getFileDate(inputStream: InputStream): String {
// 方式1:手动逐行读取(推荐,Kotlin简洁写法)
inputStream.use { inputStream -> // use 自动关闭流(AutoCloseable)
val size = inputStream.available()
val bytes = ByteArray(size)
inputStream.read(bytes)
var str = String(bytes)
return str
}
}
}
\ No newline at end of file
package com.inzy.wsmock.utils
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
object MyMapUtils {
/**
* 根据两个坐标点计算航向角
* @param fromLon 起始点经度
* @param fromLat 起始点纬度
* @param toLon 终点经度
* @param toLat 终点纬度
* @return 航向角(度),范围 0-360
*/
fun calculateBearing(fromLon: Double, fromLat: Double, toLon: Double, toLat: Double): Double {
// 将度转换为弧度
val lat1 = Math.toRadians(fromLat)
val lat2 = Math.toRadians(toLat)
val deltaLon = Math.toRadians(toLon - fromLon)
// 计算航向角
val y = sin(deltaLon) * cos(lat2)
val x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltaLon)
val bearing = Math.toDegrees(atan2(y, x))
// 确保航向角在 0-360 度范围内
return (bearing + 360) % 360
}
}
......@@ -3,7 +3,7 @@ spring:
encoding: UTF-8
application:
name: wsmock
name: avpmock
server:
port: 8088
servlet:
......@@ -28,7 +28,7 @@ logging:
# 日志文件配置
file:
name: logs/wsmock.log
name: logs/avp.log
# 日志格式
pattern:
......@@ -44,5 +44,5 @@ logging:
# Netty WebSocket配置
netty:
websocket:
port: 8089 # Netty WebSocket端口
port: 8090 # Netty WebSocket端口
package com.inzy.wsmock
import com.alibaba.fastjson2.JSON
import kotlinx.coroutines.delay
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
......@@ -8,6 +10,9 @@ class WsmockApplicationTests {
@Test
fun contextLoads() {
for (i in 0 until 3) {
println("i = ${i}")
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment