add endpoint publish

This commit is contained in:
anlicheng 2025-11-27 17:26:41 +08:00
parent 175e4b6a81
commit dc0211ecc2
2 changed files with 48 additions and 0 deletions

View File

@ -654,6 +654,40 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage)
} }
``` ```
### 5. 向endpoint发送消息
**URL**`/endpoint/publish_metric`
**Method**`POST`
###### **示例请求**
metric字段支持字符串map或者数组其他为非法数据; 如何要发送数字,可以采用数字的字符串格式, 比如: "5"
```json
{
"route_key": "/dhlr/warning",
"metric": {
"xyz": "test"
}
}
```
#### 示例响应
```json
{
"result": "ok"
}
```
#### 错误响应
```json
{
"error": {
"code": 404,
"message": "restart endpoint error"
}
}
```
### **POST /endpoint/test ### **POST /endpoint/test
用于测试指定协议HTTP / MQTT / Kafka配置是否可用** 用于测试指定协议HTTP / MQTT / Kafka配置是否可用**

View File

@ -191,6 +191,20 @@ handle_request("POST", "/endpoint/test", _, #{<<"protocol">> := <<"kafka">>, <<"
{ok, 200, iot_util:json_error(-1, Errors)} {ok, 200, iot_util:json_error(-1, Errors)}
end; end;
%%
handle_request("POST", "/endpoint/publish_metric", _, #{<<"route_key">> := RouteKey, <<"metric">> := Metric0}) when is_binary(RouteKey) ->
if
is_map(Metric0) orelse is_list(Metric0) ->
Metric = jiffy:encode(Metric0, [force_utf8]),
endpoint_subscription:publish(RouteKey, Metric),
{ok, 200, iot_util:json_data(<<"ok">>)};
is_binary(Metric0) ->
endpoint_subscription:publish(RouteKey, Metric0),
{ok, 200, iot_util:json_data(<<"ok">>)};
true ->
{ok, 200, iot_util:json_error(-1, <<"invalid metric">>)}
end;
handle_request(_, Path, _, _) -> handle_request(_, Path, _, _) ->
Path1 = list_to_binary(Path), Path1 = list_to_binary(Path),
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}. {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.