跳转至

Motion Plan Repository

controller.infrastructure.persistence.motion_plan_repository

运动方案持久化仓库

MotionPlanRepository

MotionPlanRepository(file_path: str = './motion_planning_plans.json')

运动方案持久化仓库。

职责: 1. 保存/加载方案到JSON文件 2. 管理文件系统操作

属性:

名称 类型 描述
file_path Path

方案文件路径。

初始化仓库。

参数:

名称 类型 描述 默认
file_path str

文件路径. Defaults to "./motion_planning_plans.json".

'./motion_planning_plans.json'
源代码位于: src/controller/controller/infrastructure/persistence/motion_plan_repository.py
20
21
22
23
24
25
26
def __init__(self, file_path: str = "./motion_planning_plans.json"):
    """初始化仓库。

    Args:
        file_path (str, optional): 文件路径. Defaults to "./motion_planning_plans.json".
    """
    self.file_path = Path(file_path)

save

save(plans_data: List[Dict], current_index: int)

保存方案数据到文件。

参数:

名称 类型 描述 默认
plans_data List[Dict]

方案数据列表。

必需
current_index int

当前选中方案索引。

必需
源代码位于: src/controller/controller/infrastructure/persistence/motion_plan_repository.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def save(self, plans_data: List[Dict], current_index: int):
    """保存方案数据到文件。

    Args:
        plans_data (List[Dict]): 方案数据列表。
        current_index (int): 当前选中方案索引。
    """
    data = {
        "current_index": current_index,
        "plans": plans_data
    }
    try:
        with open(self.file_path, 'w', encoding='utf-8') as f:
            json.dump(data, f, indent=2, ensure_ascii=False)
    except Exception as e:
        pass

load

load() -> tuple[List[Dict], int]

从文件加载方案数据。

返回:

类型 描述
tuple[List[Dict], int]

tuple[List[Dict], int]: (plans_data, current_index)。 如果文件不存在或加载失败,返回默认方案。

源代码位于: src/controller/controller/infrastructure/persistence/motion_plan_repository.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def load(self) -> tuple[List[Dict], int]:
    """从文件加载方案数据。

    Returns:
        tuple[List[Dict], int]: (plans_data, current_index)。
            如果文件不存在或加载失败,返回默认方案。
    """
    if not self.file_path.exists():
        # 返回默认方案
        return [{"name": "默认方案", "points": []}], 0

    try:
        with open(self.file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)

        plans = data.get("plans", [])
        current_index = data.get("current_index", 0)

        # 如果没有方案,创建默认方案
        if not plans:
            plans = [{"name": "默认方案", "points": []}]
            current_index = 0

        return plans, current_index

    except Exception as e:
        return [{"name": "默认方案", "points": []}], 0