Skip to content

API 接口文档

PowerControlClient 提供了 HTTP、TCP 和 UDP 三种接口。

HTTP API

基础 URL: http://<ip>:42000

1. 健康检查

用于检测服务是否存活。

  • URL: /health
  • Method: GET
  • Response:
    json
    {
      "status": "success",
      "time": "2024/05/20 10:00:00"
    }

2. Web 控制台

提供图形化的操作界面。

  • URL: /control (注意小写)
  • Method: GET
  • 说明: 直接在浏览器中访问,返回 HTML 页面。

3. 执行电源命令

发送关机或重启指令。

  • URL: /
  • Method: POST
  • Content-Type: application/json
  • Body:
    json
    {
      "cmd": "poweroff",          // 命令: "poweroff" 或 "reboot"
      "mac": "aa:bb:cc:dd:ee:ff"  // 目标MAC地址,或 "all"
    }

示例 (curl):

bash
# 关机
curl -X POST http://localhost:42000/ \
  -H "Content-Type: application/json" \
  -d '{"cmd":"poweroff","mac":"all"}'

TCP 接口

连接到端口 42001,建立连接后直接发送 JSON 字符串。

请求格式:

json
{"cmd":"reboot","mac":"aa:bb:cc:dd:ee:ff"}

响应格式:

json
{"status":"success","message":"Executing reboot command"}

json
{"status":"error","message":"MAC address does not match"}

UDP 设备发现

向端口 42002 发送广播包,用于发现局域网内的设备。

请求 (广播):

json
{
  "type": "discovery",
  "action": "ping",
  "server_id": "PowerControlServer"
}

响应 (来自客户端):

json
{
  "type": "discovery",
  "action": "pong",
  "mac": "00:11:22:33:44:55",
  "ip": "192.168.1.100",      // 可能包含 IP 信息
  "response_time": 1734267001
}

错误码说明

状态 (status)消息 (message)说明
errorMethod not allowedHTTP 方法错误(如用 GET 访问 POST 接口)
errorInvalid JSON format请求体不是有效的 JSON
errorMAC address does not match请求中的 MAC 地址与本机不匹配
errorUnknown command不支持的命令(仅支持 poweroff/reboot)
skippedCommand skipped命令被跳过(通常也是因为 MAC 不匹配)

Released under the MIT License.