Metadata-Version: 2.4
Name: seer-robokit-py
Version: 0.1.7
Summary: RoboKit Python SDK
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Operating System :: POSIX :: Linux
Requires-Python: <3.13,>=3.10
Description-Content-Type: text/markdown
Requires-Dist: protobuf==3.20.3

# RoboKit Python SDK

RoboKit Python SDK 为 Python 应用提供 DDS、channel、service、resource、TCP、HTTP、消息推流、日志和 TUI 能力。

PyPI 发行包名是 `seer-robokit-py`，安装后 Python 导入包名和命令行入口保持 `robokit`。

当前发行目标是 Ubuntu 22.04、CPython 3.10/3.11/3.12，并通过平台相关 wheel 分发 native DDS runtime。

## 安装

RoboKit 包含 native DDS 动态库，wheel 必须同时匹配操作系统、CPU 架构和 CPython ABI。当前只支持 Ubuntu 22.04 上的以下组合：

| CPU 架构        | `uname -m` 输出 | wheel 平台后缀           | Python ABI                |
| --------------- | --------------- | ------------------------ | ------------------------- |
| Intel/AMD 64 位 | `x86_64`        | `manylinux_2_35_x86_64`  | `cp310`、`cp311`、`cp312` |
| ARM 64 位       | `aarch64`       | `manylinux_2_35_aarch64` | `cp310`、`cp311`、`cp312` |

`arm64` 是部分工具对 ARM 64 位的称呼；在 Ubuntu 的 `uname -m` 输出和 RoboKit wheel 文件名中使用 `aarch64`。不支持 `armv7l`、32 位 Python、PyPy、macOS 或 Windows。

### 确认机器架构和 Python

安装前检查目标机器，而不是构建机或下载 wheel 的机器：

```bash
uname -m
python3 -c 'import platform, sys; print(platform.machine()); print(sys.implementation.name); print(sys.version)'
```

预期满足：

- `platform.machine()` 为 `x86_64` 或 `aarch64`；
- `sys.implementation.name` 为 `cpython`；
- Python 为 3.10、3.11 或 3.12；
- `/etc/os-release` 中 `VERSION_ID` 为 `22.04`。

### 从包索引安装

在目标机器上创建应用并安装：

```bash
uv init --app --python 3.10 my-robot-app
cd my-robot-app
uv add seer-robokit-py
uv run robokit examples --help
```

可将 `3.10` 替换为 `3.11` 或 `3.12`。`uv` 根据当前解释器 ABI 和机器架构自动选择 wheel，例如：

```text
CPython 3.11 + x86_64  → *-cp311-cp311-manylinux_2_35_x86_64.whl
CPython 3.11 + aarch64 → *-cp311-cp311-manylinux_2_35_aarch64.whl
CPython 3.12 + x86_64  → *-cp312-cp312-manylinux_2_35_x86_64.whl
CPython 3.12 + aarch64 → *-cp312-cp312-manylinux_2_35_aarch64.whl
```

不能在 x86_64 机器上运行 aarch64 wheel，也不能在 aarch64 机器上运行 x86_64 wheel。类似地，cp310、cp311、cp312 wheel 不能跨 Python ABI 复用。选择错误时，安装器通常会报告 wheel 与当前平台不兼容；不要通过改文件名或强制解包绕过检查。

### 安装本地 wheel

离线部署或验证本地构建时，明确选择与目标机器一致的文件。

x86_64 + CPython 3.11：

```bash
uv venv --python 3.11
uv pip install --python .venv/bin/python \
  /absolute/path/to/seer_robokit_py-<version>-cp311-cp311-manylinux_2_35_x86_64.whl
```

aarch64 + CPython 3.11：

```bash
uv venv --python 3.11
uv pip install --python .venv/bin/python \
  /absolute/path/to/seer_robokit_py-<version>-cp311-cp311-manylinux_2_35_aarch64.whl
```

使用其他 Python 版本时，解释器版本、两个 wheel ABI tag 必须一致：

```text
Python 3.10 ↔ cp310-cp310
Python 3.11 ↔ cp311-cp311
Python 3.12 ↔ cp312-cp312
```

每个 wheel 只携带目标架构的一套公共 DDS 运行库和目标 Python ABI binding。x86_64 与 aarch64 动态库不能混用；不同 Python 版本则在同一架构上使用相同版本的 Fast DDS/Fast CDR 公共运行库。

### 容器和交叉架构部署

