未知ai事件处理

This commit is contained in:
anlicheng 2025-03-12 14:57:46 +08:00
parent 9bbdeea0d5
commit e5773800e3
2 changed files with 34 additions and 21 deletions

View File

@ -22,6 +22,8 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, { -record(state, {
%% id
device_id :: integer(),
device_uuid :: binary(), device_uuid :: binary(),
%% %%
auth_status :: integer(), auth_status :: integer(),
@ -137,10 +139,10 @@ init([DeviceUUID]) when is_binary(DeviceUUID) ->
lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]), lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]),
ignore ignore
end; end;
init([#{<<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status, <<"model_id">> := ModelId}]) -> init([#{<<"id">> := DeviceId, <<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"status">> := Status, <<"model_id">> := ModelId}]) ->
{ok, AiEventThrottle} = application:get_env(iot, ai_event_throttle), {ok, AiEventThrottle} = application:get_env(iot, ai_event_throttle),
{ok, #state{device_uuid = DeviceUUID, ai_event_throttle = AiEventThrottle, {ok, #state{device_id = DeviceId, device_uuid = DeviceUUID, ai_event_throttle = AiEventThrottle,
status = as_integer(Status), auth_status = as_integer(AuthorizeStatus), model_id = as_integer(ModelId)}}. status = as_integer(Status), auth_status = as_integer(AuthorizeStatus), model_id = as_integer(ModelId)}}.
%% @private %% @private
@ -204,9 +206,10 @@ handle_cast({auth, false}, State = #state{device_uuid = DeviceUUID}) ->
{noreply, State#state{auth_status = 0}}; {noreply, State#state{auth_status = 0}};
%% ai事件的延迟整流逻辑, %% ai事件的延迟整流逻辑,
handle_cast({ai_event, EventType, Params0}, State = #state{device_uuid = DeviceUUID, ai_event_throttle = EventThrottle, ai_event_ttl = EventTTL}) -> handle_cast({ai_event, EventType, Params0}, State = #state{device_id = DeviceId, device_uuid = DeviceUUID, ai_event_throttle = EventThrottle, ai_event_ttl = EventTTL}) ->
%% params的部分信息 %% params的部分信息
Params = rewrite_ai_event(EventType, Params0), case rewrite_ai_event(EventType, Params0) of
{ok, Params} ->
case maps:find(EventType, EventThrottle) of case maps:find(EventType, EventThrottle) of
{ok, Interval} -> {ok, Interval} ->
LastTimestamp = maps:get(EventType, EventTTL, 0), LastTimestamp = maps:get(EventType, EventTTL, 0),
@ -227,6 +230,14 @@ handle_cast({ai_event, EventType, Params0}, State = #state{device_uuid = DeviceU
iot_ai_router:route_uuid(DeviceUUID, EventType, Params), iot_ai_router:route_uuid(DeviceUUID, EventType, Params),
{noreply, State} {noreply, State}
end; end;
error ->
%%
{ok, EventCode} = maps:find(<<"event_code">>, Params0),
Warn = iolist_to_binary([<<"设备ID:">>, integer_to_binary(DeviceId), <<", 未知ai事件:">>, EventCode]),
iot_watchdog:warn(Warn),
{noreply, State}
end;
%% %%
handle_cast({handle_data, Fields, Timestamp}, State = #state{device_uuid = DeviceUUID, model_id = ModelId}) -> handle_cast({handle_data, Fields, Timestamp}, State = #state{device_uuid = DeviceUUID, model_id = ModelId}) ->
@ -324,12 +335,12 @@ extract_build_location_code(<<BuildLocationCode:9/binary, Suffix/binary>>) ->
extract_build_location_code(Code) when is_binary(Code) -> extract_build_location_code(Code) when is_binary(Code) ->
Code. Code.
-spec rewrite_ai_event(EventType :: integer(), Params :: map()) -> NParams :: map(). -spec rewrite_ai_event(EventType :: integer(), Params :: map()) -> {ok, NParams :: map()} | error.
rewrite_ai_event(EventType, Params) when is_integer(EventType), is_map(Params) -> rewrite_ai_event(EventType, Params) when is_integer(EventType), is_map(Params) ->
%% params的部分信息 %% params的部分信息
case ai_event_bo:get_event_by_event_type(EventType) of case ai_event_bo:get_event_by_event_type(EventType) of
{ok, #{<<"event_code">> := EventCode, <<"event_name">> := EventName}} -> {ok, #{<<"event_code">> := EventCode, <<"event_name">> := EventName}} ->
Params#{<<"event_code">> => EventCode, <<"description">> => EventName}; {ok, Params#{<<"event_code">> => EventCode, <<"description">> => EventName}};
undefined -> undefined ->
Params error
end. end.

View File

@ -49,12 +49,14 @@
%%% API %%% API
%%%=================================================================== %%%===================================================================
%% CPU
-spec detection(HostUUID :: binary(), Name :: binary(), Metric :: map()) -> no_return(). -spec detection(HostUUID :: binary(), Name :: binary(), Metric :: map()) -> no_return().
detection(HostUUID, Name, Metric) when is_binary(HostUUID), is_binary(Name), is_map(Metric) -> detection(HostUUID, Name, Metric) when is_binary(HostUUID), is_binary(Name), is_map(Metric) ->
gen_server:cast(?SERVER, {detection, HostUUID, Name, Metric}); gen_server:cast(?SERVER, {detection, HostUUID, Name, Metric});
detection(HostUUID, Name, _) when is_binary(HostUUID), is_binary(Name) -> detection(HostUUID, Name, _) when is_binary(HostUUID), is_binary(Name) ->
ok. ok.
%%
-spec warn(Warn :: binary()) -> no_return(). -spec warn(Warn :: binary()) -> no_return().
warn(Warn) when is_binary(Warn) -> warn(Warn) when is_binary(Warn) ->
gen_server:cast(?SERVER, {warn, Warn}). gen_server:cast(?SERVER, {warn, Warn}).