mirror of
https://github.com/randybb/esphome-configs.git
synced 2026-12-06 01:06:52 +01:00
mcu-its-g5: decoded header as JSON on mcu-its-g5/decoded, 1 Hz per station
Message type, station type and id, position, speed and heading from the GeoNetworking header, so consumers don't need an ASN.1 decoder. Outside its/<node>/#, so the EMQX bridge keeps it off OpenTrafficMap.
This commit is contained in:
1 parent
3676c0bbcc
commit
547dea26e3
3 files changed
+39
No files matched your search
@@ -13,6 +13,7 @@ namespace its_g5_decode {
|
||||
struct Info {
|
||||
uint16_t port{0}; // BTP destination port, 0 = none
|
||||
uint8_t station_type{0}; // ETSI station type from the GN address
|
||||
uint64_t addr{0}; // GN address (M bit, station type, MID), identifies the station
|
||||
bool secured{false}; // carried in an ETSI TS 103 097 signed packet (signature not verified)
|
||||
double lat{0}, lon{0}; // degrees
|
||||
float speed_kmh{0};
|
||||
@@ -116,6 +117,8 @@ inline bool parse(const std::vector<uint8_t> &frame, Info &out) {
|
||||
|
||||
// Long position vector: GN address (8), timestamp (4), lat, lon (1/10 µdeg), PAI | speed, heading
|
||||
out.station_type = (f[pv] >> 2) & 0x1F;
|
||||
for (size_t k = 0; k < 8; k++)
|
||||
out.addr = (out.addr << 8) | f[pv + k];
|
||||
out.lat = be32(f + pv + 12) / 1e7;
|
||||
out.lon = be32(f + pv + 16) / 1e7;
|
||||
int16_t speed = be16(f + pv + 20) & 0x7FFF;
|
||||
@@ -151,4 +154,26 @@ inline bool handle(const std::vector<uint8_t> &frame) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Rate limit per station: true if addr was not let through in the last period_ms.
|
||||
// ponytail: 32 slots, the oldest is evicted; with more stations in range some publish more often.
|
||||
inline bool due(uint64_t addr, uint32_t now, uint32_t period_ms) {
|
||||
static struct {
|
||||
uint64_t addr;
|
||||
uint32_t ms;
|
||||
} slots[32]{};
|
||||
auto *slot = &slots[0];
|
||||
for (auto &s : slots) {
|
||||
if (s.addr == addr) {
|
||||
slot = &s;
|
||||
break;
|
||||
}
|
||||
if (now - s.ms > now - slot->ms)
|
||||
slot = &s;
|
||||
}
|
||||
if (slot->addr == addr && now - slot->ms < period_ms)
|
||||
return false;
|
||||
*slot = {addr, now};
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace its_g5_decode
|
||||
@@ -25,7 +25,9 @@ int main() {
|
||||
assert(i.secured && i.port == 2001 && i.station_type == 5);
|
||||
assert(std::fabs(i.lat - 48.1528384) < 1e-7 && std::fabs(i.lon - 17.1203168) < 1e-7);
|
||||
assert(std::fabs(i.speed_kmh - 50.04f) < 0.01f && std::fabs(i.heading - 123.4f) < 0.01f);
|
||||
assert(i.addr == 0x1400010203040506ULL);
|
||||
assert(handle(f) && stats.cam == 1);
|
||||
assert(due(i.addr, 1000, 1000) && !due(i.addr, 1999, 1000) && due(2, 1999, 1000) && due(i.addr, 2000, 1000));
|
||||
f[30] = 0x48; assert(!parse(f, i)); // not GeoNetworking
|
||||
std::puts("ok");
|
||||
}
|
||||
@@ -137,6 +137,18 @@ its_g5_receiver:
|
||||
ESP_LOGD("its_g5", "%s from %s %.6f,%.6f %.0f km/h %.0f deg%s, rssi %.0f dBm",
|
||||
its_g5_decode::port_name(i.port), its_g5_decode::station_name(i.station_type),
|
||||
i.lat, i.lon, i.speed_kmh, i.heading, i.secured ? ", signed" : "", rssi);
|
||||
// Decoded header for HA/maptool, 1 Hz per station (CAMs come up to 10 Hz per vehicle)
|
||||
if (!its_g5_decode::due(i.addr, millis(), 1000))
|
||||
return;
|
||||
char json[192];
|
||||
int n = snprintf(json, sizeof(json),
|
||||
"{\"msg\":\"%s\",\"station\":\"%s\",\"id\":\"%016llx\",\"lat\":%.7f,\"lon\":%.7f,"
|
||||
"\"speed\":%.1f,\"heading\":%.1f,\"secured\":%s,\"rssi\":%.0f}",
|
||||
its_g5_decode::port_name(i.port), its_g5_decode::station_name(i.station_type),
|
||||
(unsigned long long) i.addr, i.lat, i.lon, i.speed_kmh, i.heading,
|
||||
i.secured ? "true" : "false", rssi);
|
||||
// outside its/${node_id}/#, so the EMQX bridge keeps it off OpenTrafficMap
|
||||
id(mqtt_client).publish("mcu-${device}/decoded", json, n, 0, false);
|
||||
|
||||
sensor:
|
||||
- platform: its_g5_receiver
|
||||
|
||||
Reference in new issue
Block a user