解决告警的问题

This commit is contained in:
anlicheng 2025-07-04 12:05:12 +08:00
parent 80c88d91f4
commit 23632be7e3
3 changed files with 33 additions and 20 deletions

View File

@ -37,10 +37,12 @@
retry_timeout :: integer(),
%%
metrics :: list() | undefined,
metrics = [] :: list(),
%%
controls :: list() | undefined
controls = [] :: list(),
%
alarms = [] :: list()
}).
-record(modbus_device, {

View File

@ -47,15 +47,6 @@ parse(Input) when is_binary(Input) ->
_ -> false
end
end, Trees),
Alarms = lists:filter(fun(E) ->
case E of
#modbus_alarm{} ->
true;
_ ->
false
end
end, Trees),
lager:debug("alarms is: ~p", [Alarms]),
case length(Modbus) == 1 of
true ->
@ -185,13 +176,6 @@ parse_ast0(#block{ident = <<"device", Name0/binary>>, props = Props}) ->
retry_timeout = maps:get(<<"retry_timeout">>, MapProps, undefined),
metrics = maps:get(<<"metrics">>, MapProps, undefined),
controls = maps:get(<<"controls">>, MapProps, undefined)
};
parse_ast0(#block{ident = <<"alarm", Name0/binary>>, props = Props}) ->
MapProps = parse_ast1(Props, []),
#modbus_alarm {
name = string:trim(Name0),
condition = map_of_binary(<<"condition">>, MapProps, <<>>),
hold_time = map_of_time(<<"hold_time">>, MapProps, 0)
}.
parse_ast1(Props) ->
@ -238,6 +222,22 @@ parse_ast1([#block{ident = <<"controls">>, props = Controls0}|T], Acc) ->
}
end, Controls0),
parse_ast1(T, [{<<"controls">>, Controls}|Acc]);
parse_ast1([#block{ident = <<"alarms">>, props = Controls0}|T], Acc) ->
Alarms = lists:map(fun(#block{ident = AlarmName, props = Props0}) ->
Props = lists:map(fun(Prop0) ->
[Name|Vars] = binary:split(Prop0, <<" ">>, [trim]),
{Name, Vars}
end, Props0),
PropsMap = maps:from_list(Props),
#modbus_alarm{
name = AlarmName,
condition = map_of_binary(<<"condition">>, PropsMap, undefined),
hold_time = map_of_time(<<"hold_time">>, PropsMap, undefined)
}
end, Controls0),
parse_ast1(T, [{<<"alarms">>, Alarms}|Acc]);
parse_ast1([#block{ident = <<"transport", Name0/binary>>, props = Props}|T], Acc) ->
PropsMap = parse_ast1(Props),
Transport = case string:trim(Name0) of
@ -349,14 +349,15 @@ merge_io(Device = #modbus_device{device_io = undefined}, _Templates) ->
Device;
merge_io(Device = #modbus_device{device_io = IOName}, Templates) ->
case lists:search(fun(#modbus_device_io{name = T0}) -> IOName =:= T0 end, Templates) of
{value, #modbus_device_io{poll_interval = PollInterval, retries = Retries, retry_timeout = RetryTimeout, metrics = Metrics, controls = Controls}} ->
{value, #modbus_device_io{poll_interval = PollInterval, retries = Retries, retry_timeout = RetryTimeout, metrics = Metrics, controls = Controls, alarms = Alarms}} ->
%%
Device#modbus_device{
poll_interval = PollInterval,
retries = Retries,
retry_timeout = RetryTimeout,
metrics = Metrics,
controls = Controls
controls = Controls,
alarms = Alarms
};
false ->
Device

View File

@ -157,4 +157,14 @@ device xyz {
precision 0.1;
}
}
alarms {
high_temperature {
# 触发条件
condition $temperature > 90.0;
# 持续判定
hold_time 30s;
}
}
}