fix donghuoliren

This commit is contained in:
anlicheng 2024-12-16 17:32:54 +08:00
parent c8697631de
commit ebf5c95f5e
2 changed files with 57 additions and 37 deletions

View File

@ -35,7 +35,7 @@
-define(METHOD_FEEDBACK_RESULT, 16#06). -define(METHOD_FEEDBACK_RESULT, 16#06).
-define(METHOD_EVENT, 16#07). -define(METHOD_EVENT, 16#07).
%% ai识别的事件上报 %% ai识别的事件上报
-define(METHOD_AI_EVENT, 16#08). -define(METHOD_AI_EVENT, 17#08).
%% %%
-define(PACKET_REQUEST, 16#01). -define(PACKET_REQUEST, 16#01).

View File

@ -99,21 +99,25 @@ handle_cast({forward, LocationCode, DynamicLocationCode, EventType, Params},
State = #state{url = Url, token = Token, location_map = LocationMap, logger_pid = LoggerPid, succ_counter = SuccCounter, fail_counter = FailCounter}) -> State = #state{url = Url, token = Token, location_map = LocationMap, logger_pid = LoggerPid, succ_counter = SuccCounter, fail_counter = FailCounter}) ->
Location = maps:get(LocationCode, LocationMap, <<"">>), Location = maps:get(LocationCode, LocationMap, <<"">>),
Body = format_event(Location, DynamicLocationCode, EventType, Params), case format_event(Location, DynamicLocationCode, EventType, Params) of
%% {ok, Body} ->
Sign = iot_util:md5(iolist_to_binary([Token, Body, Token])), %%
Url1 = Url ++ "?sign=" ++ binary_to_list(Sign), Sign = iot_util:md5(iolist_to_binary([Token, Body, Token])),
Url1 = Url ++ "?sign=" ++ binary_to_list(Sign),
case do_post(Url1, Body) of case do_post(Url1, Body) of
{ok, RespBody} -> {ok, RespBody} ->
%% %%
iot_logger:write(LoggerPid, [<<"OK">>, list_to_binary(Url1), Body, RespBody]), iot_logger:write(LoggerPid, [<<"OK">>, list_to_binary(Url1), Body, RespBody]),
{noreply, State#state{succ_counter = SuccCounter + 1}}; {noreply, State#state{succ_counter = SuccCounter + 1}};
{error, Reason} -> {error, Reason} ->
NReason = iolist_to_binary(io_lib:format("~p", [Reason])), NReason = iolist_to_binary(io_lib:format("~p", [Reason])),
iot_logger:write(LoggerPid, [<<"ERROR">>, list_to_binary(Url1), Body, NReason]), iot_logger:write(LoggerPid, [<<"ERROR">>, list_to_binary(Url1), Body, NReason]),
{noreply, State#state{fail_counter = FailCounter + 1}} {noreply, State#state{fail_counter = FailCounter + 1}}
end. end;
error ->
lager:notice("[iot_donghuoliren_endpoint] format_event error, message is: ~p", [Params])
end.
%% @private %% @private
%% @doc Handling all non call/cast messages %% @doc Handling all non call/cast messages
@ -165,32 +169,48 @@ do_post(Url, Body) when is_binary(Body) ->
end. end.
%% %%
-spec format_event(LocationCode :: binary(), DynamicLocationCode :: binary(), EventType :: integer(), Params :: map()) -> Body :: binary(). -spec format_event(LocationCode :: binary(), DynamicLocationCode :: binary(), EventType :: integer(), Params :: map()) -> {ok, Body :: binary()} | error.
format_event(Location, DynamicLocationCode, EventType, format_event(Location, DynamicLocationCode, EventType,
#{<<"datetime">> := Datetime, <<"attachments">> := Attachments0}) #{<<"datetime">> := Datetime, <<"rtsp">> := RTSPUrl, <<"attachments">> := Attachments0})
when is_binary(Location), is_binary(DynamicLocationCode), is_integer(EventType) -> when is_binary(Location), is_binary(DynamicLocationCode), is_integer(EventType), is_binary(RTSPUrl) ->
%Attachments = lists:map(fun(#{<<"filename">> := Filename}) -> Attachments = lists:map(fun(#{<<"filename">> := Filename, <<"type">> := Type}) ->
% {ok, FileUrl} = iot_util:file_uri(Filename), {ok, FileUrl} = iot_util:file_uri(Filename),
% Name = filename:basename(FileUrl), {Type, FileUrl}
% #{<<"name">> => Name, <<"url">> => FileUrl} end, Attachments0),
% end, Attachments0),
Attachments = [ %%
<<"https://lgsiot.njau.edu.cn/upload/2024/11/29/2024-11-29-1732842080-1732842100.mp4">>, Videos = lists:filtermap(fun({Type, Url}) ->
<<"https://lgsiot.njau.edu.cn/upload/2024/11/29/2024-11-29-1732842080-1732842100.jpg">> case Type =:= <<"video">> of
], true ->
{true, Url};
false -> false
end
end, Attachments),
%%
Thumbnails = lists:filtermap(fun({Type, Url}) ->
case Type =:= <<"thumbnail">> of
true ->
{true, Url};
false ->
false
end
end, Attachments),
%% key的顺序限制 case length(Videos) > 0 andalso length(Thumbnails) > 0 of
Params = #{ true ->
<<"attachments">> => Attachments, %% key的顺序限制
<<"eventLocation">> => Location, Params = #{
<<"eventTime">> => Datetime, <<"attachments">> => [hd(Videos), hd(Thumbnails)],
<<"eventType">> => <<"动火离人"/utf8>>, <<"eventLocation">> => Location,
<<"videoJkAddr">> => <<"rtsp://admin:admin@123@192.168.111.147/cam/realmonitor?channel=1&subtype=0">> <<"eventTime">> => Datetime,
}, <<"eventType">> => <<"动火离人"/utf8>>,
<<"videoJkAddr">> => RTSPUrl
iolist_to_binary(serialize(Params)). },
{ok, iolist_to_binary(serialize(Params))};
false ->
error
end.
%% %%
-spec serialize(M :: map()) -> JsonString :: binary(). -spec serialize(M :: map()) -> JsonString :: binary().