This commit is contained in:
anlicheng 2025-05-20 12:06:52 +08:00
parent 9418ae70bd
commit a9ef277402
3 changed files with 44 additions and 13 deletions

View File

@ -60,9 +60,12 @@
%%%% , %%%% ,
-define(PUSH_DEPLOY, 16#01). -define(PUSH_DEPLOY, 16#01).
-define(PUSH_SERVICE_CONFIG, 16#02). -define(PUSH_START_SERVICE, 16#02).
-define(PUSH_INVOKE, 16#03). -define(PUSH_STOP_SERVICE, 16#03).
-define(PUSH_TASK_LOG, 16#04).
-define(PUSH_SERVICE_CONFIG, 16#04).
-define(PUSH_INVOKE, 16#05).
-define(PUSH_TASK_LOG, 16#05).
%% %%
-record(kv, { -record(kv, {

View File

@ -73,6 +73,7 @@ handle_request("POST", "/service/delete_config", _, #{<<"service_id">> := Servic
{ok, 200, iot_util:json_error(-1, <<"delete service config errror">>)} {ok, 200, iot_util:json_error(-1, <<"delete service config errror">>)}
end; end;
%%
handle_request("POST", "/service/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"service_id">> := ServiceId, <<"tar_url">> := TarUrl}) handle_request("POST", "/service/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"service_id">> := ServiceId, <<"tar_url">> := TarUrl})
when is_binary(UUID), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) -> when is_binary(UUID), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) ->
@ -80,7 +81,26 @@ handle_request("POST", "/service/deploy", _, #{<<"uuid">> := UUID, <<"task_id">>
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:async_deploy(Pid, TaskId, ServiceId, TarUrl) of case iot_host:deploy_service(Pid, TaskId, ServiceId, TarUrl) 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("POST", "/service/start", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId}) when is_binary(UUID), is_binary(ServiceId) ->
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:deploy_service(Pid, TaskId, ServiceId, TarUrl) 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} ->
@ -101,7 +121,7 @@ handle_request("POST", "/service/invoke", _, #{<<"uuid">> := UUID, <<"service_id
{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) ->
Timeout = Timeout0 * 1000, Timeout = Timeout0 * 1000,
case iot_host:async_invoke(Pid, ServiceId, Payload, Timeout) of case iot_host:invoke_service(Pid, ServiceId, Payload, Timeout) 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} ->
@ -119,7 +139,7 @@ handle_request("POST", "/service/task_log", _, #{<<"uuid">> := UUID, <<"task_id"
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:async_task_log(Pid, TaskId) of case iot_host:task_log(Pid, TaskId) 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} ->

View File

@ -25,7 +25,7 @@
-export([get_metric/1, get_status/1]). -export([get_metric/1, get_status/1]).
%% %%
-export([pub/3, attach_channel/2, command/3]). -export([pub/3, attach_channel/2, command/3]).
-export([async_deploy/4, async_invoke/4, async_service_config/4, async_task_log/2, await_reply/2]). -export([deploy_service/4, start_service/2, stop_service/2, invoke_service/4, async_service_config/4, task_log/2, await_reply/2]).
%% %%
-export([reload_device/2, delete_device/2, activate_device/3]). -export([reload_device/2, delete_device/2, activate_device/3]).
-export([heartbeat/1]). -export([heartbeat/1]).
@ -94,18 +94,26 @@ async_service_config(Pid, ServiceId, ConfigJson, Timeout) when is_pid(Pid), is_b
ConfigBin = message_pb:encode_msg(#push_service_config{service_id = ServiceId, config_json = ConfigJson, timeout = Timeout}), ConfigBin = message_pb:encode_msg(#push_service_config{service_id = ServiceId, config_json = ConfigJson, timeout = Timeout}),
gen_statem:call(Pid, {async_call, self(), ?PUSH_SERVICE_CONFIG, ConfigBin}). gen_statem:call(Pid, {async_call, self(), ?PUSH_SERVICE_CONFIG, ConfigBin}).
-spec async_deploy(Pid :: pid(), TaskId :: integer(), ServiceId :: binary(), TarUrl :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec deploy_service(Pid :: pid(), TaskId :: integer(), ServiceId :: binary(), TarUrl :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
async_deploy(Pid, TaskId, ServiceId, TarUrl) when is_pid(Pid), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) -> deploy_service(Pid, TaskId, ServiceId, TarUrl) when is_pid(Pid), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) ->
PushBin = message_pb:encode_msg(#deploy{task_id = TaskId, service_id = ServiceId, tar_url = TarUrl}), PushBin = message_pb:encode_msg(#deploy{task_id = TaskId, service_id = ServiceId, tar_url = TarUrl}),
gen_statem:call(Pid, {async_call, self(), ?PUSH_DEPLOY, PushBin}). gen_statem:call(Pid, {async_call, self(), ?PUSH_DEPLOY, PushBin}).
-spec async_invoke(Pid :: pid(), ServiceId :: binary(), Payload :: binary(), Timeout :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec start_service(Pid :: pid(), ServiceId :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
async_invoke(Pid, ServiceId, Payload, Timeout) when is_pid(Pid), is_binary(ServiceId), is_binary(Payload), is_integer(Timeout) -> start_service(Pid, ServiceId) when is_pid(Pid), is_binary(ServiceId) ->
gen_statem:call(Pid, {async_call, self(), ?PUSH_START_SERVICE, ServiceId}).
-spec stop_service(Pid :: pid(), ServiceId :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
stop_service(Pid, ServiceId) when is_pid(Pid), is_binary(ServiceId) ->
gen_statem:call(Pid, {async_call, self(), ?PUSH_STOP_SERVICE, ServiceId}).
-spec invoke_service(Pid :: pid(), ServiceId :: binary(), Payload :: binary(), Timeout :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
invoke_service(Pid, ServiceId, Payload, Timeout) when is_pid(Pid), is_binary(ServiceId), is_binary(Payload), is_integer(Timeout) ->
InvokeBin = message_pb:encode_msg(#invoke{service_id = ServiceId, payload = Payload, timeout = Timeout}), InvokeBin = message_pb:encode_msg(#invoke{service_id = ServiceId, payload = Payload, timeout = Timeout}),
gen_statem:call(Pid, {async_call, self(), ?PUSH_INVOKE, InvokeBin}). gen_statem:call(Pid, {async_call, self(), ?PUSH_INVOKE, InvokeBin}).
-spec async_task_log(Pid :: pid(), TaskId :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec task_log(Pid :: pid(), TaskId :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
async_task_log(Pid, TaskId) when is_pid(Pid), is_integer(TaskId) -> task_log(Pid, TaskId) when is_pid(Pid), is_integer(TaskId) ->
TaskLogBin = message_pb:encode_msg(#fetch_task_log{task_id = TaskId}), TaskLogBin = message_pb:encode_msg(#fetch_task_log{task_id = TaskId}),
gen_statem:call(Pid, {async_call, self(), ?PUSH_TASK_LOG, TaskLogBin}). gen_statem:call(Pid, {async_call, self(), ?PUSH_TASK_LOG, TaskLogBin}).