2026-11-30 14:00:59 +01:00
|
|
|
"""Adds MAPEMs to the list the app ships with (app/src/main/assets/mapem/hamburg.txt).
|
2026-11-30 13:45:34 +01:00
|
|
|
|
2026-11-30 14:00:59 +01:00
|
|
|
py -3.11 tools/mapem_bundle.py FILE [FILE ...]
|
2026-11-30 13:45:34 +01:00
|
|
|
|
2026-11-30 14:00:59 +01:00
|
|
|
FILE is either
|
|
|
|
|
- a .db saved by the app's V2X data logger (its MAPEM rows are taken), or
|
|
|
|
|
- a text file with one UPER MAPEM per line as hex (`#hex <hex>` lines, as in the test corpus, too).
|
|
|
|
|
|
|
|
|
|
The list keeps what it already has and gains one entry per intersection and revision. A MAPEM of an
|
|
|
|
|
intersection and revision already listed replaces that entry. Review the printed changes, run the
|
|
|
|
|
unit tests, rebuild the app. Needs asn1tools (the ETSI modules in asn1/).
|
2026-11-30 13:45:34 +01:00
|
|
|
"""
|
|
|
|
|
import re
|
2026-11-30 14:00:59 +01:00
|
|
|
import sqlite3
|
|
|
|
|
import sys
|
2026-11-30 13:45:34 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-11-30 14:00:59 +01:00
|
|
|
import asn1tools
|
|
|
|
|
|
2026-11-30 13:45:34 +01:00
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
2026-11-30 14:00:59 +01:00
|
|
|
ASN = ROOT / 'asn1'
|
2026-11-30 13:45:34 +01:00
|
|
|
OUT = ROOT / 'app/src/main/assets/mapem/hamburg.txt'
|
2026-11-30 14:00:59 +01:00
|
|
|
HEADER = ['# Stored MAPEMs. One UPER MAPEM per line, hex, each below a comment naming its intersections.',
|
|
|
|
|
'# Written by tools/mapem_bundle.py; add to it with that script.']
|
|
|
|
|
HEX = re.compile(r'(?:#hex )?((?:[0-9a-fA-F]{2})+)$')
|
2026-11-30 13:45:34 +01:00
|
|
|
|
|
|
|
|
|
2026-11-30 14:00:59 +01:00
|
|
|
def read_hex_lines(path):
|
|
|
|
|
for line in path.read_text(encoding='utf-8').splitlines():
|
|
|
|
|
found = HEX.match(line.strip())
|
|
|
|
|
if found:
|
|
|
|
|
yield bytes.fromhex(found[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_log(path):
|
|
|
|
|
db = sqlite3.connect(f'file:{path.resolve().as_posix()}?mode=ro', uri=True)
|
|
|
|
|
try:
|
|
|
|
|
for (payload,) in db.execute("select distinct payload from messages where msg_type = 'MAPEM'"):
|
|
|
|
|
yield bytes(payload)
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add(entries, spec, uper, source):
|
|
|
|
|
"""Puts [uper] into entries {(key, revision): (uper, label)}; returns the keys that changed."""
|
|
|
|
|
try:
|
|
|
|
|
decoded = spec.decode('MAPEM', uper)
|
|
|
|
|
except Exception as error: # asn1tools raises several types
|
|
|
|
|
print(f' skipped a message from {source} that does not decode: {error}')
|
|
|
|
|
return []
|
|
|
|
|
changed = []
|
|
|
|
|
for i in decoded['map'].get('intersections', []):
|
|
|
|
|
key = (f"{i['id'].get('region', -1)}/{i['id']['id']}", i['revision'])
|
|
|
|
|
if key not in entries or entries[key][0] != uper:
|
|
|
|
|
changed.append((key, 'replaced' if key in entries else 'added', i.get('name')))
|
|
|
|
|
entries[key] = (uper, i.get('name'))
|
|
|
|
|
return changed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(files):
|
|
|
|
|
if not files:
|
|
|
|
|
raise SystemExit(__doc__)
|
|
|
|
|
spec = asn1tools.compile_files([str(ASN / n) for n in
|
|
|
|
|
('spatem_2_2_1.asn', 'mapem_2_2_1.asn', 'dsrc_2_2_1.asn', 'cdd_2_2_1.asn')], 'uper')
|
|
|
|
|
entries = {}
|
|
|
|
|
if OUT.exists():
|
|
|
|
|
for uper in read_hex_lines(OUT):
|
|
|
|
|
add(entries, spec, uper, OUT.name)
|
|
|
|
|
|
|
|
|
|
for name in files:
|
|
|
|
|
path = Path(name)
|
|
|
|
|
if not path.is_file():
|
|
|
|
|
raise SystemExit(f'{name}: no such file')
|
|
|
|
|
reader = read_log if path.suffix.lower() == '.db' else read_hex_lines
|
|
|
|
|
changed = [c for uper in reader(path) for c in add(entries, spec, uper, path.name)]
|
|
|
|
|
print(f'{path.name}: {len(changed)} change(s)')
|
|
|
|
|
for (key, revision), what, label in changed:
|
|
|
|
|
print(f' {what} {key} rev {revision} {label}')
|
2026-11-30 13:45:34 +01:00
|
|
|
|
|
|
|
|
body = []
|
2026-11-30 14:00:59 +01:00
|
|
|
written = set()
|
|
|
|
|
for (key, revision), (uper, _) in sorted(entries.items()):
|
|
|
|
|
if uper in written:
|
2026-11-30 13:45:34 +01:00
|
|
|
continue
|
2026-11-30 14:00:59 +01:00
|
|
|
written.add(uper)
|
|
|
|
|
body += [f'# {k} rev {r} {label}' for (k, r), (u, label) in sorted(entries.items()) if u == uper]
|
|
|
|
|
body.append(uper.hex())
|
2026-11-30 13:45:34 +01:00
|
|
|
|
|
|
|
|
OUT.parent.mkdir(parents=True, exist_ok=True)
|
2026-11-30 14:00:59 +01:00
|
|
|
OUT.write_text('\n'.join(HEADER + body) + '\n', encoding='utf-8', newline='\n')
|
|
|
|
|
print(f'{OUT.relative_to(ROOT)}: {len(entries)} intersection revisions, '
|
|
|
|
|
f'{len({k for k, _ in entries})} intersections')
|
2026-11-30 13:45:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2026-11-30 14:00:59 +01:00
|
|
|
main(sys.argv[1:])
|