"""Independent reference extraction, never imports the production DTU model.""" import hashlib import re from pathlib import Path ROOT = Path(__file__).resolve().parents[3] EXAMPLES = ROOT / "IEC60870-Slave" / "examples" def reference(): text = (EXAMPLES / "iec104_dtu_profile.h").read_text(encoding="utf-8-sig") tables = {} for name in ("signals", "measurements", "energy", "commands", "parameters"): body = re.search(r"dtu_" + name + r"\[\]\s*=\s*\{(.*?)\n\};", text, re.S).group(1) tables[name] = [int(value, 16) for value in re.findall(r"\{0x([0-9A-Fa-f]+)U,", body)] parameters = {} for ioa, tag, value, configured in re.findall( r'\{0x([0-9A-Fa-f]+)U, (\d+)U, (NULL|"[^"]*"), (true|false),', text): if configured == "true": parameters[int(ioa, 16)] = (int(tag), value.strip('"')) assert [len(tables[x]) for x in tables] == [59, 30, 4, 19, 430] assert len(parameters) == 345 assert set(tables["signals"]) == set(range(1, 0x42)) - set(range(0x2C, 0x32)) assert set(tables["measurements"]) == set(range(0x4001, 0x401F)) gi = {ioa: 1 for ioa in tables["signals"]} gi.update({ioa: 3 for ioa in range(0x1001, 0x1011)}) gi.update({ioa: 13 for ioa in tables["measurements"]}) for group in range(4): for index in range(30): gi[0x8601 + group * 0x20 + index] = 112 if group == 3 else (110 if group == 0 and index >= 4 else 111) read = {ioa: 30 if typ == 1 else 31 if typ == 3 else typ for ioa, typ in gi.items() if typ < 100} read.update({ioa: 5 for ioa in range(0x1201, 0x1209)}) read.update({ioa: 7 for ioa in range(0x1301, 0x1309)}) diagnostics = {ioa: 13 for ioa in range(0x9601, 0x962D) if ioa not in range(0x961C, 0x961F)} diagnostics.update({ioa: 30 for ioa in range(0x9141, 0x919E)}) diagnostics.update({ioa: 30 for ioa in range(0xB100, 0xB110)}) return {"profile": tables, "remote": parameters, "gi": gi, "read": read, "diagnostic_aliases_pending_scope": diagnostics, "counters": list(range(0x6401, 0x6409)), "hashes": {name: hashlib.sha256((EXAMPLES / name).read_bytes()).hexdigest() for name in ("iec104_dtu_profile.h", "iec104_example_points.h", "iec104_slave_example.c")}}