跳转至

快速开始

环境要求

组件 版本要求
Python ≥ 3.11
操作系统 Windows / Linux / macOS

安装与运行

1. 克隆代码

git clone <repo-url> /opt/janpnp/web-server
cd /opt/janpnp/web-server/api

2. 创建虚拟环境并安装依赖

python3 -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install -r requirements.txt

3. 启动服务

python server.py

服务默认监听:

  • WebSocket: ws://0.0.0.0:8765
  • HTTP API: http://0.0.0.0:8766

4. 验证服务

curl http://127.0.0.1:8766/api/v1/health

预期响应:

{
  "status": "ok",
  "service": "janpnp-cloud",
  "version": "2.0.0",
  "device_count": 0,
  "uptime": 42
}

使用模拟设备

项目提供了模拟设备客户端,用于开发和测试:

# 在另一个终端中运行
python tools/mock_device.py --device-id test-001

模拟设备会自动连接到 Cloud API,完成注册并模拟心跳和状态上报。

登录并获取令牌

curl -X POST http://127.0.0.1:8766/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username": "admin", "password": "janpnp"}'

响应示例:

{
  "success": true,
  "token": "eyJhbGciOiJIUzI1NiIs...",
  "expires_in": 259200,
  "user": {
    "id": "user_admin",
    "username": "admin",
    "display_name": "系统管理员",
    "role": "admin"
  }
}

查看在线设备

curl http://127.0.0.1:8766/api/v1/devices \
  -H "Authorization: Bearer <token>"

下一步