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.
#
# 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
#
# Defaults to object_list_changed
#lwm2m.publish_update_when = object_list_changed
# Defaults to contains_object_list
#lwm2m.update_msg_publish_condition = contains_object_list
# Dir where the object definition files can be found
lwm2m.xml_dir = {{ platform_etc_dir }}/lwm2m_xml

View File

@ -112,9 +112,9 @@ end}.
{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}
{mapping, "lwm2m.update_msg_publish_condition", "emqx_lwm2m.update_msg_publish_condition", [
{datatype, {enum, [contains_object_list, always]}},
{default, contains_object_list}
]}.
{translation, "emqx_lwm2m.topics", fun(Conf) ->

View File

@ -101,12 +101,12 @@ get_lwm2m_opts(Envs) ->
AutoObserve = proplists:get_value(auto_observe, Envs, []),
QmodeTimeWindow = proplists:get_value(qmode_time_window, 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_min, LifetimeMin},
{mountpoint, list_to_binary(Mountpoint)},
{port, Sockport},
{auto_observe, AutoObserve},
{qmode_time_window, QmodeTimeWindow},
{publish_update_when, PublishUpdateWhen},
{update_msg_publish_condition, PublishCondition},
{topics, Topics}].

View File

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

View File

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