add props

This commit is contained in:
anlicheng 2023-12-14 16:45:39 +08:00
parent 1e20b8be3a
commit 924e313c80
2 changed files with 23 additions and 10 deletions

View File

@ -10,17 +10,21 @@
-author("aresei"). -author("aresei").
-include("iot.hrl"). -include("iot.hrl").
-export([insert/4]). -export([insert/6]).
%% API %% 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()}. 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">>, #{ mysql_pool:insert(mysql_iot, <<"ai_event_logs">>, #{
<<"event_type">> => EventType, <<"event_type">> => EventType,
<<"host_uuid">> => HostUUID, <<"host_uuid">> => HostUUID,
<<"device_uuid">> => DeviceUUID, <<"device_uuid">> => DeviceUUID,
<<"scene_id">> => SceneId,
<<"micro_id">> => MicroId,
<<"content">> => Content, <<"content">> => Content,
<<"created_at">> => calendar:local_time() <<"created_at">> => calendar:local_time()
}, true). }, true).

View File

@ -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]), 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}) -> lists:foreach(fun(#{<<"props">> := Props, <<"name">> := Name, <<"version">> := Version, <<"version_copy">> := VersionCopy, <<"status">> := Status}) ->
%% props id:id:id %% props id:id:id
[_, SceneId0, MicroId0] = binary:split(Props, <<":">>, [global]), {SceneId, MicroId} = parse_props(Props),
SceneId = binary_to_integer(SceneId0),
MicroId = binary_to_integer(MicroId0),
micro_inform_log:insert(#{ micro_inform_log:insert(#{
<<"host_id">> => HostId, <<"host_id">> => HostId,
<<"scene_id">> => SceneId, <<"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), EventText = iot_cipher_aes:decrypt(AES, Event0),
lager:debug("[iot_host] uuid: ~p, get ai_event: ~p", [UUID, EventText]), lager:debug("[iot_host] uuid: ~p, get ai_event: ~p", [UUID, EventText]),
case catch jiffy:decode(EventText, [return_maps]) of 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 case iot_device:is_alive(DeviceUUID) of
error -> error ->
lager:notice("[iot_host] uuid: ~p, device_uuid: ~p is not alive, get ai_event: ~p", [UUID, DeviceUUID, EventText]), lager:notice("[iot_host] uuid: ~p, device_uuid: ~p is not alive, get ai_event: ~p", [UUID, DeviceUUID, EventText]),
ok; ok;
{ok, DevicePid} -> {ok, DevicePid} ->
Params = maps:remove(<<"props">>, Params0),
{SceneId, MicroId} = parse_props(Props),
%% mysql %% mysql
Message = iolist_to_binary(jiffy:encode(Params, [force_utf8])), 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_device:change_status(DevicePid, ?DEVICE_ONLINE),
iot_ai_router:route_uuid(DeviceUUID, EventType, Params) iot_ai_router:route_uuid(DeviceUUID, EventType, Params)
@ -554,3 +554,12 @@ state_map(#state{host_id = HostId, uuid = UUID, aes = Aes, has_session = HasSess
channel_pid => ChannelPid, channel_pid => ChannelPid,
metrics => Metrics metrics => Metrics
}. }.
%% 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}.