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 %% API
-export([start_link/0]). -export([start_link/0]).
-export([forward/4, get_status/0]). -export([forward/4, get_status/0, set_push_tag/1]).
-export([test/0]). -export([test/0]).
%% gen_server callbacks %% gen_server callbacks
@ -29,6 +29,9 @@
location_map :: #{}, location_map :: #{},
succ_counter = 0, succ_counter = 0,
fail_counter = 0, fail_counter = 0,
%%
push_tag = true,
logger_pid :: pid() logger_pid :: pid()
}). }).
@ -42,6 +45,10 @@ test() ->
<<"attachments">> => [] <<"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(). -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) -> 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}). gen_server:cast(?MODULE, {forward, LocationCode, DynamicLocationCode, EventType, Params}).
@ -86,6 +93,10 @@ init([]) ->
{noreply, NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: 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}) -> 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}. {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} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_cast({forward, LocationCode, DynamicLocationCode, EventType, Params}, 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, <<"">>), Location = maps:get(LocationCode, LocationMap, <<"">>),
lager:debug("[iot_donghuoliren_endpoint] location_code: ~p, location: ~ts", [LocationCode, Location]), lager:debug("[iot_donghuoliren_endpoint] location_code: ~p, location: ~ts", [LocationCode, Location]),
@ -106,6 +117,8 @@ handle_cast({forward, LocationCode, DynamicLocationCode, EventType, Params},
Sign = iot_util:md5(iolist_to_binary([Token, Body, Token])), Sign = iot_util:md5(iolist_to_binary([Token, Body, Token])),
Url1 = Url ++ "?sign=" ++ binary_to_list(Sign), Url1 = Url ++ "?sign=" ++ binary_to_list(Sign),
case PushTag of
true ->
case do_post(Url1, Body) of case do_post(Url1, Body) of
{ok, RespBody} -> {ok, RespBody} ->
%% %%
@ -116,6 +129,9 @@ handle_cast({forward, LocationCode, DynamicLocationCode, EventType, Params},
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;
false ->
iot_logger:write(LoggerPid, [<<"DISCARD">>, list_to_binary(Url1), Body])
end;
error -> error ->
lager:notice("[iot_donghuoliren_endpoint] format_event error, message is: ~p", [Params]) lager:notice("[iot_donghuoliren_endpoint] format_event error, message is: ~p", [Params])
end. end.