API 接口文档

智慧校园物联网管理系统 RESTful API v2.0 — 完整的设备接入、告警管理和数据查询接口

📌 基础信息

API 地址http://106.52.242.83/api.php
数据格式JSON
字符编码UTF-8
鉴权方式Header: Content-Type: application/json

快速测试 — 在浏览器直接访问:
http://106.52.242.83/api.php?action=health

GET ?action=health 健康检查

验证 API 服务是否正常运行,返回服务状态和版本信息。

响应示例
{
  "status": "ok",
  "service": "智慧校园物联网API",
  "version": "2.0",
  "time": "2026-04-12 10:00:00"
}
GET ?action=dashboard 仪表板数据

获取系统仪表板的核心数据,包括告警统计、设备状态、待处理任务等。

参数类型说明
actionstring固定值: dashboard
响应示例
{
  "status": "success",
  "data": {
    "stats": {
      "total_alerts": 48,
      "today_alerts": 16,
      "pending_alerts": 3,
      "high_risk": 2,
      "online_devices": 48
    },
    "recent_alerts": [...],
    "users": [...],
    "system_status": {
      "database": "online",
      "storage": "normal",
      "memory": "normal"
    },
    "trigger_alert": false,
    "alert_data": null
  }
}
POST ?action=ingest 接收设备告警

接收物联网设备发送的告警信息,是最常用的接口。MicroPython 设备可直接调用。

参数类型必填说明
event_type string 事件类型:如 parking_access, door_open, light_on
location string 位置描述:如 停车场入口, 教学楼301
details string 详细信息:告警的具体描述或附加数据
severity int 严重程度:1(低) / 2(中) / 3(高) / 4(紧急),默认 1
device_id string 设备ID:发送告警的设备唯一标识
请求示例 (JavaScript)
fetch('http://106.52.242.83/api.php?action=ingest', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        event_type: 'parking_access',
        location: '停车场入口',
        details: '检测到车辆驶入',
        severity: 2
    })
}).then(r => r.json())
  .then(console.log);
响应示例
{
  "success": true,
  "message": "告警已接收",
  "id": 123,
  "alert_id": "ALT_20260412100001"
}

MicroPython 示例

from mpython import *
import urequests

r = urequests.post(
    'http://106.52.242.83/api.php?action=ingest',
    headers={'Content-Type':'application/json'},
    json={
        'event_type': 'door_open',
        'location': '教学楼正门',
        'details': '检测到人员通行'
    }
)
print(r.json())
r.close()

GET ?action=alerts 查询告警列表

获取告警记录列表,支持多种过滤条件。

参数类型说明
actionstring固定值: alerts
levelstring告警级别:low / medium / high / critical
severityint严重程度:1-4
statusint状态:0(未处理) / 1(已处理)
locationstring按位置模糊搜索
响应示例
{
  "success": true,
  "data": [
    {
      "id": 1,
      "event_type": "parking_access",
      "location": "停车场入口",
      "details": "检测到车辆驶入",
      "severity": 2,
      "status": 0,
      "created_at": "2026-04-12 10:00:00"
    }
  ],
  "count": 1
}
POST ?action=alert_process 标记告警已处理

将指定告警标记为已处理状态。

参数类型必填说明
id int 告警ID
user string 处理人名称,默认 "system"
请求示例
fetch('http://106.52.242.83/api.php?action=alert_process', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ id: 123, user: '管理员' })
}).then(r => r.json())
  .then(console.log);
响应示例
{
  "success": true,
  "message": "已标记为已处理"
}
GET ?action=devices 设备列表

获取已注册的物联网设备列表及其在线状态。

响应示例
{
  "success": true,
  "data": [
    {
      "id": 1,
      "device_id": "DEV_001",
      "device_name": "停车场摄像头A",
      "device_type": "camera",
      "location": "停车场入口",
      "status": 1,
      "last_seen": "2026-04-12 10:00:00"
    }
  ],
  "count": 1
}
GET ?action=stats 统计数据

获取系统核心统计数据。

响应示例
{
  "success": true,
  "stats": {
    "total_alerts": 48,
    "today_alerts": 16,
    "pending_alerts": 3,
    "high_risk": 2,
    "online_devices": 48
  }
}

快速开始

1. 设备发送告警 (MicroPython)

from mpython import *
import urequests
import json

def send_alert(event_type, location, details='', severity=1):
    url = 'http://106.52.242.83/api.php?action=ingest'
    data = {
        'event_type': event_type,
        'location': location,
        'details': details,
        'severity': severity
    }
    try:
        r = urequests.post(url, 
            headers={'Content-Type': 'application/json'},
            json=data)
        print('发送成功:', r.json())
        r.close()
    except Exception as e:
        print('发送失败:', e)

# 使用示例
send_alert('door_open', '教学楼301', '检测到人员通行')

2. JavaScript 客户端

class SmartCampus {
    constructor(apiUrl = 'http://106.52.242.83/api.php') {
        this.apiUrl = apiUrl;
    }
    
    async sendAlert(data) {
        const res = await fetch(`${this.apiUrl}?action=ingest`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        return res.json();
    }
    
    async getAlerts(filters = {}) {
        const params = new URLSearchParams({ action: 'alerts', ...filters });
        const res = await fetch(`${this.apiUrl}?${params}`);
        return res.json();
    }
    
    async getDashboard() {
        const res = await fetch(`${this.apiUrl}?action=dashboard`);
        return res.json();
    }
}

const api = new SmartCampus();
// 发送告警
api.sendAlert({ event_type: 'fire', location: '实验室', severity: 4 });
// 获取仪表板
api.getDashboard().then(d => console.log(d));

3. cURL 命令行测试

# 健康检查
curl http://106.52.242.83/api.php?action=health

# 发送告警
curl -X POST http://106.52.242.83/api.php?action=ingest \
  -H "Content-Type: application/json" \
  -d '{"event_type":"test","location":"测试位置","severity":2}'

# 查询告警列表
curl "http://106.52.242.83/api.php?action=alerts&status=0"

# 获取仪表板
curl http://106.52.242.83/api.php?action=dashboard

⚠️ 错误响应

HTTP状态码success说明
200true请求成功
400false参数错误或缺少必填参数
404false未知的 action 或资源不存在
405false请求方法不支持(如 POST 接口用了 GET)
500false服务器内部错误
{
  "success": false,
  "message": "错误描述",
  "error": "详细错误信息(仅500时)"
}