ics-simlab-config-gen-claude/services/interface_extract.py

41 lines
1.4 KiB
Python

import json
from pathlib import Path
from typing import Dict, Set, Any
def extract_plc_io(config: Dict[str, Any]) -> Dict[str, Dict[str, Set[str]]]:
out: Dict[str, Dict[str, Set[str]]] = {}
for plc in config.get("plcs", []):
name = plc["name"]
inputs: Set[str] = set()
outputs: Set[str] = set()
registers = plc.get("registers", {})
for reg_list in registers.values():
for r in reg_list:
rid = r.get("id")
rio = r.get("io")
if rid and rio == "input":
inputs.add(rid)
if rid and rio == "output":
outputs.add(rid)
out[name] = {"inputs": inputs, "outputs": outputs}
return out
def extract_hil_io(config: Dict[str, Any]) -> Dict[str, Dict[str, Set[str]]]:
out: Dict[str, Dict[str, Set[str]]] = {}
for hil in config.get("hils", []):
name = hil["name"]
inputs: Set[str] = set()
outputs: Set[str] = set()
for pv in hil.get("physical_values", []):
n = pv.get("name")
rio = pv.get("io")
if n and rio == "input":
inputs.add(n)
if n and rio == "output":
outputs.add(n)
out[name] = {"inputs": inputs, "outputs": outputs}
return out
def load_config(path: str) -> Dict[str, Any]:
return json.loads(Path(path).read_text(encoding="utf-8"))