diff --git a/apps/iot/src/http_handler/http_host_handler.erl b/apps/iot/src/http_handler/http_host_handler.erl index b6aed73..3568b34 100644 --- a/apps/iot/src/http_handler/http_host_handler.erl +++ b/apps/iot/src/http_handler/http_host_handler.erl @@ -30,34 +30,15 @@ handle_request(_, "/host/list", Params, PostParams) -> Model = maps:get(<<"model">>, PostParams, <<"">>), CellId = maps:get(<<"cell_id">>, PostParams, <<"">>), CellId1 = case CellId =/= <<>> of - true -> - binary_to_integer(CellId); - false -> - 0 + true -> binary_to_integer(CellId); + false -> 0 end, - MatchHead = #host{model = '$1', cell_id = '$2', _ = '_'}, - Guard = [], - Guard1 = case Model =/= <<"">> of - true -> - [{'=:=', '$1', Model}|Guard]; - false -> - Guard - end, - - Guard2 = case CellId1 > 0 of - true -> - [{'=:=', '$2', CellId1}|Guard1]; - false -> - Guard1 - end, - - Result = ['$_'], - - case host_model:get_hosts({MatchHead, Guard2, Result}, Start, Size) of + MatchSpec = host_model:match_spec([{model, Model}, {cell, CellId1}]), + case host_model:get_hosts(MatchSpec, Start, Size) of {ok, Hosts, TotalNum} -> Response = #{ - <<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts), + <<"hosts">> => lists:map(fun host_model:to_map/1, Hosts), <<"stat">> => host_model:get_stat(), <<"total_num">> => TotalNum }, @@ -79,16 +60,16 @@ handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) -> HostInfo = host_model:to_map(Host), %% 获取终端信息 {ok, Terminals0} = terminal_model:get_host_terminals(HostId), - Terminals = lists:map(fun(E) -> terminal_model:to_map(E) end, Terminals0), + Terminals = lists:map(fun terminal_model:to_map/1, Terminals0), HostInfo1 = maps:put(<<"terminals">>, Terminals, HostInfo), %% 获取微服务信息 {ok, Services0} = service_model:get_host_services(HostId), - Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0), + Services = lists:map(fun service_model:to_map/1, Services0), HostInfo2 = maps:put(<<"services">>, Services, HostInfo1), %% 获取部署应用场景 {ok, DeployList0} = scenario_deploy_model:get_host_deploy_list(HostId), - DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0), + DeployList = lists:map(fun scenario_deploy_model:to_map/1, DeployList0), %% 获取部署的场景信息 DeployList1 = lists:map(fun(DeployInfo = #{<<"scenario_id">> := ScenarioId}) -> case scenario_model:get_scenario(ScenarioId) of diff --git a/apps/iot/src/http_handler/http_iot_handler.erl b/apps/iot/src/http_handler/http_iot_handler.erl index 316d576..4fa9b44 100644 --- a/apps/iot/src/http_handler/http_iot_handler.erl +++ b/apps/iot/src/http_handler/http_iot_handler.erl @@ -13,9 +13,6 @@ %% API -export([handle_request/4]). -handle_request("GET", "/api/booking", _, _Params) -> - {ok, 200, iot_util:json_data(<<"success">>)}; - %% 下发参数 handle_request("POST", "/iot/send_params", _, PostParams = #{<<"host_id">> := HostId, <<"service_name">> := ServiceName, <<"params">> := Params}) -> lager:debug("body is: ~p", [PostParams]), diff --git a/apps/iot/src/http_handler/http_protocol.erl b/apps/iot/src/http_handler/http_protocol.erl index 65ca36e..29e1740 100644 --- a/apps/iot/src/http_handler/http_protocol.erl +++ b/apps/iot/src/http_handler/http_protocol.erl @@ -59,12 +59,7 @@ init(Req0, Opts = [Mod|_]) -> %% 判断是否支持gzip supported_gzip(AcceptEncoding) when is_binary(AcceptEncoding) -> - case binary:match(AcceptEncoding, <<"gzip">>) of - nomatch -> - false; - _ -> - true - end. + binary:match(AcceptEncoding, <<"gzip">>) =/= nomatch. parse_body(Req0) -> ContentType = cowboy_req:header(<<"content-type">>, Req0), diff --git a/apps/iot/src/http_handler/http_router_handler.erl b/apps/iot/src/http_handler/http_router_handler.erl index e292b11..8fbec02 100644 --- a/apps/iot/src/http_handler/http_router_handler.erl +++ b/apps/iot/src/http_handler/http_router_handler.erl @@ -29,18 +29,8 @@ handle_request(_, "/router/list", Params, PostParams) -> %% 处理查询条件 Name = maps:get(<<"name">>, PostParams, <<"">>), - MatchHead = #router{name = '$1', _ = '_'}, - Guard = [], - Guard1 = case Name =/= <<"">> of - true -> - [{'=:=', '$1', Name}|Guard]; - false -> - Guard - end, - - Result = ['$_'], - - case router_model:get_routers({MatchHead, Guard1, Result}, Start, Size) of + MatchSpec = router_model:match_spec([{name, Name}]), + case router_model:get_routers(MatchSpec, Start, Size) of {ok, Routers, TotalNum} -> Response = #{ <<"routers">> => lists:map(fun(R) -> router_model:to_map(R) end, Routers), diff --git a/apps/iot/src/http_handler/http_scenario_handler.erl b/apps/iot/src/http_handler/http_scenario_handler.erl index 7f9acfe..bf216ae 100644 --- a/apps/iot/src/http_handler/http_scenario_handler.erl +++ b/apps/iot/src/http_handler/http_scenario_handler.erl @@ -29,20 +29,11 @@ handle_request(_, "/scenario/list", Params, PostParams) -> %% 处理查询条件 Name = maps:get(<<"name">>, PostParams, <<"">>), - MatchHead = #scenario{name = '$1', _ = '_'}, - Guard = [], - Guard1 = case Name =/= <<"">> of - true -> - [{'=:=', '$1', Name}|Guard]; - false -> - Guard - end, - Result = ['$_'], - - case scenario_model:get_scenario_list({MatchHead, Guard1, Result}, Start, Size) of + MatchSpec = scenario_model:match_spec([{name, Name}]), + case scenario_model:get_scenario_list(MatchSpec, Start, Size) of {ok, ScenarioList, TotalNum} -> Response = #{ - <<"scenarios">> => lists:map(fun(Host) -> scenario_model:to_map(Host) end, ScenarioList), + <<"scenarios">> => lists:map(fun scenario_model:to_map/1, ScenarioList), <<"total_num">> => TotalNum }, {ok, 200, iot_util:json_data(Response)}; @@ -60,7 +51,7 @@ handle_request("GET", "/scenario/detail", #{<<"id">> := ScenarioId0}, _) -> ScenarioInfo = scenario_model:to_map(Scenario), %% 获取场景下部署的主机列表 {ok, DeployList0} = scenario_deploy_model:get_scenario_deploy_list(ScenarioId), - DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0), + DeployList = lists:map(fun scenario_deploy_model:to_map/1, DeployList0), ScenarioInfo1 = maps:put(<<"deploy_list">>, ScenarioInfo, DeployList), %% 获取部署的主机信息 ScenarioInfo2 = lists:map(fun(Info = #{<<"host_id">> := HostId}) -> diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl index 9068abd..7619ab4 100644 --- a/apps/iot/src/model/host_model.erl +++ b/apps/iot/src/model/host_model.erl @@ -13,7 +13,7 @@ %% 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([to_map/1]). +-export([to_map/1, match_spec/1]). get_host(HostId) when is_binary(HostId) -> case mnesia:dirty_read(host, HostId) of @@ -148,6 +148,24 @@ delete(HostId) when is_binary(HostId) -> {error, Reason} end. +%% 获取查询的过滤条件 +match_spec(Specs) when is_list(Specs) -> + MatchHead = #host{model = '$1', cell_id = '$2', _ = '_'}, + Guard = guard(Specs), + Result = ['$_'], + {MatchHead, Guard, Result}. + +guard(Specs) when is_list(Specs) -> + guard(Specs, []). +guard([], Guard) -> + Guard; +guard([{model, Model}|Tail], Guard) when Model =/= <<"">> -> + guard(Tail, [{'=:=', '$1', Model}|Guard]); +guard([{cell, CellId}|Tail], Guard) when CellId > 0 -> + guard(Tail, [{'=:=', '$2', CellId}|Guard]); +guard([_|Tail], Guard) -> + guard(Tail, Guard). + %% 获取app表的数据大小 table_size() -> mnesia:table_info(host, size). diff --git a/apps/iot/src/model/router_model.erl b/apps/iot/src/model/router_model.erl index bc6f190..1b27afb 100644 --- a/apps/iot/src/model/router_model.erl +++ b/apps/iot/src/model/router_model.erl @@ -13,7 +13,7 @@ %% API -export([get_router/1, get_routers/3, add_router/1, change_status/2, delete/1, table_size/0, get_all_routers/0, get_all_valid_routers/0]). --export([to_map/1]). +-export([to_map/1, match_spec/1]). get_router(RouterId) when is_integer(RouterId) -> case mnesia:dirty_read(router, RouterId) of @@ -96,6 +96,23 @@ delete(RouterId) when is_integer(RouterId) -> {error, Reason} end. +%% 获取查询的过滤条件 +match_spec(Specs) when is_list(Specs) -> + MatchHead = #router{name = '$1', _ = '_'}, + Result = ['$_'], + Guard = guard(Specs), + Result = ['$_'], + {MatchHead, Guard, Result}. + +guard(Specs) when is_list(Specs) -> + guard(Specs, []). +guard([], Guard) -> + Guard; +guard([{name, Name}|Tail], Guard) when Name =/= <<"">> -> + guard(Tail, [{'=:=', '$1', Name}|Guard]); +guard([_|Tail], Guard) -> + guard(Tail, Guard). + %% 获取app表的数据大小 table_size() -> mnesia:table_info(router, size). diff --git a/apps/iot/src/model/scenario_model.erl b/apps/iot/src/model/scenario_model.erl index b3dbcb8..00b844f 100644 --- a/apps/iot/src/model/scenario_model.erl +++ b/apps/iot/src/model/scenario_model.erl @@ -13,7 +13,7 @@ %% API -export([get_scenario/1, get_scenario_list/3, add_scenario/1, change_status/2, delete/1, table_size/0]). --export([to_map/1]). +-export([to_map/1, match_spec/1]). get_scenario(ScenarioId) when is_integer(ScenarioId) -> case mnesia:dirty_read(scenario, ScenarioId) of @@ -68,6 +68,23 @@ delete(ScenarioId) when is_integer(ScenarioId) -> {error, Reason} end. +%% 获取查询的过滤条件 +match_spec(Specs) when is_list(Specs) -> + MatchHead = #scenario{name = '$1', _ = '_'}, + Result = ['$_'], + Guard = guard(Specs), + Result = ['$_'], + {MatchHead, Guard, Result}. + +guard(Specs) when is_list(Specs) -> + guard(Specs, []). +guard([], Guard) -> + Guard; +guard([{name, Name}|Tail], Guard) when Name =/= <<"">> -> + guard(Tail, [{'=:=', '$1', Name}|Guard]); +guard([_|Tail], Guard) -> + guard(Tail, Guard). + %% 获取app表的数据大小 table_size() -> mnesia:table_info(scenario, size).