完善文档信息

This commit is contained in:
anlicheng 2025-11-07 15:32:30 +08:00
parent 73c31a6995
commit 00eb9dd585
2 changed files with 99 additions and 76 deletions

View File

@ -61,12 +61,11 @@ reload(Pid, NEndpoint = #endpoint{}) when is_pid(Pid) ->
clean_up(Pid) when is_pid(Pid) -> clean_up(Pid) when is_pid(Pid) ->
gen_server:call(Pid, clean_up, 5000). gen_server:call(Pid, clean_up, 5000).
-spec endpoint_record(EndpointInfo :: #{}) -> error | {ok, Endpoint :: #endpoint{}}. -spec endpoint_record(EndpointInfo :: #{}) -> error | {ok, Endpoint :: #endpoint{}}.
endpoint_record(#{<<"id">> := Id, <<"name">> := Name, <<"title">> := Title, <<"type">> := Type, <<"config_json">> := ConfigJson, endpoint_record(#{<<"id">> := Id, <<"name">> := Name, <<"title">> := Title, <<"type">> := Type, <<"config_json">> := ConfigJson,
<<"status">> := Status, <<"updated_at">> := UpdatedAt, <<"created_at">> := CreatedAt}) -> <<"status">> := Status, <<"updated_at">> := UpdatedAt, <<"created_at">> := CreatedAt}) ->
try try
Config = parse_config(Type, catch jiffy:decode(ConfigJson, [return_maps])), Config = parse_config(Type, ConfigJson),
{ok, #endpoint { {ok, #endpoint {
id = Id, id = Id,
name = Name, name = Name,

View File

@ -37,11 +37,11 @@ GET /get_all_hosts
**返回示例:** **返回示例:**
```json ```json
{"result": {"result":
[ [
"uuid-1", "uuid-1",
"uuid-2", "uuid-2",
"uuid-3" "uuid-3"
] ]
} }
```` ````
--- ---
@ -60,7 +60,7 @@ GET /get_host_by_uuid?uuid=<uuid>
| ---- | ------ | -- | ------- | | ---- | ------ | -- | ------- |
| uuid | string | ✅ | 主机 UUID | | uuid | string | ✅ | 主机 UUID |
**返回示例:** **返回示例:(包含host的全部字段)**
```json ```json
{ {
@ -69,6 +69,7 @@ GET /get_host_by_uuid?uuid=<uuid>
"uuid": "uuid-1", "uuid": "uuid-1",
"name": "HostA", "name": "HostA",
"status": 1, "status": 1,
"authorize_status": 1,
"created_at": "2024-01-01T00:00:00Z" "created_at": "2024-01-01T00:00:00Z"
} }
} }
@ -90,14 +91,15 @@ GET /get_host_by_id?host_id=<id>
| ------- | ------- | -- | ----- | | ------- | ------- | -- | ----- |
| host_id | integer | ✅ | 主机 ID | | host_id | integer | ✅ | 主机 ID |
**返回示例:** **返回示例:(包含host的全部字段)**
```json ```json
{ {
"result": { "result": {
"id": 1, "id": 1,
"uuid": "uuid-1", "uuid": "uuid-1",
"name": "HostA" "name": "HostA",
"authorize_status": 1
} }
} }
``` ```
@ -125,7 +127,7 @@ POST /change_host_status
```json ```json
{ {
"result": 1 "result": "ok"
} }
``` ```
@ -145,7 +147,7 @@ GET /get_host_devices?host_id=<id>
| ------- | ------- | -- | ----- | | ------- | ------- | -- | ----- |
| host_id | integer | ✅ | 主机 ID | | host_id | integer | ✅ | 主机 ID |
**返回示例:** **返回示例:(包含device的全部字段)**
```json ```json
{ {
@ -174,7 +176,7 @@ GET /get_device_by_uuid?device_uuid=<uuid>
| ----------- | ------ | -- | ------- | | ----------- | ------ | -- | ------- |
| device_uuid | string | ✅ | 设备 UUID | | device_uuid | string | ✅ | 设备 UUID |
**返回示例:** **返回示例:(包含device的全部字段)**
```json ```json
{ {
@ -209,7 +211,7 @@ POST /change_device_status
```json ```json
{ {
"result": 1 "result": "ok"
} }
``` ```
@ -234,12 +236,78 @@ GET /get_all_endpoints
"result": [ "result": [
{ {
"id": 1, "id": 1,
"name": "mqtt_endpoint", "name": "mqtt_endpoint_1",
"title": "MQTT 接入节点",
"type": "mqtt", "type": "mqtt",
"status": 1 "status": 1,
"creator": 1,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-05-01T12:00:00Z",
"config": {
"host": "mqtt.broker.local",
"port": 1883,
"client_id": "iot-client-1",
"username": "iot_user",
"password": "123456",
"topic": "iot/device/data",
"qos": 1
}
},
{
"id": 2,
"name": "http_endpoint_1",
"title": "HTTP 推送接口",
"type": "http",
"status": 1,
"creator": 2,
"created_at": "2024-02-15T10:00:00Z",
"updated_at": "2024-04-01T09:30:00Z",
"config": {
"url": "https://webhook.example.com/data",
"pool_size": 10
}
},
{
"id": 3,
"name": "kafka_endpoint_auth",
"title": "Kafka 接入(认证)",
"type": "kafka",
"status": 1,
"creator": 3,
"created_at": "2024-03-10T08:00:00Z",
"updated_at": "2024-06-20T09:00:00Z",
"config": {
"bootstrap_servers": [
"kafka1:9092",
"kafka2:9092"
],
"topic": "iot_topic",
"sasl_config": {
"username": "user_a",
"password": "p@ssw0rd",
"mechanism": "sha_256"
}
}
},
{
"id": 4,
"name": "kafka_endpoint_noauth",
"title": "Kafka 接入(无认证)",
"type": "kafka",
"status": 1,
"creator": 3,
"created_at": "2024-03-15T09:30:00Z",
"updated_at": "2024-06-25T10:15:00Z",
"config": {
"bootstrap_servers": [
"kafka1:9092"
],
"topic": "iot_noauth_topic"
}
} }
] ]
} }
``` ```
--- ---
@ -258,7 +326,7 @@ GET /get_endpoint?id=<id>
| --- | ------- | -- | ----------- | | --- | ------- | -- | ----------- |
| id | integer | ✅ | Endpoint ID | | id | integer | ✅ | Endpoint ID |
**返回示例:** **返回示例:()**
```json ```json
{ {
@ -267,46 +335,24 @@ GET /get_endpoint?id=<id>
"name": "mqtt_endpoint", "name": "mqtt_endpoint",
"title": "MQTT接口", "title": "MQTT接口",
"type": "mqtt", "type": "mqtt",
"config_json": "{\"host\":\"mqtt.example.com\",\"port\":1883}" "config_json": {
"id": 2,
"name": "http_endpoint_1",
"title": "HTTP 推送接口",
"type": "http",
"status": 1,
"creator": 2,
"created_at": "2024-02-15T10:00:00Z",
"updated_at": "2024-04-01T09:30:00Z",
"config": {
"url": "https://webhook.example.com/data",
"pool_size": 10
}
}
} }
} }
``` ```
---
## 🤖 AI 事件接口
### 10. 触发 AI 事件
**接口:**
```
POST {api_url}
```
**请求体:**
```json
{
"id": 123,
"token": "<md5(API_TOKEN + id + API_TOKEN)>"
}
```
**返回:**
* 成功HTTP 200
* 失败:返回错误日志(`lager` 记录)
**说明:**
`token` 通过以下 Erlang 表达式生成:
```erlang
iot_util:md5(<<API_TOKEN, integer_to_binary(Id), API_TOKEN>>)
```
---
## 🧱 Endpoint 配置结构说明 ## 🧱 Endpoint 配置结构说明
根据 `type` 不同,`config_json` 结构如下: 根据 `type` 不同,`config_json` 结构如下:
@ -370,26 +416,4 @@ iot_util:md5(<<API_TOKEN, integer_to_binary(Id), API_TOKEN>>)
"message": "Invalid parameter" "message": "Invalid parameter"
} }
} }
``` ```
---
## 📄 附录:内部函数说明(开发者参考)
| 函数名 | 作用 | 说明 |
| ------------------- | ----------------------------- | ------------------------------------- |
| `do_get/2` | 执行 HTTP GET 请求 | 返回 `{ok, Result}``{error, Reason}` |
| `do_post/2` | 执行 HTTP POST 请求 | 自动编码 JSON解析返回值 |
| `endpoint_record/1` | 将 JSON 转换为内部 `#endpoint{}` 结构 | 内部使用 |
| `parse_config/2` | 解析不同类型 endpoint 配置 | 支持 mqtt/http/kafka |
---
📅 **最后更新2025-11-07**
```
---
是否希望我帮你把这份 `.md` 文件转成一个 **可浏览的 HTML 文档**(带目录与搜索功能)?
```