feat(lwm2m): always publish update message (#4201)

* feat(lwm2m): always publish update message

* fix(lwm2m): change the publish_update_when to enum
This commit is contained in:
Shawn 2021-02-20 17:14:14 +08:00 committed by GitHub
parent 1e047e84c2
commit 1be62b7cbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 6 deletions

View File

@ -41,6 +41,15 @@ lwm2m.topics.register = up/resp
# The topic to which the lwm2m client's update message is published # The topic to which the lwm2m client's update message is published
lwm2m.topics.update = up/resp lwm2m.topics.update = up/resp
# When publish the update message.
#
# Can be one of:
# - object_list_changed: only if the object list is changed
# - always: always publish the update message
#
# Defaults to object_list_changed
#lwm2m.publish_update_when = object_list_changed
# Dir where the object definition files can be found # Dir where the object definition files can be found
lwm2m.xml_dir = {{ platform_etc_dir }}/lwm2m_xml lwm2m.xml_dir = {{ platform_etc_dir }}/lwm2m_xml

View File

@ -112,6 +112,11 @@ end}.
{default, "lwm2m/%e/up/resp"} {default, "lwm2m/%e/up/resp"}
]}. ]}.
{mapping, "lwm2m.publish_update_when", "emqx_lwm2m.publish_update_when", [
{datatype, {enum, [object_list_changed, always]}},
{default, object_list_changed}
]}.
{translation, "emqx_lwm2m.topics", fun(Conf) -> {translation, "emqx_lwm2m.topics", fun(Conf) ->
Topics = cuttlefish_variable:filter_by_prefix("lwm2m.topics", Conf), Topics = cuttlefish_variable:filter_by_prefix("lwm2m.topics", Conf),
Opts = lists:map(fun({[_,_, Key], Value}) -> Opts = lists:map(fun({[_,_, Key], Value}) ->

View File

@ -101,10 +101,12 @@ get_lwm2m_opts(Envs) ->
AutoObserve = proplists:get_value(auto_observe, Envs, []), AutoObserve = proplists:get_value(auto_observe, Envs, []),
QmodeTimeWindow = proplists:get_value(qmode_time_window, Envs, []), QmodeTimeWindow = proplists:get_value(qmode_time_window, Envs, []),
Topics = proplists:get_value(topics, Envs, []), Topics = proplists:get_value(topics, Envs, []),
PublishUpdateWhen = proplists:get_value(publish_update_when, Envs, object_list_changed),
[{lifetime_max, LifetimeMax}, [{lifetime_max, LifetimeMax},
{lifetime_min, LifetimeMin}, {lifetime_min, LifetimeMin},
{mountpoint, list_to_binary(Mountpoint)}, {mountpoint, list_to_binary(Mountpoint)},
{port, Sockport}, {port, Sockport},
{auto_observe, AutoObserve}, {auto_observe, AutoObserve},
{qmode_time_window, QmodeTimeWindow}, {qmode_time_window, QmodeTimeWindow},
{publish_update_when, PublishUpdateWhen},
{topics, Topics}]. {topics, Topics}].

View File

@ -115,15 +115,23 @@ post_init(Lwm2mState = #lwm2m_state{endpoint_name = _EndpointName,
_ = send_to_broker(<<"register">>, #{<<"data">> => RegInfo}, Lwm2mState), _ = send_to_broker(<<"register">>, #{<<"data">> => RegInfo}, Lwm2mState),
Lwm2mState#lwm2m_state{mqtt_topic = Topic}. Lwm2mState#lwm2m_state{mqtt_topic = Topic}.
update_reg_info(NewRegInfo, Lwm2mState=#lwm2m_state{life_timer = LifeTimer, register_info = RegInfo, update_reg_info(NewRegInfo, Lwm2mState = #lwm2m_state{
coap_pid = CoapPid}) -> life_timer = LifeTimer, register_info = RegInfo,
coap_pid = CoapPid}) ->
UpdatedRegInfo = maps:merge(RegInfo, NewRegInfo), UpdatedRegInfo = maps:merge(RegInfo, NewRegInfo),
%% - report the registration info update, but only when objectList is updated. case proplists:get_value(publish_update_when,
case NewRegInfo of lwm2m_coap_responder:options(), object_list_changed) of
#{<<"objectList">> := _} -> always ->
send_to_broker(<<"update">>, #{<<"data">> => UpdatedRegInfo}, Lwm2mState); send_to_broker(<<"update">>, #{<<"data">> => UpdatedRegInfo}, Lwm2mState);
_ -> ok object_list_changed ->
%% - report the registration info update, but only when objectList is updated.
case NewRegInfo of
#{<<"objectList">> := _} ->
send_to_broker(<<"update">>, #{<<"data">> => UpdatedRegInfo}, Lwm2mState);
_ -> ok
end
end, end,
%% - flush cached donwlink commands %% - flush cached donwlink commands