Skip to content

Web API 层重构

版本:未发布 | 日期:2026-05-01

概述

本次修改对后端 Web API 层进行了全面重构,将原来分散的控制器文件重新组织为统一的 src/web/api/ 模块化结构,同时将所有 API 路径从扁平的 /xxx 风格迁移到 RESTful 的 /api/xxx 风格,子路径命名从 snake_case 改为 kebab-case。前端所有 API 调用路径同步更新。

变更动机

  1. 模块化:原来 channel_controller.py 单文件超过 1100 行,将不同职责混在一起,难以维护
  2. RESTful 规范:统一使用 /api 前缀,路径命名更规范
  3. 命名一致性:将 snake_case 路径(如 import_points)改为 kebab-case(如 import-points
  4. Schema 分离:将 Pydantic 模型按业务域拆分到独立文件

目录结构变更

旧结构(已删除)

src/web/
├── app.py
├── channel/
│   ├── __init__.py
│   └── channel_controller.py          (1120 行,全部通道逻辑)
├── device/
│   ├── __init__.py
│   └── device_controller.py           (361 行)
├── device_group/
│   ├── __init__.py
│   └── device_group_controller.py     (283 行)
├── point/
│   ├── point_controller.py            (384 行)
│   ├── point_mapping.py               (126 行)
│   └── point_tree.py                  (12 行)
└── schemas/
    ├── schemas.py                      (270 行,所有模型混在一起)
    ├── schemas_point_mapping.py
    └── schemas_tree.py

新结构

src/web/
├── app.py
└── api/
    ├── __init__.py                     (统一导出所有路由器)
    ├── channel/
    │   ├── __init__.py                 (合并子路由为 channel_router)
    │   ├── router.py                   (通道 CRUD)
    │   ├── device_manage.py            (设备启动/停止/重启/重载/复制)
    │   ├── import_points.py            (Excel/ICD 点表导入)
    │   ├── iec61850.py                 (IEC61850 相关操作)
    │   └── helpers.py                  (公共辅助函数)
    ├── device/
    │   ├── __init__.py
    │   └── router.py                   (设备操作路由)
    ├── device_group/
    │   ├── __init__.py
    │   └── router.py                   (设备组管理路由)
    ├── point/
    │   ├── __init__.py
    │   ├── router.py                   (测点操作路由)
    │   ├── mapping.py                  (测点映射路由)
    │   └── tree.py                     (测点树路由)
    └── schemas/
        ├── __init__.py                 (统一导出所有 Schema)
        ├── base.py                     (BaseResponse)
        ├── channel.py                  (通道相关请求模型)
        ├── device.py                   (设备相关请求模型)
        ├── device_group.py             (设备组相关请求模型)
        ├── point.py                    (测点相关请求模型)
        ├── point_mapping.py            (测点映射相关模型)
        └── tree.py                     (树结构模型)

API 路径映射

通道管理 (/channel/api/channels)

旧路径新路径HTTP 方法
GET /channel/protocolsGET /api/channels/protocols不变
GET /channel/serial_portsGET /api/channels/serial-ports不变
POST /channel/createPOST /api/channels/create不变
POST /channel/import_pointsPOST /api/channels/import-points不变
POST /channel/import_icdPOST /api/channels/import-icd不变
POST /channel/create_and_startPOST /api/channels/create-and-start不变
POST /channel/restart/{channel_id}POST /api/channels/restart/{channel_id}不变
POST /channel/reload_config/{channel_id}POST /api/channels/reload-config/{channel_id}不变
DELETE /channel/{channel_id}DELETE /api/channels/{channel_id}不变
GET /channel/listGET /api/channels/list不变
GET /channel/iec61850_structure/{channel_id}GET /api/channels/iec61850-structure/{channel_id}不变
GET /channel/iec61850_table_data/{channel_id}GET /api/channels/iec61850-table-data/{channel_id}不变
POST /channel/iec61850_read_points/{channel_id}POST /api/channels/iec61850-read-points/{channel_id}不变
GET /channel/{channel_id}GET /api/channels/{channel_id}不变
PUT /channel/{channel_id}PUT /api/channels/{channel_id}不变
POST /channel/copyPOST /api/channels/copy不变

设备管理 (/device/api/devices)

旧路径新路径HTTP 方法变更
POST /device/get_device_listGET /api/devices/listPOST → GET
POST /device/get_device_infoPOST /api/devices/info不变
POST /device/get_slave_id_listPOST /api/devices/slave-id-list不变
POST /device/get_device_tablePOST /api/devices/table不变
POST /device/start_simulationPOST /api/devices/start-simulation不变
POST /device/stop_simulationPOST /api/devices/stop-simulation不变
GET /device/current_table/GET /api/devices/current-table不变
POST /device/startPOST /api/devices/start不变
POST /device/stopPOST /api/devices/stop不变
POST /device/get_auto_read_statusPOST /api/devices/auto-read-status不变
POST /device/start_auto_readPOST /api/devices/start-auto-read不变
POST /device/stop_auto_readPOST /api/devices/stop-auto-read不变
POST /device/manual_readPOST /api/devices/manual-read不变
POST /device/get_messagesPOST /api/devices/messages不变
POST /device/clear_messagesPOST /api/devices/clear-messages不变
POST /device/get_avg_timePOST /api/devices/avg-time不变
POST /device/add_slavePOST /api/devices/add-slave不变
POST /device/delete_slavePOST /api/devices/delete-slave不变
POST /device/edit_slavePOST /api/devices/edit-slave不变

测点管理 (/point/api/points)

旧路径新路径HTTP 方法
POST /point/edit_point_data/POST /api/points/edit-data不变
POST /point/edit_point_limit/POST /api/points/edit-limit不变
POST /point/get_point_limit/POST /api/points/get-limit不变
POST /point/set_single_point_simulate_methodPOST /api/points/set-simulate-method不变
POST /point/set_single_point_stepPOST /api/points/set-simulate-step不变
POST /point/get_point_infoPOST /api/points/info不变
POST /point/set_point_simulation_rangePOST /api/points/set-simulation-range不变
POST /point/edit_point_metadata/POST /api/points/edit-metadata不变
POST /point/read_single_pointPOST /api/points/read-single不变
POST /point/add_pointPOST /api/points/add不变
POST /point/add_points_batchPOST /api/points/add-batch不变
POST /point/delete_pointPOST /api/points/delete不变
POST /point/clear_pointsPOST /api/points/clear-by-slave不变
POST /point/reset_point_dataPOST /api/points/reset-data不变
POST /point/get_point_change_historyPOST /api/points/change-history不变
POST /point/set_change_trackingPOST /api/points/set-change-tracking不变
POST /point/clear_point_change_historyPOST /api/points/clear-change-history不变

测点映射 (/point_mapping/api/point-mappings)

旧路径新路径HTTP 方法
POST /point_mapping/createPOST /api/point-mappings/create不变
POST /point_mapping/updatePOST /api/point-mappings/update不变
POST /point_mapping/deletePOST /api/point-mappings/delete不变
GET /point_mapping/listGET /api/point-mappings/list不变

测点树 (/point_tree/api/point-tree)

旧路径新路径HTTP 方法
GET /point_tree/treeGET /api/point-tree/tree不变

设备组 (/device_group/api/device-groups)

设备组路由为本次重构中新增的模块,旧版为 /device_group 前缀,现已迁移至 /api/device-groups

修改文件清单

后端 — 删除文件

文件说明
src/web/channel/__init__.py旧通道模块入口
src/web/channel/channel_controller.py旧通道控制器(1120 行)
src/web/device/__init__.py旧设备模块入口
src/web/device/device_controller.py旧设备控制器
src/web/device_group/__init__.py旧设备组模块入口
src/web/device_group/device_group_controller.py旧设备组控制器
src/web/point/point_controller.py旧测点控制器
src/web/point/point_mapping.py旧测点映射路由
src/web/point/point_tree.py旧测点树路由
src/web/schemas/schemas.py旧 Schema(270 行混合模型)
src/web/schemas/schemas_point_mapping.py旧映射 Schema
src/web/schemas/schemas_tree.py旧树 Schema

后端 — 新增文件

文件说明
src/web/api/__init__.pyAPI 模块统一导出
src/web/api/channel/__init__.py通道模块入口(合并子路由)
src/web/api/channel/router.py通道 CRUD 路由
src/web/api/channel/device_manage.py设备管理操作路由
src/web/api/channel/import_points.py点表导入路由
src/web/api/channel/iec61850.pyIEC61850 操作路由
src/web/api/channel/helpers.py公共辅助函数
src/web/api/device/__init__.py设备模块入口
src/web/api/device/router.py设备操作路由
src/web/api/device_group/__init__.py设备组模块入口
src/web/api/device_group/router.py设备组管理路由
src/web/api/point/__init__.py测点模块入口
src/web/api/point/router.py测点操作路由
src/web/api/point/mapping.py测点映射路由
src/web/api/point/tree.py测点树路由
src/web/api/schemas/__init__.pySchema 统一导出
src/web/api/schemas/base.pyBaseResponse
src/web/api/schemas/channel.py通道请求模型
src/web/api/schemas/device.py设备请求模型
src/web/api/schemas/device_group.py设备组请求模型
src/web/api/schemas/point.py测点请求模型
src/web/api/schemas/point_mapping.py映射请求模型
src/web/api/schemas/tree.py树结构模型

后端 — 修改文件

文件修改内容
src/web/app.py路由导入路径从旧模块切换到 src.web.api;路由注册去掉 prefix=""
src/data/service/point_tree_service.pySchema 导入路径从 src.web.schemas.schemas_tree 改为 src.web.api.schemas.tree
src/enums/points/base_point.py类型注解优化:address 属性返回类型改为 `int
src/tests/test_api_live.py测试路径从 /api/point_mapping 改为 /api/point-mappings

前端 — 修改文件

文件修改内容
front/src/api/channelApi.ts所有路径从 /channel/* 改为 /api/channels/*snake_case 改为 kebab-case
front/src/api/deviceApi.ts所有路径从 /device/* 改为 /api/devices/*snake_case 改为 kebab-case
front/src/api/pointApi.ts所有路径从 /point/* 改为 /api/points/*snake_case 改为 kebab-case
front/src/api/pointMappingApi.tsBASE_URL/point_mapping 改为 /api/point-mappings
front/src/api/pointTreeApi.tsBASE_URL/point_tree 改为 /api/point-tree

破坏性变更

所有 API 路径均已变更,前后端必须同时部署。

不兼容变更

  1. 所有 API 路径前缀从无变为 /api,Vite 代理配置需确保 /api 正确代理到后端
  2. 子路径 snake_casekebab-case:如 import_pointsimport-pointsserial_portsserial-ports
  3. 设备列表接口 HTTP 方法变更POST /device/get_device_listGET /api/devices/list
  4. 部分路径简化:如 get_device_infoinfoget_point_infoinfo

升级步骤

  1. 部署后端新代码
  2. 部署前端新代码
  3. 确认 Vite 代理配置中 /api 路径正确代理到后端端口

Released under the Apache 2.0 License.