فهرست منبع

feat(lwm2m): observe specified object lists

JianBo He 4 سال پیش
والد
کامیت
36c7258337
3فایلهای تغییر یافته به همراه30 افزوده شده و 7 حذف شده
  1. 6 2
      apps/emqx_lwm2m/etc/emqx_lwm2m.conf
  2. 10 2
      apps/emqx_lwm2m/priv/emqx_lwm2m.schema
  3. 14 3
      apps/emqx_lwm2m/src/emqx_lwm2m_protocol.erl

+ 6 - 2
apps/emqx_lwm2m/etc/emqx_lwm2m.conf

@@ -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

+ 10 - 2
apps/emqx_lwm2m/priv/emqx_lwm2m.schema

@@ -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}) ->

+ 14 - 3
apps/emqx_lwm2m/src/emqx_lwm2m_protocol.erl

@@ -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) ->