diff --git a/apps/iot/src/endpoint/iot_zd_endpoint.erl b/apps/iot/src/endpoint/iot_zd_endpoint.erl index b941ef6..23806ba 100644 --- a/apps/iot/src/endpoint/iot_zd_endpoint.erl +++ b/apps/iot/src/endpoint/iot_zd_endpoint.erl @@ -36,10 +36,7 @@ %% 定时器 timer_ref :: undefined | reference(), %% 是否繁忙 - is_busy = false :: boolean(), - - %% 记录成功处理的消息数, 记日期和总数的映射关系 - acc_num = #{} + is_busy = false :: boolean() }). %%%=================================================================== @@ -136,7 +133,7 @@ handle_event(info, fetch_next, connected, State = #state{postman_pid = PostmanPi end; %% 收到确认消息 -handle_event(info, {ack, AssocMessage}, StateName, State = #state{timer_ref = TimerRef, acc_num = AccNum, logger_pid = LoggerPid}) -> +handle_event(info, {ack, AssocMessage}, StateName, State = #state{timer_ref = TimerRef, logger_pid = LoggerPid}) -> %% 记录日志信息 iot_logger:write(LoggerPid, AssocMessage), @@ -146,11 +143,10 @@ handle_event(info, {ack, AssocMessage}, StateName, State = #state{timer_ref = Ti end, is_reference(TimerRef) andalso erlang:cancel_timer(TimerRef), - Date = iot_util:date(), - Num = maps:get(Date, AccNum, 0), - NAccNum = AccNum#{Date => Num + 1}, + Key = get_counter_key(iot_util:date()), + mnesia_counter:inc(Key), - {keep_state, State#state{timer_ref = undefined, acc_num = NAccNum, is_busy = false}, Actions}; + {keep_state, State#state{timer_ref = undefined, is_busy = false}, Actions}; %% 收到重发过期请求 handle_event(info, {timeout, _, {repost_ticker, Body}}, connected, State = #state{postman_pid = PostmanPid}) -> @@ -177,7 +173,10 @@ handle_event(info, {timeout, _, create_postman}, disconnected, State = #state{mq end; %% 获取当前统计信息 -handle_event({call, From}, get_stat, StateName, State = #state{acc_num = AccNum, iot_queue = Q}) -> +handle_event({call, From}, get_stat, StateName, State = #state{iot_queue = Q}) -> + Key = get_counter_key(iot_util:date()), + AccNum = mnesia_counter:get_count(Key), + Stat = #{ <<"acc_num">> => AccNum, <<"queue_num">> => iot_queue:len(Q), @@ -186,8 +185,11 @@ handle_event({call, From}, get_stat, StateName, State = #state{acc_num = AccNum, {keep_state, State, [{reply, From, Stat}]}; %% 获取当前统计信息 -handle_event({call, From}, {get_num, Date}, _StateName, State = #state{acc_num = AccNum}) -> - {keep_state, State, [{reply, From, maps:get(Date, AccNum, 0)}]}; +handle_event({call, From}, {get_num, Date}, _StateName, State = #state{}) -> + Key = get_counter_key(Date), + AccNum = mnesia_counter:get_count(Key), + + {keep_state, State, [{reply, From, AccNum}]}; %% postman进程挂掉时,重新建立新的 handle_event(info, {'EXIT', PostmanPid, Reason}, connected, State = #state{timer_ref = TimerRef, postman_pid = PostmanPid}) -> @@ -265,4 +267,8 @@ format_data(LocationCode, DynamicLocationCode, Fields, Timestamp) when is_binary catch _:Reason -> lager:warning("[iot_zd_endpoint] location_code: ~p, format_data get error: ~p", [LocationCode, Reason]), error - end. \ No newline at end of file + end. + +-spec get_counter_key(Date :: string()) -> string(). +get_counter_key(Date) when is_list(Date) -> + "iot_zd:" ++ Date. \ No newline at end of file