From 23632be7e3dbc581af2e8eed6a503c1a31760c38 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 4 Jul 2025 12:05:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=91=8A=E8=AD=A6=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/modbus/include/modbus_ast.hrl | 6 +++-- apps/modbus/src/modbus_parser.erl | 37 +++++++++++++++--------------- modbus.conf | 10 ++++++++ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/apps/modbus/include/modbus_ast.hrl b/apps/modbus/include/modbus_ast.hrl index 7bd27a7..7d7ec31 100644 --- a/apps/modbus/include/modbus_ast.hrl +++ b/apps/modbus/include/modbus_ast.hrl @@ -37,10 +37,12 @@ retry_timeout :: integer(), %% 数据定义 - metrics :: list() | undefined, + metrics = [] :: list(), %% 写入控制器 - controls :: list() | undefined + controls = [] :: list(), + % 告警信息 + alarms = [] :: list() }). -record(modbus_device, { diff --git a/apps/modbus/src/modbus_parser.erl b/apps/modbus/src/modbus_parser.erl index 22ac188..25159d7 100644 --- a/apps/modbus/src/modbus_parser.erl +++ b/apps/modbus/src/modbus_parser.erl @@ -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 diff --git a/modbus.conf b/modbus.conf index 5ac076c..4ff96bd 100644 --- a/modbus.conf +++ b/modbus.conf @@ -157,4 +157,14 @@ device xyz { precision 0.1; } } + + alarms { + high_temperature { + # 触发条件 + condition $temperature > 90.0; + + # 持续判定 + hold_time 30s; + } + } } \ No newline at end of file