This commit is contained in:
anlicheng 2026-04-24 15:46:59 +08:00
parent 611b325cd4
commit e93c0fbcfd

View File

@ -10,7 +10,6 @@
-author("aresei"). -author("aresei").
-include("iot.hrl"). -include("iot.hrl").
-include("domain_model.hrl"). -include("domain_model.hrl").
-include("protocol.hrl").
-include("message_pb.hrl"). -include("message_pb.hrl").
-behaviour(gen_statem). -behaviour(gen_statem).
@ -26,7 +25,7 @@
-export([start_link/2, get_name/1, get_alias_name/1, get_pid/1, handle/2, activate/2]). -export([start_link/2, get_name/1, get_alias_name/1, get_pid/1, handle/2, activate/2]).
-export([get_metric/1, get_status/1, kill/1]). -export([get_metric/1, get_status/1, kill/1]).
%% %%
-export([pub/4, attach_channel/2, command/3]). -export([pub/4, attach_channel/2, command/2]).
-export([deploy_container/3, start_container/2, stop_container/2, remove_container/2, kill_container/2, config_container/3, get_containers/1, await_reply/3]). -export([deploy_container/3, start_container/2, stop_container/2, remove_container/2, kill_container/2, config_container/3, get_containers/1, await_reply/3]).
-export([heartbeat/1]). -export([heartbeat/1]).
@ -145,9 +144,9 @@ await_reply(Pid, Ref, Timeout) when is_pid(Pid), is_reference(Ref), is_integer(T
pub(Pid, Topic, Qos, Content) when is_pid(Pid), is_binary(Topic), is_integer(Qos), is_binary(Content) -> pub(Pid, Topic, Qos, Content) when is_pid(Pid), is_binary(Topic), is_integer(Qos), is_binary(Content) ->
gen_statem:call(Pid, {pub, Topic, Qos, Content}). gen_statem:call(Pid, {pub, Topic, Qos, Content}).
-spec command(Pid :: pid(), CommandType :: integer(), Command :: binary()) -> ok | {error, Reason :: any()}. -spec command(Pid :: pid(), Command :: message_pb:'CastFrame.Command'()) -> ok | {error, Reason :: any()}.
command(Pid, CommandType, Command) when is_pid(Pid), is_integer(CommandType), is_binary(Command) -> command(Pid, Command) when is_pid(Pid) ->
gen_statem:call(Pid, {command, CommandType, Command}). gen_statem:call(Pid, {command, Command}).
-spec heartbeat(Pid :: pid()) -> no_return(). -spec heartbeat(Pid :: pid()) -> no_return().
heartbeat(undefined) -> heartbeat(undefined) ->
@ -249,15 +248,15 @@ handle_event({call, From}, {pub, Topic, Qos, Content}, ?STATE_ACTIVATED, State =
end; end;
%% %%
handle_event({call, From}, {command, CommandType, Command}, ?STATE_ACTIVATED, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) -> handle_event({call, From}, {command, Command}, ?STATE_ACTIVATED, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) ->
case HasSession andalso is_pid(ChannelPid) of case HasSession andalso is_pid(ChannelPid) of
true -> true ->
logger:debug("[iot_host] host: ~p, command_type: ~p, command: ~p", [UUID, CommandType, Command]), logger:debug("[iot_host] host: ~p, command: ~p", [UUID, Command]),
%% websocket发送请求 %% websocket发送请求
ssl_channel:command(ChannelPid, CommandType, Command), ssl_channel:command(ChannelPid, Command),
{keep_state, State, [{reply, From, ok}]}; {keep_state, State, [{reply, From, ok}]};
false -> false ->
logger:debug("[iot_host] host: ~p, command_type: ~p, command: ~p, invalid state: ~p", [UUID, CommandType, Command, state_map(State)]), logger:debug("[iot_host] host: ~p, command: ~p, invalid state: ~p", [UUID, Command, state_map(State)]),
{keep_state, State, [{reply, From, {error, <<"主机离线,发送指令失败"/utf8>>}}]} {keep_state, State, [{reply, From, {error, <<"主机离线,发送指令失败"/utf8>>}}]}
end; end;