diff --git a/TODO.md b/TODO.md index 45e20e7..64d818a 100644 --- a/TODO.md +++ b/TODO.md @@ -32,6 +32,13 @@ With the CAM pinger on and a listener that decodes yawRate: left-positive (316 of 316 turning samples), its `obu_gnss` `yawRate_degps` right-positive despite the API document (27 of 27, bacospat ride of 2026-03-18). Only 27 samples from one ride: re-check on the next CiT One ride with turns. +- [ ] CiT One JSON parsers (fixed 2026-11-18): `ObuGnssParser` returned null for every real `obu_gnss` + message (position is under `gnss`, it read `own_info`), and `CamParser` for every real CAM-topic + message (`stationType` is a name). So on the CiT One the ego state never came from `obu_gnss`, + the app's V2X logger wrote no OBU fixes, and the processed CAM fallback was dead. On the next + CiT One session: the use-case engine runs on `obu_gnss` (the phone GNSS fallback only kicks in + after 2.5 s without it), the own station ID is learned so own CAMs stop showing as remote, and + the V2X log has `obu` rows in its `gnss` table. ### obu-firmware on Niklas Dathe's microbu-firmware (added 2026-11-10, bench run 2026-11-11) diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt index 94eb3d3..eb834ca 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt @@ -14,6 +14,20 @@ object StationType { * is 15 and not 12 - mapping by list position mislabels every RSU. */ const val ROAD_SIDE_UNIT = 15 + + /** + * The Use Case API's `stationType` string enum ("passengerCar") mapped to its ITS-G5 integer, + * per StationType in the ETSI CDD (CiT MQTT API v6, sections 2.1.1 and 2.2.3). Mapped by name, + * never by list position, because of the gap before [ROAD_SIDE_UNIT]. + */ + private val BY_API_NAME = mapOf( + "unknown" to 0, "pedestrian" to 1, "cyclist" to 2, "moped" to 3, "motorcycle" to 4, + "passengerCar" to 5, "bus" to 6, "lightTruck" to 7, "heavyTruck" to 8, "trailer" to 9, + "specialVehicles" to 10, "tram" to 11, "roadSideUnit" to 15, + ) + + /** The ITS-G5 integer for an API station type name, or null for a blank or unknown name. */ + fun fromApiName(name: String?): Int? = name?.takeIf { it.isNotBlank() }?.let { BY_API_NAME[it] } } /** diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt index e8f58f4..df92a80 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt @@ -27,7 +27,11 @@ object CamParser { val obj = runCatching { JSONObject(json) }.getOrNull() ?: return null val stationId = JsonFieldReader.firstLong(obj, "stationId", "stationID", "station_id") ?: return null - val stationType = JsonFieldReader.firstInt(obj, "stationType", "station_type") ?: return null + // The API sends the name ("passengerCar", CiT MQTT API v6 section 2.2.3); a number is still + // accepted. Until 2026-11-18 only a number was, so every real payload was dropped here. + val stationType = JsonFieldReader.firstInt(obj, "stationType", "station_type") + ?: StationType.fromApiName(obj.optString("stationType")) + ?: return null val (lat, lon) = JsonFieldReader.firstLatLon(obj) ?: return null @@ -39,8 +43,8 @@ object CamParser { ?.let { -it } val driveDirection = JsonFieldReader.firstInt(obj, "driveDirection", "drive_direction") - val vehicleLength = JsonFieldReader.firstDouble(obj, "vehicleLength", "vehicle_length") - val vehicleWidth = JsonFieldReader.firstDouble(obj, "vehicleWidth", "vehicle_width") + val vehicleLength = JsonFieldReader.firstDouble(obj, "vehicleLength_m", "vehicleLength", "vehicle_length") + val vehicleWidth = JsonFieldReader.firstDouble(obj, "vehicleWidth_m", "vehicleWidth", "vehicle_width") val accel = JsonFieldReader.firstDouble(obj, "accelerationControl", "longitudinalAcceleration", "acceleration_mps2") // Own/remote: prefer an explicit flag if the API provides one; otherwise compare diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt index a2968b7..651eeac 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt @@ -24,15 +24,27 @@ object ObuGnssParser { */ fun parseEgo(json: String, fallbackStationId: Long = 0L, timestamp: Long = System.currentTimeMillis()): Cam? { val root = runCatching { JSONObject(json) }.getOrNull() ?: return null + // Identity and yaw rate are in `own_info`, kinematics in `gnss` (CiT MQTT API v6 Listing 2.1, + // and every logged payload). Until 2026-11-18 everything was read from `own_info`, which + // has no position, so every real message was dropped and the ego state never came from + // here. A flat payload with everything on the root is still accepted. val obj = root.optJSONObject("own_info") ?: root + val gnss = root.optJSONObject("gnss") ?: obj val stationId = JsonFieldReader.firstLong(obj, "stationID", "stationId", "station_id") ?: fallbackStationId - val stationType = JsonFieldReader.firstInt(obj, "stationType", "station_type") ?: StationType.CYCLIST + val stationType = JsonFieldReader.firstInt(obj, "stationType", "station_type") + ?: StationType.fromApiName(obj.optString("stationType")) + ?: StationType.CYCLIST - val (lat, lon) = JsonFieldReader.firstLatLon(obj) ?: return null + val (lat, lon) = JsonFieldReader.firstLatLon(gnss) ?: JsonFieldReader.firstLatLon(obj) ?: return null + // 91 / 181 is the API's "no 2D fix" marker, not a place. + if (lat > 90.0 || lon > 180.0) return null - val speed = JsonFieldReader.firstDouble(obj, "speed_mps", "speed", "speedMps") ?: 0.0 - val heading = JsonFieldReader.firstDouble(obj, "heading_deg", "heading", "headingDeg") ?: 0.0 + // 163.82 m/s is the API's "invalid speed" marker; treated like a missing speed. + val speed = JsonFieldReader.firstDouble(gnss, "speed_mps", "speed", "speedMps") + ?.takeIf { it < INVALID_SPEED_MPS } ?: 0.0 + val heading = JsonFieldReader.firstDouble(gnss, "heading_deg", "heading", "headingDeg") ?: 0.0 + val acceleration = JsonFieldReader.firstDouble(gnss, "acceleration_mpss") // `yawRate_degps` is this topic's name (CiT MQTT API v6, Listing 2.1, own_info, and every // logged payload). The document says positive to the left, but the CiT One sends it positive // to the RIGHT: in the 2026-03-18 bacospat log, all 27 one-second windows of turning at over @@ -49,11 +61,17 @@ object ObuGnssParser { speedMps = speed, headingDeg = JsonFieldReader.normaliseHeadingDeg(heading), yawRateDps = yawRate, + vehicleLengthM = JsonFieldReader.firstDouble(obj, "vehicleLength_m"), + vehicleWidthM = JsonFieldReader.firstDouble(obj, "vehicleWidth_m"), + accelerationMps2 = acceleration, timestamp = timestamp, isOwn = true, ) } + /** The API's "invalid speed calculation" value (CiT MQTT API v6 section 2.1.1). */ + private const val INVALID_SPEED_MPS = 163.82 + /** Just the stationID/stationType, for when only identity (not a full fix) is needed. */ fun parseOwnIdentity(json: String): Pair? { val root = runCatching { JSONObject(json) }.getOrNull() ?: return null diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/denm/DenmEvent.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/denm/DenmEvent.kt index 955f709..8346f39 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/denm/DenmEvent.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/denm/DenmEvent.kt @@ -1,6 +1,7 @@ package com.hawhamburg.micr0bu.domain.denm import com.hawhamburg.micr0bu.domain.cam.JsonFieldReader +import com.hawhamburg.micr0bu.domain.cam.StationType import org.json.JSONObject /** @@ -123,17 +124,6 @@ object DenmParser { */ fun causeCodeName(causeCode: Int?): String? = causeCode?.let { NAME_BY_CAUSE_CODE[it] } - /** - * The Use Case API's `stationType` string enum mapped to its ITS-G5 integer, per StationType in - * the ETSI CDD. Note roadSideUnit is **15**, not 12 - the enumeration has a gap after tram(11), - * so mapping by list position would silently mislabel every RSU. - */ - private val STATION_TYPE_BY_NAME = mapOf( - "unknown" to 0, "pedestrian" to 1, "cyclist" to 2, "moped" to 3, "motorcycle" to 4, - "passengerCar" to 5, "bus" to 6, "lightTruck" to 7, "heavyTruck" to 8, "trailer" to 9, - "specialVehicles" to 10, "tram" to 11, "roadSideUnit" to 15, - ) - /** * Parses the API's RFC3339 timestamps ("2021-05-11T12:01:02+00:00") to epoch millis. The air * path carries a binary TimestampIts instead, so the two transports arrive here in completely @@ -187,7 +177,7 @@ object DenmParser { val isTermination = obj.has("termination") && obj.optBoolean("termination", true) val stationType = JsonFieldReader.firstInt(obj, "stationType", "station_type") - ?: STATION_TYPE_BY_NAME[obj.optString("stationType").takeIf { it.isNotBlank() }] + ?: StationType.fromApiName(obj.optString("stationType")) return DenmEvent( stationId = stationId, diff --git a/app/src/test/java/com/hawhamburg/micr0bu/CitOneJsonParsersTest.kt b/app/src/test/java/com/hawhamburg/micr0bu/CitOneJsonParsersTest.kt new file mode 100644 index 0000000..99c0a65 --- /dev/null +++ b/app/src/test/java/com/hawhamburg/micr0bu/CitOneJsonParsersTest.kt @@ -0,0 +1,110 @@ +package com.hawhamburg.micr0bu + +import com.hawhamburg.micr0bu.domain.cam.CamParser +import com.hawhamburg.micr0bu.domain.cam.ObuGnssParser +import com.hawhamburg.micr0bu.domain.cam.StationType +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * The two CiT One JSON parsers against payloads shaped exactly like the ones the CiT One really + * sends (keys, nesting and types copied from the logged runs, values changed). Both parsers used to + * return null for every one of them: `obu_gnss` keeps its position under `gnss`, not `own_info`, + * and the CAM topic sends `stationType` as a name. + */ +class CitOneJsonParsersTest { + + private val obuGnss = """ + { "type": "v2xgw-owninfo", "obu_timestamp_sec": 1773834329.0660601, + "gnss": { "latitude": 53.5551, "longitude": 10.0222, "altitude_m": 60.5, "speed_mps": 5.25, + "time_utc": "2026-03-18T11:45:29Z", "heading_deg": 87.5, "acceleration_mpss": -0.25, + "curvature": { "radiusOfCurve_m": 3276.0, "confidence": 100.0 }, + "positionError_stdDev": { "posErrSemiMajorAxisLen_m": 0.446, "posErrSemiMinorAxisLen_m": 0.446, + "posErrSemiMajorAxisOrientation_deg": 0.0, "altitudeErr_m": 0.609, "headingErr_deg": 361.0, + "groundSpeedErr_mps": 0.077, "verticalSpeedErr_mps": -1.0, "DOP": -4.45e-25 }, + "satInfo": { "fix": "3d", "numSatUsed": 14, + "numSatVisible": { "gps": 0, "gbas": 0, "sbas": 0, "gnss": 0 } } }, + "own_info": { "stationID": 319450879, "stationType": 5, "vehicleRole": 0, "vehicleWidth_m": 1.8, + "vehicleLength_m": 4.26, "yawRate_degps": 6.5, "isBreaking": false }, + "pathHistory": { "refLatitude": 53.5551, "refLongitude": 10.0222, "refAltitude": 60.5, + "pathPoints": [ { "deltaLatitude": -3.5e-06, "deltaLongitude": 1.31e-05, + "deltaAltitude_m": -5.0, "pathDeltaTime_ms": 834 } ] } } + """.trimIndent() + + private val processedCam = """ + {"heading_deg":1.2,"pathHistory":{"path":{"coordinates":[[10.0258218,53.5526996,52.6], + [10.0258295,53.5524988,51.6]],"type":"LineString"}},"position":{"coordinates":[10.0258076,53.5528397,52.6], + "type":"Point"},"positionError_stdDev":{"altitudeErr_m":10.0,"groundSpeedErr_mps":0.105,"headingErr_deg":0.5, + "posErrSemiMajorAxisLen_m":1.57,"posErrSemiMajorAxisOrientation_deg":5.4,"posErrSemiMinorAxisLen_m":1.56}, + "speed_mps":7.25,"stationId":3124041003,"stationType":"passengerCar","type":"v2x-cam", + "vehicleLength_m":4.9,"vehicleRole":"default","vehicleWidth_m":1.8,"yawRate_dps":-1.43} + """.trimIndent() + + @Test + fun `obu_gnss - kinematics from gnss, identity and yaw rate from own_info`() { + val ego = ObuGnssParser.parseEgo(obuGnss, timestamp = 1_000L)!! + assertEquals(319450879L, ego.stationId) + assertEquals(StationType.PASSENGER_CAR, ego.stationType) + assertEquals(53.5551, ego.latitude, 1e-9) + assertEquals(10.0222, ego.longitude, 1e-9) + assertEquals(5.25, ego.speedMps, 1e-9) + assertEquals(87.5, ego.headingDeg, 1e-9) + assertEquals(-0.25, ego.accelerationMps2!!, 1e-9) + // Right-positive on this topic in practice, as Cam.yawRateDps is: taken as it is. + assertEquals(6.5, ego.yawRateDps!!, 1e-9) + assertEquals(4.26, ego.vehicleLengthM!!, 1e-9) + assertEquals(1.8, ego.vehicleWidthM!!, 1e-9) + assertTrue(ego.isOwn) + } + + @Test + fun `obu_gnss - the no-fix marker is not a position`() { + val noFix = obuGnss.replace("\"latitude\": 53.5551", "\"latitude\": 91.0") + .replace("\"longitude\": 10.0222", "\"longitude\": 181.0") + assertNull(ObuGnssParser.parseEgo(noFix)) + } + + @Test + fun `obu_gnss - the invalid speed marker reads as no speed`() { + val badSpeed = obuGnss.replace("\"speed_mps\": 5.25", "\"speed_mps\": 163.82") + assertEquals(0.0, ObuGnssParser.parseEgo(badSpeed)!!.speedMps, 0.0) + } + + @Test + fun `CAM topic - station type name, vehicle size and a left-positive yaw rate`() { + val cam = CamParser.parse(processedCam, ownStationId = null)!! + assertEquals(3124041003L, cam.stationId) + assertEquals(StationType.PASSENGER_CAR, cam.stationType) + assertEquals(53.5528397, cam.latitude, 1e-9) + assertEquals(10.0258076, cam.longitude, 1e-9) + assertEquals(7.25, cam.speedMps, 1e-9) + assertEquals(1.2, cam.headingDeg, 1e-9) + // -1.43 on this left-positive topic is 1.43 deg/s to the right: +1.43 in Cam.yawRateDps. + assertEquals(1.43, cam.yawRateDps!!, 1e-9) + assertEquals(4.9, cam.vehicleLengthM!!, 1e-9) + assertEquals(1.8, cam.vehicleWidthM!!, 1e-9) + assertFalse(cam.isOwn) + } + + @Test + fun `CAM topic - own CAM recognised by the station id learned from obu_gnss`() { + assertTrue(CamParser.parse(processedCam, ownStationId = 3124041003L)!!.isOwn) + } + + @Test + fun `CAM topic - an unknown station type name is still rejected`() { + assertNull(CamParser.parse(processedCam.replace("passengerCar", "spaceship"), ownStationId = null)) + } + + @Test + fun `station type names map by name, with the gap before roadSideUnit`() { + assertEquals(2, StationType.fromApiName("cyclist")) + assertEquals(11, StationType.fromApiName("tram")) + assertEquals(15, StationType.fromApiName("roadSideUnit")) + assertNull(StationType.fromApiName("")) + assertNull(StationType.fromApiName(null)) + } +}