fix jinzhi

This commit is contained in:
anlicheng 2024-12-02 16:48:45 +08:00
parent c01cb4f725
commit 6ee341f2bd

View File

@ -93,12 +93,16 @@ handle_call(_Request, _From, State = #state{}) ->
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast({forward, LocationCode, DynamicLocationCode, EventType, Params}, State = #state{id = Id, timer_map = TimerMap, pri_key = PriKey, url = Url}) ->
ReqBody = format_event(LocationCode, DynamicLocationCode, EventType, Params, PriKey),
Res = catch do_post(Url, Id, ReqBody),
lager:debug("[iot_jinzhi_endpoint] format_data: ~p, post result: ~p", [ReqBody, Res]),
TimerRef = erlang:start_timer(?RETRY_INTERVAL, self(), {repost_ticker, Id, ReqBody}),
case format_event(LocationCode, DynamicLocationCode, EventType, Params, PriKey) of
error ->
{noreply, State};
{ok, ReqBody} ->
Res = catch do_post(Url, Id, ReqBody),
lager:debug("[iot_jinzhi_endpoint] format_data: ~p, post result: ~p", [ReqBody, Res]),
TimerRef = erlang:start_timer(?RETRY_INTERVAL, self(), {repost_ticker, Id, ReqBody}),
{noreply, State#state{id = Id + 1, timer_map = maps:put(Id, TimerRef, TimerMap)}}.
{noreply, State#state{id = Id + 1, timer_map = maps:put(Id, TimerRef, TimerMap)}}
end.
%% @private
%% @doc Handling all non call/cast messages
@ -176,36 +180,42 @@ do_post(Url, Id, Body) when is_list(Url), is_integer(Id), is_binary(Body) ->
end).
%%
-spec format_event(LocationCode :: binary(), DynamicLocationCode :: binary(), EventType :: integer(), Params :: map(), PriKey :: public_key:private_key()) -> binary().
-spec format_event(LocationCode :: binary(), DynamicLocationCode :: binary(), EventType :: integer(), Params :: map(), PriKey :: public_key:private_key()) -> error | {ok, binary()}.
format_event(LocationCode, DynamicLocationCode, EventType, #{<<"event_code">> := EventCode, <<"description">> := Description, <<"datetime">> := Datetime, <<"attachments">> := Attachments0}, PriKey)
when is_binary(LocationCode), is_binary(DynamicLocationCode), is_integer(EventType) ->
Attachments = lists:map(fun(#{<<"filename">> := Filename}) ->
{ok, FileUrl} = iot_util:file_uri(Filename),
Name = filename:basename(FileUrl),
#{<<"name">> => Name, <<"url">> => FileUrl}
end, Attachments0),
%% 2024-12-02
case lists:member(EventCode, [<<"23104">>]) of
true ->
error;
false ->
Attachments = lists:map(fun(#{<<"filename">> := Filename}) ->
{ok, FileUrl} = iot_util:file_uri(Filename),
Name = filename:basename(FileUrl),
#{<<"name">> => Name, <<"url">> => FileUrl}
end, Attachments0),
% <<"occurrenceTime">> => <<"2023-06-10 12:00:00">>,
% <<"occurrenceTime">> => <<"2023-06-10 12:00:00">>,
LocationCode1 = fake_location_code(EventCode, LocationCode),
DeviceInfo = #{
% <<"location">> => LocationCode,
<<"location">> => LocationCode1,
<<"category">> => EventCode,
<<"description">> => Description,
<<"occurrenceTime">> => Datetime,
<<"attachments">> => Attachments
},
LocationCode1 = fake_location_code(EventCode, LocationCode),
DeviceInfo = #{
% <<"location">> => LocationCode,
<<"location">> => LocationCode1,
<<"category">> => EventCode,
<<"description">> => Description,
<<"occurrenceTime">> => Datetime,
<<"attachments">> => Attachments
},
ReqData = #{
<<"sign">> => sign(DeviceInfo, PriKey),
<<"sysId">> => ?SYS_ID,
<<"taskId">> => generate_task_id(LocationCode1, EventCode),
<<"count">> => 1,
<<"deviceInfo">> => DeviceInfo
},
iolist_to_binary(jiffy:encode(ReqData, [force_utf8])).
ReqData = #{
<<"sign">> => sign(DeviceInfo, PriKey),
<<"sysId">> => ?SYS_ID,
<<"taskId">> => generate_task_id(LocationCode1, EventCode),
<<"count">> => 1,
<<"deviceInfo">> => DeviceInfo
},
{ok, iolist_to_binary(jiffy:encode(ReqData, [force_utf8]))}
end.
-spec generate_task_id(LocationCode :: binary(), EventCode :: binary()) -> binary().
generate_task_id(LocationCode, EventCode) when is_binary(LocationCode), is_binary(EventCode) ->