fix: add lw gateway endpoint placeholder support (#6267)

This commit is contained in:
DDDHuang 2021-11-23 12:08:45 +08:00 committed by GitHub
parent af5002733b
commit 6a60c17970
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 11 deletions

View File

@ -76,6 +76,8 @@
-define(PH_NODE, <<"${node}">> ). -define(PH_NODE, <<"${node}">> ).
-define(PH_REASON, <<"${reason}">> ). -define(PH_REASON, <<"${reason}">> ).
-define(PH_ENDPOINT_NAME, <<"${endpoint_name}">> ).
%% sync change these place holder with binary def. %% sync change these place holder with binary def.
-define(PH_S_ACTION, "${action}" ). -define(PH_S_ACTION, "${action}" ).
-define(PH_S_CERT_SUBJECT, "${cert_subject}" ). -define(PH_S_CERT_SUBJECT, "${cert_subject}" ).
@ -108,5 +110,6 @@
-define(PH_S_DISCONNECTED_AT, "${disconnected_at}" ). -define(PH_S_DISCONNECTED_AT, "${disconnected_at}" ).
-define(PH_S_NODE, "${node}" ). -define(PH_S_NODE, "${node}" ).
-define(PH_S_REASON, "${reason}" ). -define(PH_S_REASON, "${reason}" ).
-define(PH_S_ENDPOINT_NAME, "${endpoint_name}" ).
-endif. -endif.

View File

@ -67,14 +67,17 @@ unmount(MountPoint, Msg = #message{topic = Topic}) ->
-spec(replvar(maybe(mountpoint()), map()) -> maybe(mountpoint())). -spec(replvar(maybe(mountpoint()), map()) -> maybe(mountpoint())).
replvar(undefined, _Vars) -> replvar(undefined, _Vars) ->
undefined; undefined;
replvar(MountPoint, #{clientid := ClientId, username := Username}) -> replvar(MountPoint, Vars) ->
lists:foldl(fun feed_var/2, MountPoint, ClientID = maps:get(clientid, Vars, undefined),
[{?PH_CLIENTID, ClientId}, {?PH_USERNAME, Username}]). UserName = maps:get(username, Vars, undefined),
EndpointName = maps:get(endpoint_name, Vars, undefined),
List = [ {?PH_CLIENTID, ClientID}
, {?PH_USERNAME, UserName}
, {?PH_ENDPOINT_NAME, EndpointName}
],
lists:foldl(fun feed_var/2, MountPoint, List).
feed_var({?PH_CLIENTID, ClientId}, MountPoint) -> feed_var({_PlaceHolder, undefined}, MountPoint) ->
emqx_topic:feed_var(?PH_CLIENTID, ClientId, MountPoint);
feed_var({?PH_USERNAME, undefined}, MountPoint) ->
MountPoint; MountPoint;
feed_var({?PH_USERNAME, Username}, MountPoint) -> feed_var({PlaceHolder, Value}, MountPoint) ->
emqx_topic:feed_var(?PH_USERNAME, Username, MountPoint). emqx_topic:feed_var(PlaceHolder, Value, MountPoint).

View File

@ -256,7 +256,7 @@ gateway.lwm2m {
enable_stats = true enable_stats = true
## When publishing or subscribing, prefix all topics with a mountpoint string. ## When publishing or subscribing, prefix all topics with a mountpoint string.
mountpoint = "lwm2m/%u" mountpoint = "lwm2m/${username}"
xml_dir = "{{ platform_etc_dir }}/lwm2m_xml" xml_dir = "{{ platform_etc_dir }}/lwm2m_xml"

View File

@ -16,6 +16,8 @@
%% %%
-module(emqx_gateway_api). -module(emqx_gateway_api).
-include_lib("emqx/include/emqx_placeholder.hrl").
-behaviour(minirest_api). -behaviour(minirest_api).
-import(emqx_gateway_http, -import(emqx_gateway_http,
@ -293,7 +295,7 @@ schema_gateway_overview_list() ->
<<"type">> => <<"udp">>, <<"type">> => <<"udp">>,
<<"running">> => true, <<"running">> => true,
<<"bind">> => 5783}], <<"bind">> => 5783}],
<<"mountpoint">> => <<"lwm2m/%e/">>, <<"mountpoint">> => <<"lwm2m/", ?PH_S_ENDPOINT_NAME, "/">>,
<<"qmode_time_windonw">> => 22, <<"qmode_time_windonw">> => 22,
<<"translators">> => <<"translators">> =>
#{<<"command">> => <<"dn/#">>,<<"notify">> => <<"up/notify">>, #{<<"command">> => <<"dn/#">>,<<"notify">> => <<"up/notify">>,