规则/桥接有bug

规则如下:

SELECT
    clientid as cid,
    now_timestamp('millisecond') as now_ts_ms,
    nth(6, split(topic, '/')) as attr_old,
    ['INT32', 'FLOAT'] as data_types,
    ['level', 'voltage'] as measurements,
    jq('.level', payload) as lvls,
    jq('.voltage', payload) as vols,
FROM
    "dev/+/r/battery"

调试输入数据:

{"ts": 1695885691, "level": 61, "voltage": 3.968182}

调试输出结果:

attr_old battery
cid c_emqx
data_types
0 INT32
1 FLOAT
lvls
0 61
measurements
0 level
1 voltage
now_ts_ms 1695885932319
vols
0 3.968182

实际环境发布数据:

mosquitto_pub -t dev/500000000001/r/battery -i 500000000001 -m '{"level": 61, "voltage": 3.968182}'

实际环境错误日志:

2023-09-28T07:26:47.730354+00:00 [error] msg: action_failed, mfa: emqx_rule_runtime:handle_action/4, line: 343, peername: 172.17.0.1:41910, clientid: 500000000001, action: #{func => console,mod => emqx_rule_actions}, exception: error, reason: function_clause, stacktrace: [{emqx_rule_runtime,do_handle_action,[<<"doorbell-events">>,#{func => console,mod => emqx_rule_actions},#{<<"attr_old">> => <<"battery">>,<<"cid">> => <<"500000000001">>,<<"data_types">> => [<<"INT32">>,<<"FLOAT">>],<<"lvls">> => "=",<<"measurements">> => [<<"level">>,<<"voltage">>],<<"now_ts_ms">> => 1695886007728,<<"vols">> => [3.968182]},#{clientid => <<"500000000001">>,event => 'message.publish',flags => #{dup => false,retain => false},headers => #{peerhost => {172,17,0,1},properties => #{},proto_ver => 4,protocol => mqtt,username => undefined},id => <<"000606663B11FA61E8B1010078570002">>,metadata => #{rule_id => <<"doorbell-events">>},node => 'emqx@172.17.0.16',payload => <<"{\"level\": 61, \"voltage\": 3.968182}">>,peerhost => <<"172.17.0.1">>,pub_props => #{'User-Property' => #{}},publish_received_at => 1695886007728,qos => 0,timestamp => 1695886007728,topic => <<"dev/500000000001/r/battery">>,username => undefined}],[{file,"emqx_rule_runtime.erl"},{line,347}]},{emqx_rule_runtime,handle_action,4,[{file,"emqx_rule_runtime.erl"},{line,326}]},{emqx_rule_runtime,'-handle_action_list/4-lc$^0/1-0-',4,[{file,"emqx_rule_runtime.erl"},{line,321}]},{emqx_rule_runtime,do_apply_rule,3,[{file,"emqx_rule_runtime.erl"},{line,180}]},{emqx_rule_runtime,apply_rule,3,[{file,"emqx_rule_runtime.erl"},{line,72}]},{emqx_rule_runtime,apply_rule_discard_result,3,[{file,"emqx_rule_runtime.erl"},{line,65}]},{emqx_rule_runtime,apply_rules,3,[{file,"emqx_rule_runtime.erl"},{line,61}]},{emqx_rule_events,on_message_publish,2,[{file,"emqx_rule_events.erl"},{line,142}]},{emqx_hooks,safe_execute,2,[{file,"emqx_hooks.erl"},{line,200}]},{emqx_hooks,do_run_fold,3,[{file,"emqx_hooks.erl"},{line,180}]},{emqx_broker,publish,1,[{file,"emqx_broker.erl"},{line,219}]},{emqx_channel,do_publish,3,[{file,"emqx_channel.erl"},{line,714}]},{emqx_connection,with_channel,3,[{file,"emqx_connection.erl"},{line,840}]},{emqx_connection,process_msg,2,[{file,"emqx_connection.erl"},{line,493}]},{emqx_connection,process_msg,2,[{file,"emqx_connection.erl"},{line,499}]},{emqx_connection,handle_recv,3,[{file,"emqx_connection.erl"},{line,455}]},{proc_lib,wake_up,3,[{file,"proc_lib.erl"},{line,250}]}]

lvls成了一个等于号

<<"lvls">> => "="

啊,你用的是哪个 emqx 版本?

我看到了起码两个 bug:

  • 第一个是你的规则报了 function_clause 错误,可能是已经修复过的,我需要问问同事。
  • 第二个是 jq 取 .level 字段时,返回了错误的值(不知道为什么数字当成 ASCII 码处理了)。

docker hub拉下来的EMQX 5.2.1 is running now!

function_clause 的错误已经在 5.3.0 修复。

@Chris

<<"lvls">> => "="

这个 “=” 是 console 打印的显示问题,因为 jq 返回的是个列表,即 [61], 然后被 console 按照 ASCII 打印为 "=" 了。

这是因为 Erlang 里面字符串就是列表,就是说 Erlang 里面,[61]"=" 完全是一回事儿。

真正使用的时候,你还是需要使用 nth 函数来取出其中的值的:nth(1, lvls)