From 88f52d596ff06b85c3b89bd7358096efc0a21166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Tue, 18 Apr 2023 19:36:31 +0800 Subject: [PATCH] fix --- apps/iot/include/iot.hrl | 10 +-- .../src/http_handler/http_host_handler.erl | 67 ++++++++++++++++++- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index a3907f0..c82720e 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -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(), diff --git a/apps/iot/src/http_handler/http_host_handler.erl b/apps/iot/src/http_handler/http_host_handler.erl index 3568b34..7867d1f 100644 --- a/apps/iot/src/http_handler/http_host_handler.erl +++ b/apps/iot/src/http_handler/http_host_handler.erl @@ -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">>), @@ -89,4 +141,17 @@ handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) -> handle_request("POST", "/host/update", _, _Params) -> lager:debug("[host_handler] post params is: ~p", [_Params]), - {ok, 200, iot_util:json_data(<<"success">>)}. \ No newline at end of file + {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.