add push tag

This commit is contained in:
anlicheng 2024-12-20 10:00:34 +08:00
parent 0ffbfbeb66
commit c2ca75ba16

View File

@ -13,7 +13,7 @@
%% API
-export([start_link/0]).
-export([forward/4, get_status/0]).
-export([forward/4, get_status/0, set_push_tag/1]).
-export([test/0]).
%% gen_server callbacks
@ -29,6 +29,9 @@
location_map :: #{},
succ_counter = 0,
fail_counter = 0,
%%
push_tag = true,
logger_pid :: pid()
}).
@ -42,6 +45,10 @@ test() ->
<<"attachments">> => []
}).
-spec set_push_tag(Tag :: boolean()) -> ok.
set_push_tag(Tag) when is_boolean(Tag) ->
gen_server:call(?MODULE, {set_push_tag, Tag}).
-spec forward(LocationCode :: binary(), DynamicLocationCode :: binary(), EventType :: integer(), Params :: map()) -> no_return().
forward(LocationCode, DynamicLocationCode, EventType, Params) when is_binary(LocationCode), is_binary(DynamicLocationCode), is_integer(EventType), is_map(Params) ->
gen_server:cast(?MODULE, {forward, LocationCode, DynamicLocationCode, EventType, Params}).
@ -86,6 +93,10 @@ init([]) ->
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call({set_push_tag, NTag}, _From, State = #state{push_tag = OldPushTag}) ->
lager:debug("[iot_donghuoliren_endpoint] push tag from: ~p, to: ~p", [OldPushTag, NTag]),
{reply, ok, State#state{push_tag = NTag}};
handle_call(get_status, _From, State = #state{succ_counter = SuccCounter, location_map = LocationMap, fail_counter = FailCounter}) ->
{reply, {ok, #{succ => SuccCounter, location_map => LocationMap, fail => FailCounter}}, State}.
@ -96,7 +107,7 @@ handle_call(get_status, _From, State = #state{succ_counter = SuccCounter, locati
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
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, push_tag = PushTag, succ_counter = SuccCounter, fail_counter = FailCounter}) ->
Location = maps:get(LocationCode, LocationMap, <<"">>),
lager:debug("[iot_donghuoliren_endpoint] location_code: ~p, location: ~ts", [LocationCode, Location]),
@ -106,19 +117,24 @@ handle_cast({forward, LocationCode, DynamicLocationCode, EventType, Params},
Sign = iot_util:md5(iolist_to_binary([Token, Body, Token])),
Url1 = Url ++ "?sign=" ++ binary_to_list(Sign),
case do_post(Url1, Body) of
{ok, RespBody} ->
%%
iot_logger:write(LoggerPid, [<<"OK">>, list_to_binary(Url1), Body, RespBody]),
{noreply, State#state{succ_counter = SuccCounter + 1}};
{error, Reason} ->
NReason = iolist_to_binary(io_lib:format("~p", [Reason])),
iot_logger:write(LoggerPid, [<<"ERROR">>, list_to_binary(Url1), Body, NReason]),
{noreply, State#state{fail_counter = FailCounter + 1}}
case PushTag of
true ->
case do_post(Url1, Body) of
{ok, RespBody} ->
%%
iot_logger:write(LoggerPid, [<<"OK">>, list_to_binary(Url1), Body, RespBody]),
{noreply, State#state{succ_counter = SuccCounter + 1}};
{error, Reason} ->
NReason = iolist_to_binary(io_lib:format("~p", [Reason])),
iot_logger:write(LoggerPid, [<<"ERROR">>, list_to_binary(Url1), Body, NReason]),
{noreply, State#state{fail_counter = FailCounter + 1}}
end;
false ->
iot_logger:write(LoggerPid, [<<"DISCARD">>, list_to_binary(Url1), Body])
end;
error ->
lager:notice("[iot_donghuoliren_endpoint] format_event error, message is: ~p", [Params])
end.
end.
%% @private
%% @doc Handling all non call/cast messages