fix handler

This commit is contained in:
安礼成 2023-03-01 19:33:46 +08:00
parent ee9bfc3bc4
commit fc53ed34b9
8 changed files with 43 additions and 8 deletions

View File

@ -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},

View File

@ -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.
%%

View File

@ -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)).

View File

@ -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()} |