This commit is contained in:
安礼成 2023-04-18 19:36:31 +08:00
parent 747cc8583d
commit 88f52d596f
2 changed files with 72 additions and 5 deletions

View File

@ -98,16 +98,18 @@
-record(host, {
%% ID
host_id :: binary(),
%%
serial_number = <<>> :: binary(),
%%
name :: binary(),
name = <<>> :: binary(),
%%
model :: binary(),
model = <<>> :: binary(),
%%
cell_id :: integer(),
%% rsa公钥
public_rsa :: binary(),
public_rsa = <<>> :: binary(),
%% aes的key,
aes :: binary(),
aes = <<>> :: binary(),
metric = #host_metric{},
%%
activated_ts = 0 :: integer(),

View File

@ -17,6 +17,58 @@
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
handle_request("POST", "/host/create", _,
PostParams = #{<<"name">> := Name, <<"model">> := Model, <<"cell_id">> := CellId, <<"serial_number">> := SerialNumber}) ->
lager:debug("[host_handler] create post params: ~p", [PostParams]),
HostId = iot_util:uuid(),
Host = #host{
host_id = HostId,
serial_number = SerialNumber,
name = Name,
model = Model,
cell_id = CellId,
status = 1
},
case host_model:add_host(Host) of
ok ->
{ok, 200, iot_util:json_data(HostId)};
{error, Reason} ->
lager:warning("[host_handler] get a error: ~p", [Reason]),
{ok, 200, iot_util:json_error(404, <<"database error">>)}
end;
%%
handle_request("POST", "/host/batch_create", _, HostInfos) ->
lager:debug("[host_handler] batch_create post params: ~p", [HostInfos]),
case lists:all(fun valid_host_info/1, HostInfos) of
true ->
Result = lists:map(fun(#{<<"name">> := Name, <<"model">> := Model, <<"cell_id">> := CellId, <<"serial_number">> := SerialNumber}) ->
HostId = iot_util:uuid(),
Host = #host{
host_id = HostId,
serial_number = SerialNumber,
name = Name,
model = Model,
cell_id = CellId,
status = 1
},
case host_model:add_host(Host) of
ok ->
{SerialNumber, HostId};
{error, Reason} ->
lager:debug("[host_handler] add_host get error: ~p", [Reason]),
{SerialNumber, <<"failed">>}
end
end, HostInfos),
{ok, 200, iot_util:json_data(Result)};
false ->
{ok, 200, iot_util:json_error(-1, <<"invalid arguments">>)}
end;
handle_request(_, "/host/list", Params, PostParams) ->
Page0 = maps:get(<<"page">>, Params, <<"1">>),
Size0 = maps:get(<<"size">>, Params, <<"10">>),
@ -90,3 +142,16 @@ handle_request("POST", "/host/update", _, _Params) ->
lager:debug("[host_handler] post params is: ~p", [_Params]),
{ok, 200, iot_util:json_data(<<"success">>)}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
valid_host_info(#{<<"name">> := Name, <<"model">> := Model, <<"cell_id">> := CellId, <<"serial_number">> := SerialNumber}) ->
Name =/= <<"">> andalso Model =/= <<"">> andalso CellId > 0 and SerialNumber =/= <<"">>.
host_error(M = #{<<"name">> := <<>>}) when not is_map_key(<<"name">>, M) ->
host_error(M = #{<<"name">> := <<>>}) when not is_map_key(<<"name">>, M) ->
ok.