fix heartbeat
This commit is contained in:
parent
aa4a62f3e8
commit
7fd82ac61d
@ -307,7 +307,8 @@ handle_event(cast, {handle, {ping, Metrics}}, ?STATE_ACTIVATED, State = #state{u
|
||||
{keep_state, State#state{metrics = Metrics}};
|
||||
|
||||
%% 心跳机制
|
||||
handle_event(cast, heartbeat, _, State = #state{heartbeat_counter = HeartbeatCounter}) ->
|
||||
handle_event(cast, heartbeat, _, State = #state{uuid = UUID, heartbeat_counter = HeartbeatCounter}) ->
|
||||
maybe_mark_host_online(UUID),
|
||||
{keep_state, State#state{heartbeat_counter = HeartbeatCounter + 1}};
|
||||
|
||||
%% 没有收到心跳包,主机下线, 设备状态不变
|
||||
@ -403,3 +404,18 @@ maybe_mark_host_offline(UUID) ->
|
||||
logger:warning("[iot_host] host: ~p, load status failed while marking offline: ~p", [UUID, Other]),
|
||||
ok
|
||||
end.
|
||||
|
||||
-spec maybe_mark_host_online(binary()) -> ok.
|
||||
maybe_mark_host_online(UUID) ->
|
||||
case iot_api_client:get_host_by_uuid(UUID) of
|
||||
{ok, #host_info{status = ?HOST_OFFLINE}} ->
|
||||
_ = iot_api_client:change_host_status(UUID, ?HOST_ONLINE),
|
||||
ok;
|
||||
{ok, #host_info{status = ?HOST_ONLINE}} ->
|
||||
ok;
|
||||
{ok, #host_info{status = ?HOST_NOT_JOINED}} ->
|
||||
ok;
|
||||
Other ->
|
||||
logger:warning("[iot_host] host: ~p, load status failed while marking online: ~p", [UUID, Other]),
|
||||
ok
|
||||
end.
|
||||
|
||||
@ -21,6 +21,7 @@ loop(Transport = {udp, Server, _Sock}, Peer) ->
|
||||
Pid = iot_host:get_pid(HostUUID),
|
||||
iot_host:heartbeat(Pid),
|
||||
loop(Transport, Peer);
|
||||
{datagram, Server, _} ->
|
||||
exit(normal)
|
||||
{datagram, Server, Packet} ->
|
||||
logger:warning("[udp_server] ignore invalid heartbeat packet from peer: ~p, packet: ~p", [Peer, Packet]),
|
||||
loop(Transport, Peer)
|
||||
end.
|
||||
Loading…
x
Reference in New Issue
Block a user