Skip to content

Commit

Permalink
cust: json payload ts unit
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Apr 18, 2023
1 parent 88324b0 commit 13c30a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/emqtt_bench.erl
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,13 @@ publish(Client, Opts) ->

publish_topic(Client, Topic, #{ name := Topic
, qos := QoS
, is_inject_ts := IsTS
, inject_ts := TsUnit
, payload := PayloadTemplate
}, ClientOpts) ->
ok = ensure_publish_begin_time(),
NewPayload = case IsTS of
true -> PayloadTemplate#{<<"timestamp">> => erlang:system_time(millisecond)};
false -> PayloadTemplate
NewPayload = case TsUnit of
false -> PayloadTemplate;
_ -> PayloadTemplate#{<<"timestamp">> => erlang:system_time(TsUnit)}
end,
case emqtt:publish(Client, feed_var(Topic, ClientOpts), jsx:encode(NewPayload), [{qos, QoS}]) of
ok -> ok;
Expand Down Expand Up @@ -1251,7 +1251,7 @@ validate_payload_hdrs([Hdr | T]) ->
interval_ms := non_neg_integer(),
qos := 0 | 1 |_2,
render_field := undefined | binary(),
is_inject_ts := boolean(), payload := string(),
inject_ts := false | second | millisecond | microsecond | nanosecond,
payload := map()
}}.
parse_topics_payload(Opts) ->
Expand All @@ -1262,7 +1262,7 @@ parse_topics_payload(Opts) ->
#{<<"topics">> := TopicSpecs} = jsx:decode(Content),
%% Example
%% #{<<"topics">> =>
%% [#{<<"inject_timestamp">> => true,<<"interval_ms">> => <<"1000">>,
%% [#{<<"inject_timestamp">> => <<"ms">>,<<"interval_ms">> => <<"1000">>,
%% <<"name">> => <<"Topic1">>,
%% <<"payload">> =>
%% #{<<"foo">> => <<"bar">>,<<"timestamp">> => <<"0">>}},
Expand All @@ -1276,10 +1276,18 @@ parse_topics_payload(Opts) ->
<<"QoS">> := QoS,
<<"payload">> := Payload} = Spec, Acc) ->
RFieldName = maps:get(<<"render">>, Spec, undefined),
TSUnit = case WithTS of
<<"s">> -> second;
<<"ms">> -> millisecond;
<<"us">> -> microsecond;
<<"ns">> -> nanosecond;
true -> millisecond;
false -> false
end,
Acc#{TopicName =>
#{ name => TopicName
, interval_ms => binary_to_integer(IntervalMS)
, is_inject_ts => WithTS
, inject_ts => TSUnit
, qos => QoS
, render_field => RFieldName
, payload => Payload
Expand Down
2 changes: 1 addition & 1 deletion topic_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

{"name": "Topic2",
"interval_ms": "100",
"inject_timestamp" : true,
"inject_timestamp" : "us",
"QoS": 0,
"render" : "VIN",
"payload": {"foo2" : "bar2", "timestamp": "0", "VIN" : "VIN-%i"}
Expand Down

0 comments on commit 13c30a9

Please sign in to comment.