This commit is contained in:
anlicheng 2025-09-18 15:07:47 +08:00
parent fb0ad40a44
commit 61d450fd89

View File

@ -15,8 +15,8 @@
%% config.json, %% config.json,
handle_request("POST", "/container/push_config", _, handle_request("POST", "/container/push_config", _,
#{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0}) #{<<"uuid">> := UUID, <<"container_name">> := ContainerName, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0})
when is_binary(UUID), is_binary(ServiceId), is_binary(ConfigJson), is_integer(Timeout0) -> when is_binary(UUID), is_binary(ContainerName), is_binary(ConfigJson), is_integer(Timeout0) ->
%% ConfigJson是否是合法的json字符串 %% ConfigJson是否是合法的json字符串
true = iot_util:is_json(ConfigJson), true = iot_util:is_json(ConfigJson),
@ -25,7 +25,7 @@ handle_request("POST", "/container/push_config", _,
{ok, 200, iot_util:json_error(-1, <<"host not found">>)}; {ok, 200, iot_util:json_error(-1, <<"host not found">>)};
Pid when is_pid(Pid) -> Pid when is_pid(Pid) ->
Timeout = Timeout0 * 1000, Timeout = Timeout0 * 1000,
case iot_host:async_service_config(Pid, ServiceId, ConfigJson, Timeout) of case iot_host:config_container(Pid, ContainerName, ConfigJson) of
{ok, Ref} -> {ok, Ref} ->
case iot_host:await_reply(Ref, Timeout) of case iot_host:await_reply(Ref, Timeout) of
{ok, Result} -> {ok, Result} ->
@ -39,14 +39,14 @@ handle_request("POST", "/container/push_config", _,
end; end;
%% %%
handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"service_id">> := ServiceId, <<"tar_url">> := TarUrl}) handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"config">> := Config})
when is_binary(UUID), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) -> when is_binary(UUID), is_integer(TaskId), is_binary(Config) ->
case iot_host:get_pid(UUID) of case iot_host:get_pid(UUID) of
undefined -> undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; {ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) -> Pid when is_pid(Pid) ->
case iot_host:deploy_service(Pid, TaskId, ServiceId, TarUrl) of case iot_host:deploy_container(Pid, TaskId, Config) of
{ok, Ref} -> {ok, Ref} ->
case iot_host:await_reply(Ref, 5000) of case iot_host:await_reply(Ref, 5000) of
{ok, Result} -> {ok, Result} ->
@ -60,12 +60,12 @@ handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id"
end; end;
%% %%
handle_request("POST", "/container/start", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId}) when is_binary(UUID), is_binary(ServiceId) -> handle_request("POST", "/container/start", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName}) when is_binary(UUID), is_binary(ContainerName) ->
case iot_host:get_pid(UUID) of case iot_host:get_pid(UUID) of
undefined -> undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; {ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) -> Pid when is_pid(Pid) ->
case iot_host:start_service(Pid, ServiceId) of case iot_host:start_container(Pid, ContainerName) of
{ok, Ref} -> {ok, Ref} ->
case iot_host:await_reply(Ref, 5000) of case iot_host:await_reply(Ref, 5000) of
{ok, Result} -> {ok, Result} ->
@ -79,12 +79,12 @@ handle_request("POST", "/container/start", _, #{<<"uuid">> := UUID, <<"service_i
end; end;
%% %%
handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId}) when is_binary(UUID), is_binary(ServiceId) -> handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName}) when is_binary(UUID), is_binary(ServiceId) ->
case iot_host:get_pid(UUID) of case iot_host:get_pid(UUID) of
undefined -> undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; {ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) -> Pid when is_pid(Pid) ->
case iot_host:stop_service(Pid, ServiceId) of case iot_host:stop_container(Pid, ContainerName) of
{ok, Ref} -> {ok, Ref} ->
case iot_host:await_reply(Ref, 5000) of case iot_host:await_reply(Ref, 5000) of
{ok, Result} -> {ok, Result} ->
@ -97,45 +97,23 @@ handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"service_id
end end
end; end;
%% , json %handle_request("POST", "/container/task_log", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId}) when is_binary(UUID), is_integer(TaskId) ->
handle_request("POST", "/service/invoke", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"payload">> := Payload, <<"timeout">> := Timeout0}) % case iot_host:get_pid(UUID) of
when is_binary(UUID), is_binary(ServiceId), is_binary(Payload), is_integer(Timeout0) -> % undefined ->
% {ok, 200, iot_util:json_error(404, <<"host not found">>)};
case iot_host:get_pid(UUID) of % Pid when is_pid(Pid) ->
undefined -> % case iot_host:task_log(Pid, TaskId) of
{ok, 200, iot_util:json_error(404, <<"host not found">>)}; % {ok, Ref} ->
Pid when is_pid(Pid) -> % case iot_host:await_reply(Ref, 5000) of
Timeout = Timeout0 * 1000, % {ok, Result} ->
case iot_host:invoke_service(Pid, ServiceId, Payload, Timeout) of % {ok, 200, iot_util:json_data(Result)};
{ok, Ref} -> % {error, Reason} ->
case iot_host:await_reply(Ref, Timeout) of % {ok, 200, iot_util:json_error(400, Reason)}
{ok, Result} -> % end;
{ok, 200, iot_util:json_data(Result)}; % {error, Reason} when is_binary(Reason) ->
{error, Reason} -> % {ok, 200, iot_util:json_error(400, Reason)}
{ok, 200, iot_util:json_error(400, Reason)} % end
end; % end;
{error, Reason} when is_binary(Reason) ->
{ok, 200, iot_util:json_error(400, Reason)}
end
end;
handle_request("POST", "/container/task_log", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId}) when is_binary(UUID), is_integer(TaskId) ->
case iot_host:get_pid(UUID) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) ->
case iot_host:task_log(Pid, TaskId) of
{ok, Ref} ->
case iot_host:await_reply(Ref, 5000) of
{ok, Result} ->
{ok, 200, iot_util:json_data(Result)};
{error, Reason} ->
{ok, 200, iot_util:json_error(400, Reason)}
end;
{error, Reason} when is_binary(Reason) ->
{ok, 200, iot_util:json_error(400, Reason)}
end
end;
handle_request(_, Path, _, _) -> handle_request(_, Path, _, _) ->
Path1 = list_to_binary(Path), Path1 = list_to_binary(Path),