add push tag
This commit is contained in:
parent
0ffbfbeb66
commit
c2ca75ba16
@ -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,19 +117,24 @@ 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 do_post(Url1, Body) of
|
case PushTag of
|
||||||
{ok, RespBody} ->
|
true ->
|
||||||
%% 记录日志
|
case do_post(Url1, Body) of
|
||||||
iot_logger:write(LoggerPid, [<<"OK">>, list_to_binary(Url1), Body, RespBody]),
|
{ok, RespBody} ->
|
||||||
{noreply, State#state{succ_counter = SuccCounter + 1}};
|
%% 记录日志
|
||||||
{error, Reason} ->
|
iot_logger:write(LoggerPid, [<<"OK">>, list_to_binary(Url1), Body, RespBody]),
|
||||||
NReason = iolist_to_binary(io_lib:format("~p", [Reason])),
|
{noreply, State#state{succ_counter = SuccCounter + 1}};
|
||||||
iot_logger:write(LoggerPid, [<<"ERROR">>, list_to_binary(Url1), Body, NReason]),
|
{error, Reason} ->
|
||||||
{noreply, State#state{fail_counter = FailCounter + 1}}
|
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;
|
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.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Handling all non call/cast messages
|
%% @doc Handling all non call/cast messages
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user