From feea06fc3509550ee5b6faf866ae04a8592eecdc Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Mon, 13 Jul 2026 18:43:57 +0200 Subject: [PATCH] Improve tactic for J1939 metadata parsing on proprietary frames --- decoder.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/decoder.py b/decoder.py index f47397c..47f850d 100644 --- a/decoder.py +++ b/decoder.py @@ -14,17 +14,14 @@ def decode_j1939_metadata(lf: pl.LazyFrame) -> pl.LazyFrame: """ id_int = pl.col("ID").str.to_integer(base=16).cast(pl.UInt32) - id_shifted_8 = id_int // 256 - id_shifted_16 = id_int // 65536 - priority = ((id_int // 67108864) % 8).cast(pl.UInt8) - pf = (id_shifted_16 % 256).cast(pl.UInt8) - ps = (id_shifted_8 % 256).cast(pl.UInt8) + pf = ((id_int // 65536) % 256).cast(pl.UInt8) + ps = ((id_int // 256) % 256).cast(pl.UInt8) sa = (id_int % 256).cast(pl.UInt8) - da = pl.when(pf < 240).then(ps).otherwise(pl.lit(255, dtype=pl.UInt8)) + da = pl.when(pf < 240).then(ps).otherwise(pl.lit(255, dtype=pl.UInt8)).cast(pl.UInt8) - pgn = pl.when(pf < 240).then(id_shifted_8 % 65536).otherwise(id_shifted_8 % 262144).cast(pl.UInt32) + pgn = pl.when(pf < 240).then(((id_int // 256) & 0x3FF00)).otherwise(((id_int // 256) & 0x3FFFF)).cast(pl.UInt32) return lf.with_columns( pl.struct([ @@ -42,21 +39,27 @@ def decode_j1939_frames(df: pl.DataFrame) -> pl.DataFrame: is_j1939 = id_int > 0x7FF - priority = (id_int // 67108864) % 8 - pf = (id_int // 65536) % 256 - ps = (id_int // 256) % 256 - sa = id_int % 256 - da = pl.when(pf < 240).then(ps).otherwise(255) - pgn = pl.when(pf < 240).then((id_int // 256) % 65536).otherwise((id_int // 256) % 262144) + priority = ((id_int // 67108864) % 8).cast(pl.UInt8) + pf = ((id_int // 65536) % 256).cast(pl.UInt8) + ps = ((id_int // 256) % 256).cast(pl.UInt8) + sa = (id_int % 256).cast(pl.UInt8) + + da = pl.when(pf < 240).then(ps).otherwise(pl.lit(255, dtype=pl.UInt8)).cast(pl.UInt8) + + pgn = pl.when(pf < 240).then( + ((id_int // 256) & 0x3FF00) + ).otherwise( + ((id_int // 256) & 0x3FFFF) + ).cast(pl.UInt32) j1939_meta = pl.when(is_j1939).then( pl.struct([ - priority.cast(pl.UInt8).alias("Priority"), - pf.cast(pl.UInt8).alias("PF"), - ps.cast(pl.UInt8).alias("PS"), - sa.cast(pl.UInt8).alias("SA"), - da.cast(pl.UInt8).alias("DA"), - pgn.cast(pl.UInt32).alias("PGN") + priority.alias("Priority"), + pf.alias("PF"), + ps.alias("PS"), + sa.alias("SA"), + da.alias("DA"), + pgn.alias("PGN") ]) ).otherwise(None)