增加对事件触发间隔的逻辑处理
This commit is contained in:
parent
06e901df43
commit
8d0b900b92
@ -29,6 +29,11 @@
|
|||||||
cache_size = 200,
|
cache_size = 200,
|
||||||
%% 设备是否授权
|
%% 设备是否授权
|
||||||
auth_status :: integer(),
|
auth_status :: integer(),
|
||||||
|
%% 事件触发周期
|
||||||
|
ai_event_throttle = #{},
|
||||||
|
%% 记录事件的ttl值
|
||||||
|
ai_event_ttl = #{},
|
||||||
|
|
||||||
status = ?DEVICE_OFFLINE
|
status = ?DEVICE_OFFLINE
|
||||||
}).
|
}).
|
||||||
|
|
||||||
@ -139,8 +144,9 @@ init([DeviceUUID]) when is_binary(DeviceUUID) ->
|
|||||||
end;
|
end;
|
||||||
init([#{<<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status}]) ->
|
init([#{<<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status}]) ->
|
||||||
{ok, CacheSize} = application:get_env(iot, device_cache_size),
|
{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
|
%% @private
|
||||||
%% @doc Handling call messages
|
%% @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]),
|
lager:debug("[iot_device] device_uuid: ~p, auth: false", [DeviceUUID]),
|
||||||
{noreply, State#state{auth_status = 0}};
|
{noreply, State#state{auth_status = 0}};
|
||||||
|
|
||||||
%% ai事件的延迟整流逻辑
|
%% ai事件的延迟整流逻辑, 保证在间隔事件内只发送一次
|
||||||
handle_cast({ai_event, EventType, Params}, State = #state{device_uuid = DeviceUUID}) ->
|
handle_cast({ai_event, EventType, Params}, State = #state{device_uuid = DeviceUUID, ai_event_throttle = EventThrottle, ai_event_ttl = EventTTL}) ->
|
||||||
iot_ai_router:route_uuid(DeviceUUID, EventType, Params),
|
case maps:find(EventType, EventThrottle) of
|
||||||
{noreply, State}.
|
{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
|
%% @private
|
||||||
%% @doc Handling all non call/cast messages
|
%% @doc Handling all non call/cast messages
|
||||||
|
|||||||
@ -14,6 +14,11 @@
|
|||||||
%% 数据的最大缓存量
|
%% 数据的最大缓存量
|
||||||
{device_cache_size, 200},
|
{device_cache_size, 200},
|
||||||
|
|
||||||
|
%% 事件的间隔处理逻辑
|
||||||
|
{ai_event_throttle, #{
|
||||||
|
17 => 300
|
||||||
|
}},
|
||||||
|
|
||||||
{api_url, "http://39.98.184.67:8800"},
|
{api_url, "http://39.98.184.67:8800"},
|
||||||
|
|
||||||
{watchdog, [
|
{watchdog, [
|
||||||
|
|||||||
@ -14,6 +14,11 @@
|
|||||||
%% 数据的最大缓存量
|
%% 数据的最大缓存量
|
||||||
{device_cache_size, 200},
|
{device_cache_size, 200},
|
||||||
|
|
||||||
|
%% 事件的间隔处理逻辑
|
||||||
|
{ai_event_throttle, #{
|
||||||
|
17 => 300
|
||||||
|
}},
|
||||||
|
|
||||||
{fake_location_codes, [
|
{fake_location_codes, [
|
||||||
{<<"23103">>, <<"0508103010001050300001">>},
|
{<<"23103">>, <<"0508103010001050300001">>},
|
||||||
{<<"20407">>, <<"0512104030001050400003">>},
|
{<<"20407">>, <<"0512104030001050400003">>},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user