refactor(mqttbridge): move into separate application
This commit is contained in:
parent
ebd612b194
commit
7e7b50c5ba
|
@ -58,14 +58,14 @@
|
||||||
).
|
).
|
||||||
|
|
||||||
-if(?EMQX_RELEASE_EDITION == ee).
|
-if(?EMQX_RELEASE_EDITION == ee).
|
||||||
bridge_to_resource_type(<<"mqtt">>) -> emqx_connector_mqtt;
|
bridge_to_resource_type(<<"mqtt">>) -> emqx_bridge_mqtt_connector;
|
||||||
bridge_to_resource_type(mqtt) -> emqx_connector_mqtt;
|
bridge_to_resource_type(mqtt) -> emqx_bridge_mqtt_connector;
|
||||||
bridge_to_resource_type(<<"webhook">>) -> emqx_connector_http;
|
bridge_to_resource_type(<<"webhook">>) -> emqx_connector_http;
|
||||||
bridge_to_resource_type(webhook) -> emqx_connector_http;
|
bridge_to_resource_type(webhook) -> emqx_connector_http;
|
||||||
bridge_to_resource_type(BridgeType) -> emqx_ee_bridge:resource_type(BridgeType).
|
bridge_to_resource_type(BridgeType) -> emqx_ee_bridge:resource_type(BridgeType).
|
||||||
-else.
|
-else.
|
||||||
bridge_to_resource_type(<<"mqtt">>) -> emqx_connector_mqtt;
|
bridge_to_resource_type(<<"mqtt">>) -> emqx_bridge_mqtt_connector;
|
||||||
bridge_to_resource_type(mqtt) -> emqx_connector_mqtt;
|
bridge_to_resource_type(mqtt) -> emqx_bridge_mqtt_connector;
|
||||||
bridge_to_resource_type(<<"webhook">>) -> emqx_connector_http;
|
bridge_to_resource_type(<<"webhook">>) -> emqx_connector_http;
|
||||||
bridge_to_resource_type(webhook) -> emqx_connector_http.
|
bridge_to_resource_type(webhook) -> emqx_connector_http.
|
||||||
-endif.
|
-endif.
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
%% -*- mode: erlang -*-
|
||||||
|
{application, emqx_bridge_mqtt, [
|
||||||
|
{description, "EMQX MQTT Broker Bridge"},
|
||||||
|
{vsn, "0.1.0"},
|
||||||
|
{registered, []},
|
||||||
|
{applications, [
|
||||||
|
kernel,
|
||||||
|
stdlib,
|
||||||
|
emqx,
|
||||||
|
emqx_resource,
|
||||||
|
emqx_bridge,
|
||||||
|
emqtt
|
||||||
|
]},
|
||||||
|
{env, []},
|
||||||
|
{modules, []},
|
||||||
|
{licenses, ["Apache 2.0"]},
|
||||||
|
{links, []}
|
||||||
|
]}.
|
|
@ -13,7 +13,7 @@
|
||||||
%% See the License for the specific language governing permissions and
|
%% See the License for the specific language governing permissions and
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-module(emqx_connector_mqtt).
|
-module(emqx_bridge_mqtt_connector).
|
||||||
|
|
||||||
-include_lib("emqx/include/emqx_mqtt.hrl").
|
-include_lib("emqx/include/emqx_mqtt.hrl").
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
@ -83,7 +83,7 @@ start_ingress(ResourceId, Ingress, ClientOpts) ->
|
||||||
{ingress, Ingress},
|
{ingress, Ingress},
|
||||||
{client_opts, ClientOpts}
|
{client_opts, ClientOpts}
|
||||||
],
|
],
|
||||||
case emqx_resource_pool:start(PoolName, emqx_connector_mqtt_ingress, Options) of
|
case emqx_resource_pool:start(PoolName, emqx_bridge_mqtt_ingress, Options) of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, #{ingress_pool_name => PoolName}};
|
{ok, #{ingress_pool_name => PoolName}};
|
||||||
{error, {start_pool_failed, _, Reason}} ->
|
{error, {start_pool_failed, _, Reason}} ->
|
||||||
|
@ -128,11 +128,11 @@ start_egress(ResourceId, Egress, ClientOpts) ->
|
||||||
{pool_size, PoolSize},
|
{pool_size, PoolSize},
|
||||||
{client_opts, ClientOpts}
|
{client_opts, ClientOpts}
|
||||||
],
|
],
|
||||||
case emqx_resource_pool:start(PoolName, emqx_connector_mqtt_egress, Options) of
|
case emqx_resource_pool:start(PoolName, emqx_bridge_mqtt_egress, Options) of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, #{
|
{ok, #{
|
||||||
egress_pool_name => PoolName,
|
egress_pool_name => PoolName,
|
||||||
egress_config => emqx_connector_mqtt_egress:config(Egress)
|
egress_config => emqx_bridge_mqtt_egress:config(Egress)
|
||||||
}};
|
}};
|
||||||
{error, {start_pool_failed, _, Reason}} ->
|
{error, {start_pool_failed, _, Reason}} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
|
@ -197,7 +197,7 @@ on_query_async(ResourceId, {send_message, Msg}, _Callback, #{}) ->
|
||||||
}).
|
}).
|
||||||
|
|
||||||
with_egress_client(ResourceId, Fun, Args) ->
|
with_egress_client(ResourceId, Fun, Args) ->
|
||||||
ecpool:pick_and_do(ResourceId, {emqx_connector_mqtt_egress, Fun, Args}, no_handover).
|
ecpool:pick_and_do(ResourceId, {emqx_bridge_mqtt_egress, Fun, Args}, no_handover).
|
||||||
|
|
||||||
on_async_result(Callback, Result) ->
|
on_async_result(Callback, Result) ->
|
||||||
apply_callback_function(Callback, handle_send_result(Result)).
|
apply_callback_function(Callback, handle_send_result(Result)).
|
||||||
|
@ -250,9 +250,9 @@ on_get_status(_ResourceId, State) ->
|
||||||
get_status({Pool, Worker}) ->
|
get_status({Pool, Worker}) ->
|
||||||
case ecpool_worker:client(Worker) of
|
case ecpool_worker:client(Worker) of
|
||||||
{ok, Client} when Pool == ingress_pool_name ->
|
{ok, Client} when Pool == ingress_pool_name ->
|
||||||
emqx_connector_mqtt_ingress:status(Client);
|
emqx_bridge_mqtt_ingress:status(Client);
|
||||||
{ok, Client} when Pool == egress_pool_name ->
|
{ok, Client} when Pool == egress_pool_name ->
|
||||||
emqx_connector_mqtt_egress:status(Client);
|
emqx_bridge_mqtt_egress:status(Client);
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
disconnected
|
disconnected
|
||||||
end.
|
end.
|
||||||
|
@ -300,7 +300,7 @@ mk_client_opts(
|
||||||
ssl := #{enable := EnableSsl} = Ssl
|
ssl := #{enable := EnableSsl} = Ssl
|
||||||
}
|
}
|
||||||
) ->
|
) ->
|
||||||
HostPort = emqx_connector_mqtt_schema:parse_server(Server),
|
HostPort = emqx_bridge_mqtt_connector_schema:parse_server(Server),
|
||||||
Options = maps:with(
|
Options = maps:with(
|
||||||
[
|
[
|
||||||
proto_ver,
|
proto_ver,
|
|
@ -14,7 +14,7 @@
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-module(emqx_connector_mqtt_schema).
|
-module(emqx_bridge_mqtt_connector_schema).
|
||||||
|
|
||||||
-include_lib("typerefl/include/types.hrl").
|
-include_lib("typerefl/include/types.hrl").
|
||||||
-include_lib("hocon/include/hoconsc.hrl").
|
-include_lib("hocon/include/hoconsc.hrl").
|
||||||
|
@ -138,13 +138,13 @@ fields("ingress") ->
|
||||||
{remote,
|
{remote,
|
||||||
mk(
|
mk(
|
||||||
ref(?MODULE, "ingress_remote"),
|
ref(?MODULE, "ingress_remote"),
|
||||||
#{desc => ?DESC(emqx_connector_mqtt_schema, "ingress_remote")}
|
#{desc => ?DESC("ingress_remote")}
|
||||||
)},
|
)},
|
||||||
{local,
|
{local,
|
||||||
mk(
|
mk(
|
||||||
ref(?MODULE, "ingress_local"),
|
ref(?MODULE, "ingress_local"),
|
||||||
#{
|
#{
|
||||||
desc => ?DESC(emqx_connector_mqtt_schema, "ingress_local")
|
desc => ?DESC("ingress_local")
|
||||||
}
|
}
|
||||||
)}
|
)}
|
||||||
];
|
];
|
||||||
|
@ -211,7 +211,7 @@ fields("egress") ->
|
||||||
mk(
|
mk(
|
||||||
ref(?MODULE, "egress_local"),
|
ref(?MODULE, "egress_local"),
|
||||||
#{
|
#{
|
||||||
desc => ?DESC(emqx_connector_mqtt_schema, "egress_local"),
|
desc => ?DESC("egress_local"),
|
||||||
required => false
|
required => false
|
||||||
}
|
}
|
||||||
)},
|
)},
|
||||||
|
@ -219,7 +219,7 @@ fields("egress") ->
|
||||||
mk(
|
mk(
|
||||||
ref(?MODULE, "egress_remote"),
|
ref(?MODULE, "egress_remote"),
|
||||||
#{
|
#{
|
||||||
desc => ?DESC(emqx_connector_mqtt_schema, "egress_remote"),
|
desc => ?DESC("egress_remote"),
|
||||||
required => true
|
required => true
|
||||||
}
|
}
|
||||||
)}
|
)}
|
|
@ -14,7 +14,7 @@
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-module(emqx_connector_mqtt_egress).
|
-module(emqx_bridge_mqtt_egress).
|
||||||
|
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
-include_lib("emqx/include/emqx.hrl").
|
-include_lib("emqx/include/emqx.hrl").
|
||||||
|
@ -51,7 +51,7 @@
|
||||||
local => #{
|
local => #{
|
||||||
topic => emqx_topic:topic()
|
topic => emqx_topic:topic()
|
||||||
},
|
},
|
||||||
remote := emqx_connector_mqtt_msg:msgvars()
|
remote := emqx_bridge_mqtt_msg:msgvars()
|
||||||
}.
|
}.
|
||||||
|
|
||||||
%% @doc Start an ingress bridge worker.
|
%% @doc Start an ingress bridge worker.
|
||||||
|
@ -102,7 +102,7 @@ connect(Pid, Name) ->
|
||||||
-spec config(map()) ->
|
-spec config(map()) ->
|
||||||
egress().
|
egress().
|
||||||
config(#{remote := RC = #{}} = Conf) ->
|
config(#{remote := RC = #{}} = Conf) ->
|
||||||
Conf#{remote => emqx_connector_mqtt_msg:parse(RC)}.
|
Conf#{remote => emqx_bridge_mqtt_msg:parse(RC)}.
|
||||||
|
|
||||||
-spec send(pid(), message(), egress()) ->
|
-spec send(pid(), message(), egress()) ->
|
||||||
ok.
|
ok.
|
||||||
|
@ -118,7 +118,7 @@ send_async(Pid, MsgIn, Callback, Egress) ->
|
||||||
export_msg(Msg, #{remote := Remote}) ->
|
export_msg(Msg, #{remote := Remote}) ->
|
||||||
to_remote_msg(Msg, Remote).
|
to_remote_msg(Msg, Remote).
|
||||||
|
|
||||||
-spec to_remote_msg(message(), emqx_connector_mqtt_msg:msgvars()) ->
|
-spec to_remote_msg(message(), emqx_bridge_mqtt_msg:msgvars()) ->
|
||||||
remote_message().
|
remote_message().
|
||||||
to_remote_msg(#message{flags = Flags} = Msg, Vars) ->
|
to_remote_msg(#message{flags = Flags} = Msg, Vars) ->
|
||||||
{EventMsg, _} = emqx_rule_events:eventmsg_publish(Msg),
|
{EventMsg, _} = emqx_rule_events:eventmsg_publish(Msg),
|
||||||
|
@ -129,7 +129,7 @@ to_remote_msg(Msg = #{}, Remote) ->
|
||||||
payload := Payload,
|
payload := Payload,
|
||||||
qos := QoS,
|
qos := QoS,
|
||||||
retain := Retain
|
retain := Retain
|
||||||
} = emqx_connector_mqtt_msg:render(Msg, Remote),
|
} = emqx_bridge_mqtt_msg:render(Msg, Remote),
|
||||||
PubProps = maps:get(pub_props, Msg, #{}),
|
PubProps = maps:get(pub_props, Msg, #{}),
|
||||||
#mqtt_msg{
|
#mqtt_msg{
|
||||||
qos = QoS,
|
qos = QoS,
|
|
@ -14,7 +14,7 @@
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-module(emqx_connector_mqtt_ingress).
|
-module(emqx_bridge_mqtt_ingress).
|
||||||
|
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@
|
||||||
topic := emqx_topic:topic(),
|
topic := emqx_topic:topic(),
|
||||||
qos => emqx_types:qos()
|
qos => emqx_types:qos()
|
||||||
},
|
},
|
||||||
local := emqx_connector_mqtt_msg:msgvars(),
|
local := emqx_bridge_mqtt_msg:msgvars(),
|
||||||
on_message_received := {module(), atom(), [term()]}
|
on_message_received := {module(), atom(), [term()]}
|
||||||
}.
|
}.
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ subscribe_remote_topic(Pid, #{remote := #{topic := RemoteTopic, qos := QoS}}) ->
|
||||||
config(#{remote := RC, local := LC} = Conf, BridgeName) ->
|
config(#{remote := RC, local := LC} = Conf, BridgeName) ->
|
||||||
Conf#{
|
Conf#{
|
||||||
remote => parse_remote(RC, BridgeName),
|
remote => parse_remote(RC, BridgeName),
|
||||||
local => emqx_connector_mqtt_msg:parse(LC)
|
local => emqx_bridge_mqtt_msg:parse(LC)
|
||||||
}.
|
}.
|
||||||
|
|
||||||
parse_remote(#{qos := QoSIn} = Conf, BridgeName) ->
|
parse_remote(#{qos := QoSIn} = Conf, BridgeName) ->
|
||||||
|
@ -261,7 +261,7 @@ to_broker_msg(#{dup := Dup} = Msg, Local, Props) ->
|
||||||
payload := Payload,
|
payload := Payload,
|
||||||
qos := QoS,
|
qos := QoS,
|
||||||
retain := Retain
|
retain := Retain
|
||||||
} = emqx_connector_mqtt_msg:render(Msg, Local),
|
} = emqx_bridge_mqtt_msg:render(Msg, Local),
|
||||||
PubProps = maps:get(pub_props, Msg, #{}),
|
PubProps = maps:get(pub_props, Msg, #{}),
|
||||||
emqx_message:set_headers(
|
emqx_message:set_headers(
|
||||||
Props#{properties => emqx_utils:pub_props_to_packet(PubProps)},
|
Props#{properties => emqx_utils:pub_props_to_packet(PubProps)},
|
|
@ -14,7 +14,7 @@
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-module(emqx_connector_mqtt_msg).
|
-module(emqx_bridge_mqtt_msg).
|
||||||
|
|
||||||
-export([parse/1]).
|
-export([parse/1]).
|
||||||
-export([render/2]).
|
-export([render/2]).
|
|
@ -42,7 +42,7 @@ fields("config") ->
|
||||||
}
|
}
|
||||||
)}
|
)}
|
||||||
] ++
|
] ++
|
||||||
emqx_connector_mqtt_schema:fields("config");
|
emqx_bridge_mqtt_connector_schema:fields("config");
|
||||||
fields("creation_opts") ->
|
fields("creation_opts") ->
|
||||||
Opts = emqx_resource_schema:fields("creation_opts"),
|
Opts = emqx_resource_schema:fields("creation_opts"),
|
||||||
[O || {Field, _} = O <- Opts, not is_hidden_opts(Field)];
|
[O || {Field, _} = O <- Opts, not is_hidden_opts(Field)];
|
|
@ -130,13 +130,11 @@ suite() ->
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
_ = application:load(emqx_conf),
|
_ = application:load(emqx_conf),
|
||||||
%% some testcases (may from other app) already get emqx_connector started
|
|
||||||
_ = application:stop(emqx_resource),
|
|
||||||
_ = application:stop(emqx_connector),
|
|
||||||
ok = emqx_common_test_helpers:start_apps(
|
ok = emqx_common_test_helpers:start_apps(
|
||||||
[
|
[
|
||||||
emqx_rule_engine,
|
emqx_rule_engine,
|
||||||
emqx_bridge,
|
emqx_bridge,
|
||||||
|
emqx_bridge_mqtt,
|
||||||
emqx_dashboard
|
emqx_dashboard
|
||||||
],
|
],
|
||||||
fun set_special_configs/1
|
fun set_special_configs/1
|
||||||
|
@ -150,9 +148,10 @@ init_per_suite(Config) ->
|
||||||
|
|
||||||
end_per_suite(_Config) ->
|
end_per_suite(_Config) ->
|
||||||
emqx_common_test_helpers:stop_apps([
|
emqx_common_test_helpers:stop_apps([
|
||||||
emqx_rule_engine,
|
emqx_dashboard,
|
||||||
|
emqx_bridge_mqtt,
|
||||||
emqx_bridge,
|
emqx_bridge,
|
||||||
emqx_dashboard
|
emqx_rule_engine
|
||||||
]),
|
]),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
@ -307,7 +306,7 @@ t_mqtt_egress_bridge_ignores_clean_start(_) ->
|
||||||
emqx_resource_manager:lookup_cached(ResourceID),
|
emqx_resource_manager:lookup_cached(ResourceID),
|
||||||
ClientInfo = ecpool:pick_and_do(
|
ClientInfo = ecpool:pick_and_do(
|
||||||
EgressPoolName,
|
EgressPoolName,
|
||||||
{emqx_connector_mqtt_egress, info, []},
|
{emqx_bridge_mqtt_egress, info, []},
|
||||||
no_handover
|
no_handover
|
||||||
),
|
),
|
||||||
?assertMatch(
|
?assertMatch(
|
1
mix.exs
1
mix.exs
|
@ -372,6 +372,7 @@ defmodule EMQXUmbrella.MixProject do
|
||||||
emqx_gateway_exproto: :permanent,
|
emqx_gateway_exproto: :permanent,
|
||||||
emqx_exhook: :permanent,
|
emqx_exhook: :permanent,
|
||||||
emqx_bridge: :permanent,
|
emqx_bridge: :permanent,
|
||||||
|
emqx_bridge_mqtt: :permanent,
|
||||||
emqx_rule_engine: :permanent,
|
emqx_rule_engine: :permanent,
|
||||||
emqx_modules: :permanent,
|
emqx_modules: :permanent,
|
||||||
emqx_management: :permanent,
|
emqx_management: :permanent,
|
||||||
|
|
|
@ -426,6 +426,7 @@ relx_apps(ReleaseType, Edition) ->
|
||||||
emqx_gateway_exproto,
|
emqx_gateway_exproto,
|
||||||
emqx_exhook,
|
emqx_exhook,
|
||||||
emqx_bridge,
|
emqx_bridge,
|
||||||
|
emqx_bridge_mqtt,
|
||||||
emqx_rule_engine,
|
emqx_rule_engine,
|
||||||
emqx_modules,
|
emqx_modules,
|
||||||
emqx_management,
|
emqx_management,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
emqx_connector_mqtt_schema {
|
emqx_bridge_mqtt_connector_schema {
|
||||||
|
|
||||||
bridge_mode.desc:
|
bridge_mode.desc:
|
||||||
"""If enable bridge mode.
|
"""If enable bridge mode.
|
Loading…
Reference in New Issue