From 924e313c8030754c43aa183b3d1fbf8e58a14a11 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 14 Dec 2023 16:45:39 +0800 Subject: [PATCH] add props --- apps/iot/src/database/ai_event_logs_bo.erl | 10 +++++++--- apps/iot/src/iot_host.erl | 23 +++++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/apps/iot/src/database/ai_event_logs_bo.erl b/apps/iot/src/database/ai_event_logs_bo.erl index 6bfd09b..b9a4349 100644 --- a/apps/iot/src/database/ai_event_logs_bo.erl +++ b/apps/iot/src/database/ai_event_logs_bo.erl @@ -10,17 +10,21 @@ -author("aresei"). -include("iot.hrl"). --export([insert/4]). +-export([insert/6]). %% API --spec insert(HostUUID :: binary(), DeviceUUID :: binary(), EventType :: integer(), Content :: binary()) -> +-spec insert(HostUUID :: binary(), DeviceUUID :: binary(), SceneId :: integer(), MicroId :: integer(), EventType :: integer(), Content :: binary()) -> ok | {ok, InsertId :: integer()} | {error, Reason :: any()}. -insert(HostUUID, DeviceUUID, EventType, Content) when is_integer(EventType), is_binary(HostUUID), is_binary(DeviceUUID), is_binary(Content) -> +insert(HostUUID, DeviceUUID, SceneId, MicroId, EventType, Content) + when is_integer(EventType), is_binary(HostUUID), is_binary(DeviceUUID), is_integer(SceneId), is_integer(MicroId), is_binary(Content) -> + mysql_pool:insert(mysql_iot, <<"ai_event_logs">>, #{ <<"event_type">> => EventType, <<"host_uuid">> => HostUUID, <<"device_uuid">> => DeviceUUID, + <<"scene_id">> => SceneId, + <<"micro_id">> => MicroId, <<"content">> => Content, <<"created_at">> => calendar:local_time() }, true). \ No newline at end of file diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 2e2cf52..2f4f3a4 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -340,10 +340,7 @@ handle_event(cast, {handle, {inform, Info0}}, ?STATE_ACTIVATED, State = #state{u lager:debug("[iot_host] host: ~p, service infos is: ~p", [UUID, ServiceInforms]), lists:foreach(fun(#{<<"props">> := Props, <<"name">> := Name, <<"version">> := Version, <<"version_copy">> := VersionCopy, <<"status">> := Status}) -> %% props 主机id:场景id:微服务id - [_, SceneId0, MicroId0] = binary:split(Props, <<":">>, [global]), - SceneId = binary_to_integer(SceneId0), - MicroId = binary_to_integer(MicroId0), - + {SceneId, MicroId} = parse_props(Props), micro_inform_log:insert(#{ <<"host_id">> => HostId, <<"scene_id">> => SceneId, @@ -410,15 +407,18 @@ handle_event(cast, {handle, {ai_event, Event0}}, ?STATE_ACTIVATED, State = #stat EventText = iot_cipher_aes:decrypt(AES, Event0), lager:debug("[iot_host] uuid: ~p, get ai_event: ~p", [UUID, EventText]), case catch jiffy:decode(EventText, [return_maps]) of - #{<<"event_type">> := EventType, <<"params">> := Params = #{<<"device_uuid">> := DeviceUUID}} -> + #{<<"event_type">> := EventType, <<"params">> := Params0 = #{<<"device_uuid">> := DeviceUUID, <<"props">> := Props}} -> case iot_device:is_alive(DeviceUUID) of error -> lager:notice("[iot_host] uuid: ~p, device_uuid: ~p is not alive, get ai_event: ~p", [UUID, DeviceUUID, EventText]), ok; {ok, DevicePid} -> + Params = maps:remove(<<"props">>, Params0), + {SceneId, MicroId} = parse_props(Props), + %% 保存数据到mysql Message = iolist_to_binary(jiffy:encode(Params, [force_utf8])), - ai_event_logs_bo:insert(UUID, DeviceUUID, EventType, Message), + ai_event_logs_bo:insert(UUID, DeviceUUID, SceneId, MicroId, EventType, Message), iot_device:change_status(DevicePid, ?DEVICE_ONLINE), iot_ai_router:route_uuid(DeviceUUID, EventType, Params) @@ -553,4 +553,13 @@ state_map(#state{host_id = HostId, uuid = UUID, aes = Aes, has_session = HasSess heartbeat_counter => HeartbeatCounter, channel_pid => ChannelPid, metrics => Metrics - }. \ No newline at end of file + }. + +%% props 主机id:场景id:微服务id +-spec parse_props(Props :: undefined | binary()) -> {SceneId :: integer(), MicroId :: integer()}. +parse_props(Props) when is_binary(Props) -> + %% props 主机id:场景id:微服务id + [_, SceneId0, MicroId0] = binary:split(Props, <<":">>, [global]), + SceneId = binary_to_integer(SceneId0), + MicroId = binary_to_integer(MicroId0), + {SceneId, MicroId}. \ No newline at end of file