容器中的用户空间架构必须与 wheel 匹配。x86_64 主机上的普通 `linux/amd64` 容器安装 x86_64 wheel；ARM 设备上的 `linux/arm64` 容器安装 aarch64 wheel。通过 QEMU 运行 `linux/arm64` 容器可用于构建或验证，但不代表 x86_64 进程可以直接加载 aarch64 动态库。

RoboKit 运行时会再次校验架构、CPython ABI 和 Ubuntu 版本。缺少对应 native binding 时会明确报出 `_native/<arch>/<cp3xx>/` 路径，避免静默回退到其他 ABI。

## 快速检查

```bash
uv run python - <<'PY'
import json
import robokit
from robokit import version

print(robokit.__version__)
print(json.dumps(version.build_info(), ensure_ascii=False, indent=2))
PY
```

`robokit.version.build_info()` 会返回 wheel 或 SDK 工作区的构建审计信息，包括包版本、git commit、目标架构、Python ABI、platform tag，以及 `robokit/proto`、`robokit/registry` 等子模块提交信息。

## 配置

wheel 安装不会写入配置。首次使用 DDS 时，RoboKit 会在最近 `pyproject.toml` 所在目录的 `config/robokit.json` 创建默认配置；没有 `pyproject.toml` 时使用当前目录。可主动运行 `uv run robokit devtools --conf` 直接打开并编辑该文件。部署环境可用 `ROBOKIT_CONFIG` 或 `system.init("app", config_path=...)` 指定已有配置文件。

同一主机上若 C++ RoboKit 进程以 `root` 启动，Fast DDS 创建的共享内存对象只能由 root 访问。此时以普通用户运行的 Python 程序可能发现订阅端点但收不到数据；SDK 会输出告警。临时处理可用 `sudo` 启动 Python 程序，更推荐让所有 DDS 进程使用同一用户；也可显式设置 `FASTDDS_BUILTIN_TRANSPORTS=UDPv4` 改用 UDP。

## 示例

SDK wheel 包含可运行示例，可通过统一入口查看：

```bash
uv run robokit examples --help
```

DDS、service、transform、datahub 和消息推流示例需要对应 RoboKit 服务已启动。

StatusManager 的查询和推流使用 TCP 19204；`robokit.api.http` 是 universalPost 网关。

```python
from robokit.api import tcp

with tcp.GetChannelsDataClient("127.0.0.1", 19204) as client:
    response = client.get((
        tcp.ChannelSubscription("/BatteryInfo/Battery-000", "rbk4.protocol.MessageV4_Battery"),
    ))

with tcp.MessageStreamClient("127.0.0.1", 19204) as stream:
    stream.start((
        tcp.StreamSubscription("/BatteryInfo/Battery-000", "rbk4.protocol.MessageV4_Battery"),
    ))
    message = stream.receive()
```

推流支持 protobuf 和 JSON；服务端状态帧由 `message.is_status` 标识。

```bash
uv run robokit examples --get-channels-data --auto
uv run robokit examples --message-stream
uv run robokit examples --tcp robot-info
```

## 运行期 TUI 工具

配置编辑器和监视器统一通过 devtools 入口启动，source、SDK 工作区和 wheel 均可使用：

```bash
uv run robokit devtools --conf
uv run robokit devtools --monitor
```

### rbkconf

`--conf` 直接编辑应用根目录的 `config/robokit.json`，进入后只有参数编辑页面。

### rbkmonitor

`--monitor` 打开 `rbkmonitor` 监视器。

三个主页面分别提供：

- **Process**：显示进程名、master 和 Running/Stopped 状态；按 Enter 通过 service dispatcher 切换选中进程的启停状态
- **Node**：显示进程节点及主机信息；详情页列出插件、service server、发布话题和订阅话题
- **Channel**：显示话题名和 protobuf 类型；详情页列出 publisher/subscriber，监听页显示频率、延迟、消息大小和字段树

表格和帮助页均支持鼠标选择、滚轮滚动。Process 启停依赖目标 master 的 service dispatcher；Node、Channel 和消息监听依赖 DDS topology 与对应 publisher。若 C++ RoboKit 进程以 root 运行，monitor 也应使用同一用户启动，或设置 `FASTDDS_BUILTIN_TRANSPORTS=UDPv4`，否则可能发现端点但收不到共享内存数据。

## SDK 工作区与自定义构建

`seer-robokit-py-sdk-<version>.tar.gz` 是用于开发和验证的源码工作区，不带架构后缀。它包含 `robokit/proto/`、`robokit/registry/`、测试、示例、构建配置、两种架构的 native runtime，以及 `build_support/proto/bin/` 中的 x86_64 `protoc`。已安装的 wheel 只包含运行期生成物，不能在 wheel 内修改 proto 或 registry 后重新生成。

