2025-11-15 14:36:16 +08:00

58 lines
2.2 KiB
Markdown
Raw Permalink 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.

## ESP32 压力传感器数据采集与回看系统
本项目实现:
- 后端 API接收设备上传数据、查询列表、查询详情、删除、批量导出
- 前端页面:记录列表页、详情页(时序曲线、直方图、散点图)
- 数据库Prisma + PostgreSQL`Recording` 表)
### 环境变量
- `DATABASE_URL`PostgreSQL 连接串(已在 `.env` 中配置)
- `SAMPLE_RATE_HZ`:采样率(默认 `42.67`),用于计算时长
- `ADMIN_TOKEN`:可选,若设置则删除接口需请求头 `x-admin-token: <token>`
### 安装与运行
```pwsh
# 安装依赖
npm install
# 生成 Prisma Client
npx prisma generate
# 初始化数据库(开发库)
# 建议:如果当前数据库已存在其他表,请使用独立 schema例如
# 修改 .env 为 ...?schema=esp32 后再执行迁移
npx prisma migrate dev --name init-recording-table
# 启动开发环境
npm run dev
```
如果数据库为共享库且不便迁移,建议将连接串中的 `schema` 改为自定义(例如 `esp32`),避免与既有表冲突:
```
postgresql://user:pass@host:5432/dbname?schema=esp32
```
如需强制重置(会清空数据),开发环境可用:
```pwsh
npx prisma migrate reset --force
```
### API 速览
- `POST /api/data`:接收 `{"code": number[], "fit"?: {a,b}, "recStartMs"?: number, "recEndMs"?: number }`,最大 4096 点;若提供 `recStartMs/recEndMs`(毫秒),后端将用它们计算 `duration`(秒)
- `GET /api/records`:分页、筛选(日期、是否拟合)、排序(时间/时长/最大值)
- `GET /api/records/:id`:详情,返回完整 `code[]`、统计与拟合参数
- `DELETE /api/records/:id`:删除(若设置 `ADMIN_TOKEN` 则需携带 `x-admin-token`
- `POST /api/export`:批量导出 `ids[]``csv | json`
### 页面
- 列表页 `/`:过滤、分页、卡片视图
- 详情页 `/records/:id`:基本信息、统计、时序曲线(码值/力值双轴)、直方图、线性度散点图、导出 CSV
### 备注
- 数据库存储 `code_data``Int[]`PostgreSQL 数组)
- 时长 `duration = sampleCount / SAMPLE_RATE_HZ`(默认 `42.67`
- 导出 CSV 的时间列使用 `i * (duration / sampleCount)` 计算,避免采样率变更带来的误差