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), Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0),
HostInfo2 = maps:put(<<"services">>, Services, HostInfo1), 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; end;
handle_request("POST", "/host/update", _, _Params) -> handle_request("POST", "/host/update", _, _Params) ->

View File

@ -31,58 +31,76 @@ handle_request(_, "/scenario/list", Params, PostParams) ->
MatchHead = #scenario{name = '$1', _ = '_'}, MatchHead = #scenario{name = '$1', _ = '_'},
Guard = [], Guard = [],
Guard1 = case Model =/= <<"">> of Guard1 = case Name =/= <<"">> of
true -> true ->
[{'=:=', '$1', Model}|Guard]; [{'=:=', '$1', Name}|Guard];
false -> false ->
Guard Guard
end, end,
Guard2 = case CellId1 > 0 of
true ->
[{'=:=', '$2', CellId1}|Guard1];
false ->
Guard1
end,
Result = ['$_'], Result = ['$_'],
case host_model:get_hosts({MatchHead, Guard2, Result}, Start, Size) of case scenario_model:get_scenario_list({MatchHead, Guard1, Result}, Start, Size) of
{ok, Hosts, TotalNum} -> {ok, ScenarioList, TotalNum} ->
Response = #{ Response = #{
<<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts), <<"scenarios">> => lists:map(fun(Host) -> scenario_model:to_map(Host) end, ScenarioList),
<<"stat">> => host_model:get_stat(),
<<"total_num">> => TotalNum <<"total_num">> => TotalNum
}, },
lager:debug("resp is: ~p", [Response]),
{ok, 200, iot_util:json_data(Response)}; {ok, 200, iot_util:json_data(Response)};
{error, Reason} -> {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">>)} {ok, 200, iot_util:json_error(404, <<"database error">>)}
end; end;
handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) -> handle_request("GET", "/scenario/detail", #{<<"id">> := ScenarioId0}, _) ->
lager:debug("[host_handler] detail id is: ~p", [HostId]), ScenarioId = binary_to_integer(ScenarioId0),
case host_model:get_host(HostId) of case scenario_model:get_scenario(ScenarioId) of
undefined -> undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; {ok, 200, iot_util:json_error(404, <<"scenario not found">>)};
{ok, Host} -> {ok, Scenario} ->
HostInfo = host_model:to_map(Host), ScenarioInfo = scenario_model:to_map(Scenario),
%% %%
{ok, Terminals0} = terminal_model:get_host_terminals(HostId), {ok, DeployList0} = scenario_deploy_model:get_scenario_deploy_list(ScenarioId),
Terminals = lists:map(fun(E) -> terminal_model:to_map(E) end, Terminals0), DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0),
HostInfo1 = maps:put(<<"terminals">>, Terminals, HostInfo), ScenarioInfo1 = maps:put(<<"deploy_list">>, ScenarioInfo, DeployList),
%% %%
{ok, Services0} = service_model:get_host_services(HostId), ScenarioInfo2 = lists:map(fun(Info = #{<<"host_id">> := HostId}) ->
Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0), case host_model:get_host(HostId) of
HostInfo2 = maps:put(<<"services">>, Services, HostInfo1), {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; end;
handle_request("POST", "/host/update", _, _Params) -> handle_request("POST", "/scenario/change_status", _, #{<<"scenario_id">> := ScenarioId, <<"status">> := Status}) when is_integer(ScenarioId), is_integer(Status) ->
lager:debug("[host_handler] post params is: ~p", [_Params]), 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() -> Fun = fun() ->
case mnesia:read(scenario, ScenarioId) of case mnesia:read(scenario, ScenarioId) of
[] -> [] ->
mnesia:abort(<<"host not found">>); mnesia:abort(<<"scenario not found">>);
[Scenario] -> [Scenario] ->
mnesia:write(scenario, Scenario#scenario{status = Status}, write) mnesia:write(scenario, Scenario#scenario{status = Status}, write)
end end