From 8d0b900b9298cac5346eab17acdd9e36c7f9463d Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Sat, 14 Dec 2024 22:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E9=97=B4=E9=9A=94=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/iot/src/iot_device.erl | 31 ++++++++++++++++++++++++++----- config/sys-dev.config | 5 +++++ config/sys-prod.config | 5 +++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/apps/iot/src/iot_device.erl b/apps/iot/src/iot_device.erl index 02c6bbd..370a7bf 100644 --- a/apps/iot/src/iot_device.erl +++ b/apps/iot/src/iot_device.erl @@ -29,6 +29,11 @@ cache_size = 200, %% 设备是否授权 auth_status :: integer(), + %% 事件触发周期 + ai_event_throttle = #{}, + %% 记录事件的ttl值 + ai_event_ttl = #{}, + status = ?DEVICE_OFFLINE }). @@ -139,8 +144,9 @@ init([DeviceUUID]) when is_binary(DeviceUUID) -> end; init([#{<<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status}]) -> {ok, CacheSize} = application:get_env(iot, device_cache_size), + {ok, AiEventThrottle} = application:get_env(iot, ai_event_throttle), - {ok, #state{device_uuid = DeviceUUID, cache_size = CacheSize, status = as_integer(Status), auth_status = as_integer(AuthorizeStatus)}}. + {ok, #state{device_uuid = DeviceUUID, ai_event_throttle = AiEventThrottle, cache_size = CacheSize, status = as_integer(Status), auth_status = as_integer(AuthorizeStatus)}}. %% @private %% @doc Handling call messages @@ -247,10 +253,25 @@ handle_cast({auth, false}, State = #state{device_uuid = DeviceUUID}) -> lager:debug("[iot_device] device_uuid: ~p, auth: false", [DeviceUUID]), {noreply, State#state{auth_status = 0}}; -%% ai事件的延迟整流逻辑 -handle_cast({ai_event, EventType, Params}, State = #state{device_uuid = DeviceUUID}) -> - iot_ai_router:route_uuid(DeviceUUID, EventType, Params), - {noreply, State}. +%% ai事件的延迟整流逻辑, 保证在间隔事件内只发送一次 +handle_cast({ai_event, EventType, Params}, State = #state{device_uuid = DeviceUUID, ai_event_throttle = EventThrottle, ai_event_ttl = EventTTL}) -> + case maps:find(EventType, EventThrottle) of + {ok, Interval} -> + LastTimestamp = maps:get(EventType, EventTTL, 0), + Timestamp = iot_util:current_time(), + case Timestamp >= LastTimestamp + Interval of + true -> + iot_ai_router:route_uuid(DeviceUUID, EventType, Params), + {noreply, State#state{ai_event_ttl = maps:put(EventType, Timestamp, EventTTL)}}; + false -> + lager:debug("[iot_device] device_uuid: ~p, ai_event trigger less than interval: ~p, last_timestamp: ~p, current_timestamp: ~p", + [DeviceUUID, Interval, LastTimestamp, Timestamp]), + {noreply, State} + end; + error -> + iot_ai_router:route_uuid(DeviceUUID, EventType, Params), + {noreply, State} + end. %% @private %% @doc Handling all non call/cast messages diff --git a/config/sys-dev.config b/config/sys-dev.config index 66d0381..a2c0a64 100644 --- a/config/sys-dev.config +++ b/config/sys-dev.config @@ -14,6 +14,11 @@ %% 数据的最大缓存量 {device_cache_size, 200}, + %% 事件的间隔处理逻辑 + {ai_event_throttle, #{ + 17 => 300 + }}, + {api_url, "http://39.98.184.67:8800"}, {watchdog, [ diff --git a/config/sys-prod.config b/config/sys-prod.config index 4992105..7ae6323 100644 --- a/config/sys-prod.config +++ b/config/sys-prod.config @@ -14,6 +14,11 @@ %% 数据的最大缓存量 {device_cache_size, 200}, + %% 事件的间隔处理逻辑 + {ai_event_throttle, #{ + 17 => 300 + }}, + {fake_location_codes, [ {<<"23103">>, <<"0508103010001050300001">>}, {<<"20407">>, <<"0512104030001050400003">>},