fix jinzhi endpoint

This commit is contained in:
anlicheng 2024-07-23 17:49:59 +08:00
parent 23b144e48a
commit e305ce0fff
2 changed files with 33 additions and 46 deletions

View File

@ -175,36 +175,33 @@ do_post(Url, Id, Body) when is_list(Url), is_integer(Id), is_binary(Body) ->
%%
-spec format_events(LocationCode :: binary(), DynamicLocationCode :: binary(), Events :: list(), PriKey :: public_key:private_key()) -> binary().
format_events(LocationCode, DynamicLocationCode, Events, PriKey) ->
% <<"occurrenceTime">> => <<"2023-06-10 12:00:00">>,
%%
TextEvents = lists:map(fun({_EventType, #{<<"event_code">> := EventCode, <<"description">> := Description, <<"datetime">> := Datetime, <<"attachments">> := Attachments0}}) ->
format_events(LocationCode, DynamicLocationCode, Events, PriKey) when is_binary(LocationCode), is_binary(DynamicLocationCode), is_list(Events) ->
DeviceInfo = format_event(LocationCode, hd(Events)),
ReqData = #{
<<"sign">> => sign(DeviceInfo, PriKey),
<<"sysId">> => ?SYS_ID,
<<"taskId">> => <<"">>,
<<"count">> => length(Events),
<<"deviceInfo">> => DeviceInfo
},
iolist_to_binary(jiffy:encode(ReqData, [force_utf8])).
format_event(LocationCode, {_EventType, #{<<"event_code">> := EventCode, <<"description">> := Description, <<"datetime">> := Datetime, <<"attachments">> := Attachments0}}) ->
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">>,
#{
<<"location">> => LocationCode,
<<"category">> => EventCode,
<<"description">> => Description,
<<"occurrenceTime">> => Datetime,
<<"attachments">> => Attachments
}
end, Events),
DeviceInfo = #{
<<"location">> => LocationCode,
<<"dynamic_location">> => DynamicLocationCode,
<<"events">> => TextEvents
},
ReqData = #{
<<"sign">> => sign(DeviceInfo, PriKey),
<<"sysId">> => ?SYS_ID,
<<"deviceInfo">> => DeviceInfo
},
iolist_to_binary(jiffy:encode(ReqData, [force_utf8])).
}.
-spec generate_private_key(PriFile :: string()) -> public_key:private_key().
generate_private_key(PriFile) when is_list(PriFile) ->

View File

@ -19,16 +19,9 @@
-define(SERVER, ?MODULE).
%%
-record(task, {
counter = 0,
%%
buffer = []
}).
-record(state, {
device_uuid :: binary(),
group_tasks = #{} :: map()
group_buffers = #{} :: map()
}).
%%%===================================================================
@ -77,20 +70,19 @@ handle_call(_Request, _From, State = #state{}) ->
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
%% ;
handle_cast({publish, EventType, Params}, State = #state{device_uuid = DeviceUUID, group_tasks = GroupTasks}) ->
handle_cast({publish, EventType, Params}, State = #state{device_uuid = DeviceUUID, group_buffers = GroupBuffers}) ->
GroupKey = group_by(EventType, Params),
case maps:find(GroupKey, GroupTasks) of
{ok, Task0 = #task{counter = Counter, buffer = Buffer}} ->
Task = Task0#task{counter = Counter + 1, buffer = [{EventType, Params}|Buffer]},
{noreply, State#state{group_tasks = maps:put(GroupKey, Task, GroupTasks)}};
case maps:find(GroupKey, GroupBuffers) of
{ok, Buffer} ->
NBuffer = [{EventType, Params}|Buffer],
{noreply, State#state{group_buffers = maps:put(GroupKey, NBuffer, GroupBuffers)}};
error ->
ThrottleTime = iot_event_period_settings:get_throttle(GroupKey),
erlang:start_timer(ThrottleTime * 1000, self(), {throttle_ticker, GroupKey}),
%%
iot_ai_router:batch_route_uuid(DeviceUUID, [{EventType, Params}]),
Task = #task{buffer = [], counter = 1},
{noreply, State#state{group_tasks = maps:put(GroupKey, Task, GroupTasks)}}
{noreply, State#state{group_buffers = maps:put(GroupKey, [], GroupBuffers)}}
end.
%% @private
@ -99,21 +91,19 @@ handle_cast({publish, EventType, Params}, State = #state{device_uuid = DeviceUUI
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_info({timeout, _, {throttle_ticker, GroupKey}}, State = #state{device_uuid = DeviceUUID, group_tasks = GroupTasks}) ->
handle_info({timeout, _, {throttle_ticker, GroupKey}}, State = #state{device_uuid = DeviceUUID, group_buffers = GroupBuffers}) ->
ThrottleTime = iot_event_period_settings:get_throttle(GroupKey),
erlang:start_timer(ThrottleTime * 1000, self(), {throttle_ticker, GroupKey}),
case maps:find(GroupKey, GroupTasks) of
{ok, Task0 = #task{buffer = Buffer}} ->
case maps:find(GroupKey, GroupBuffers) of
{ok, Buffer} ->
case length(Buffer) > 0 of
true ->
Events = lists:reverse(Buffer),
iot_ai_router:batch_route_uuid(DeviceUUID, Events);
iot_ai_router:batch_route_uuid(DeviceUUID, Buffer);
false ->
ok
end,
Task = Task0#task{buffer = []},
{noreply, State#state{group_tasks = maps:put(GroupKey, Task, GroupTasks)}};
{noreply, State#state{group_buffers = maps:put(GroupKey, [], GroupBuffers)}};
error ->
{noreply, State}
end.