fix handler

This commit is contained in:
安礼成 2023-02-26 21:53:29 +08:00
parent 70386fbed2
commit 35041b9708
3 changed files with 66 additions and 29 deletions

View File

@ -8,6 +8,7 @@
%%%-------------------------------------------------------------------
-module(http_host_handler).
-author("licheng5").
-include("iot.hrl").
%% API
-export([handle_request/4]).
@ -16,7 +17,7 @@
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle_request(_, "/host/list", Params, _) ->
handle_request(_, "/host/list", Params, PostParams) ->
Page0 = maps:get(<<"page">>, Params, <<"1">>),
Size0 = maps:get(<<"size">>, Params, <<"10">>),
Page = binary_to_integer(Page0),
@ -25,16 +26,42 @@ handle_request(_, "/host/list", Params, _) ->
true = Page > 0 andalso Size > 0,
Start = (Page - 1) * Size,
case host_model:get_hosts(Start, Size) of
{ok, Hosts, Stat} ->
%%
Model = maps:get(<<"model">>, PostParams, <<"">>),
CellId = maps:get(<<"cell_id">>, PostParams, <<"">>),
CellId1 = case CellId =/= <<>> of
true ->
binary_to_integer(CellId);
false ->
0
end,
%%
Filter = fun(#host{model = Model0, cell_id = CellId0}) ->
lists:all(fun(E) ->
case E of
{model, M} ->
lager:debug("m: ~p, mid: ~p", [M, Model0]),
case M =/= <<"">> of
true -> M =:= Model0;
_ -> true
end;
{cell_id, C} ->
lager:debug("c: ~p, cid: ~p", [C, CellId0]),
case C > 0 of
true -> C == CellId0;
_ -> true
end
end
end, [{model, Model}, {cell_id, CellId1}])
end,
case host_model:get_hosts(Filter, Start, Size) of
{ok, Hosts, TotalNum} ->
Response = #{
<<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts),
<<"stat">> => lists:map(fun({Status, Num}) ->
#{
<<"status">> => Status,
<<"num">> => Num
}
end, maps:to_list(Stat))
<<"stat">> => host_model:get_stat(),
<<"total_num">> => TotalNum
},
lager:debug("resp is: ~p", [Response]),

View File

@ -21,7 +21,7 @@ insert_hosts() ->
host_id = integer_to_binary(Id0),
name = <<"N1000_0001">>,
model = <<"N1000">>,
cell_id = <<"公共教学楼"/utf8>>
cell_id = rand:uniform(100)
},
host_model:add_host(Host)

View File

@ -12,7 +12,7 @@
-include_lib("stdlib/include/qlc.hrl").
%% API
-export([get_host/1, get_hosts/2, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]).
-export([get_host/1, get_hosts/3, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]).
-export([to_map/1]).
get_host(HostId) when is_binary(HostId) ->
@ -24,32 +24,42 @@ get_host(HostId) when is_binary(HostId) ->
end.
%% app信息
-spec get_hosts(Start :: integer(), Limit :: integer()) ->
{ok, Items :: list(), Stat :: maps:map()} |
-spec get_hosts(Filter :: fun(), Start :: integer(), Limit :: integer()) ->
{ok, Items :: list(), TotalNum :: integer()} |
{error, Reason :: any()}.
get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
get_hosts(Filter, Start, Limit) when is_function(Filter, 1), is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(host)]),
QC = qlc:cursor(Q),
case qlc:next_answers(QC, Start + Limit) of
Answers when is_list(Answers) ->
lists:sublist(Answers, Start + 1, Limit);
_ ->
[]
mnesia:foldl(fun(Host, Acc) ->
case Filter(Host) of
true -> [Host|Acc];
false -> Acc
end
end, [], host)
end,
case mnesia:transaction(Fun) of
{atomic, Items} when is_list(Items) ->
%%
Stat = lists:foldl(fun(#host{status = Status}, Acc) ->
Num = maps:get(Status, Acc, 0),
Acc#{Status => Num + 1}
end, #{}, Items),
{ok, Items, Stat};
{atomic, Items0} when is_list(Items0) ->
Items = lists:reverse(Items0),
NItems = lists:sublist(Items, Start + 1, Limit),
{ok, NItems, length(Items)};
{aborted, Error} ->
Error
end.
%%
get_stat() ->
Fun = fun() ->
mnesia:foldl(fun(#host{status = Status}, Acc) ->
Num = maps:get(Status, Acc, 0),
Acc#{Status => Num + 1}
end, #{}, host)
end,
case mnesia:transaction(Fun) of
{atomic, Stat} when is_map(Stat) ->
lists:map(fun({Status, Num}) -> #{<<"status">> => Status, <<"num">> => Num} end, maps:to_list(Stat));
{aborted, _} ->
#{}
end.
-spec find_hosts(Pred :: fun((#host{}) -> boolean()), Start :: integer(), Limit :: integer()) ->
{ok, Items :: [#host{}], Num :: integer()} |
{error, Reason :: any()}.