fix
This commit is contained in:
parent
747cc8583d
commit
88f52d596f
@ -98,16 +98,18 @@
|
|||||||
-record(host, {
|
-record(host, {
|
||||||
%% ID
|
%% ID
|
||||||
host_id :: binary(),
|
host_id :: binary(),
|
||||||
|
%% 序列号
|
||||||
|
serial_number = <<>> :: binary(),
|
||||||
%% 名称
|
%% 名称
|
||||||
name :: binary(),
|
name = <<>> :: binary(),
|
||||||
%% 型号
|
%% 型号
|
||||||
model :: binary(),
|
model = <<>> :: binary(),
|
||||||
%% 单元网格编号
|
%% 单元网格编号
|
||||||
cell_id :: integer(),
|
cell_id :: integer(),
|
||||||
%% rsa公钥
|
%% rsa公钥
|
||||||
public_rsa :: binary(),
|
public_rsa = <<>> :: binary(),
|
||||||
%% aes的key, 后续通讯需要基于这个加密
|
%% aes的key, 后续通讯需要基于这个加密
|
||||||
aes :: binary(),
|
aes = <<>> :: binary(),
|
||||||
metric = #host_metric{},
|
metric = #host_metric{},
|
||||||
%% 接入时间
|
%% 接入时间
|
||||||
activated_ts = 0 :: integer(),
|
activated_ts = 0 :: integer(),
|
||||||
|
|||||||
@ -17,6 +17,58 @@
|
|||||||
%% helper methods
|
%% 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) ->
|
handle_request(_, "/host/list", Params, PostParams) ->
|
||||||
Page0 = maps:get(<<"page">>, Params, <<"1">>),
|
Page0 = maps:get(<<"page">>, Params, <<"1">>),
|
||||||
Size0 = maps:get(<<"size">>, Params, <<"10">>),
|
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]),
|
lager:debug("[host_handler] post params is: ~p", [_Params]),
|
||||||
|
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)}.
|
{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.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user