From 0da6bb543ca9cdd23bd5649748ddfa19936324ef Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 26 Sep 2025 16:21:27 +0800 Subject: [PATCH] fix --- .../src/http_handlers/container_handler.erl | 19 +++++++++++++++++++ apps/iot/src/iot_host.erl | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/apps/iot/src/http_handlers/container_handler.erl b/apps/iot/src/http_handlers/container_handler.erl index 5de9b9d..208d882 100644 --- a/apps/iot/src/http_handlers/container_handler.erl +++ b/apps/iot/src/http_handlers/container_handler.erl @@ -13,6 +13,25 @@ %% API -export([handle_request/4]). +handle_request("GET", "/container/get_all", #{<<"uuid">> := UUID}, _) when is_binary(UUID) -> + %% 检查ConfigJson是否是合法的json字符串 + case iot_host:get_pid(UUID) of + undefined -> + {ok, 200, iot_util:json_error(-1, <<"host not found">>)}; + Pid when is_pid(Pid) -> + case iot_host:get_containers(Pid) of + {ok, Ref} -> + case iot_host:await_reply(Ref, 10000) of + {ok, Result} -> + {ok, 200, iot_util:json_data(Result)}; + {error, Reason} -> + {ok, 200, iot_util:json_error(-1, Reason)} + end; + {error, Reason} when is_binary(Reason) -> + {ok, 200, iot_util:json_error(-1, Reason)} + end + end; + %% 下发config.json, 微服务接受后,保存服务配置 handle_request("POST", "/container/push_config", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName, <<"config">> := Config, <<"timeout">> := Timeout0}) diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index b65d05c..daefc87 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -25,7 +25,7 @@ -export([get_metric/1, get_status/1]). %% 通讯相关 -export([pub/3, attach_channel/2, command/3]). --export([deploy_container/3, start_container/2, stop_container/2, config_container/3, await_reply/2]). +-export([deploy_container/3, start_container/2, stop_container/2, config_container/3, get_containers/1, await_reply/2]). %% 设备管理 -export([reload_device/2, delete_device/2, activate_device/3]). -export([heartbeat/1]). @@ -89,6 +89,12 @@ get_metric(Pid) when is_pid(Pid) -> attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) -> gen_statem:call(Pid, {attach_channel, ChannelPid}). +-spec get_containers(Pid :: pid()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. +get_containers(Pid) when is_pid(Pid) -> + Request = #jsonrpc_request{method = <<"get_containers">>, params = #{}}, + EncConfigBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), + gen_statem:call(Pid, {rpc_call, self(), EncConfigBin}). + -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) -> Request = #jsonrpc_request{method = <<"config_container">>, params = #{<<"container_name">> => ContainerName, <<"config">> => ConfigJson}},