From 148773241ce3ae78ec226f527ab2fac84eb1adb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Fri, 10 Mar 2023 15:34:38 +0800 Subject: [PATCH] fix handler --- .../src/http_handler/http_host_handler.erl | 18 +++- .../http_handler/http_scenario_handler.erl | 90 +++++++++++-------- apps/iot/src/model/scenario_model.erl | 2 +- 3 files changed, 72 insertions(+), 38 deletions(-) diff --git a/apps/iot/src/http_handler/http_host_handler.erl b/apps/iot/src/http_handler/http_host_handler.erl index dcbe936..3ab3a8a 100644 --- a/apps/iot/src/http_handler/http_host_handler.erl +++ b/apps/iot/src/http_handler/http_host_handler.erl @@ -86,7 +86,23 @@ handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) -> Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0), HostInfo2 = maps:put(<<"services">>, Services, HostInfo1), - {ok, 200, iot_util:json_data(HostInfo2)} + %% 获取部署应用场景 + {ok, DeployList0} = scenario_deploy_model:get_host_deploy_list(HostId), + DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0), + %% 获取部署的场景信息 + DeployList1 = lists:map(fun(DeployInfo = #{<<"scenario_id">> := ScenarioId}) -> + case scenario_model:get_scenario(ScenarioId) of + {ok, Scenario} -> + ScenarioInfo = scenario_model:to_map(Scenario), + DeployInfo#{<<"scenario_info">> => ScenarioInfo}; + undefined -> + DeployInfo#{<<"scenario_info">> => #{}} + end + end, DeployList), + + HostInfo3 = maps:put(<<"deploy_list">>, HostInfo2, DeployList1), + + {ok, 200, iot_util:json_data(HostInfo3)} end; handle_request("POST", "/host/update", _, _Params) -> diff --git a/apps/iot/src/http_handler/http_scenario_handler.erl b/apps/iot/src/http_handler/http_scenario_handler.erl index ab7b741..7f9acfe 100644 --- a/apps/iot/src/http_handler/http_scenario_handler.erl +++ b/apps/iot/src/http_handler/http_scenario_handler.erl @@ -31,58 +31,76 @@ handle_request(_, "/scenario/list", Params, PostParams) -> MatchHead = #scenario{name = '$1', _ = '_'}, Guard = [], - Guard1 = case Model =/= <<"">> of + Guard1 = case Name =/= <<"">> of true -> - [{'=:=', '$1', Model}|Guard]; + [{'=:=', '$1', Name}|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 - {ok, Hosts, TotalNum} -> + case scenario_model:get_scenario_list({MatchHead, Guard1, Result}, Start, Size) of + {ok, ScenarioList, TotalNum} -> Response = #{ - <<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts), - <<"stat">> => host_model:get_stat(), + <<"scenarios">> => lists:map(fun(Host) -> scenario_model:to_map(Host) end, ScenarioList), <<"total_num">> => TotalNum }, - - lager:debug("resp is: ~p", [Response]), - {ok, 200, iot_util:json_data(Response)}; {error, Reason} -> - lager:warning("[host_handler] get a error: ~p", [Reason]), + lager:warning("[http_scenario_handler] get a error: ~p", [Reason]), {ok, 200, iot_util:json_error(404, <<"database error">>)} end; -handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) -> - lager:debug("[host_handler] detail id is: ~p", [HostId]), - case host_model:get_host(HostId) of +handle_request("GET", "/scenario/detail", #{<<"id">> := ScenarioId0}, _) -> + ScenarioId = binary_to_integer(ScenarioId0), + case scenario_model:get_scenario(ScenarioId) of undefined -> - {ok, 200, iot_util:json_error(404, <<"host not found">>)}; - {ok, Host} -> - 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), - 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), - HostInfo2 = maps:put(<<"services">>, Services, HostInfo1), + {ok, 200, iot_util:json_error(404, <<"scenario not found">>)}; + {ok, Scenario} -> + 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), + ScenarioInfo1 = maps:put(<<"deploy_list">>, ScenarioInfo, DeployList), + %% 获取部署的主机信息 + ScenarioInfo2 = lists:map(fun(Info = #{<<"host_id">> := HostId}) -> + case host_model:get_host(HostId) of + {ok, Host} -> + HostInfo = host_model:to_map(Host), + Info#{<<"host_info">> => HostInfo}; + undefined -> + Info#{<<"host_info">> => #{}} + end + end, ScenarioInfo1), - {ok, 200, iot_util:json_data(HostInfo2)} + {ok, 200, iot_util:json_data(ScenarioInfo2)} end; -handle_request("POST", "/host/update", _, _Params) -> - lager:debug("[host_handler] post params is: ~p", [_Params]), +handle_request("POST", "/scenario/change_status", _, #{<<"scenario_id">> := ScenarioId, <<"status">> := Status}) when is_integer(ScenarioId), is_integer(Status) -> + case scenario_model:change_status(ScenarioId, Status) of + ok -> + {ok, 200, iot_util:json_data(<<"success">>)}; + {error, Reason} when is_binary(Reason) -> + lager:warning("[http_scenario_handler] change_status get a error: ~p", [Reason]), + {ok, 200, iot_util:json_error(404, Reason)} + end; - {ok, 200, iot_util:json_data(<<"success">>)}. \ No newline at end of file +%% 部署 +handle_request("POST", "/scenario/deploy", _, #{<<"scenario_id">> := ScenarioId, <<"status">> := Status}) when is_integer(ScenarioId), is_integer(Status) -> + case scenario_model:change_status(ScenarioId, Status) of + ok -> + {ok, 200, iot_util:json_data(<<"success">>)}; + {error, Reason} when is_binary(Reason) -> + lager:warning("[http_scenario_handler] change_status get a error: ~p", [Reason]), + {ok, 200, iot_util:json_error(404, Reason)} + end; + +%% 删除?? +handle_request("POST", "/scenario/change_status", _, #{<<"scenario_id">> := ScenarioId, <<"status">> := Status}) when is_integer(ScenarioId), is_integer(Status) -> + case scenario_model:change_status(ScenarioId, Status) of + ok -> + {ok, 200, iot_util:json_data(<<"success">>)}; + {error, Reason} when is_binary(Reason) -> + lager:warning("[http_scenario_handler] change_status get a error: ~p", [Reason]), + {ok, 200, iot_util:json_error(404, Reason)} + end. \ No newline at end of file diff --git a/apps/iot/src/model/scenario_model.erl b/apps/iot/src/model/scenario_model.erl index d573465..b3dbcb8 100644 --- a/apps/iot/src/model/scenario_model.erl +++ b/apps/iot/src/model/scenario_model.erl @@ -47,7 +47,7 @@ change_status(ScenarioId, Status) when is_integer(ScenarioId), is_integer(Status Fun = fun() -> case mnesia:read(scenario, ScenarioId) of [] -> - mnesia:abort(<<"host not found">>); + mnesia:abort(<<"scenario not found">>); [Scenario] -> mnesia:write(scenario, Scenario#scenario{status = Status}, write) end