package com.mogo.functions.test import android.text.TextUtils import android.util.Log import androidx.test.core.app.ActivityScenario import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.LargeTest import androidx.test.platform.app.InstrumentationRegistry import com.google.protobuf.ByteString import com.mogo.eagle.core.data.autopilot.AutopilotControlParameters import com.mogo.eagle.core.data.autopilot.AutopilotControlParameters.AutoPilotLine import com.mogo.eagle.core.data.config.FunctionBuildConfig import com.mogo.eagle.core.data.multidisplay.TelematicConstant import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotStatusListenerManager import com.mogo.eagle.core.function.call.autopilot.CallerAutopilotIdentifyListenerManager import com.mogo.eagle.core.function.call.autopilot.CallerChassisLocationWGS84ListenerManager import com.mogo.eagle.core.function.call.autopilot.CallerV2nNioEventListenerManager import com.mogo.eagle.core.function.call.telematic.CallerTelematicManager import com.mogo.eagle.core.function.main.MainLauncherActivity import com.mogo.eagle.core.utilcode.util.CoordinateUtils import com.mogo.eagle.core.utilcode.util.DrivingDirectionUtils import com.mogo.eagle.core.utilcode.util.GsonUtils import com.mogo.map.MapDataWrapper import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import mogo.telematics.pad.MessagePad import mogo.telematics.pad.MessagePad.Location import mogo.telematics.pad.MessagePad.TrackedObject import mogo.telematics.pad.MessagePad.V2nCrossSpeed import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import java.util.Collections import java.util.concurrent.TimeUnit import kotlin.math.cos import kotlin.math.sin @RunWith(AndroidJUnit4::class) @LargeTest class V2NTest { companion object { private const val TAG = "GreenWaveTest" } lateinit var launch: ActivityScenario @Before fun before() { launch = ActivityScenario.launch(MainLauncherActivity::class.java) } @Test fun testRoadShiGong(): Unit = runBlocking { val arguments = InstrumentationRegistry.getArguments() val millis = arguments.getString("delay", "0").toLong() if (millis > 0) { delay(millis) } FunctionBuildConfig.v2nTotalSwitch = true runCatching { val map = HashMap().also { it["f1"] = FunctionBuildConfig.v2nTotalSwitch } CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_NEW_LINK_SWITCH, GsonUtils.toJson(map).toByteArray()) } delay(2000) val targetX = arguments.getString("lon", "0").toDouble() val targetY = arguments.getString("lat", "0").toDouble() val targetDistance = arguments.getString("distance", "0").toDouble() val eventDistance = arguments.getString("event_distance", "50").toDouble() val r = arguments.getString("r", "1").toDouble() var car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() var distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance, event_distance: $eventDistance") while (distance > targetDistance) { delay(2000) car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") } car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() val point = DrivingDirectionUtils.calculateNewPoint(car.longitude, car.latitude, car.heading , eventDistance) val polygon = generateTriangle(point.first, point.second, car.heading, r * 1e-5) val data = TrackedObject.getDefaultInstance().toBuilder().setType(501).setLongitude(point.first).setLatitude(point.second).setAltitude(0.0).setSystemTime(System.currentTimeMillis() * 1.0 / 1000).setSatelliteTime(1.0).setUuid(10).setCarID("1").setColor("#ffffff").setHeading(car.heading).setSpeed(0.0).setDrawLevel(1).setDriverTime(1.0).setCameraIp("172.18.7.40").setVideoUrl("").addAllImageUrl(Collections.singletonList("http://www.baidu.com")).addAllPolygon(polygon.map { Location.getDefaultInstance().toBuilder().setLongitude(it.first).setLatitude(it.second).setAltitude(0.0).setHeading(0.0).setStation(false).build() }).setDetectStartTime(System.currentTimeMillis()).setV2XUuid("xxxxx1").setStrUuid("xxxxx2").build() val trafficData = Collections.singletonList(data) CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_AI_ROAD_SHI_GONG, data.toByteArray()) CallerAutopilotIdentifyListenerManager.invokeAutopilotIdentifyDataUpdate(trafficData) delay(TimeUnit.MINUTES.toMillis(10)) } @Test fun testRoadShiGu(): Unit = runBlocking { val arguments = InstrumentationRegistry.getArguments() val millis = arguments.getString("delay", "0").toLong() if (millis > 0) { delay(millis) } // FunctionBuildConfig.v2nNewLinked = true FunctionBuildConfig.v2nTotalSwitch = true runCatching { val map = HashMap().also { it["f1"] = FunctionBuildConfig.v2nTotalSwitch } CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_NEW_LINK_SWITCH, GsonUtils.toJson(map).toByteArray()) } delay(2000) val targetX = arguments.getString("lon", "0").toDouble() val targetY = arguments.getString("lat", "0").toDouble() val targetDistance = arguments.getString("distance", "0").toDouble() val eventDistance = arguments.getString("event_distance", "0").toDouble() val r = arguments.getString("r", "1").toDouble() var car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() var distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance, event_distance: $eventDistance") while (distance > targetDistance) { delay(2000) car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") } car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() val point = DrivingDirectionUtils.calculateNewPoint(car.longitude, car.latitude, car.heading , eventDistance) val polygon = generateTriangle(point.first, point.second, car.heading, r * 1e-5) val data = TrackedObject.getDefaultInstance().toBuilder().setType(13).setLongitude(point.first).setLatitude(point.second).setAltitude(0.0).setSystemTime(System.currentTimeMillis() * 1.0 / 1000).setSatelliteTime(1.0).setUuid(10).setCarID("1").setColor("#ffffff").setHeading(car.heading).setSpeed(0.0).setDrawLevel(1).setDriverTime(1.0).setCameraIp("172.18.7.40").setVideoUrl("").addAllImageUrl(Collections.singletonList("http://www.baidu.com")).addAllPolygon(polygon.map { Location.getDefaultInstance().toBuilder().setLongitude(it.first).setLatitude(it.second).setAltitude(0.0).setHeading(0.0).setStation(false).build() }).setDetectStartTime(System.currentTimeMillis()).setV2XUuid("xxxxx1").setStrUuid("xxxxx2").build() val trafficData = Collections.singletonList(data) CallerAutoPilotStatusListenerManager.updateAutopilotControlParameters(AutopilotControlParameters().also { it.autoPilotLine = AutoPilotLine(10L, "", "","", "", System.currentTimeMillis(), "") }) CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_AI_ROAD_SHI_GU, data.toByteArray()) CallerAutopilotIdentifyListenerManager.invokeAutopilotIdentifyDataUpdate(trafficData) delay(TimeUnit.MINUTES.toMillis(10)) } private fun generateTriangle(x: Double, y: Double, angle: Double, r: Double): List> { val angles = arrayOf(0, 120, 240) val coordinates = ArrayList>() for(i in 0 until 3) { val radian = Math.toRadians(angles[i] + angle) val x1 = x + (r * cos(radian)) val y1 = y + (r * sin(radian)) coordinates += Pair(x1, y1) } coordinates.add(coordinates[0]) return coordinates } @Test fun testGreenWave(): Unit = runBlocking { val arguments = InstrumentationRegistry.getArguments() val millis = arguments.getString("delay", "0").toLong() if (millis > 0) { delay(millis) } val targetX = arguments.getString("lon", "0").toDouble() val targetY = arguments.getString("lat", "0").toDouble() val targetDistance = arguments.getString("distance", "0").toDouble() var car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() var distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") while (distance > targetDistance) { delay(2000) car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") } Log.d(TAG, "开始获取路口数据...") var crossInfo = MapDataWrapper.getCrossRoad(car.longitude, car.latitude, car.heading) while (crossInfo == null || TextUtils.isEmpty(crossInfo.cross_id) || TextUtils.isEmpty(crossInfo.cross_id_end)) { Log.d(TAG, "获取到的路口数据无效, 1秒后重试") delay(TimeUnit.SECONDS.toMillis(1)) Log.d(TAG, "1秒时间到,开始计算新的路口数据") car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() crossInfo = MapDataWrapper.getCrossRoad(car.longitude, car.latitude, car.heading) } val newPoint = DrivingDirectionUtils.calculateNewPoint(car.longitude, car.latitude, car.heading , 200.0) Log.d(TAG, "获取到路口数据..., 自车:[${car.longitude}, ${car.latitude}]前方200米外的点: [${newPoint.first}, ${newPoint.second}]") // FunctionBuildConfig.v2nNewLinked = true FunctionBuildConfig.v2nTotalSwitch = true runCatching { val map = HashMap().also { it["f1"] = FunctionBuildConfig.v2nTotalSwitch } CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_NEW_LINK_SWITCH, GsonUtils.toJson(map).toByteArray()) } delay(2000) val data = V2nCrossSpeed.getDefaultInstance().toBuilder().setSpeedLeftMin(0).setSpeedLeftMax(0).setSpeedRightMin(0).setSpeedRightMax(0).setSpeedStraightMin(30).setSpeedStraightMax(54).setMaxSpeed(0).setMaxStatus(0).setLng(newPoint.first).setLat(newPoint.second).build() CallerV2nNioEventListenerManager.invokeV2nNioGreenWavePassageEvent(data) CallerAutoPilotStatusListenerManager.updateAutopilotControlParameters(AutopilotControlParameters().also { it.autoPilotLine = AutoPilotLine(10L, "", "","", "", System.currentTimeMillis(), "") }) while (!CallerTelematicManager.getServerStarted()) { Log.d(TAG , "乘客屏未连接, 2秒后重试...") delay(2000) } Log.d(TAG, "乘客屏已连接, 将数据发送给乘客屏...") CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_AI_ROAD_GREEN_WAVE, data.toByteArray()) delay(TimeUnit.MINUTES.toMillis(10)) } @Test fun testPeopleCross(): Unit = runBlocking { val arguments = InstrumentationRegistry.getArguments() val millis = arguments.getString("delay", "0").toLong() if (millis > 0) { delay(millis) } // FunctionBuildConfig.v2nNewLinked = true FunctionBuildConfig.v2nTotalSwitch = true runCatching { val map = HashMap().also { it["f1"] = FunctionBuildConfig.v2nTotalSwitch } CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_NEW_LINK_SWITCH, GsonUtils.toJson(map).toByteArray()) } delay(2000) val targetX = arguments.getString("lon", "0").toDouble() val targetY = arguments.getString("lat", "0").toDouble() val targetHeading = arguments.getString("angle", "0").toDouble() val targetDistance = arguments.getString("distance", "0").toDouble() val eventDistance = arguments.getString("event_distance", "60").toDouble() var car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() var distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") while (distance > targetDistance) { delay(2000) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) } Log.d(TAG, "2 -- > target_distance: $targetDistance, current distance: $distance") car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() var newPoint = DrivingDirectionUtils.calculateNewPoint(car.longitude, car.latitude, car.heading , eventDistance) Log.d(TAG, "location:[x:${targetX}, y:${targetY}, new_location:[x:${newPoint.first}, y:${newPoint.second}]") val eventId = "xxxxxxxxxxxxx" CallerAutoPilotStatusListenerManager.updateAutopilotControlParameters(AutopilotControlParameters().also { it.autoPilotLine = AutoPilotLine(10L, "", "","", "", System.currentTimeMillis(), "") }) for (i in 0 until 500) { val data = MessagePad.Event.getDefaultInstance().toBuilder().setLongitude(newPoint.first).setLatitude(newPoint.second).setGnssType(2).setEventId(eventId).setTimestamp(System.currentTimeMillis()).addAllTargetIds(emptyList()).addTargetIdsBytes(ByteString.EMPTY).addTargetIds("0").setExts("{ \"cameraIp\": \"172.18.7.40\",\"eventExtUnits\":[{\"heading\":266.5414733886719,\"uuid\":\"e440951e-5eb6-4091-8560-72a5d8aaf229\"}] }").build() CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_AI_ROAD_PEOPLE_CROSS, data.toByteArray()) CallerV2nNioEventListenerManager.invokeV2nNioCrossoverEvent(data) delay(50) newPoint = DrivingDirectionUtils.calculateNewPoint(newPoint.first, newPoint.second, targetHeading, 1.0) } delay(TimeUnit.MINUTES.toMillis(10)) } @Test fun testOtherRetrogradeVehicle(): Unit = runBlocking { val arguments = InstrumentationRegistry.getArguments() val millis = arguments.getString("delay", "0").toLong() if (millis > 0) { delay(millis) } // FunctionBuildConfig.v2nNewLinked = true FunctionBuildConfig.v2nTotalSwitch = true runCatching { val map = HashMap().also { it["f1"] = FunctionBuildConfig.v2nTotalSwitch } CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_NEW_LINK_SWITCH, GsonUtils.toJson(map).toByteArray()) } delay(2000) val targetX = arguments.getString("lon", "0").toDouble() val targetY = arguments.getString("lat", "0").toDouble() val targetHeading = arguments.getString("angle", "0").toDouble() val targetDistance = arguments.getString("distance", "0").toDouble() val eventDistance = arguments.getString("event_distance", "60").toDouble() var car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() var distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") while (distance > targetDistance) { delay(2000) Log.d(TAG, "target_distance: $targetDistance, current distance: $distance") car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() distance = CoordinateUtils.calculateLineDistance(car.longitude, car.latitude, targetX, targetY) } car = CallerChassisLocationWGS84ListenerManager.getChassisLocationWGS84() Log.d(TAG, "2 -- > target_distance: $targetDistance, current distance: $distance") var newPoint = DrivingDirectionUtils.calculateNewPoint(car.longitude, car.latitude, car.heading , eventDistance) Log.d(TAG, "location:[x:${targetX}, y:${targetY}, new_location:[x:${newPoint.first}, y:${newPoint.second}]") val eventId = System.currentTimeMillis().toString() CallerAutoPilotStatusListenerManager.updateAutopilotControlParameters(AutopilotControlParameters().also { it.autoPilotLine = AutoPilotLine(10L, "", "","", "", System.currentTimeMillis(), "") }) for (i in 0 until 500) { val data = MessagePad.Event.getDefaultInstance().toBuilder().setLongitude(newPoint.first).setLatitude(newPoint.second).setEventId(eventId).setTimestamp(System.currentTimeMillis()).addAllTargetIds(emptyList()).addTargetIdsBytes(ByteString.EMPTY).setGnssType(2).addTargetIds("0").setExts("{ \"cameraIp\": \"172.18.7.40\",\"eventExtUnits\":[{\"heading\":266.5414733886719,\"uuid\":\"e440951e-5eb6-4091-8560-72a5d8aaf229\"}] }").build() CallerTelematicManager.sendMsgToAllClients(TelematicConstant.V2N_AI_ROAD_OTHER_RETROGRADE_VEHICLE, data.toByteArray()) CallerV2nNioEventListenerManager.invokeV2nNioOtherRetrogradeEvent(data) delay(50) newPoint = DrivingDirectionUtils.calculateNewPoint(newPoint.first, newPoint.second, targetHeading, 1.0) } delay(TimeUnit.MINUTES.toMillis(10)) } }