20 lines
636 B
Python
20 lines
636 B
Python
"""设备 ID 衍生字段(MD5 等),供 API 与控制台展示。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import hashlib
|
||
|
||
|
||
def device_id_md5(device_id: str) -> str:
|
||
"""设备 serial / device_id 的 MD5(小写 hex,32 位)。"""
|
||
raw = (device_id or "").strip()
|
||
return hashlib.md5(raw.encode("utf-8")).hexdigest()
|
||
|
||
|
||
def enrich_device_id_fields(device: dict) -> dict:
|
||
"""为设备 dict 补充 device_id_md5(不改变原 dict 引用时可就地更新)。"""
|
||
did = device.get("device_id") or device.get("serial") or ""
|
||
if did:
|
||
device["device_id_md5"] = device_id_md5(str(did))
|
||
return device
|