diff --git a/apps/iot/src/iot_device.erl b/apps/iot/src/iot_device.erl index 6ddb14a..8ae835f 100644 --- a/apps/iot/src/iot_device.erl +++ b/apps/iot/src/iot_device.erl @@ -22,6 +22,8 @@ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state, { + %% 设备id + device_id :: integer(), device_uuid :: binary(), %% 设备是否授权 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]), ignore 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, #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)}}. %% @private @@ -204,27 +206,36 @@ handle_cast({auth, false}, State = #state{device_uuid = DeviceUUID}) -> {noreply, State#state{auth_status = 0}}; %% 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 = rewrite_ai_event(EventType, Params0), - 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 -> - lager:debug("[iot_device] device_uuid: ~p, ai_event triggered last_timestamp: ~p, current_timestamp: ~p, interval is: ~p", - [DeviceUUID, LastTimestamp, Timestamp, Interval]), + case rewrite_ai_event(EventType, Params0) of + {ok, Params} -> + 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 -> + lager:debug("[iot_device] device_uuid: ~p, ai_event triggered last_timestamp: ~p, current_timestamp: ~p, interval is: ~p", + [DeviceUUID, LastTimestamp, Timestamp, Interval]), + 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 -> + lager:debug("[iot_device] device_uuid: ~p, ai_event type: ~p not limited", [DeviceUUID, EventType]), 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 -> - lager:debug("[iot_device] device_uuid: ~p, ai_event type: ~p not limited", [DeviceUUID, EventType]), - iot_ai_router:route_uuid(DeviceUUID, EventType, Params), + %% 未知事件类型,报警 + {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; @@ -324,12 +335,12 @@ extract_build_location_code(<>) -> extract_build_location_code(Code) when is_binary(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) -> %% 查询数据库,覆盖params的部分信息 case ai_event_bo:get_event_by_event_type(EventType) of {ok, #{<<"event_code">> := EventCode, <<"event_name">> := EventName}} -> - Params#{<<"event_code">> => EventCode, <<"description">> => EventName}; + {ok, Params#{<<"event_code">> => EventCode, <<"description">> => EventName}}; undefined -> - Params + error end. \ No newline at end of file diff --git a/apps/iot/src/iot_watchdog.erl b/apps/iot/src/iot_watchdog.erl index 6aef686..3410c26 100644 --- a/apps/iot/src/iot_watchdog.erl +++ b/apps/iot/src/iot_watchdog.erl @@ -49,12 +49,14 @@ %%% API %%%=================================================================== +%% 检测主机的硬盘 内存 CPU -spec detection(HostUUID :: binary(), Name :: binary(), Metric :: map()) -> no_return(). detection(HostUUID, Name, Metric) when is_binary(HostUUID), is_binary(Name), is_map(Metric) -> gen_server:cast(?SERVER, {detection, HostUUID, Name, Metric}); detection(HostUUID, Name, _) when is_binary(HostUUID), is_binary(Name) -> ok. +%% 直接报警 -spec warn(Warn :: binary()) -> no_return(). warn(Warn) when is_binary(Warn) -> gen_server:cast(?SERVER, {warn, Warn}).