# RMDC MQTT Topic Registry

## Topic Naming Convention

```
wdd/RDMC/{type}/{direction}[/{project_id}]
```

| Segment | Values | Description |
|---------|--------|-------------|
| type | `command`, `message` | 指令类 / 数据类 |
| direction | `up`, `down` | 上行(边缘→平台) / 下行(平台→边缘) |
| project_id | namespace_xxx | 项目标识（下行必需） |

## Registered Topics

### Uplink (Edge → Platform)

| Topic | Publisher | Subscriber | Purpose |
|-------|-----------|------------|---------|
| `wdd/RDMC/command/up` | watchdog | exchange-hub | 边缘发送指令响应 |
| `wdd/RDMC/message/up` | watchdog | exchange-hub | 边缘发送数据/状态 |

### Downlink (Platform → Edge)

| Topic Pattern | Publisher | Subscriber | Purpose |
|---------------|-----------|------------|---------|
| `wdd/RDMC/command/down/{project_id}` | exchange-hub | watchdog | 下发操作指令 |
| `wdd/RDMC/message/down/{project_id}` | exchange-hub | watchdog | 下发配置/数据 |

## Message Payload Schema

```json
{
    "message_id": "uuid-v4",
    "type": "command|message",
    "project_id": "namespace_xxx",
    "command_type": "k8s_exec|host_exec|register|heartbeat|...",
    "timestamp": 1704501234567,
    "version": "1.0",
    "signature": "hmac-sha256-hex",
    "payload": {
        // command-specific data
    }
}
```

## Command Types

| command_type | Direction | Description |
|--------------|-----------|-------------|
| `register` | up | Watchdog 注册请求 |
| `heartbeat` | up/down | 心跳保活 |
| `k8s_exec` | down | K8S 操作指令 |
| `host_exec` | down | 主机操作指令 |
| `result` | up | 指令执行结果 |
| `log_stream` | up | 日志流数据 |
| `metrics` | up | 监控指标数据 |

## QoS Requirements

| Message Type | QoS Level | Reason |
|--------------|-----------|--------|
| Command (指令) | 1 | 至少一次，确保送达 |
| Result (结果) | 1 | 至少一次，确保响应 |
| Heartbeat | 0 | 最多一次，允许丢失 |
| Metrics | 0 | 最多一次，允许丢失 |
| Log stream | 0 | 最多一次，高频数据 |

## Adding New Topic

1. Update this file with new topic definition
2. Register in `exchange-hub/internal/mqtt/subscriber.go`
3. Add handler in `exchange-hub/internal/handler/mqtt_handler.go`
4. Update watchdog subscription if needed
