fix handler

This commit is contained in:
安礼成 2023-03-10 15:34:38 +08:00
parent a425947db4
commit 148773241c
3 changed files with 72 additions and 38 deletions

View File

@ -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) ->

View File

@ -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">>)}.
%%
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.

View File

@ -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