diff --git a/apps/iot/src/http_api_handler.erl b/apps/iot/src/http_api_handler.erl index ce9c853..7af6d71 100644 --- a/apps/iot/src/http_api_handler.erl +++ b/apps/iot/src/http_api_handler.erl @@ -16,7 +16,7 @@ %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -handle_request(_, "/api/booking", _, _Params) -> +handle_request("GET", "/api/booking", _, _Params) -> {ok, 200, iot_util:json_data(<<"success">>)}; handle_request("POST", "/api/booking", _, _Params) -> {ok, 200, iot_util:json_data(<<"success">>)}. \ No newline at end of file diff --git a/apps/iot/src/http_host_handler.erl b/apps/iot/src/http_host_handler.erl index c856cfd..213dd6e 100644 --- a/apps/iot/src/http_host_handler.erl +++ b/apps/iot/src/http_host_handler.erl @@ -16,8 +16,34 @@ %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -handle_request(_, "/api/booking", _, _Params) -> - {ok, 200, iot_util:json_data(<<"success">>)}; +handle_request(_, "/host/list", Params, _) -> + Page0 = maps:get(<<"page">>, Params, <<"1">>), + Size0 = maps:get(<<"size">>, Params, <<"10">>), + Page = binary_to_integer(Page0), + Size = binary_to_integer(Size0), + + true = Page > 0 andalso Size > 0, + Start = (Page - 1) * Size, + + case host_model:get_hosts(Start, Size) of + {ok, Hosts, Stat} -> + 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)) + }, + + lager:debug("resp is: ~p", [Response]), + + {ok, 200, iot_util:json_data(Response)}; + {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/update", _, _Params) -> lager:debug("[host_handler] post params is: ~p", [_Params]), diff --git a/apps/iot/src/iot_mnesia.erl b/apps/iot/src/iot_mnesia.erl index d3b5542..408a981 100644 --- a/apps/iot/src/iot_mnesia.erl +++ b/apps/iot/src/iot_mnesia.erl @@ -29,7 +29,7 @@ init_database() -> {attributes, record_info(fields, host)}, {record_name, host}, {disc_copies, [node()]}, - {type, set} + {type, ordered_set} ]), %% 主机终端管理 @@ -37,7 +37,7 @@ init_database() -> {attributes, record_info(fields, terminal)}, {record_name, terminal}, {disc_copies, [node()]}, - {type, set} + {type, ordered_set} ]), %% 主机微服务管理 @@ -45,7 +45,7 @@ init_database() -> {attributes, record_info(fields, service)}, {record_name, service}, {disc_copies, [node()]}, - {type, set} + {type, ordered_set} ]), ok. diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl index f515767..8d87670 100644 --- a/apps/iot/src/model/host_model.erl +++ b/apps/iot/src/model/host_model.erl @@ -13,6 +13,7 @@ %% API -export([get_host/1, get_hosts/2, 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) -> case mnesia:dirty_read(host, HostId) of @@ -113,4 +114,17 @@ table_size() -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% helper methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ No newline at end of file +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +to_map(#host{host_id = HostId, name = Name, model = Model, cell_id = CellId, activated_ts = ActivatedTs, + update_ts = UpdateTs, status = Status}) -> + + #{ + <<"host_id">> => HostId, + <<"name">> => Name, + <<"model">> => Model, + <<"cell_id">> => CellId, + <<"activated_ts">> => ActivatedTs, + <<"update_ts">> => UpdateTs, + <<"status">> => Status + }. \ No newline at end of file