From 237d3c0543819b9b13895261919707b7d070ead4 Mon Sep 17 00:00:00 2001 From: DDDHuang <44492639+DDDHuang@users.noreply.github.com> Date: Wed, 15 Sep 2021 11:40:05 +0800 Subject: [PATCH] Auto subscribe format (#5740) * fix: auto sub list format with default --- .../etc/emqx_auto_subscribe.conf | 46 +++++++++---------- .../src/emqx_auto_subscribe.erl | 13 +++++- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/apps/emqx_auto_subscribe/etc/emqx_auto_subscribe.conf b/apps/emqx_auto_subscribe/etc/emqx_auto_subscribe.conf index f6d041dab..becc87516 100644 --- a/apps/emqx_auto_subscribe/etc/emqx_auto_subscribe.conf +++ b/apps/emqx_auto_subscribe/etc/emqx_auto_subscribe.conf @@ -1,28 +1,28 @@ auto_subscribe { topics = [ - # { - # topic = "/c/${clientid}", - # qos = 0 - # rh = 0 - # rap = 0 - # nl = 0 - # } - # { - # topic = "/u/${username}", - # }, - # { - # topic = "/h/${host}", - # qos = 2 - # }, - # { - # topic = "/p/${port}", - # }, - # { - # topic = "/topic/abc", - # }, - # { - # topic = "/client/${clientid}/username/${username}/host/${host}/port/${port}", - # } + ## { + ## topic = "/c/${clientid}" + ## qos = 0 + ## rh = 0 + ## rap = 0 + ## nl = 0 + ## }, + ## { + ## topic = "/u/${username}" + ## }, + ## { + ## topic = "/h/${host}" + ## qos = 2 + ## }, + ## { + ## topic = "/p/${port}" + ## }, + ## { + ## topic = "/topic/abc" + ## }, + ## { + ## topic = "/client/${clientid}/username/${username}/host/${host}/port/${port}" + ## } ] } diff --git a/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl b/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl index b13b3760a..56bbf3a1c 100644 --- a/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl +++ b/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl @@ -38,7 +38,7 @@ max_limit() -> ?MAX_AUTO_SUBSCRIBE. list() -> - emqx:get_config([auto_subscribe, topics], []). + format(emqx:get_config([auto_subscribe, topics], [])). update(Topics) -> update_(Topics). @@ -68,6 +68,17 @@ on_client_connected(_, _, _) -> %%-------------------------------------------------------------------- %% internal +format(Rules) when is_list(Rules) -> + [format(Rule) || Rule <- Rules]; +format(Rule = #{topic := Topic}) when is_map(Rule) -> + #{ + topic => Topic, + qos => maps:get(qos, Rule, 0), + rh => maps:get(rh, Rule, 0), + rap => maps:get(rap, Rule, 0), + nl => maps:get(nl, Rule, 0) + }. + update_(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE -> {ok, _} = emqx:update_config([auto_subscribe, topics], Topics), update_hook();