diff --git a/apps/iot/src/handler/http_api_handler.erl b/apps/iot/src/handler/http_api_handler.erl index 7af6d71..24c3e96 100644 --- a/apps/iot/src/handler/http_api_handler.erl +++ b/apps/iot/src/handler/http_api_handler.erl @@ -18,5 +18,6 @@ handle_request("GET", "/api/booking", _, _Params) -> {ok, 200, iot_util:json_data(<<"success">>)}; -handle_request("POST", "/api/booking", _, _Params) -> +handle_request("POST", _, _, Params) -> + lager:debug("body is: ~p", [Params]), {ok, 200, iot_util:json_data(<<"success">>)}. \ No newline at end of file diff --git a/apps/iot/src/iot.app.src b/apps/iot/src/iot.app.src index 5df4b41..9d1ffb3 100644 --- a/apps/iot/src/iot.app.src +++ b/apps/iot/src/iot.app.src @@ -13,8 +13,11 @@ parse_trans, hackney, poolboy, + mnesia, crypto, + public_key, + ssl, kernel, stdlib ]}, diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index 94801c6..07414f3 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -36,6 +36,7 @@ start_http_server() -> Dispatcher = cowboy_router:compile([ {'_', [ {"/host/[...]", http_protocol, [http_host_handler]}, + {"/api/[...]", http_protocol, [http_api_handler]}, {"/router/[...]", http_protocol, [http_router_handler]} ]} ]), diff --git a/apps/iot/src/iot_http_client.erl b/apps/iot/src/iot_http_client.erl new file mode 100644 index 0000000..f1f73f6 --- /dev/null +++ b/apps/iot/src/iot_http_client.erl @@ -0,0 +1,40 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 03. 3月 2023 11:48 +%%%------------------------------------------------------------------- +-module(iot_http_client). +-author("licheng5"). + +%% API +-export([post/2]). + +post(Url, Body) when is_list(Url), is_binary(Body) -> + case hackney:request(post, Url, [], Body) of + {ok, 200, _, ClientRef} -> + case hackney:body(ClientRef) of + {ok, RespBody} -> + lager:debug("[iot_http_client] url: ~p, response is: ~p", [Url, RespBody]), + ok; + {error, Reason} -> + lager:warning("[iot_http_client] url: ~p, get error: ~p", [Url, Reason]), + {error, failed} + end; + + {ok, HttpCode, _, ClientRef} -> + case hackney:body(ClientRef) of + {ok, RespBody} -> + lager:debug("[iot_http_client] url: ~p, http_code: ~p, response is: ~p", [Url, HttpCode, RespBody]), + ok; + {error, Reason} -> + lager:warning("[iot_http_client] url: ~p, http_code: ~p, get error: ~p", [Url, HttpCode, Reason]), + {error, failed} + end; + + {error, Reason} -> + lager:warning("[iot_http_client] url: ~p, get error: ~p", [Url, Reason]), + {error, failed} + end. \ No newline at end of file diff --git a/apps/iot/src/iot_mock.erl b/apps/iot/src/iot_mock.erl index afa7708..8eb16e3 100644 --- a/apps/iot/src/iot_mock.erl +++ b/apps/iot/src/iot_mock.erl @@ -12,6 +12,7 @@ %% API -export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/0]). +-export([start_router/1]). -export([rsa_encode/1]). insert_hosts() -> @@ -81,6 +82,16 @@ insert_routers() -> router_model:add_router(R) end, lists:seq(1, 100)). +start_router(Id0) when is_integer(Id0) -> + R = #router{ + router_id = Id0, + name = <<"计费电表"/utf8>>, + rule = <<"测试规则"/utf8>>, + endpoint = #http_endpoint{url = <<"http://127.0.0.1:8080/data">>}, + status = 1 + }, + router_model:add_router(R), + iot_router_sup:start_new_router(R). rsa_encode(Data) when is_binary(Data) -> %% 读取相关配置 diff --git a/apps/iot/src/iot_router_sup.erl b/apps/iot/src/iot_router_sup.erl index e369e8b..3f85710 100644 --- a/apps/iot/src/iot_router_sup.erl +++ b/apps/iot/src/iot_router_sup.erl @@ -47,8 +47,12 @@ init([]) -> SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, %% 启动目前生效的全部转发规则 - {ok, Routers} = router_model:get_all_valid_routers(), - Specs = lists:map(fun generate_router_spec/1, Routers), + Specs = case router_model:get_all_valid_routers() of + {ok, Routers} -> + lists:map(fun generate_router_spec/1, Routers); + error -> + [] + end, {ok, {SupFlags, Specs}}.