### 准备工作区

在 SDK 根目录执行：

```bash
uv sync
uv run robokit devtools --help
```

proto 和 registry 模块支持自定义：

- proto 描述业务消息结构，源文件在 `robokit/proto/*.proto`，生成产物在 `robokit/protocol/`。
- registry 描述 channel、resource、service 和 topology 的命名常量，源文件在 `robokit/registry/src/*.json`，生成产物在 `robokit/registry/*.py` 和 `registry_namespace_index.md`。

生成产物用于工作区验证和运行期导入，不应手工编辑。

### 修改并生成 Proto

修改或新增 `robokit/proto/*.proto` 后执行：

```bash
uv run robokit devtools --compile-proto
```

该命令使用 `build_support/proto/bin/protoc` 重建 `robokit/protocol/` 下的 Python protobuf 模块。

### 修改并生成 Registry

registry 源文件位于 `robokit/registry/src/`：

- `channel.json`
- `resource.json`
- `service.json`
- `topology.json`

修改后执行：

```bash
uv run robokit devtools --generate-registry
uv run robokit devtools --generate-registry --check
```

生成命令使用 `robokit/registry/registrygen.py` 更新 `robokit/registry/channel.py`、`resource.py`、`service.py`、`topology.py` 和 `registry_namespace_index.md`。Python registry 模块供运行期导入。

### 验证自定义改动

proto 或 registry 生成后，应运行与改动对应的测试、示例或导入检查。默认单元测试入口：

```bash
uv run robokit devtools --test
```

### 构建 wheel

构建前确认 `pyproject.toml` 的 `[project].version` 已更新为新的 PEP 440 版本。不要在内容变化后复用同一个版本和同名 wheel，否则 uv 的锁文件哈希可能仍指向旧产物。

从 SDK 根目录构建目标架构的 wheel：

```bash
uv build --wheel -C target-arch=x86_64 -C target-python=cp310
uv build --wheel -C target-arch=x86_64 -C target-python=cp311
uv build --wheel -C target-arch=x86_64 -C target-python=cp312
uv build --wheel -C target-arch=aarch64 -C target-python=cp310
uv build --wheel -C target-arch=aarch64 -C target-python=cp311
uv build --wheel -C target-arch=aarch64 -C target-python=cp312
```

wheel 写入 `dist/`，文件名同时包含 Python ABI 和 CPU 架构：

```text
seer_robokit_py-<version>-<python-tag>-<python-tag>-manylinux_2_35_<architecture>.whl
```

其中 `<python-tag>` 为 `cp310`、`cp311` 或 `cp312`，`<architecture>` 为 `x86_64` 或 `aarch64`；完整发布矩阵应包含六个文件。

构建会从 `robokit/proto/` 和 `robokit/registry/src/` 重新生成 wheel 内的协议和 registry；因此构建前不要求提交生成产物，但前面的生成步骤仍用于在 SDK 工作区测试改动。

### 安装和验证 wheel

在使用该 wheel 的应用根目录，选择同时匹配目标 Python 和 CPU 架构的文件。以下以 CPython 3.11、x86_64 为例：

```bash
uv venv --python 3.11
uv pip install --python .venv/bin/python --reinstall \
  /absolute/path/to/seer_robokit_py-<version>-cp311-cp311-manylinux_2_35_x86_64.whl
uv run --python .venv/bin/python python - <<'PY'
import platform

import robokit
from robokit import version
from robokit.registry import channel, resource, service

print(platform.machine())
print(robokit.__version__)
print(version.build_info())
print(channel.__name__, resource.__name__, service.__name__)
PY
```

验证其他组合时，Python 3.10/3.11/3.12 必须分别使用 cp310/cp311/cp312 wheel；在 aarch64 设备上必须使用 `manylinux_2_35_aarch64` wheel。`version.build_info()` 中的 `architecture` 和 `python_tag` 应与当前机器和解释器一致。

应用已通过 `uv add` 声明此 wheel 时，替换同一路径文件后还应运行：

```bash
uv lock --upgrade-package seer-robokit-py
uv sync --reinstall-package seer-robokit-py
```

这会更新锁文件记录的 wheel 哈希并重新安装，避免复用旧文件。

## 更多文档

- 示例说明见 [examples/README.md](examples/README.md)。
- 源码维护、SDK 归档和 wheel 构建细节见 [devtools/README.md](devtools/README.md)。
- registry 数据定义见 [robokit/registry/README.md](robokit/registry/README.md)。
