simple code

This commit is contained in:
安礼成 2023-04-19 18:06:09 +08:00
parent 2794fe393d
commit 996540fa21
4 changed files with 115 additions and 82 deletions

View File

@ -21,72 +21,48 @@
handle_request("POST", "/host/create", _, HostInfo) -> handle_request("POST", "/host/create", _, HostInfo) ->
lager:debug("[host_handler] create post params: ~p", [HostInfo]), lager:debug("[host_handler] create post params: ~p", [HostInfo]),
case valid_host_info(HostInfo) of case convert_host_info(HostInfo) of
true -> {ok, Host} ->
HostId = iot_util:uuid(), HostId = iot_util:uuid(),
#{<<"name">> := Name, <<"model">> := Model, <<"cell_id">> := CellId, <<"serial_number">> := SerialNumber} = HostInfo, case host_model:add_host(Host#host{host_id = HostId}) of
Host = #host{
host_id = HostId,
serial_number = SerialNumber,
name = Name,
model = Model,
cell_id = CellId,
status = ?HOST_STATUS_INACTIVE
},
case host_model:add_host(Host) of
ok -> ok ->
{ok, 200, iot_util:json_data(HostId)}; {ok, 200, iot_util:json_data(HostId)};
{error, Reason} when is_binary(Reason) -> {error, Reason} when is_binary(Reason) ->
lager:warning("[host_handler] get a error: ~p", [Reason]),
{ok, 200, iot_util:json_error(-1, Reason)}; {ok, 200, iot_util:json_error(-1, Reason)};
{error, Reason} when is_binary(Reason) -> {error, Reason} when is_binary(Reason) ->
{ok, 200, iot_util:json_error(-1, <<"database error">>)} {ok, 200, iot_util:json_error(-1, <<"database error">>)}
end; end;
false -> {error, Reason} ->
Error = host_error(HostInfo), {ok, 200, iot_util:json_error(-1, Reason)}
{ok, 200, iot_util:json_error(-1, Error)}
end; end;
%% %%
handle_request("POST", "/host/batch_import", _, HostInfos) -> handle_request("POST", "/host/batch_import", _, HostInfos) ->
lager:debug("[host_handler] batch_create post params: ~p", [HostInfos]), lager:debug("[host_handler] batch_create post params: ~p", [HostInfos]),
%% serial_number是必填项 {SuccessList, FailedList} = lists:foldl(fun(HostInfo, {SuccessList0, FailedList0}) ->
case lists:any(fun(Info) -> not is_map_key(<<"serial_number">>, Info) orelse maps:get(<<"serial_number">>, Info) == <<"">> end, HostInfos) of case convert_host_info(HostInfo) of
true -> {ok, Host = #host{serial_number = SerialNumber}} ->
{ok, 200, iot_util:json_error(-1, <<"serial_number missed">>)}; HostId = iot_util:uuid(),
false -> case host_model:add_host(Host#host{host_id = HostId}) of
case lists:all(fun valid_host_info/1, HostInfos) of ok ->
true -> SuccessItem = #{<<"serial_number">> => SerialNumber, <<"host_id">> => HostId},
Result = lists:map(fun(#{<<"name">> := Name, <<"model">> := Model, <<"cell_id">> := CellId, <<"serial_number">> := SerialNumber}) -> {[SuccessItem|SuccessList0], FailedList0};
HostId = iot_util:uuid(), {error, Reason} when is_binary(Reason) ->
Host = #host{ ErrorItem = maps:put(<<"error_message">>, Reason, HostInfo),
host_id = HostId, {SuccessList0, [ErrorItem|FailedList0]}
serial_number = SerialNumber, end;
name = Name, {error, Reason} ->
model = Model, ErrorItem = maps:put(<<"error_message">>, Reason, HostInfo),
cell_id = CellId, {SuccessList0, [ErrorItem|FailedList0]}
status = ?HOST_STATUS_INACTIVE end
}, end, {[], []}, HostInfos),
case host_model:add_host(Host) of ImportResult = #{
ok -> <<"success_list">> => lists:reverse(SuccessList),
{SerialNumber, HostId}; <<"failed_list">> => lists:reverse(FailedList)
{error, Reason} when is_binary(Reason) -> },
lager:debug("[host_handler] add_host get error: ~p", [Reason]),
{SerialNumber, Reason};
{error, Reason} ->
{SerialNumber, <<"failed">>}
end
end, HostInfos),
{ok, 200, iot_util:json_data(Result)}; {ok, 200, iot_util:json_data(ImportResult)};
false ->
%%
InvalidHostInfos = lists:filter(fun(Info) -> not valid_host_info(Info) end, HostInfos),
ErrorInfos = lists:map(fun(Info = #{<<"serial_number">> := SerialNumber}) -> {SerialNumber, host_error(Info)} end, InvalidHostInfos),
{ok, 200, iot_util:json_error(-1, maps:from_list(ErrorInfos))}
end
end;
handle_request(_, "/host/list", GetParams, PostParams) -> handle_request(_, "/host/list", GetParams, PostParams) ->
Page0 = maps:get(<<"page">>, GetParams, <<"1">>), Page0 = maps:get(<<"page">>, GetParams, <<"1">>),
@ -157,28 +133,50 @@ handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) ->
{ok, 200, iot_util:json_data(HostInfo3)} {ok, 200, iot_util:json_data(HostInfo3)}
end; end;
handle_request("POST", "/host/update", _, _Params) -> handle_request("POST", "/host/update", _, Fields0 = #{<<"host_id">> := HostId}) when is_binary(HostId) ->
lager:debug("[host_handler] post params is: ~p", [_Params]), lager:debug("[host_handler] post params is: ~p", [Fields0]),
{ok, 200, iot_util:json_data(<<"success">>)}. Fields = maps:remove(<<"host_id">>, Fields0),
case host_model:update_host(HostId, Fields) of
ok ->
{ok, 200, iot_util:json_data(<<"success">>)};
{error, Reason} when is_binary(Reason) ->
{ok, 200, iot_util:json_error(-1, Reason)}
end;
handle_request(_, Path, _, _) ->
Path1 = list_to_binary(Path),
{ok, 200, iot_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helper methods %% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% convert_host_info(#{<<"name">> := Name, <<"model">> := Model, <<"cell_id">> := CellId, <<"serial_number">> := SerialNumber}) ->
valid_host_info(#{<<"name">> := Name, <<"model">> := Model, <<"cell_id">> := CellId, <<"serial_number">> := SerialNumber}) -> CheckResult = if
Name =/= <<"">> andalso Model =/= <<"">> andalso CellId > 0 andalso SerialNumber =/= <<"">>. Name == <<>> ->
{error, <<"name is empty">>};
Model == <<>> ->
{error, <<"model is empty">>};
SerialNumber == <<>> ->
{error, <<"serial_number is empty">>};
not is_integer(CellId) orelse CellId < 0 ->
{ok, <<"cell_id is error">>};
true ->
ok
end,
%% case CheckResult of
host_error(M) when is_map(M) -> ok ->
if {ok, #host{
not is_map_key(<<"name">>, M) orelse map_get(<<"name">>, M) == <<>> -> serial_number = SerialNumber,
<<"name is empty">>; name = Name,
not is_map_key(<<"model">>, M) orelse map_get(<<"model">>, M) == <<>> -> model = Model,
<<"model is empty">>; cell_id = CellId,
not is_map_key(<<"serial_number">>, M) orelse map_get(<<"serial_number">>, M) == <<>> -> status = ?HOST_STATUS_INACTIVE
<<"serial_number is empty">>; }};
true -> {error, Reason} ->
<<"unknown error">> {error, Reason}
end. end;
convert_host_info(_) ->
{error, <<"miss required param">>}.

View File

@ -206,4 +206,6 @@ convert_terminal_info(#{<<"host_id">> := HostId, <<"serial_number">> := SerialNu
}}; }};
{error, Reason} -> {error, Reason} ->
{error, Reason} {error, Reason}
end. end;
convert_terminal_info(_) ->
{error, <<"miss required param">>}.

View File

@ -11,12 +11,14 @@
-include("iot.hrl"). -include("iot.hrl").
-include_lib("stdlib/include/qlc.hrl"). -include_lib("stdlib/include/qlc.hrl").
-define(TAB_NAME, host).
%% API %% API
-export([get_host/1, get_hosts/3, get_all_hosts/0, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3, activate/1]). -export([get_host/1, get_hosts/3, get_all_hosts/0, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3, activate/1, update_host/2]).
-export([to_map/1, match_spec/1]). -export([to_map/1, match_spec/1]).
get_host(HostId) when is_binary(HostId) -> get_host(HostId) when is_binary(HostId) ->
case mnesia:dirty_read(host, HostId) of case mnesia:dirty_read(?TAB_NAME, HostId) of
[Host = #host{}] -> [Host = #host{}] ->
{ok, Host}; {ok, Host};
_ -> _ ->
@ -25,7 +27,7 @@ get_host(HostId) when is_binary(HostId) ->
get_all_hosts() -> get_all_hosts() ->
Fun = fun() -> Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(host), E#host.status == 1]), Q = qlc:q([E || E <- mnesia:table(?TAB_NAME), E#host.status == ?HOST_STATUS_ONLINE]),
qlc:e(Q) qlc:e(Q)
end, end,
case mnesia:transaction(Fun) of case mnesia:transaction(Fun) of
@ -40,8 +42,7 @@ get_all_hosts() ->
{ok, Items :: list(), TotalNum :: integer()} | {ok, Items :: list(), TotalNum :: integer()} |
{error, Reason :: any()}. {error, Reason :: any()}.
get_hosts(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> get_hosts(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
Hosts0 = mnesia:dirty_select(host, [Spec]), Hosts0 = mnesia:dirty_select(?TAB_NAME, [Spec]),
%% TODO host的排序方式
SortedHosts = sort(Hosts0), SortedHosts = sort(Hosts0),
Len = length(SortedHosts), Len = length(SortedHosts),
case Len >= Start + 1 of case Len >= Start + 1 of
@ -57,7 +58,7 @@ get_stat() ->
mnesia:foldl(fun(#host{status = Status}, Acc) -> mnesia:foldl(fun(#host{status = Status}, Acc) ->
Num = maps:get(Status, Acc, 0), Num = maps:get(Status, Acc, 0),
Acc#{Status => Num + 1} Acc#{Status => Num + 1}
end, #{}, host) end, #{}, ?TAB_NAME)
end, end,
case mnesia:transaction(Fun) of case mnesia:transaction(Fun) of
{atomic, Stat} when is_map(Stat) -> {atomic, Stat} when is_map(Stat) ->
@ -71,7 +72,7 @@ get_stat() ->
{error, Reason :: any()}. {error, Reason :: any()}.
find_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> find_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
Fun = fun() -> Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(host)]), Q = qlc:q([E || E <- mnesia:table(?TAB_NAME)]),
qlc:fold(fun(Host, Acc) -> qlc:fold(fun(Host, Acc) ->
case Pred(Host) of case Pred(Host) of
true -> [Host|Acc]; true -> [Host|Acc];
@ -97,12 +98,12 @@ find_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), is_
-spec add_host(Host :: #host{}) -> ok | {error, Reason :: binary()}. -spec add_host(Host :: #host{}) -> ok | {error, Reason :: binary()}.
add_host(Host = #host{serial_number = SerialNumber}) -> add_host(Host = #host{serial_number = SerialNumber}) ->
Fun = fun() -> Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(host), E#host.serial_number =:= SerialNumber]), Q = qlc:q([E || E <- mnesia:table(?TAB_NAME), E#host.serial_number =:= SerialNumber]),
case qlc:e(Q) of case qlc:e(Q) of
[_SomeHost|_] -> [_SomeHost|_] ->
mnesia:abort(<<"serial_number exists">>); mnesia:abort(<<"serial_number exists">>);
[] -> [] ->
mnesia:write(host, Host, write) mnesia:write(?TAB_NAME, Host, write)
end end
end, end,
@ -116,11 +117,43 @@ add_host(Host = #host{serial_number = SerialNumber}) ->
-spec change_status(HostId :: binary(), Status :: integer()) -> ok | {error, Reason :: any()}. -spec change_status(HostId :: binary(), Status :: integer()) -> ok | {error, Reason :: any()}.
change_status(HostId, Status) when is_binary(HostId), is_integer(Status) -> change_status(HostId, Status) when is_binary(HostId), is_integer(Status) ->
Fun = fun() -> Fun = fun() ->
case mnesia:read(host, HostId) of case mnesia:read(?TAB_NAME, HostId) of
[] -> [] ->
mnesia:abort(<<"host not found">>); mnesia:abort(<<"host not found">>);
[Host] -> [Host] ->
mnesia:write(host, Host#host{status = Status}, write) mnesia:write(?TAB_NAME, Host#host{status = Status}, write)
end
end,
case mnesia:transaction(Fun) of
{atomic, ok} ->
ok;
{aborted, Reason} ->
{error, Reason}
end.
-spec update_host(HostId :: binary(), Fields :: #{}) -> ok | {error, Reason :: any()}.
update_host(HostId, Fields) when is_binary(HostId), is_map(Fields) ->
Fun = fun() ->
case mnesia:read(?TAB_NAME, HostId) of
[] ->
mnesia:abort(<<"host not found">>);
[Host] ->
NHost = lists:foldl(fun(E, Host0) ->
case E of
{<<"name">>, Name} when is_binary(Name) ->
Host0#host{name = Name};
{<<"serial_number">>, SerialNumber} when is_binary(SerialNumber) ->
Host0#host{serial_number = SerialNumber};
{<<"model">>, Model} when is_binary(Model) ->
Host0#host{model = Model};
{<<"cell_id">>, CellId} when is_integer(CellId) ->
Host0#host{cell_id = CellId};
{Name, _} ->
mnesia:abort(<<"invalid: ", Name/binary>>)
end
end, Host, maps:to_list(Fields)),
mnesia:write(?TAB_NAME, NHost, write)
end end
end, end,
case mnesia:transaction(Fun) of case mnesia:transaction(Fun) of

View File

@ -101,7 +101,7 @@ add_terminal(Terminal = #terminal{}) ->
{error, Error} {error, Error}
end. end.
-spec change_status(TerminalId :: integer(), Fields :: #{}) -> ok | {error, Reason :: any()}. -spec update_terminal(TerminalId :: integer(), Fields :: #{}) -> ok | {error, Reason :: any()}.
update_terminal(TerminalId, Fields) when is_integer(TerminalId), is_map(Fields) -> update_terminal(TerminalId, Fields) when is_integer(TerminalId), is_map(Fields) ->
Fun = fun() -> Fun = fun() ->
case mnesia:read(?TAB_NAME, TerminalId) of case mnesia:read(?TAB_NAME, TerminalId) of