This commit is contained in:
anlicheng 2026-05-11 10:26:24 +08:00
parent 627be12a6e
commit 748a4c2d9a
3 changed files with 21 additions and 13 deletions

View File

@ -247,17 +247,22 @@ handle_event({call, From}, {cancel_command_call, Ref}, _, State = #state{channel
end; end;
%% , pub/sub %% , pub/sub
handle_event({call, From}, {pub, Topic, Qos, Content}, ?STATE_ACTIVATED, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) -> handle_event({call, From}, {pub, Topic, Qos, Content}, StateName, State = #state{uuid = UUID, channel_pid = ChannelPid, has_session = HasSession}) ->
case HasSession andalso is_pid(ChannelPid) of case StateName of
true -> ?STATE_ACTIVATED ->
logger:debug("[iot_host] host: ~p, publish to topic: ~p, content: ~p", [UUID, Topic, Content]), case HasSession andalso is_pid(ChannelPid) of
%% websocket发送消息 true ->
ssl_channel:pub(ChannelPid, Topic, Qos, Content), logger:debug("[iot_host] host: ~p, publish to topic: ~p, content: ~p", [UUID, Topic, Content]),
%% websocket发送消息
ssl_channel:pub(ChannelPid, Topic, Qos, Content),
{keep_state, State, [{reply, From, ok}]}; {keep_state, State, [{reply, From, ok}]};
false -> false ->
logger:debug("[iot_host] uuid: ~p, publish to topic: ~p, content: ~p, invalid state: ~p", [UUID, Topic, Content, state_map(State)]), logger:debug("[iot_host] uuid: ~p, publish to topic: ~p, content: ~p, invalid state: ~p", [UUID, Topic, Content, state_map(State)]),
{keep_state, State, [{reply, From, {error, <<"主机离线,发送失败"/utf8>>}}]} {keep_state, State, [{reply, From, {error, <<"主机离线,发送失败"/utf8>>}}]}
end;
?STATE_DENIED ->
{keep_state, State, [{reply, From, {error, <<"主机未激活,发送失败"/utf8>>}}]}
end; end;
%% / iot efka 线 host %% / iot efka 线 host

View File

@ -84,8 +84,12 @@ handle_request("POST", "/host/pub", _, #{<<"uuid">> := UUID, <<"topic">> := Topi
logger:debug("[host_handler] pub host_id: ~p, topic: ~p, failed with reason: ~p", [UUID, Topic, Reason]), logger:debug("[host_handler] pub host_id: ~p, topic: ~p, failed with reason: ~p", [UUID, Topic, Reason]),
{ok, 200, iot_util:json_error(400, <<"host not found">>)}; {ok, 200, iot_util:json_error(400, <<"host not found">>)};
{ok, Pid} when is_pid(Pid) -> {ok, Pid} when is_pid(Pid) ->
ok = iot_host:pub(Pid, Topic, Qos, Content), case iot_host:pub(Pid, Topic, Qos, Content) of
{ok, 200, iot_util:json_data(<<"success">>)} ok ->
{ok, 200, iot_util:json_data(<<"success">>)};
{error, Reason} ->
{ok, 200, iot_util:json_error(400, Reason)}
end
end; end;
handle_request(_, Path, _, _) -> handle_request(_, Path, _, _) ->

View File

@ -118,7 +118,6 @@ handle_heartbeat_packet(Packet, Peer) when is_binary(Packet) ->
{ok, UUID, Timestamp, Payload, Mac} -> {ok, UUID, Timestamp, Payload, Mac} ->
case efka_client_store:verify_heartbeat(UUID, Timestamp, Payload, Mac) of case efka_client_store:verify_heartbeat(UUID, Timestamp, Payload, Mac) of
true -> true ->
logger:warning("[udp_server] heartbeat auth from peer: ~p, uuid: ~p", [Peer, UUID]),
Pid = iot_host:get_pid(UUID), Pid = iot_host:get_pid(UUID),
iot_host:heartbeat(Pid); iot_host:heartbeat(Pid);
false -> false ->