| 1 |
#!/usr/bin/env python |
| 2 |
import os |
| 3 |
from pathlib import Path |
| 4 |
import tempfile |
| 5 |
import unittest |
| 6 |
import urllib |
| 7 |
import urllib.request |
| 8 |
from zipfile import ZipFile |
| 9 |
|
| 10 |
import numpy as np |
| 11 |
|
| 12 |
from asammdf import MDF |
| 13 |
|
| 14 |
|
| 15 |
@unittest.skipIf(os.getenv("NO_NET_ACCESS"), "Test requires Internet access") |
| 16 |
class TestCANBusLogging(unittest.TestCase): |
| 17 |
tempdir_obd: tempfile.TemporaryDirectory[str] |
| 18 |
tempdir_j1939: tempfile.TemporaryDirectory[str] |
| 19 |
|
| 20 |
@classmethod |
| 21 |
def setUpClass(cls) -> None: |
| 22 |
cls.tempdir_obd = tempfile.TemporaryDirectory() |
| 23 |
cls.tempdir_j1939 = tempfile.TemporaryDirectory() |
| 24 |
|
| 25 |
url = "https://github.com/danielhrisca/asammdf/files/4328945/OBD2-DBC-MDF4.zip" |
| 26 |
urllib.request.urlretrieve(url, "test.zip") |
| 27 |
ZipFile(r"test.zip").extractall(cls.tempdir_obd.name) |
| 28 |
Path("test.zip").unlink() |
| 29 |
|
| 30 |
url = "https://github.com/danielhrisca/asammdf/files/4076869/J1939-DBC-MDF4.zip" |
| 31 |
urllib.request.urlretrieve(url, "test.zip") |
| 32 |
ZipFile(r"test.zip").extractall(cls.tempdir_j1939.name) |
| 33 |
Path("test.zip").unlink() |
| 34 |
|
| 35 |
def test_obd_extract(self) -> None: |
| 36 |
print("OBD extract") |
| 37 |
|
| 38 |
temp_dir = Path(TestCANBusLogging.tempdir_obd.name) |
| 39 |
|
| 40 |
for file in temp_dir.iterdir(): |
| 41 |
print(file) |
| 42 |
|
| 43 |
path = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".mf4"][0] |
| 44 |
dbc = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".dbc"][0] |
| 45 |
signals = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".npy"] |
| 46 |
|
| 47 |
with MDF(path) as mdf: |
| 48 |
out = mdf.extract_bus_logging({"CAN": [(dbc, 0)]}, ignore_value2text_conversion=True) |
| 49 |
|
| 50 |
for signal in signals: |
| 51 |
name = signal.stem |
| 52 |
|
| 53 |
target = np.load(signal) |
| 54 |
sig = out.get(name) |
| 55 |
if sig.samples.dtype.kind == "S": |
| 56 |
sig = out.get(name, raw=True) |
| 57 |
|
| 58 |
self.assertTrue(np.array_equal(sig.samples, target), f"{name} {sig} {target}") |
| 59 |
|
| 60 |
def test_j1939_extract(self) -> None: |
| 61 |
print("J1939 extract") |
| 62 |
|
| 63 |
temp_dir = Path(TestCANBusLogging.tempdir_j1939.name) |
| 64 |
dbc = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".dbc"][0] |
| 65 |
signals = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".npy"] |
| 66 |
path = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".mf4"][0] |
| 67 |
|
| 68 |
with MDF(path) as mdf: |
| 69 |
out = mdf.extract_bus_logging({"CAN": [(dbc, 0)]}, ignore_value2text_conversion=True) |
| 70 |
|
| 71 |
for signal in signals: |
| 72 |
name = signal.stem |
| 73 |
|
| 74 |
target = np.load(signal) |
| 75 |
values = out.get(name).samples |
| 76 |
if values.dtype.kind == "S": |
| 77 |
values = out.get(name, raw=True).samples |
| 78 |
|
| 79 |
self.assertTrue(np.array_equal(values, target)) |
| 80 |
|
| 81 |
def test_almost_j1939_extract(self) -> None: |
| 82 |
print("non-standard J1939 extract") |
| 83 |
|
| 84 |
temp_dir = Path(TestCANBusLogging.tempdir_j1939.name) |
| 85 |
|
| 86 |
# dbc = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".dbc"][0] |
| 87 |
# This dbc throws exception without the suggested changes in branch "relaxed_j1939"... |
| 88 |
# else it is identical to the CSS Electronics demo file in test package |
| 89 |
d = os.path.dirname(__file__) |
| 90 |
dbc = os.path.join(d, "almost-J1939.dbc") # Pls replace with file from expanded zip file |
| 91 |
signals = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".npy"] |
| 92 |
path = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".mf4"][0] |
| 93 |
|
| 94 |
with MDF(path) as mdf: |
| 95 |
out = mdf.extract_bus_logging({"CAN": [(dbc, 0)]}) |
| 96 |
|
| 97 |
for signal in signals: |
| 98 |
name = signal.stem |
| 99 |
|
| 100 |
target = np.load(signal) |
| 101 |
values = out.get(name).samples |
| 102 |
|
| 103 |
self.assertTrue(np.array_equal(values, target)) |
| 104 |
|
| 105 |
def test_j1939_get_can_signal(self) -> None: |
| 106 |
print("J1939 get CAN signal") |
| 107 |
|
| 108 |
temp_dir = Path(TestCANBusLogging.tempdir_j1939.name) |
| 109 |
|
| 110 |
path = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".mf4"][0] |
| 111 |
dbc = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".dbc"][0] |
| 112 |
signals = [input_file for input_file in temp_dir.iterdir() if input_file.suffix == ".npy"] |
| 113 |
|
| 114 |
with MDF(path) as mdf: |
| 115 |
for signal in signals: |
| 116 |
name = signal.stem |
| 117 |
|
| 118 |
target = np.load(signal) |
| 119 |
|
| 120 |
values = mdf.get_can_signal(name=name, database=str(dbc)).samples |
| 121 |
self.assertTrue(np.array_equal(values, target)) |
| 122 |
|
| 123 |
values = mdf.get_bus_signal("CAN", name=name, database=str(dbc)).samples |
| 124 |
self.assertTrue(np.array_equal(values, target)) |
| 125 |
|
| 126 |
|
| 127 |
if __name__ == "__main__": |
| 128 |
unittest.main() |