|
|
@@ -17,6 +17,7 @@
|
|
|
|
|
|
-include_lib("typerefl/include/types.hrl").
|
|
|
-include_lib("emqx_resource/include/emqx_resource_behaviour.hrl").
|
|
|
+-include_lib("emqx/include/logger.hrl").
|
|
|
|
|
|
-behaviour(supervisor).
|
|
|
|
|
|
@@ -88,13 +89,14 @@ drop_bridge(Name) ->
|
|
|
%% ===================================================================
|
|
|
%% When use this bridge as a data source, ?MODULE:on_message_received/2 will be called
|
|
|
%% if the bridge received msgs from the remote broker.
|
|
|
-on_message_received(Msg, ChannelName) ->
|
|
|
- Name = atom_to_binary(ChannelName, utf8),
|
|
|
+on_message_received(Msg, ChannId) ->
|
|
|
+ Name = atom_to_binary(ChannId, utf8),
|
|
|
emqx:run_hook(<<"$bridges/", Name/binary>>, [Msg]).
|
|
|
|
|
|
%% ===================================================================
|
|
|
on_start(InstId, Conf) ->
|
|
|
- logger:info("starting mqtt connector: ~p, ~p", [InstId, Conf]),
|
|
|
+ ?SLOG(info, #{msg => "starting mqtt connector",
|
|
|
+ connector => InstId, config => Conf}),
|
|
|
"bridge:" ++ NamePrefix = binary_to_list(InstId),
|
|
|
BasicConf = basic_config(Conf),
|
|
|
InitRes = {ok, #{name_prefix => NamePrefix, baisc_conf => BasicConf, channels => []}},
|
|
|
@@ -111,7 +113,8 @@ on_start(InstId, Conf) ->
|
|
|
end, InitRes, InOutConfigs).
|
|
|
|
|
|
on_stop(InstId, #{channels := NameList}) ->
|
|
|
- logger:info("stopping mqtt connector: ~p", [InstId]),
|
|
|
+ ?SLOG(info, #{msg => "stopping mqtt connector",
|
|
|
+ connector => InstId}),
|
|
|
lists:foreach(fun(Name) ->
|
|
|
remove_channel(Name)
|
|
|
end, NameList).
|
|
|
@@ -122,7 +125,8 @@ on_query(_InstId, {create_channel, Conf}, _AfterQuery, #{name_prefix := Prefix,
|
|
|
baisc_conf := BasicConf}) ->
|
|
|
create_channel(Conf, Prefix, BasicConf);
|
|
|
on_query(_InstId, {send_message, ChannelId, Msg}, _AfterQuery, _State) ->
|
|
|
- logger:debug("send msg to remote node on channel: ~p, msg: ~p", [ChannelId, Msg]),
|
|
|
+ ?SLOG(debug, #{msg => "send msg to remote node", message => Msg,
|
|
|
+ channel_id => ChannelId}),
|
|
|
emqx_connector_mqtt_worker:send_to_remote(ChannelId, Msg).
|
|
|
|
|
|
on_health_check(_InstId, #{channels := NameList} = State) ->
|
|
|
@@ -135,35 +139,43 @@ on_health_check(_InstId, #{channels := NameList} = State) ->
|
|
|
create_channel({{ingress_channels, Id}, #{subscribe_remote_topic := RemoteT} = Conf},
|
|
|
NamePrefix, BasicConf) ->
|
|
|
LocalT = maps:get(local_topic, Conf, undefined),
|
|
|
- Name = ingress_channel_name(NamePrefix, Id),
|
|
|
- logger:info("creating ingress channel ~p, remote ~s -> local ~s", [Name, RemoteT, LocalT]),
|
|
|
+ ChannId = ingress_channel_id(NamePrefix, Id),
|
|
|
+ ?SLOG(info, #{msg => "creating ingress channel",
|
|
|
+ remote_topic => RemoteT,
|
|
|
+ local_topic => LocalT,
|
|
|
+ channel_id => ChannId}),
|
|
|
do_create_channel(BasicConf#{
|
|
|
- name => Name,
|
|
|
- clientid => clientid(Name),
|
|
|
+ name => ChannId,
|
|
|
+ clientid => clientid(ChannId),
|
|
|
subscriptions => Conf#{
|
|
|
local_topic => LocalT,
|
|
|
- on_message_received => {fun ?MODULE:on_message_received/2, [Name]}
|
|
|
+ on_message_received => {fun ?MODULE:on_message_received/2, [ChannId]}
|
|
|
},
|
|
|
forwards => undefined});
|
|
|
|
|
|
create_channel({{egress_channels, Id}, #{remote_topic := RemoteT} = Conf},
|
|
|
NamePrefix, BasicConf) ->
|
|
|
LocalT = maps:get(subscribe_local_topic, Conf, undefined),
|
|
|
- Name = egress_channel_name(NamePrefix, Id),
|
|
|
- logger:info("creating egress channel ~p, local ~s -> remote ~s", [Name, LocalT, RemoteT]),
|
|
|
+ ChannId = egress_channel_id(NamePrefix, Id),
|
|
|
+ ?SLOG(info, #{msg => "creating egress channel",
|
|
|
+ remote_topic => RemoteT,
|
|
|
+ local_topic => LocalT,
|
|
|
+ channel_id => ChannId}),
|
|
|
do_create_channel(BasicConf#{
|
|
|
- name => Name,
|
|
|
- clientid => clientid(Name),
|
|
|
+ name => ChannId,
|
|
|
+ clientid => clientid(ChannId),
|
|
|
subscriptions => undefined,
|
|
|
forwards => Conf#{subscribe_local_topic => LocalT}}).
|
|
|
|
|
|
-remove_channel(ChannelName) ->
|
|
|
- logger:info("removing channel ~p", [ChannelName]),
|
|
|
- case ?MODULE:drop_bridge(ChannelName) of
|
|
|
+remove_channel(ChannId) ->
|
|
|
+ ?SLOG(info, #{msg => "removing channel",
|
|
|
+ channel_id => ChannId}),
|
|
|
+ case ?MODULE:drop_bridge(ChannId) of
|
|
|
ok -> ok;
|
|
|
{error, not_found} -> ok;
|
|
|
{error, Reason} ->
|
|
|
- logger:error("stop channel ~p failed, error: ~p", [ChannelName, Reason])
|
|
|
+ ?SLOG(error, #{msg => "stop channel failed",
|
|
|
+ channel_id => ChannId, reason => Reason})
|
|
|
end.
|
|
|
|
|
|
do_create_channel(#{name := Name} = Conf) ->
|
|
|
@@ -216,9 +228,9 @@ basic_config(#{
|
|
|
taged_map_list(Tag, Map) ->
|
|
|
[{{Tag, K}, V} || {K, V} <- maps:to_list(Map)].
|
|
|
|
|
|
-ingress_channel_name(Prefix, Id) ->
|
|
|
+ingress_channel_id(Prefix, Id) ->
|
|
|
channel_name("ingress_channels", Prefix, Id).
|
|
|
-egress_channel_name(Prefix, Id) ->
|
|
|
+egress_channel_id(Prefix, Id) ->
|
|
|
channel_name("egress_channels", Prefix, Id).
|
|
|
|
|
|
channel_name(Type, Prefix, Id) ->
|