diff --git a/apps/iot/src/http_api_handler.erl b/apps/iot/src/handler/http_api_handler.erl similarity index 100% rename from apps/iot/src/http_api_handler.erl rename to apps/iot/src/handler/http_api_handler.erl diff --git a/apps/iot/src/http_host_handler.erl b/apps/iot/src/handler/http_host_handler.erl similarity index 100% rename from apps/iot/src/http_host_handler.erl rename to apps/iot/src/handler/http_host_handler.erl diff --git a/apps/iot/src/http_router_handler.erl b/apps/iot/src/handler/http_router_handler.erl similarity index 100% rename from apps/iot/src/http_router_handler.erl rename to apps/iot/src/handler/http_router_handler.erl diff --git a/apps/iot/src/iot_message_handler.erl b/apps/iot/src/handler/iot_message_handler.erl similarity index 100% rename from apps/iot/src/iot_message_handler.erl rename to apps/iot/src/handler/iot_message_handler.erl diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index 1a118a2..94801c6 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -35,8 +35,9 @@ start_http_server() -> Dispatcher = cowboy_router:compile([ {'_', [ - {"/host/[...]", http_protocol, [http_host_handler]} - ]} + {"/host/[...]", http_protocol, [http_host_handler]}, + {"/router/[...]", http_protocol, [http_router_handler]} + ]} ]), TransOpts = [ {port, Port}, diff --git a/apps/iot/src/iot_mnesia.erl b/apps/iot/src/iot_mnesia.erl index 5e88006..928b899 100644 --- a/apps/iot/src/iot_mnesia.erl +++ b/apps/iot/src/iot_mnesia.erl @@ -56,6 +56,14 @@ init_database() -> {type, ordered_set} ]), + %% 转发规则表 + mnesia:create_table(router, [ + {attributes, record_info(fields, router)}, + {record_name, router}, + {disc_copies, [node()]}, + {type, ordered_set} + ]), + ok. %% 加入集群 diff --git a/apps/iot/src/iot_mock.erl b/apps/iot/src/iot_mock.erl index 738b2f4..afa7708 100644 --- a/apps/iot/src/iot_mock.erl +++ b/apps/iot/src/iot_mock.erl @@ -11,7 +11,7 @@ -include("iot.hrl"). %% API --export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/1]). +-export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/0]). -export([rsa_encode/1]). insert_hosts() -> @@ -53,8 +53,6 @@ insert_services(HostId) -> insert_terminals(HostId) -> lists:foreach(fun(Id0) -> - Q0 = queue:new(), - Q = queue:in({1234, 21}, Q0), Terminal = #terminal{ terminal_id = integer_to_binary(Id0), host_id = HostId, @@ -71,14 +69,14 @@ insert_terminals(HostId) -> terminal_model:add_terminal(Terminal) end, lists:seq(1, 100)). -insert_routers(HostId) -> +insert_routers() -> lists:foreach(fun(Id0) -> R = #router{ router_id = Id0, name = <<"计费电表"/utf8>>, rule = <<"测试规则"/utf8>>, endpoint = #http_endpoint{url = <<"http://127.0.0.1:8080/data">>}, - status = 0 + status = 1 }, router_model:add_router(R) end, lists:seq(1, 100)). diff --git a/apps/iot/src/model/router_model.erl b/apps/iot/src/model/router_model.erl index eb9be11..bc6f190 100644 --- a/apps/iot/src/model/router_model.erl +++ b/apps/iot/src/model/router_model.erl @@ -12,7 +12,7 @@ -include_lib("stdlib/include/qlc.hrl"). %% API --export([get_router/1, get_routers/3, add_router/1, change_status/2, delete/1, table_size/0]). +-export([get_router/1, get_routers/3, add_router/1, change_status/2, delete/1, table_size/0, get_all_routers/0, get_all_valid_routers/0]). -export([to_map/1]). get_router(RouterId) when is_integer(RouterId) -> @@ -23,6 +23,34 @@ get_router(RouterId) when is_integer(RouterId) -> undefined end. +%% 获取app信息 +-spec get_all_routers() -> {ok, Items :: list()} | {error, Reason :: any()}. +get_all_routers() -> + Fun = fun() -> + Q = qlc:q([E || E <- mnesia:table(router)]), + qlc:e(Q) + end, + case mnesia:transaction(Fun) of + {atomic, Routers} -> + {ok, Routers}; + {aborted, _} -> + error + end. + +%% 获取app信息 +-spec get_all_valid_routers() -> {ok, Items :: list()} | {error, Reason :: any()}. +get_all_valid_routers() -> + Fun = fun() -> + Q = qlc:q([E || E <- mnesia:table(router), E#router.status =:= 1]), + qlc:e(Q) + end, + case mnesia:transaction(Fun) of + {atomic, Routers} -> + {ok, Routers}; + {aborted, _} -> + error + end. + %% 获取app信息 -spec get_routers(Filter :: any(), Start :: integer(), Limit :: integer()) -> {ok, Items :: list(), TotalNum :: integer()} |