"""이전 보고서에도 적용할 수 있도록 저장된 모니터 항목에서 식별 정보를 읽습니다.""" import hashlib import re def usable_serial(value): value = value.strip() if value.casefold() in {"", "unknown", "n/a", "none", "not specified", "serial number", "알 수 없음"}: return "" if re.fullmatch(r"[0\s-]+", value): return "" return value def compatible_monitor(asset, device): """다른 제조사나 다른 모델이 같은 시리얼을 쓰면 자동 연결하지 않습니다.""" def normalized(value): return "".join(c for c in value.casefold() if c.isalnum()) brand = normalized(asset.get("manufacturer", "")) reported_brand = normalized(device["manufacturer"]) if brand and reported_brand and brand != reported_brand: return False model = normalized(asset.get("model", "")) if not model: return True return any(model == value or len(model) >= 4 and model in value for value in (normalized(device["model"]), normalized(device["name"]))) def monitor_devices(parsed): devices = [] for index, item in enumerate(parsed.get("devices", {}).get("MONITOR", [])): props = item.get("properties", []) def pick(*labels, kind=""): return next((p["value"].strip() for p in props if p.get("label", "").casefold() in {label.casefold() for label in labels} or kind and p.get("type") == kind), "") name = item.get("name", "모니터") model = pick("모니터 이름(제조업체)", "Monitor Name (Manuf)", "Monitor Name (Manufacturer)") or name serial = usable_serial(pick("일련 번호", "Serial Number", "Monitor Serial Number")) instance = pick("디바이스 인스턴스 ID", "Device Instance ID", kind="DeviceInstanceId") # 시리얼이 없는 장치는 모델명만으로 동일 자산이라고 판단하지 않습니다. identity = ("serial:" + serial.casefold() + ":" + model.casefold()) if serial else ( "instance:" + instance.casefold() if instance else f"report:{parsed.get('sha256', '')}:{index}") devices.append({"index": index, "key": hashlib.sha256(identity.encode()).hexdigest(), "name": name, "model": model[:200], "serial": serial[:200], "manufacturer": name.split(" ", 1)[0][:150], "properties": props}) return devices