-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
54 lines (43 loc) · 1.39 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from argparse import ArgumentParser
from collections import Counter
import json
from cannot.ascfile import ASC
from cannot.dbcfile import J1939DBC
def parse_flags():
a = ArgumentParser()
a.add_argument('--dbc', type=str, required=True)
a.add_argument('--asc', type=str, required=True)
a.add_argument('--jsonl', type=str, required=True)
return a.parse_args()
def test_j1939(j1939):
id = 0x18f00430
data = b'\xFF\xFF\xFF\x68\x13\xFF\xFF'
decoding = j1939.decode(id, data)
assert decoding.message.id == 61444
assert decoding.message.name == 'EEC1'
assert decoding.params['EngSpeed'] == 621
def json_dumps_bytes(b):
return ''.join(map(lambda b: '%02X' % int(b), b))
def main(flags):
j1939 = J1939DBC.from_file(flags.dbc)
test_j1939(j1939)
log = ASC.from_file(flags.asc)
out = open(flags.jsonl, 'w')
stats = Counter()
for event in log.events:
decoding = j1939.decode(event.id, event.data)
if decoding:
stats[decoding.message.name] += 1
x = {
'abs_ts': event.abs_ts,
'rel_ts': event.rel_ts,
'id': event.id,
'data': json_dumps_bytes(event.data),
'decoding': decoding.to_json() if decoding else None,
}
line = '%s\n' % json.dumps(x)
out.write(line)
out.close()
print(stats)
if __name__ == '__main__':
main(parse_flags())