fix(rule): reformat some code

* fix(rule): reformat some code for rule-engine

* fix(lwm2m): change publish_update_when to publish_update_msg_when

Change the option name publish_update_when -> publish_update_msg_when.

Also change the object_list_changed to contains_object_list, as the
the later describes the default behavior correctly.

* fix(lwm2m): publish_update_msg_when -> update_msg_publish_condition
This commit is contained in:
Shawn 2021-03-01 20:15:12 +08:00 committed by GitHub
parent 62a350949c
commit 6081d45d81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 31 deletions

View File

@ -44,11 +44,11 @@ lwm2m.topics.update = up/resp
# When publish the update message. # When publish the update message.
# #
# Can be one of: # Can be one of:
# - object_list_changed: only if the object list is changed # - contains_object_list: only if the update message contains object list
# - always: always publish the update message # - always: always publish the update message
# #
# Defaults to object_list_changed # Defaults to contains_object_list
#lwm2m.publish_update_when = object_list_changed #lwm2m.update_msg_publish_condition = contains_object_list
# 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,9 +112,9 @@ end}.
{default, "lwm2m/%e/up/resp"} {default, "lwm2m/%e/up/resp"}
]}. ]}.
{mapping, "lwm2m.publish_update_when", "emqx_lwm2m.publish_update_when", [ {mapping, "lwm2m.update_msg_publish_condition", "emqx_lwm2m.update_msg_publish_condition", [
{datatype, {enum, [object_list_changed, always]}}, {datatype, {enum, [contains_object_list, always]}},
{default, object_list_changed} {default, contains_object_list}
]}. ]}.
{translation, "emqx_lwm2m.topics", fun(Conf) -> {translation, "emqx_lwm2m.topics", fun(Conf) ->

View File

@ -101,12 +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), PublishCondition = proplists:get_value(update_msg_publish_condition, Envs, contains_object_list),
[{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}, {update_msg_publish_condition, PublishCondition},
{topics, Topics}]. {topics, Topics}].

View File

@ -121,11 +121,11 @@ update_reg_info(NewRegInfo, Lwm2mState = #lwm2m_state{
UpdatedRegInfo = maps:merge(RegInfo, NewRegInfo), UpdatedRegInfo = maps:merge(RegInfo, NewRegInfo),
case proplists:get_value(publish_update_when, case proplists:get_value(update_msg_publish_condition,
lwm2m_coap_responder:options(), object_list_changed) of lwm2m_coap_responder:options(), contains_object_list) of
always -> always ->
send_to_broker(<<"update">>, #{<<"data">> => UpdatedRegInfo}, Lwm2mState); send_to_broker(<<"update">>, #{<<"data">> => UpdatedRegInfo}, Lwm2mState);
object_list_changed -> contains_object_list ->
%% - report the registration info update, but only when objectList is updated. %% - report the registration info update, but only when objectList is updated.
case NewRegInfo of case NewRegInfo of
#{<<"objectList">> := _} -> #{<<"objectList">> := _} ->

View File

@ -269,16 +269,10 @@ do_update_resource_check(Id, NewParams) ->
config = OldConfig, config = OldConfig,
description = OldDescription} = _OldResource} -> description = OldDescription} = _OldResource} ->
try try
do_update_resource(#{id => Id, Conifg = maps:get(<<"config">>, NewParams, OldConfig),
config => case maps:find(<<"config">>, NewParams) of Descr = maps:get(<<"description">>, NewParams, OldDescription),
{ok, NewConfig} -> NewConfig; do_update_resource(#{id => Id, config => Conifg, type => Type,
error -> OldConfig description => Descr}),
end,
type => Type,
description => case maps:find(<<"description">>, NewParams) of
{ok, NewDescription} -> NewDescription;
error -> OldDescription
end}),
ok ok
catch _ : Reason -> catch _ : Reason ->
{error, Reason} {error, Reason}
@ -294,11 +288,13 @@ do_update_resource(#{id := Id, type := Type, description := NewDescription, conf
Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec), Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec),
case test_resource(#{type => Type, config => NewConfig}) of case test_resource(#{type => Type, config => NewConfig}) of
ok -> ok ->
Resource = #resource{id = Id, Resource = #resource{
id = Id,
type = Type, type = Type,
config = Config, config = Config,
description = NewDescription, description = NewDescription,
created_at = erlang:system_time(millisecond)}, created_at = erlang:system_time(millisecond)
},
cluster_call(init_resource, [Module, Create, Id, Config]), cluster_call(init_resource, [Module, Create, Id, Config]),
emqx_rule_registry:add_resource(Resource); emqx_rule_registry:add_resource(Resource);
{error, Reason} -> {error, Reason} ->
@ -468,18 +464,19 @@ may_update_rule_params(Rule, Params = #{rawsql := SQL}) ->
maps:remove(rawsql, Params)); maps:remove(rawsql, Params));
Reason -> throw(Reason) Reason -> throw(Reason)
end; end;
may_update_rule_params(Rule = #rule{enabled = OldE, actions = Actions}, may_update_rule_params(Rule = #rule{enabled = OldEnb, actions = Actions},
Params = #{enabled := ToE}) -> Params = #{enabled := NewEnb}) ->
case {OldE, ToE} of case {OldEnb, NewEnb} of
{false, true} -> refresh_rule(Rule); {false, true} -> refresh_rule(Rule);
{true, false} -> clear_actions(Actions); {true, false} -> clear_actions(Actions);
_ -> ok _ -> ok
end, end,
may_update_rule_params(Rule#rule{enabled = ToE}, maps:remove(enabled, Params)); may_update_rule_params(Rule#rule{enabled = NewEnb}, maps:remove(enabled, Params));
may_update_rule_params(Rule, Params = #{description := Descr}) -> may_update_rule_params(Rule, Params = #{description := Descr}) ->
may_update_rule_params(Rule#rule{description = Descr}, maps:remove(description, Params)); may_update_rule_params(Rule#rule{description = Descr}, maps:remove(description, Params));
may_update_rule_params(Rule, Params = #{on_action_failed := OnFailed}) -> may_update_rule_params(Rule, Params = #{on_action_failed := OnFailed}) ->
may_update_rule_params(Rule#rule{on_action_failed = OnFailed}, maps:remove(on_action_failed, Params)); may_update_rule_params(Rule#rule{on_action_failed = OnFailed},
maps:remove(on_action_failed, Params));
may_update_rule_params(Rule = #rule{actions = OldActions}, Params = #{actions := Actions}) -> may_update_rule_params(Rule = #rule{actions = OldActions}, Params = #{actions := Actions}) ->
%% prepare new actions before removing old ones %% prepare new actions before removing old ones
NewActions = prepare_actions(Actions, maps:get(enabled, Params, true)), NewActions = prepare_actions(Actions, maps:get(enabled, Params, true)),