diff --git a/HTTP_API_README.md b/HTTP_API_README.md index 627a637..0b71f10 100644 --- a/HTTP_API_README.md +++ b/HTTP_API_README.md @@ -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 用于测试指定协议(HTTP / MQTT / Kafka)配置是否可用** diff --git a/apps/iot/src/http_handlers/endpoint_handler.erl b/apps/iot/src/http_handlers/endpoint_handler.erl index 5be9f1c..b390b62 100644 --- a/apps/iot/src/http_handlers/endpoint_handler.erl +++ b/apps/iot/src/http_handlers/endpoint_handler.erl @@ -191,6 +191,20 @@ handle_request("POST", "/endpoint/test", _, #{<<"protocol">> := <<"kafka">>, <<" {ok, 200, iot_util:json_error(-1, Errors)} 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, _, _) -> Path1 = list_to_binary(Path), {ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}. \ No newline at end of file