plc_control/run.md

64 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 怎么运行 operation-system
## 1. 准备数据库
应用启动**不会自动执行迁移**`db.rs:13` 注释:「如有迁移请手动执行」),第一次启动前要把 `migrations/` 目录里的 SQL 跑到 Postgres 里:
```bash
# 任选一种
sqlx migrate run --source migrations
# 或用 psql / DataGrip 直接执行 migrations/*.sql 按文件名升序
```
仓库根目录已有 `.env`,指向远程 `postgresql://postgres:zcDsj%402024@10.0.11.51:5432/gateway`。要本地跑就在 `.env` 里改 `DATABASE_URL`
## 2. 设置环境变量
`.env` 会被 `bootstrap::init_process` 通过 `dotenv` 自动加载,所以直接改 `.env` 或在 shell 里 export 都行。
| 变量 | 默认 | 说明 |
| --- | --- | --- |
| `DATABASE_URL` | **必填** | Postgres 连接串 |
| `OPS_SERVER_HOST` | `127.0.0.1` | 监听 host |
| `OPS_SERVER_PORT` | `3100` | 监听端口 |
| `RUST_LOG` | — | 建议 `info` |
| `OPS_SEED_TEMPLATES` | 关 | 设为 `1` 启动时自动写入 12 段 + 11 工位骨架 |
| `SIMULATE_PLC` | 关 | 设为 `1` 引擎发命令后自动回写确认信号,无需 PLC 也能跑通段 |
## 3. 启动
```bash
# 开发态
cargo run -p app_operation_system
# 或带种子 + 模拟器,方便首次端到端验证
OPS_SEED_TEMPLATES=1 SIMULATE_PLC=1 cargo run -p app_operation_system
# 打 release exe
cargo build -p app_operation_system --release
# 产物在 target\release\app_operation_system.exe
```
启动日志会打印 `Starting operation-system server at http://127.0.0.1:3100`
## 4. 验证
- 健康检查 — `curl http://127.0.0.1:3100/api/health` → 返回 `operation-system:ok`
- 段总览 — `curl http://127.0.0.1:3100/api/runtime/overview`
- 前端 — 浏览器打开 `http://127.0.0.1:3100/ui/`,左上角两个 tab
- **运行监控** — 段卡片 + WebSocket 实时刷新 + 启停 / 故障确认 / 复位
- **段 / 工位配置** — 工位 CRUD含信号绑定和段 CRUD含步骤 / 联锁 / 资源声明)
## 5. 端到端冒烟(无 PLC
1.`OPS_SEED_TEMPLATES=1 SIMULATE_PLC=1` 启动。
2. 浏览器进 **段 / 工位配置**,挑一个段(例如 `SEG-DRY1-INFEED`),把 `mode` 改成 `auto`
3. 切到 **运行监控**,点该卡片的「启动」。
4. 看着卡片上 state 从 `idle``checking``executing``confirming` → 下一步循环(因为 `SIMULATE_PLC` 在每步派发后会注入确认信号)。
## 注意事项
- 与 feeder 同时跑没问题:进程互斥名是 `PLCControl.OperationSystem`,端口默认 3100feeder 是 60309
- Windows release 用 `windows_subsystem = "windows"`,不出黑窗;要看日志直接打开 `logs/` 下的 `app.log*`,或者从命令行跑 debug build。
- OPC UA 数据源在 `source` 表里配,启动时 `connect_all_enabled_sources` 会自动连。没配数据源不影响 UI / 段配置使用。