智慧校园物联网管理系统 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
验证 API 服务是否正常运行,返回服务状态和版本信息。
{
"status": "ok",
"service": "智慧校园物联网API",
"version": "2.0",
"time": "2026-04-12 10:00:00"
}
获取系统仪表板的核心数据,包括告警统计、设备状态、待处理任务等。
| 参数 | 类型 | 说明 |
|---|---|---|
| action | string | 固定值: 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
}
}
接收物联网设备发送的告警信息,是最常用的接口。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:发送告警的设备唯一标识 |
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()
获取告警记录列表,支持多种过滤条件。
| 参数 | 类型 | 说明 |
|---|---|---|
| action | string | 固定值: alerts |
| level | string | 告警级别:low / medium / high / critical |
| severity | int | 严重程度:1-4 |
| status | int | 状态:0(未处理) / 1(已处理) |
| location | string | 按位置模糊搜索 |
{
"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
}
将指定告警标记为已处理状态。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| 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": "已标记为已处理"
}
获取已注册的物联网设备列表及其在线状态。
{
"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
}
获取系统核心统计数据。
{
"success": true,
"stats": {
"total_alerts": 48,
"today_alerts": 16,
"pending_alerts": 3,
"high_risk": 2,
"online_devices": 48
}
}
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', '检测到人员通行')
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));
# 健康检查
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 | 说明 |
|---|---|---|
| 200 | true | 请求成功 |
| 400 | false | 参数错误或缺少必填参数 |
| 404 | false | 未知的 action 或资源不存在 |
| 405 | false | 请求方法不支持(如 POST 接口用了 GET) |
| 500 | false | 服务器内部错误 |
{
"success": false,
"message": "错误描述",
"error": "详细错误信息(仅500时)"
}