Commit d42c40cd authored by p x's avatar p x
Browse files

Http SSE 车辆位姿

parent 761ed3e9
package com.inzy.wsmock.bean
/**车辆位姿请求**/
class CarSseReq {
var id=""
}
\ No newline at end of file
package com.sd.cavphmi.bean
package com.inzy.wsmock.bean
/***网联车辆位姿
* {
......
package com.inzy.wsmock.bean
/***车辆位姿*/
/***车辆位姿模拟文件*/
class RouteCarVeh {
/**业务状态 park call park_com call_com**/
var status = "park"
/***车辆行驶的点位**/
var carPos = 0
// var carPos = 0
/***全局路径**/
var rs: List<MutableList<Double>>? = null
}
/***车辆位姿模拟文件*/
class RouteCar {
/***全局路径**/
var rs: List<List<Double>>? = null
}
/***推给APP的数据***/
class CarVehicle {
//vehiclePos 里面分别是 经度、纬度、航向角
var vehiclePos: List<Double>? = null
}
\ No newline at end of file
......@@ -10,13 +10,12 @@ 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 com.sd.cavphmi.bean.CarVehBak
import com.inzy.wsmock.bean.CarVehBak
import io.netty.channel.Channel
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame
import jakarta.annotation.PostConstruct
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import kotlinx.coroutines.time.delay
import org.slf4j.LoggerFactory
import org.springframework.core.io.ResourceLoader
import org.springframework.stereotype.Component
......@@ -125,7 +124,7 @@ class AvpPushTask(
override fun pushMsgFromType(type: String, channel: Channel) {
when (type) {
RequestParamHandler.PARAM_TYPE_VALUE_1 -> {//车辆位姿
pushCarPos(channel)
// pushCarPos(channel)
}
RequestParamHandler.PARAM_TYPE_VALUE_2 -> {//感知物
......
package com.inzy.wsmock.push
import com.alibaba.fastjson2.JSON
import com.inzy.wsmock.bean.CarSseReq
import com.inzy.wsmock.bean.CarVehicle
import com.inzy.wsmock.bean.ParkPath
import com.inzy.wsmock.bean.RouteCar
import com.inzy.wsmock.utils.FileIoUtil
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import org.springframework.core.io.ResourceLoader
import org.springframework.http.MediaType
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.context.request.async.AsyncRequestNotUsableException
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
import java.io.IOException
import java.time.LocalDateTime
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.random.Random
@Controller
class SseController(private val resourceLoader: ResourceLoader) {
companion object {
}
private val emitters = ConcurrentHashMap<String, SseEmitter>()
private val executor = Executors.newScheduledThreadPool(2)
init {
// 定时发送心跳消息以保持连接活跃
executor.scheduleAtFixedRate({
val heartbeatData = mapOf(
"type" to "heartbeat",
"timestamp" to LocalDateTime.now().toString()
)
emitters.entries.forEach { (userId, emitter) ->
try {
emitter.send(
SseEmitter.event()
.name("heartbeat")
.data(heartbeatData)
)
} catch (e: IOException) {
println("Heartbeat failed for user: $userId")
emitter.completeWithError(e)
true
}
}
}, 30, 30, TimeUnit.SECONDS)
}
//推给APP的
var carVehicle = CarVehicle()
@PostMapping("/api/avpweb/hmi/monitor/v1/monitorDrivenStatus", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
fun car(@RequestBody carSseReq: CarSseReq): SseEmitter {
val emitter = SseEmitter(0L) // 设置超时时间为0表示永不超时
val carid = carSseReq.id
emitter.onCompletion {
emitters.remove(carid)
println("SSE connection completed for carid: $carid")
}
emitter.onTimeout {
emitters.remove(carid)
println("SSE connection timed out for carId: $carid")
try {
emitter.complete()
} catch (e: Exception) {
e.printStackTrace()
}
}
emitter.onError { ex ->
emitters.remove(carid)
println("SSE connection error for carId: $carid - ${ex.message}")
try {
emitter.completeWithError(ex)
} catch (e: Exception) {
e.printStackTrace()
}
}
emitters[carid] = emitter
// println("New SSE connection established for user: $carid")
Thread.sleep(25 * 1000)
CoroutineScope(Dispatchers.Default).launch {
onVehicleMockPark()
onVehicleMockCall()
}
return emitter
}
//开始泊车
private suspend fun onVehicleMockPark(): Int {
return withContext(Dispatchers.IO) {
// Blocking network request code
var classpathResource = resourceLoader.getResource("classpath:avpm/Car_fangzhen_park.txt")
val parkStr = FileIoUtil.getFileDate(classpathResource.inputStream)
val routeCarPark = JSON.parseObject(parkStr, RouteCar::class.java)
routeCarPark.rs?.forEachIndexed { index, doubles ->
// var carVehicle = CarVehicle()
carVehicle.vehiclePos = doubles
sendCar(carVehicle)
delay(300)
}
//开始倒车入库 //读取倒车入库数据
classpathResource = resourceLoader.getResource("classpath:avpm/parkpath.txt")
val parking = FileIoUtil.getFileDate(classpathResource.inputStream)
var parkPath = JSON.parseObject(parking, ParkPath::class.java)
//开始后退
val forback = parkPath!!.result.routes.find { it.gear == 1 }
forback?.polyline?.forEachIndexed { index, doubles ->
// var carVehicle = CarVehicle()
carVehicle.vehiclePos = listOf(doubles[0], doubles[1], doubles[3])
sendCar(carVehicle)
delay(300)
}
return@withContext 1
}
}
//开始召车
private suspend fun onVehicleMockCall(): Int {
return withContext(Dispatchers.IO) {
//读取召车数据
var classpathResource = resourceLoader.getResource("classpath:avpm/Car_fangzhen_call.txt")
val callStr = FileIoUtil.getFileDate(classpathResource.inputStream)
var routeCarCall = JSON.parseObject(callStr, RouteCar::class.java)
routeCarCall.rs?.forEachIndexed { index, doubles ->
// var carVehicle = CarVehicle()
carVehicle.vehiclePos = doubles
sendCar(carVehicle)
delay(300)
}
return@withContext 1
}
}
private fun sendCar(carVehicle: CarVehicle) {
emitters.entries.forEachIndexed { index, entry ->
try {
entry.value.send(
SseEmitter.event()
.name("data")
.data(carVehicle)
)
} catch (e: AsyncRequestNotUsableException) {
// 客户端断开连接,移除对应的 emitter
emitters.remove(entry.key)
}
}
}
@PreDestroy
fun cleanup() {
emitters.forEach { (carId, emitter) ->
try {
emitter.complete()
} catch (e: Exception) {
e.printStackTrace()
}
}
executor.shutdown()
try {
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
executor.shutdownNow()
}
} catch (e: InterruptedException) {
executor.shutdownNow()
Thread.currentThread().interrupt()
}
}
@GetMapping("/sse", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
fun sse(): SseEmitter {
val emitter = SseEmitter(0L) // 设置超时时间为0表示永不超时
// 在单独的线程中发送数据
CompletableFuture.runAsync {
try {
repeat(20) { index ->
val data = mapOf(
"id" to index,
"message" to "Server message #${index + 1}",
"timestamp" to System.currentTimeMillis(),
"randomValue" to Random.nextInt(100)
)
emitter.send(
SseEmitter.event()
.name("data")
.data(data)
)
Thread.sleep(1000) // 每秒发送一次
}
emitter.complete()
} catch (e: IOException) {
emitter.completeWithError(e)
}
}
return emitter
}
}
\ No newline at end of file
{"rs":[[116.50238005214746,39.81014469509955],[116.50236486876796,39.81013731694055],[116.50234968537274,39.81012993879976],[116.50233450197756,39.8101225606582],[116.50231931858238,39.81011518251584],[116.5023041333868,39.81010780655952],[116.50229565547512,39.81010368888862],[116.50228036234131,39.810096445719445],[116.50226506920752,39.8100892025495],[116.50224985746125,39.81008185923798],[116.50223467091115,39.81007448492373],[116.5022194843611,39.81006711060869],[116.502204297811,39.81005973629286],[116.50218911126096,39.81005236197624],[116.50217392471086,39.81004498765881],[116.50215873816082,39.810037613340604],[116.50214354204267,39.81003025071994],[116.50212940967664,39.81002347155654],[116.50211414052076,39.810016198589445],[116.50209895321649,39.81000882529198],[116.50208377514453,39.8100014406772],[116.5020685970726,39.809994056061605],[116.50205341900066,39.80998667144523],[116.50203824092873,39.809979286828046],[116.50202306285678,39.80997190221008],[116.50200788607032,39.80996451603342],[116.50199271461229,39.809957123398306],[116.50197175150723,39.80994690865124],[116.5019565677659,39.809939530910334],[116.50194138402462,39.809932153168646],[116.5019261959587,39.80992478068344],[116.50191100349002,39.809917413549634],[116.50189579233196,39.809910069682026],[116.50188040671078,39.80990294300845],[116.50186502108963,39.80989581633414],[116.50184281562053,39.80988553068078],[116.50182802471463,39.809877696172016],[116.50181322516318,39.80986987170683],[116.50179804843465,39.80986248544798],[116.50178280602617,39.80985518091484],[116.50176075352947,39.80984499368132],[116.50174177475655,39.809835769889],[116.5017274954714,39.80982739397197],[116.50171740536683,39.80981635635335],[116.50171295482671,39.809803030251885],[116.50171301661533,39.80978929564729],[116.50171746496443,39.80977596765635],[116.5017259444861,39.8097638114287],[116.5017379185738,39.80974819470519],[116.50174729851142,39.80973642369948],[116.50175667844906,39.80972465269176],[116.50176605838669,39.80971288168202],[116.50177543832429,39.809701110670275],[116.50178481826192,39.8096893396565],[116.50179419819955,39.80967756864072],[116.50180357813717,39.809665797622905],[116.5018129580748,39.8096540266031],[116.50182233801245,39.80964225558127],[116.50183171795007,39.80963048455742],[116.50184109788769,39.809618713531556],[116.5018504778253,39.80960694250368],[116.50185985776292,39.80959517147379],[116.50186923770057,39.80958340044187],[116.5018786176382,39.80957162940795],[116.50188799757582,39.80955985837201],[116.50189737751344,39.80954808733406],[116.50190675745107,39.80953631629408],[116.50191613738868,39.8095245452521],[116.5019255173263,39.80951277420809],[116.50193493157849,39.80950101936984],[116.50194437236506,39.80948927706254],[116.50195381315163,39.80947753475324],[116.5019632539382,39.80946579244194],[116.50197269472476,39.809454050128615],[116.50198213551133,39.809442307813306],[116.5019915762979,39.80943056549599],[116.50200101708447,39.80941882317666],[116.50201045787104,39.809407080855316],[116.50201989865761,39.809395338531964],[116.50202933944418,39.80938359620662],[116.50203878023075,39.80937185387928],[116.50204822101732,39.809360111549914],[116.50205766180389,39.809348369218554],[116.50206710259046,39.809336626885184],[116.50207654337703,39.80932488454981],[116.50208598416357,39.809313142212424],[116.50209542495014,39.80930139987304],[116.50210499804051,39.809289720982065],[116.50211458612095,39.80927804927806],[116.50212417420141,39.80926637757208],[116.50213376228183,39.80925470586412],[116.50214335036227,39.809243034154164],[116.50215293844273,39.80923136244224],[116.50216252652316,39.809219690728334],[116.5021721146036,39.809208019012445],[116.50218170268406,39.809196347294574],[116.5021912907645,39.80918467557471],[116.50220087884492,39.80917300385288],[116.50221046692533,39.80916133212906],[116.50222194778321,39.80914735629438],[116.50223079042868,39.80913537219419],[116.50224437375732,39.80912656561062],[116.5022616599528,39.80912330256496],[116.50227897733187,39.809126399012214],[116.50229461795695,39.80913317723447],[116.50231015045868,39.80914011342922],[116.5023256829604,39.80914704962326],[116.50234121546217,39.80915398581661],[116.50235709967029,39.80916040951712],[116.50237406319151,39.80916484398448],[116.50239187097037,39.80916636876694],[116.50240970211905,39.80916504764615],[116.50242686120255,39.80916110814383],[116.50244272398614,39.809154696622954],[116.50245637374258,39.80914577271494],[116.50246747881728,39.809134975672535],[116.50247697374292,39.80912326014347],[116.50248632266518,39.80911147447951],[116.5024956715874,39.80909968881352],[116.50250735122155,39.80908496494592],[116.50251677946527,39.80907321662218],[116.50252620770894,39.80906146829645],[116.50253563595264,39.8090497199687],[116.50254394816011,39.809039362307594]]}
\ No newline at end of file
{
"rs": [
[
116.50238005214746,
39.81014469509955,
237.6829820703691
],
[
116.50236486876796,
39.81013731694055,
237.68307561987575
],
[
116.50234968537274,
39.81012993879976,
237.6830756097038
],
[
116.50233450197756,
39.8101225606582,
237.68307554373752
],
[
116.50231931858238,
39.81011518251584,
237.6938174827698
],
[
116.5023041333868,
39.81010780655952,
237.69591515912174
],
[
116.50229565547512,
39.81010368888862,
238.3437364798304
],
[
116.50228036234131,
39.810096445719445,
238.3437364455466
],
[
116.50226506920752,
39.8100892025495,
237.85362814526735
],
[
116.50224985746125,
39.81008185923798,
237.70190041312034
],
[
116.50223467091115,
39.81007448492373,
237.70190034557228
],
[
116.5022194843611,
39.81006711060869,
237.70190039564733
],
[
116.502204297811,
39.81005973629286,
237.70190033926013
],
[
116.50218911126096,
39.81005236197624,
237.70190040049607
],
[
116.50217392471086,
39.81004498765881,
237.7019002784291
],
[
116.50215873816082,
39.810037613340604,
237.75925608043258
],
[
116.50214354204267,
39.81003025071994,
238.01696949712567
],
[
116.50212940967664,
39.81002347155654,
238.19833426358676
],
[
116.50211414052076,
39.810016198589445,
237.70677835452744
],
[
116.50209895321649,
39.81000882529198,
237.65133473173375
],
[
116.50208377514453,
39.8100014406772,
237.65133465944604
],
[
116.5020685970726,
39.809994056061605,
237.65133467519559
],
[
116.50205341900066,
39.80998667144523,
237.6513346240156
],
[
116.50203824092873,
39.809979286828046,
237.65133466207467
],
[
116.50202306285678,
39.80997190221008,
237.6436772388487
],
[
116.50200788607032,
39.80996451603342,
237.61193653625492
],
[
116.50199271461229,
39.809957123398306,
237.61193837906546
],
[
116.50197175150723,
39.80994690865124,
237.6851403015522
],
[
116.5019565677659,
39.809939530910334,
237.68514031338162
],
[
116.50194138402462,
39.809932153168646,
237.71095815409723
],
[
116.5019261959587,
39.80992478068344,
237.73724224420545
],
[
116.50191100349002,
39.809917413549634,
237.85073976040232
],
[
116.50189579233196,
39.809910069682026,
238.91028611911815
],
[
116.50188040671078,
39.80990294300845,
238.91028614503145
],
[
116.50186502108963,
39.80989581633414,
238.9102882886388
],
[
116.50184281562053,
39.80988553068078,
235.4122851196367
],
[
116.50182802471463,
39.809877696172016,
235.4622667362538
],
[
116.50181322516318,
39.80986987170683,
237.64332857152914
],
[
116.50179804843465,
39.80986248544798,
238.0420609398446
],
[
116.50178280602617,
39.80985518091484,
238.97871780698017
],
[
116.50176075352947,
39.80984499368132,
237.6794114813822
],
[
116.50174177475655,
39.809835769889,
232.6346154985382
],
[
116.5017274954714,
39.80982739397197,
215.07770889258606
],
[
116.50171740536683,
39.80981635635335,
194.38881781174774
],
[
116.50171295482671,
39.809803030251885,
179.80199650469456
],
[
116.50171301661533,
39.80978929564729,
165.61992089690648
],
[
116.50171746496443,
39.80977596765635,
151.81596405333198
],
[
116.5017259444861,
39.8097638114287,
149.50207750396544
],
[
116.5017379185738,
39.80974819470519,
148.52778764862148
],
[
116.50174729851142,
39.80973642369948,
148.52778758147338
],
[
116.50175667844906,
39.80972465269176,
148.52778759732007
],
[
116.50176605838669,
39.80971288168202,
148.52778770332623
],
[
116.50177543832429,
39.809701110670275,
148.52778767064717
],
[
116.50178481826192,
39.8096893396565,
148.5277876382395
],
[
116.50179419819955,
39.80967756864072,
148.5277876954492
],
[
116.50180357813717,
39.809665797622905,
148.52778762857406
],
[
116.5018129580748,
39.8096540266031,
148.5277875821091
],
[
116.50182233801245,
39.80964225558127,
148.5277876671654
],
[
116.50183171795007,
39.80963048455742,
148.5277876347593
],
[
116.50184109788769,
39.809618713531556,
148.52778765750259
],
[
116.5018504778253,
39.80960694250368,
148.52778763199086
],
[
116.50185985776292,
39.80959517147379,
148.5277875993147
],
[
116.50186923770057,
39.80958340044187,
148.52778766369175
],
[
116.5018786176382,
39.80957162940795,
148.52778763818105
],
[
116.50188799757582,
39.80955985837201,
148.52778764713867
],
[
116.50189737751344,
39.80954808733406,
148.5277876767775
],
[
116.50190675745107,
39.80953631629408,
148.5277876236933
],
[
116.50191613738868,
39.8095245452521,
148.52778768090718
],
[
116.5019255173263,
39.80951277420809,
148.3993248448242
],
[
116.50193493157849,
39.80950101936984,
148.299988529318
],
[
116.50194437236506,
39.80948927706254,
148.29998850427387
],
[
116.50195381315163,
39.80947753475324,
148.2999884310068
],
[
116.5019632539382,
39.80946579244194,
148.29998861342312
],
[
116.50197269472476,
39.809454050128615,
148.2999884846501
],
[
116.50198213551133,
39.809442307813306,
148.29998850123724
],
[
116.5019915762979,
39.80943056549599,
148.29998851782466
],
[
116.50200101708447,
39.80941882317666,
148.29998852747417
],
[
116.50201045787104,
39.809407080855316,
148.2999885371239
],
[
116.50201989865761,
39.809395338531964,
148.29998850514414
],
[
116.50202933944418,
39.80938359620662,
148.29998849397964
],
[
116.50203878023075,
39.80937185387928,
148.29998852444533
],
[
116.50204822101732,
39.809360111549914,
148.2999885063432
],
[
116.50205766180389,
39.809348369218554,
148.29998851599464
],
[
116.50206710259046,
39.809336626885184,
148.29998851870812
],
[
116.50207654337703,
39.80932488454981,
148.29998858352178
],
[
116.50208598416357,
39.809313142212424,
148.29998851025937
],
[
116.50209542495014,
39.80930139987304,
147.80258536886106
],
[
116.50210499804051,
39.809289720982065,
147.74623006383433
],
[
116.50211458612095,
39.80927804927806,
147.74623007056869
],
[
116.50212417420141,
39.80926637757208,
147.7462301177402
],
[
116.50213376228183,
39.80925470586412,
147.74623008403853
],
[
116.50214335036227,
39.809243034154164,
147.74623006258776
],
[
116.50215293844273,
39.80923136244224,
147.7462300552304
],
[
116.50216252652316,
39.809219690728334,
147.7462301446825
],
[
116.5021721146036,
39.809208019012445,
147.74623004051665
],
[
116.50218170268406,
39.809196347294574,
147.74623007543954
],
[
116.5021912907645,
39.80918467557471,
147.746230143753
],
[
116.50220087884492,
39.80917300385288,
147.7462301293507
],
[
116.50221046692533,
39.80916133212906,
147.7462294387051
],
[
116.50222194778321,
39.80914735629438,
150.45482594530847
],
[
116.50223079042868,
39.80913537219419,
130.16403373831827
],
[
116.50224437375732,
39.80912656561062,
103.80579695054894
],
[
116.5022616599528,
39.80912330256496,
76.89685891576113
],
[
116.50227897733187,
39.809126399012214,
60.570293126005936
],
[
116.50229461795695,
39.80913317723447,
59.829656142251
],
[
116.50231015045868,
39.80914011342922,
59.82965618751763
],
[
116.5023256829604,
39.80914704962326,
59.829656193658195
],
[
116.50234121546217,
39.80915398581661,
62.23549249524524
],
[
116.50235709967029,
39.80916040951712,
71.2065606517699
],
[
116.50237406319151,
39.80916484398448,
83.63983258266211
],
[
116.50239187097037,
39.80916636876694,
95.50909787603081
],
[
116.50240970211905,
39.80916504764615,
106.6398691894982
],
[
116.50242686120255,
39.80916110814383,
117.75155381771447
],
[
116.50244272398614,
39.809154696622954,
130.40018165667937
],
[
116.50245637374258,
39.80914577271494,
141.68783579051768
],
[
116.50246747881728,
39.809134975672535,
148.09452716931543
],
[
116.50247697374292,
39.80912326014347,
148.64368608711084
],
[
116.50248632266518,
39.80911147447951,
148.64368616831428
],
[
116.5024956715874,
39.80909968881352,
148.64368534784933
],
[
116.50250735122155,
39.80908496494592,
148.34699064777806
],
[
116.50251677946527,
39.80907321662218,
148.3469906874264
],
[
116.50252620770894,
39.80906146829645,
148.34699065821923
],
[
116.50253563595264,
39.8090497199687,
148.3469909977715
],
[
116.50254394816011,
39.809039362307594,
148.3469909977715
]
]
}
\ No newline at end of file
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