fix(mqtt-bridge): transmit raw msg payload with empty template

This commit is contained in:
JimMoen 2023-01-03 17:54:07 +08:00
parent e7883e0ee2
commit 59ac0b1424
No known key found for this signature in database
GPG Key ID: 87A520B4F76BA86D
3 changed files with 11 additions and 8 deletions

View File

@ -71,14 +71,13 @@ to_remote_msg(#message{flags = Flags0} = Msg, Vars) ->
to_remote_msg(MapMsg, #{ to_remote_msg(MapMsg, #{
remote := #{ remote := #{
topic := TopicToken, topic := TopicToken,
payload := PayloadToken,
qos := QoSToken, qos := QoSToken,
retain := RetainToken retain := RetainToken
}, } = Remote,
mountpoint := Mountpoint mountpoint := Mountpoint
}) when is_map(MapMsg) -> }) when is_map(MapMsg) ->
Topic = replace_vars_in_str(TopicToken, MapMsg), Topic = replace_vars_in_str(TopicToken, MapMsg),
Payload = process_payload(PayloadToken, MapMsg), Payload = process_payload(Remote, MapMsg),
QoS = replace_simple_var(QoSToken, MapMsg), QoS = replace_simple_var(QoSToken, MapMsg),
Retain = replace_simple_var(RetainToken, MapMsg), Retain = replace_simple_var(RetainToken, MapMsg),
PubProps = maps:get(pub_props, MapMsg, #{}), PubProps = maps:get(pub_props, MapMsg, #{}),
@ -100,16 +99,15 @@ to_broker_msg(
#{ #{
local := #{ local := #{
topic := TopicToken, topic := TopicToken,
payload := PayloadToken,
qos := QoSToken, qos := QoSToken,
retain := RetainToken retain := RetainToken
}, } = Local,
mountpoint := Mountpoint mountpoint := Mountpoint
}, },
Props Props
) -> ) ->
Topic = replace_vars_in_str(TopicToken, MapMsg), Topic = replace_vars_in_str(TopicToken, MapMsg),
Payload = process_payload(PayloadToken, MapMsg), Payload = process_payload(Local, MapMsg),
QoS = replace_simple_var(QoSToken, MapMsg), QoS = replace_simple_var(QoSToken, MapMsg),
Retain = replace_simple_var(RetainToken, MapMsg), Retain = replace_simple_var(RetainToken, MapMsg),
PubProps = maps:get(pub_props, MapMsg, #{}), PubProps = maps:get(pub_props, MapMsg, #{}),
@ -121,9 +119,12 @@ to_broker_msg(
) )
). ).
process_payload([], Msg) -> process_payload(From, MapMsg) ->
do_process_payload(maps:get(payload, From, undefined), MapMsg).
do_process_payload(undefined, Msg) ->
emqx_json:encode(Msg); emqx_json:encode(Msg);
process_payload(Tks, Msg) -> do_process_payload(Tks, Msg) ->
replace_vars_in_str(Tks, Msg). replace_vars_in_str(Tks, Msg).
%% Replace a string contains vars to another string in which the placeholders are replace by the %% Replace a string contains vars to another string in which the placeholders are replace by the

View File

@ -0,0 +1 @@
Fix the problem that the bridge is not available when the Payload template is empty in the MQTT bridge.

View File

@ -0,0 +1 @@
修复 MQTT 桥接中 Payload 模板为空时桥接不可用的问题。