diff --git a/src/host/iot_host.erl b/src/host/iot_host.erl index 503f283..25c7a8e 100644 --- a/src/host/iot_host.erl +++ b/src/host/iot_host.erl @@ -10,7 +10,6 @@ -author("aresei"). -include("iot.hrl"). -include("domain_model.hrl"). --include("protocol.hrl"). -include("message_pb.hrl"). -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([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([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) -> gen_statem:call(Pid, {pub, Topic, Qos, Content}). --spec command(Pid :: pid(), CommandType :: integer(), Command :: binary()) -> ok | {error, Reason :: any()}. -command(Pid, CommandType, Command) when is_pid(Pid), is_integer(CommandType), is_binary(Command) -> - gen_statem:call(Pid, {command, CommandType, Command}). +-spec command(Pid :: pid(), Command :: message_pb:'CastFrame.Command'()) -> ok | {error, Reason :: any()}. +command(Pid, Command) when is_pid(Pid) -> + gen_statem:call(Pid, {command, Command}). -spec heartbeat(Pid :: pid()) -> no_return(). heartbeat(undefined) -> @@ -249,15 +248,15 @@ handle_event({call, From}, {pub, Topic, Qos, Content}, ?STATE_ACTIVATED, State = 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 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发送请求 - ssl_channel:command(ChannelPid, CommandType, Command), + ssl_channel:command(ChannelPid, Command), {keep_state, State, [{reply, From, ok}]}; 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>>}}]} end;