This commit is contained in:
anlicheng 2025-09-26 15:45:31 +08:00
parent 8db8698332
commit fd15599557
3 changed files with 9 additions and 10 deletions

View File

@ -46,8 +46,7 @@ handle_request("POST", "/container/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) ->
ConfigBin = jiffy:encode(Config, [force_utf8]), case iot_host:deploy_container(Pid, TaskId, Config) of
case iot_host:deploy_container(Pid, TaskId, ConfigBin) 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

@ -91,25 +91,25 @@ attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) ->
-spec config_container(Pid :: pid(), ContainerName :: binary(), ConfigJson :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec config_container(Pid :: pid(), ContainerName :: binary(), ConfigJson :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
config_container(Pid, ContainerName, ConfigJson) when is_pid(Pid), is_binary(ContainerName), is_binary(ConfigJson) -> config_container(Pid, ContainerName, ConfigJson) when is_pid(Pid), is_binary(ContainerName), is_binary(ConfigJson) ->
Request = #jsonrpc_request{id = 0, method = <<"config_container">>, params = #{<<"container_name">> => ContainerName, <<"config">> => ConfigJson}}, Request = #jsonrpc_request{method = <<"config_container">>, params = #{<<"container_name">> => ContainerName, <<"config">> => ConfigJson}},
EncConfigBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), EncConfigBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request),
gen_statem:call(Pid, {rpc_call, self(), EncConfigBin}). gen_statem:call(Pid, {rpc_call, self(), EncConfigBin}).
-spec deploy_container(Pid :: pid(), TaskId :: integer(), Config :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec deploy_container(Pid :: pid(), TaskId :: integer(), Config :: map()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
deploy_container(Pid, TaskId, Config) when is_pid(Pid), is_integer(TaskId), is_binary(Config) -> deploy_container(Pid, TaskId, Config) when is_pid(Pid), is_integer(TaskId), is_map(Config) ->
Request = #jsonrpc_request{id = 0, method = <<"deploy">>, params = #{<<"task_id">> => TaskId, <<"config">> => Config}}, Request = #jsonrpc_request{method = <<"deploy">>, params = #{<<"task_id">> => TaskId, <<"config">> => Config}},
EncDeployBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), EncDeployBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request),
gen_statem:call(Pid, {rpc_call, self(), EncDeployBin}). gen_statem:call(Pid, {rpc_call, self(), EncDeployBin}).
-spec start_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec start_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
start_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> start_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) ->
Request = #jsonrpc_request{id = 0, method = <<"start_container">>, params = #{<<"container_name">> => ContainerName}}, Request = #jsonrpc_request{method = <<"start_container">>, params = #{<<"container_name">> => ContainerName}},
EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request),
gen_statem:call(Pid, {rpc_call, self(), EncCallBin}). gen_statem:call(Pid, {rpc_call, self(), EncCallBin}).
-spec stop_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. -spec stop_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
stop_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> stop_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) ->
Request = #jsonrpc_request{id = 0, method = <<"stop_container">>, params = #{<<"container_name">> => ContainerName}}, Request = #jsonrpc_request{method = <<"stop_container">>, params = #{<<"container_name">> => ContainerName}},
EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request),
gen_statem:call(Pid, {rpc_call, self(), EncCallBin}). gen_statem:call(Pid, {rpc_call, self(), EncCallBin}).

View File

@ -37,7 +37,7 @@ encode0(#jsonrpc_reply{result = Result, error = undefined}) ->
ResultBin = jiffy:encode(#{<<"result">> => Result}, [force_utf8]), ResultBin = jiffy:encode(#{<<"result">> => Result}, [force_utf8]),
iolist_to_binary([marshal(?Bytes, ResultBin)]); iolist_to_binary([marshal(?Bytes, ResultBin)]);
encode0(#jsonrpc_reply{result = undefined, error = Error}) -> encode0(#jsonrpc_reply{result = undefined, error = Error}) ->
ResultBin = jiffy:encode(#{<<"error">> => Error}, [force_utf8]), ResultBin = iolist_to_binary(jiffy:encode(#{<<"error">> => Error}, [force_utf8])),
iolist_to_binary([marshal(?Bytes, ResultBin)]); iolist_to_binary([marshal(?Bytes, ResultBin)]);
encode0(#pub{topic = Topic, content = Content}) -> encode0(#pub{topic = Topic, content = Content}) ->
iolist_to_binary([ iolist_to_binary([
@ -51,7 +51,7 @@ encode0(#command{command_type = CommandType, command = Command}) ->
]); ]);
encode0(#jsonrpc_request{method = Method, params = Params}) -> encode0(#jsonrpc_request{method = Method, params = Params}) ->
ReqBody = jiffy:encode(#{<<"method">> => Method, <<"params">> => Params}, [force_utf8]), ReqBody = iolist_to_binary(jiffy:encode(#{<<"method">> => Method, <<"params">> => Params}, [force_utf8])),
marshal(?Bytes, ReqBody); marshal(?Bytes, ReqBody);
encode0(#data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}) -> encode0(#data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}) ->
iolist_to_binary([ iolist_to_binary([