feat(lwm2m): observe specified object lists

This commit is contained in:
JianBo He 2021-05-07 15:07:06 +08:00 committed by turtleDeng
parent 82330ffa00
commit 36c7258337
3 changed files with 30 additions and 7 deletions

View File

@ -13,8 +13,12 @@ lwm2m.lifetime_max = 86400s
# the downlink commands sent to the client will be cached.
#lwm2m.qmode_time_window = 22
# Auto send observer command to device
# on | off
# Auto send observer command to device. It can be configured as an OjbectList
# so that emqx will automatically observe the objects in this list.
#
# For examples: "/3/0,/3/0/1,/32976"
#
# Value: off | on | String
#lwm2m.auto_observe = off
# The topic subscribed by the lwm2m client after it is connected

View File

@ -26,8 +26,8 @@
]}.
{mapping, "lwm2m.auto_observe", "emqx_lwm2m.auto_observe", [
{datatype, flag},
{default, off}
{datatype, string},
{default, "off"} %% BACKW: v4.3.0
]}.
{mapping, "lwm2m.lb", "emqx_lwm2m.options", [
@ -39,6 +39,14 @@
{datatype, bytesize}
]}.
{translation, "emqx_lwm2m.auto_observe", fun(Conf) ->
case cuttlefish:conf_get("lwm2m.auto_observe", Conf, "off") of
"off" -> false; %% BACKW: v4.3.0
"on" -> true; %% BACKW: v4.3.0
Str -> string:tokens(Str, ", ")
end
end}.
{translation, "emqx_lwm2m.bind_udp", fun(Conf) ->
Options = cuttlefish_variable:filter_by_prefix("lwm2m.bind.udp", Conf),
lists:map(fun({_, Bind}) ->

View File

@ -275,13 +275,24 @@ do_send_to_broker(EventType, Payload, Lwm2mState) ->
%% Auto Observe
%%--------------------------------------------------------------------
auto_observe_object_list(true = _Expected, Registered) ->
Registered;
auto_observe_object_list(Expected, Registered) ->
Expected1 = lists:map(fun(S) -> iolist_to_binary(S) end, Expected),
lists:filter(fun(S) -> lists:member(S, Expected1) end, Registered).
send_auto_observe(CoapPid, RegInfo) ->
%% - auto observe the objects
case proplists:get_value(auto_observe, lwm2m_coap_responder:options(), false) of
true ->
false ->
?LOG(info, "Auto Observe Disabled", []);
TrueOrObjList ->
Objectlists = auto_observe_object_list(
TrueOrObjList,
maps:get(<<"objectList">>, RegInfo, [])
),
AlternatePath = maps:get(<<"alternatePath">>, RegInfo, <<"/">>),
auto_observe(AlternatePath, maps:get(<<"objectList">>, RegInfo, []), CoapPid);
_ -> ?LOG(info, "Auto Observe Disabled", [])
auto_observe(AlternatePath, Objectlists, CoapPid)
end.
auto_observe(AlternatePath, ObjectList, CoapPid) ->