fix
This commit is contained in:
parent
8786df0525
commit
764a7f67c0
@ -34,8 +34,8 @@
|
|||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
transport_pid :: undefined | pid(),
|
transport_pid :: undefined | pid(),
|
||||||
%% 映射关系 #{Ref => PacketId}
|
%% 服务器端推送的消息的未确认列表, 映射关系 #{Ref => PacketId}
|
||||||
inflight = #{}
|
push_inflight = #{}
|
||||||
}).
|
}).
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
@ -98,6 +98,13 @@ callback_mode() ->
|
|||||||
%% @doc If callback_mode is handle_event_function, then whenever a
|
%% @doc If callback_mode is handle_event_function, then whenever a
|
||||||
%% gen_statem receives an event from call/2, cast/2, or as a normal
|
%% gen_statem receives an event from call/2, cast/2, or as a normal
|
||||||
%% process message, this function is called.
|
%% process message, this function is called.
|
||||||
|
handle_event({call, From}, {request_service_config, ReceiverPid, ServiceId}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
|
||||||
|
Ref = efka_transport:request(TransportPid, ReceiverPid, ?METHOD_REQUEST_SERVICE_CONFIG, ServiceId),
|
||||||
|
{keep_state, State, [{reply, From, {ok, Ref}}]};
|
||||||
|
|
||||||
|
handle_event({call, From}, {request_service_config, _ReceiverPid, _ServiceId}, _, State) ->
|
||||||
|
{keep_state, State, [{reply, From, {error, <<"transport is not alive">>}}]};
|
||||||
|
|
||||||
handle_event(cast, {metric_data, ServiceId, DeviceUUID, LineProtocolData}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
|
handle_event(cast, {metric_data, ServiceId, DeviceUUID, LineProtocolData}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
|
||||||
Packet = message_pb:encode_msg(#data{
|
Packet = message_pb:encode_msg(#data{
|
||||||
service_id = ServiceId,
|
service_id = ServiceId,
|
||||||
@ -255,7 +262,7 @@ handle_event(info, {server_push, PacketId, <<?PUSH_STOP_SERVICE:8, ServiceId/bin
|
|||||||
{keep_state, State};
|
{keep_state, State};
|
||||||
|
|
||||||
%% config.json配置信息
|
%% config.json配置信息
|
||||||
handle_event(info, {server_push, PacketId, <<?PUSH_SERVICE_CONFIG:8, ConfigBin/binary>>}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid, inflight = Inflight}) ->
|
handle_event(info, {server_push, PacketId, <<?PUSH_SERVICE_CONFIG:8, ConfigBin/binary>>}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid, push_inflight = PushInflight}) ->
|
||||||
#push_service_config{service_id = ServiceId, config_json = ConfigJson, timeout = Timeout} = message_pb:decode_msg(ConfigBin, push_service_config),
|
#push_service_config{service_id = ServiceId, config_json = ConfigJson, timeout = Timeout} = message_pb:decode_msg(ConfigBin, push_service_config),
|
||||||
|
|
||||||
case efka_service:get_pid(ServiceId) of
|
case efka_service:get_pid(ServiceId) of
|
||||||
@ -270,11 +277,11 @@ handle_event(info, {server_push, PacketId, <<?PUSH_SERVICE_CONFIG:8, ConfigBin/b
|
|||||||
%% 处理超时逻辑
|
%% 处理超时逻辑
|
||||||
erlang:start_timer(Timeout, self(), {request_timeout, Ref}),
|
erlang:start_timer(Timeout, self(), {request_timeout, Ref}),
|
||||||
|
|
||||||
{keep_state, State#state{inflight = maps:put(Ref, PacketId, Inflight)}}
|
{keep_state, State#state{push_inflight = maps:put(Ref, PacketId, PushInflight)}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% 收到需要回复的指令
|
%% 收到需要回复的指令
|
||||||
handle_event(info, {server_push, PacketId, <<?PUSH_INVOKE:8, InvokeBin/binary>>}, ?STATE_ACTIVATED, State = #state{inflight = Inflight, transport_pid = TransportPid}) ->
|
handle_event(info, {server_push, PacketId, <<?PUSH_INVOKE:8, InvokeBin/binary>>}, ?STATE_ACTIVATED, State = #state{push_inflight = PushInflight, transport_pid = TransportPid}) ->
|
||||||
#invoke{service_id = ServiceId, payload = Payload, timeout = Timeout} = message_pb:decode_msg(InvokeBin, invoke),
|
#invoke{service_id = ServiceId, payload = Payload, timeout = Timeout} = message_pb:decode_msg(InvokeBin, invoke),
|
||||||
%% 消息发送到订阅系统
|
%% 消息发送到订阅系统
|
||||||
case efka_service:get_pid(ServiceId) of
|
case efka_service:get_pid(ServiceId) of
|
||||||
@ -289,7 +296,7 @@ handle_event(info, {server_push, PacketId, <<?PUSH_INVOKE:8, InvokeBin/binary>>}
|
|||||||
%% 处理超时逻辑
|
%% 处理超时逻辑
|
||||||
erlang:start_timer(Timeout, self(), {request_timeout, Ref}),
|
erlang:start_timer(Timeout, self(), {request_timeout, Ref}),
|
||||||
|
|
||||||
{keep_state, State#state{inflight = maps:put(Ref, PacketId, Inflight)}}
|
{keep_state, State#state{push_inflight = maps:put(Ref, PacketId, PushInflight)}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% 处理task_log
|
%% 处理task_log
|
||||||
@ -331,11 +338,11 @@ handle_event(info, {server_pub, Topic, Content}, ?STATE_ACTIVATED, State) ->
|
|||||||
{keep_state, State};
|
{keep_state, State};
|
||||||
|
|
||||||
%% 收到来自efka_service的回复
|
%% 收到来自efka_service的回复
|
||||||
handle_event(info, {service_reply, Ref, EmsReply}, ?STATE_ACTIVATED, State = #state{inflight = Inflight, transport_pid = TransportPid}) ->
|
handle_event(info, {service_reply, Ref, EmsReply}, ?STATE_ACTIVATED, State = #state{push_inflight = PushInflight, transport_pid = TransportPid}) ->
|
||||||
case maps:take(Ref, Inflight) of
|
case maps:take(Ref, PushInflight) of
|
||||||
error ->
|
error ->
|
||||||
{keep_state, State};
|
{keep_state, State};
|
||||||
{PacketId, NInflight} ->
|
{PacketId, NPushInflight} ->
|
||||||
Reply = case EmsReply of
|
Reply = case EmsReply of
|
||||||
{ok, Result} ->
|
{ok, Result} ->
|
||||||
#async_call_reply{code = 1, result = Result};
|
#async_call_reply{code = 1, result = Result};
|
||||||
@ -344,19 +351,19 @@ handle_event(info, {service_reply, Ref, EmsReply}, ?STATE_ACTIVATED, State = #st
|
|||||||
end,
|
end,
|
||||||
efka_transport:async_call_reply(TransportPid, PacketId, message_pb:encode_msg(Reply)),
|
efka_transport:async_call_reply(TransportPid, PacketId, message_pb:encode_msg(Reply)),
|
||||||
|
|
||||||
{keep_state, State#state{inflight = NInflight}}
|
{keep_state, State#state{push_inflight = NPushInflight}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% todo 请求超时逻辑处理
|
%% todo 请求超时逻辑处理
|
||||||
handle_event(info, {timeout, _, {request_timeout, Ref}}, ?STATE_ACTIVATED, State = #state{inflight = Inflight, transport_pid = TransportPid}) ->
|
handle_event(info, {timeout, _, {request_timeout, Ref}}, ?STATE_ACTIVATED, State = #state{push_inflight = PushInflight, transport_pid = TransportPid}) ->
|
||||||
case maps:take(Ref, Inflight) of
|
case maps:take(Ref, PushInflight) of
|
||||||
error ->
|
error ->
|
||||||
{keep_state, State};
|
{keep_state, State};
|
||||||
{PacketId, NInflight} ->
|
{PacketId, NPushInflight} ->
|
||||||
Reply = #async_call_reply{code = 0, message = <<"reqeust timeout">>, result = <<>>},
|
Reply = #async_call_reply{code = 0, message = <<"reqeust timeout">>, result = <<>>},
|
||||||
efka_transport:async_call_reply(TransportPid, PacketId, message_pb:encode_msg(Reply)),
|
efka_transport:async_call_reply(TransportPid, PacketId, message_pb:encode_msg(Reply)),
|
||||||
|
|
||||||
{keep_state, State#state{inflight = NInflight}}
|
{keep_state, State#state{push_inflight = NPushInflight}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% transport进程退出
|
%% transport进程退出
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user