This commit is contained in:
anlicheng 2023-06-20 17:49:01 +08:00
parent d9fbd8b55f
commit 50ee83feba
4 changed files with 210 additions and 67 deletions

View File

@ -113,8 +113,7 @@ handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> :=
{ok, 200, iot_util:json_error(400, <<"host not found">>)}; {ok, 200, iot_util:json_error(400, <<"host not found">>)};
{ok, Pid} when is_pid(Pid) -> {ok, Pid} when is_pid(Pid) ->
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]), lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
{ok, Assoc} = iot_host:make_assoc(Pid), {ok, Assoc, ReplyTopic} = iot_mqtt_reply_subscriber:make_assoc(UUID),
ReplyTopic = iot_host:upstream_topic(UUID),
BinReply = jiffy:encode(#{<<"auth">> => true, <<"reply">> => #{<<"topic">> => ReplyTopic, <<"assoc">> => Assoc}}, [force_utf8]), BinReply = jiffy:encode(#{<<"auth">> => true, <<"reply">> => #{<<"topic">> => ReplyTopic, <<"assoc">> => Assoc}}, [force_utf8]),
case iot_mqtt_publisher:publish(iot_host:downstream_topic(UUID), <<8:8, BinReply/binary>>, 2) of case iot_mqtt_publisher:publish(iot_host:downstream_topic(UUID), <<8:8, BinReply/binary>>, 2) of
@ -151,8 +150,7 @@ handle_request("POST", "/host/activate", _, #{<<"uuid">> := UUID, <<"auth">> :=
case iot_host:has_session(Pid) of case iot_host:has_session(Pid) of
true -> true ->
lager:debug("[host_handler] activate host_id: ~p, start", [UUID]), lager:debug("[host_handler] activate host_id: ~p, start", [UUID]),
{ok, Assoc} = iot_host:make_assoc(Pid), {ok, Assoc, ReplyTopic} = iot_mqtt_reply_subscriber:make_assoc(UUID),
ReplyTopic = iot_host:upstream_topic(UUID),
BinReply = jiffy:encode(#{<<"auth">> => false, <<"reply">> => #{<<"topic">> => ReplyTopic, <<"assoc">> => Assoc}}, [force_utf8]), BinReply = jiffy:encode(#{<<"auth">> => false, <<"reply">> => #{<<"topic">> => ReplyTopic, <<"assoc">> => Assoc}}, [force_utf8]),
case iot_mqtt_publisher:publish(iot_host:downstream_topic(UUID), <<8:8, BinReply/binary>>, 2) of case iot_mqtt_publisher:publish(iot_host:downstream_topic(UUID), <<8:8, BinReply/binary>>, 2) of

View File

@ -14,7 +14,7 @@
%% API %% API
-export([start_link/2, get_name/1, get_pid/1, handle/2, reload/1, activate/2]). -export([start_link/2, get_name/1, get_pid/1, handle/2, reload/1, activate/2]).
-export([get_metric/1, aes_encode/3, downstream_topic/1, upstream_topic/1, get_aes/1, make_assoc/1, rsa_encode/3]). -export([get_metric/1, aes_encode/3, downstream_topic/1, upstream_topic/1, get_aes/1, rsa_encode/3]).
-export([has_session/1]). -export([has_session/1]).
%% gen_statem callbacks %% gen_statem callbacks
@ -41,12 +41,7 @@
metrics = #{} :: map(), metrics = #{} :: map(),
%% ping请求 %% ping请求
is_answered = false :: boolean(), is_answered = false :: boolean()
%% id
increment_id = 1,
%%
assoc_map = #{}
}). }).
%%%=================================================================== %%%===================================================================
@ -84,10 +79,6 @@ reload(Pid) when is_pid(Pid) ->
get_aes(Pid) when is_pid(Pid) -> get_aes(Pid) when is_pid(Pid) ->
gen_statem:call(Pid, get_aes). gen_statem:call(Pid, get_aes).
-spec make_assoc(Pid :: pid()) -> {ok, Assoc :: binary()}.
make_assoc(Pid) when is_pid(Pid) ->
gen_statem:call(Pid, {make_assoc, self()}).
%% , true false表示关闭激活 %% , true false表示关闭激活
-spec activate(Pid :: pid(), Auth :: boolean()) -> ok. -spec activate(Pid :: pid(), Auth :: boolean()) -> ok.
activate(Pid, Auth) when is_pid(Pid), is_boolean(Auth) -> activate(Pid, Auth) when is_pid(Pid), is_boolean(Auth) ->
@ -178,12 +169,6 @@ handle_event({call, From}, has_session, StateName, State) ->
HasSession = (StateName =:= session) orelse false, HasSession = (StateName =:= session) orelse false,
{keep_state, State, [{reply, From, HasSession}]}; {keep_state, State, [{reply, From, HasSession}]};
handle_event({call, From}, {make_assoc, ReceiverPid}, _, State = #state{uuid = UUID, increment_id = IncrementId, assoc_map = AssocMap}) ->
BinIncrementId = erlang:integer_to_binary(IncrementId),
Assoc = <<UUID/binary, ":assoc:", BinIncrementId/binary>>,
{keep_state, State#state{assoc_map = maps:put(Assoc, ReceiverPid, AssocMap), increment_id = IncrementId + 1}, [{reply, From, {ok, Assoc}}]};
%% rsa加密 %% rsa加密
handle_event({call, From}, {rsa_encode, CommandType, PlainText}, session, State = #state{pub_key = PubKey}) -> handle_event({call, From}, {rsa_encode, CommandType, PlainText}, session, State = #state{pub_key = PubKey}) ->
Reply = iot_cipher_rsa:encode(PlainText, PubKey), Reply = iot_cipher_rsa:encode(PlainText, PubKey),
@ -237,45 +222,33 @@ handle_event(cast, need_auth, _StateName, State = #state{uuid = UUID}) ->
%% json格式然后再处理, host进程里面处理 %% json格式然后再处理, host进程里面处理
%% %%
handle_event(cast, {handle, <<?METHOD_CREATE_SESSION:8, Params/binary>>}, denied, State = #state{uuid = UUID}) -> handle_event(cast, {handle, <<?METHOD_CREATE_SESSION:8, PubKey/binary>>}, denied, State = #state{uuid = UUID}) ->
case catch jiffy:decode(Params, [return_maps]) of lager:debug("[iot_host] host_id uuid: ~p, create_session", [UUID]),
#{<<"pub_key">> := PubKey} -> Reply = #{<<"a">> => false, <<"aes">> => <<"">>},
lager:debug("[iot_host] host_id uuid: ~p, create_session", [UUID]), EncReply = iot_cipher_rsa:encode(Reply, PubKey),
Reply = #{<<"a">> => false, <<"aes">> => <<"">>},
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
{ok, Ref} = iot_mqtt_publisher:publish(downstream_topic(UUID), <<10:8, EncReply/binary>>, 1), {ok, Ref} = iot_mqtt_publisher:publish(downstream_topic(UUID), <<10:8, EncReply/binary>>, 1),
receive receive
{ok, Ref, PacketId} -> {ok, Ref, PacketId} ->
lager:debug("[iot_host] host_id uuid: ~p, packet_id: ~p, publish register reply success", [UUID, PacketId]) lager:debug("[iot_host] host_id uuid: ~p, packet_id: ~p, publish register reply success", [UUID, PacketId])
after 10000 -> after 10000 ->
lager:debug("[iot_host] host_id uuid: ~p, publish register reply get error is: timeout", [UUID]) lager:debug("[iot_host] host_id uuid: ~p, publish register reply get error is: timeout", [UUID])
end, end,
{keep_state, State#state{is_answered = true}}; {keep_state, State#state{is_answered = true}};
Msg ->
lager:debug("[iot_host] host_id uuid: ~p, publish register reply get error: ~p", [UUID, Msg]),
{keep_state, State}
end;
handle_event(cast, {handle, <<?METHOD_CREATE_SESSION:8, Params/binary>>}, _StateName, State = #state{uuid = UUID, aes = Aes}) -> handle_event(cast, {handle, <<?METHOD_CREATE_SESSION:8, PubKey/binary>>}, _StateName, State = #state{uuid = UUID, aes = Aes}) ->
case catch jiffy:decode(Params, [return_maps]) of lager:debug("[iot_host] host_id uuid: ~p, create_session", [UUID]),
#{<<"pub_key">> := PubKey} -> Reply = #{<<"a">> => true, <<"aes">> => Aes},
lager:debug("[iot_host] host_id uuid: ~p, create_session", [UUID]), EncReply = iot_cipher_rsa:encode(Reply, PubKey),
Reply = #{<<"a">> => true, <<"aes">> => Aes},
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
{ok, Ref} = iot_mqtt_publisher:publish(downstream_topic(UUID), <<10:8, EncReply/binary>>, 1), {ok, Ref} = iot_mqtt_publisher:publish(downstream_topic(UUID), <<10:8, EncReply/binary>>, 1),
receive receive
{ok, Ref, PacketId} -> {ok, Ref, PacketId} ->
lager:debug("[iot_host] host_id uuid: ~p, packet_id: ~p, publish register reply success", [UUID, PacketId]), lager:debug("[iot_host] host_id uuid: ~p, packet_id: ~p, publish register reply success", [UUID, PacketId]),
{next_state, session, State#state{pub_key = PubKey, is_answered = true}} {next_state, session, State#state{pub_key = PubKey, is_answered = true}}
after 10000 -> after 10000 ->
lager:debug("[iot_host] host_id uuid: ~p, publish register reply get error is: timeout", [UUID]), lager:debug("[iot_host] host_id uuid: ~p, publish register reply get error is: timeout", [UUID]),
{keep_state, State#state{is_answered = true}} {keep_state, State#state{is_answered = true}}
end;
Msg ->
lager:debug("[iot_host] host_id uuid: ~p, publish register reply get error: ~p", [UUID, Msg]),
{keep_state, State}
end; end;
handle_event(cast, {handle, <<?METHOD_DATA:8, Data/binary>>}, session, State = #state{uuid = UUID, aes = AES}) -> handle_event(cast, {handle, <<?METHOD_DATA:8, Data/binary>>}, session, State = #state{uuid = UUID, aes = AES}) ->
@ -355,16 +328,6 @@ handle_event(cast, {handle, <<?METHOD_FEEDBACK_RESULT:8, Info0/binary>>}, sessio
end, end,
{keep_state, State}; {keep_state, State};
%% , : {"code": 0|1, "message": "", "assoc": string}
handle_event(cast, {handle_message, Msg = #{<<"code">> := _Code, <<"assoc">> := Assoc}}, _, State = #state{assoc_map = AssocMap}) ->
case maps:take(Assoc, AssocMap) of
error ->
{keep_state, State};
{ReceiverPid, NAssocMap} ->
ReceiverPid ! {host_reply, Assoc, Msg},
{keep_state, State#state{assoc_map = NAssocMap}}
end;
handle_event(info, {timeout, _, ping_ticker}, _StateName, State = #state{uuid = UUID, is_answered = IsAnswered, status = Status}) -> handle_event(info, {timeout, _, ping_ticker}, _StateName, State = #state{uuid = UUID, is_answered = IsAnswered, status = Status}) ->
erlang:start_timer(?TICKER_INTERVAL, self(), ping_ticker), erlang:start_timer(?TICKER_INTERVAL, self(), ping_ticker),
%% : keep_status %% : keep_status

View File

@ -0,0 +1,173 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%% 1.
%%% 2. host进程不能直接去监听topic线
%%% @end
%%% Created : 12. 3 2023 21:27
%%%-------------------------------------------------------------------
-module(iot_mqtt_reply_subscriber).
-author("aresei").
-include("iot.hrl").
-behaviour(gen_server).
%% API
-export([start_link/0, make_assoc/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
%%
-define(Topic, <<"system/assoc_reply">>).
-record(state, {
conn_pid :: pid(),
%%
inflight = #{} :: map(),
%%
assoc_map = #{}
}).
%%%===================================================================
%%% API
%%%===================================================================
-spec make_assoc(UUID :: binary()) -> {ok, Assoc :: binary(), Topic :: binary()}.
make_assoc(UUID) when is_binary(UUID) ->
gen_server:call(?MODULE, {make_assoc, UUID, self()}).
%% @doc Spawns the server and registers the local name (unique)
-spec(start_link() ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%% @private
%% @doc Initializes the server
-spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
%% emqx服务器的连接
Opts = iot_config:emqt_opts(<<"assoc-reply-subscriber">>),
{ok, ConnPid} = emqtt:start_link(Opts),
%% host相关的全部事件
{ok, _} = emqtt:connect(ConnPid),
lager:debug("[iot_mqtt_reply_subscriber] connect success, pid: ~p", [ConnPid]),
SubscribeResult = emqtt:subscribe(ConnPid, {?Topic, 1}),
lager:debug("[iot_mqtt_reply_subscriber] subscribe topics: ~p, result is: ~p", [?Topic, SubscribeResult]),
{ok, #state{conn_pid = ConnPid}}.
%% @private
%% @doc Handling call messages
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #state{}) ->
{reply, Reply :: term(), NewState :: #state{}} |
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call({make_assoc, UUID, ReceiverPid}, _From, State = #state{conn_pid = _ConnPid, assoc_map = AssocMap}) ->
Rand = list_to_binary(iot_util:rand_bytes(16)),
Assoc = <<UUID/binary, ":assoc:", Rand/binary>>,
{reply, {ok, Assoc, ?Topic}, State#state{assoc_map = maps:put(Assoc, ReceiverPid, AssocMap)}}.
%% @private
%% @doc Handling cast messages
-spec(handle_cast(Request :: term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast(_Request, State = #state{}) ->
{noreply, State}.
%% @private
%% @doc Handling all non call/cast messages
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_info({disconnected, ReasonCode, Properties}, State = #state{}) ->
lager:debug("[iot_mqtt_reply_subscriber] Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [ReasonCode, Properties]),
{stop, disconnected, State};
%% json反序列需要在host进程进行
handle_info({publish, #{payload := Payload, qos := Qos}}, State = #state{assoc_map = AssocMap}) ->
lager:debug("[iot_mqtt_reply_subscriber] Recv a reply packet: ~p, qos: ~p", [Payload, Qos]),
%% , : {"code": 0|1, "message": "", "assoc": string}
case catch jiffy:decode(Payload, [return_maps]) of
Msg = #{<<"code">> := _Code, <<"assoc">> := Assoc} ->
case maps:take(Assoc, AssocMap) of
error ->
{noreply, State};
{ReceiverPid, NAssocMap} ->
ReceiverPid ! {host_reply, Assoc, Msg},
{noreply, State#state{assoc_map = NAssocMap}}
end;
_ ->
{noreply, State}
end;
handle_info({puback, Packet = #{packet_id := _PacketId}}, State = #state{}) ->
lager:debug("[iot_mqtt_reply_subscriber] receive puback packet: ~p", [Packet]),
{noreply, State};
%%
handle_info({ok, Ref, _PacketId}, State = #state{inflight = Inflight}) ->
case maps:take(Ref, Inflight) of
error ->
{noreply, State};
{{UUID, Msg}, NInflight} ->
lager:debug("[iot_mqtt_reply_subscriber] send message: ~p, to uuid: ~p, success", [Msg, UUID]),
{noreply, State#state{inflight = NInflight}}
end;
handle_info(Info, State = #state{}) ->
lager:debug("[iot_mqtt_reply_subscriber] get info: ~p", [Info]),
{noreply, State}.
%% @private
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #state{}) -> term()).
terminate(Reason, _State = #state{conn_pid = ConnPid}) when is_pid(ConnPid) ->
%% topic的订阅
TopicNames = lists:map(fun({Name, _}) -> Name end, ?Topics),
{ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, TopicNames),
ok = emqtt:disconnect(ConnPid),
lager:debug("[iot_mqtt_reply_subscriber] terminate with reason: ~p", [Reason]),
ok;
terminate(Reason, _State) ->
lager:debug("[iot_mqtt_reply_subscriber] terminate with reason: ~p", [Reason]),
ok.
%% @private
%% @doc Convert process state when code is changed
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
Extra :: term()) ->
{ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #state{}, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================

View File

@ -39,6 +39,15 @@ init([]) ->
modules => ['iot_mqtt_subscriber'] modules => ['iot_mqtt_subscriber']
}, },
#{
id => 'iot_mqtt_reply_subscriber',
start => {'iot_mqtt_reply_subscriber', start_link, []},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['iot_mqtt_reply_subscriber']
},
#{ #{
id => 'iot_mqtt_sys_subscriber', id => 'iot_mqtt_sys_subscriber',
start => {'iot_mqtt_sys_subscriber', start_link, []}, start => {'iot_mqtt_sys_subscriber', start_link, []},