Files
pressureSensor/dataServer/README.md
2025-09-09 15:00:30 +08:00

89 lines
2.0 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.

# 压力数据接收服务器Flask + SQLite, uv 管理)
本服务用于接收 ESP32 固件上报的压力数据,并提供增删改查接口。
- 语言/框架Python 3.10+、Flask
- 存储SQLite本地文件 `readings.db`
- 依赖管理uvPEP 标准 `pyproject.toml`
## 快速开始Windows PowerShell
1) 安装 uv任选一种
```powershell
irm https://astral.sh/uv/install.ps1 | iex
```
或使用 pipx
```powershell
pipx install uv
```
2) 安装依赖并创建虚拟环境:
```powershell
cd dataServer
uv sync
```
3) 启动服务(两种方式):
- 方式 AFlask 命令(可热重载)
```powershell
$env:FLASK_APP = "app.py"
uv run flask run --host 0.0.0.0 --port 5000
```
- 方式 B直接运行 Python
```powershell
uv run python app.py
```
> 如需我等待你手动启动服务:请直接告诉我,我会等待 30 秒再继续。
## 配置
- 环境变量:
- `DB_PATH`SQLite 文件路径,默认 `./readings.db`
- `HOST`:服务监听地址(用于 `python app.py`),默认 `0.0.0.0`
- `PORT`:服务端口(用于 `python app.py`),默认 `5000`
## 接口说明
- 健康检查:
- `GET /health`
- 接收上报(供 ESP32 固件使用):
- `POST /data`
- JSON 示例:
```bash
curl -X POST http://localhost:5000/data \
-H "Content-Type: application/json" \
-d '{"device_id":"test","timestamp":"2023-01-01T12:00:00Z","pressure":0.1,"voltage":2.5}'
```
- 查询列表:
- `GET /api/readings?device_id=<id>&start=<iso>&end=<iso>&page=1&page_size=50&sort=desc`
- 获取单条:
- `GET /api/readings/<id>`
- 新增:
- `POST /api/readings`(同 `/data` 体)
- 更新:
- `PUT /api/readings/<id>`(可部分字段)
- 删除:
- `DELETE /api/readings/<id>`
## 备注
- 时间戳接受 ISO8601支持 `Z`(将归一化为 `+00:00`)。未提供时间戳则使用服务器当前 UTC 时间。
- SQLite 采用 WAL 模式,适合多读少写。
- 若需持久化到指定目录,请设置 `DB_PATH` 并确保写权限。