增加对事件触发间隔的逻辑处理

This commit is contained in:
anlicheng 2024-12-14 22:49:12 +08:00
parent 06e901df43
commit 8d0b900b92
3 changed files with 36 additions and 5 deletions

View File

@ -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}) ->
%% 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}.
{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

View File

@ -14,6 +14,11 @@
%% 数据的最大缓存量
{device_cache_size, 200},
%% 事件的间隔处理逻辑
{ai_event_throttle, #{
17 => 300
}},
{api_url, "http://39.98.184.67:8800"},
{watchdog, [

View File

@ -14,6 +14,11 @@
%% 数据的最大缓存量
{device_cache_size, 200},
%% 事件的间隔处理逻辑
{ai_event_throttle, #{
17 => 300
}},
{fake_location_codes, [
{<<"23103">>, <<"0508103010001050300001">>},
{<<"20407">>, <<"0512104030001050400003">>},