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">>},