From c6e7f7669e735dc50dec988780dc1980e5e8350f Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 8 May 2025 13:01:02 +0800 Subject: [PATCH] fix server --- apps/iot/include/iot.hrl | 10 +++- .../{http_handler => http}/device_handler.erl | 0 .../endpoint_handler.erl | 0 .../{http_handler => http}/host_handler.erl | 0 .../{http_handler => http}/http_protocol.erl | 0 apps/iot/src/http/http_server.erl | 40 +++++++++++++ .../service_handler.erl | 0 apps/iot/src/iot_app.erl | 57 ++----------------- .../tcp_channel.erl} | 7 +-- apps/iot/src/tcp/tcp_server.erl | 37 ++++++++++++ 10 files changed, 90 insertions(+), 61 deletions(-) rename apps/iot/src/{http_handler => http}/device_handler.erl (100%) rename apps/iot/src/{http_handler => http}/endpoint_handler.erl (100%) rename apps/iot/src/{http_handler => http}/host_handler.erl (100%) rename apps/iot/src/{http_handler => http}/http_protocol.erl (100%) create mode 100644 apps/iot/src/http/http_server.erl rename apps/iot/src/{http_handler => http}/service_handler.erl (100%) rename apps/iot/src/{iot_tcp_channel.erl => tcp/tcp_channel.erl} (96%) create mode 100644 apps/iot/src/tcp/tcp_server.erl diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index dce6040..afd255b 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -31,6 +31,14 @@ -define(PACKET_PUBLISH, 16#03). -define(PACKET_PUBLISH_RESPONSE, 16#04). +%% 消息体类型 +-define(PACKET_REQUEST, 16#01). +-define(PACKET_RESPONSE, 16#02). + +%% 服务器端推送消息 +-define(PACKET_PUBLISH, 16#03). +-define(PACKET_PUBLISH_RESPONSE, 16#04). + %% 主机端上报数据类型标识 %% 建立到websocket的register关系 -define(METHOD_AUTH, 16#00). @@ -42,8 +50,6 @@ -define(METHOD_FEEDBACK_STEP, 16#05). -define(METHOD_EVENT, 16#07). -%% ai识别的事件上报 --define(METHOD_AI_EVENT, 16#08). -define(METHOD_PHASE, 16#09). diff --git a/apps/iot/src/http_handler/device_handler.erl b/apps/iot/src/http/device_handler.erl similarity index 100% rename from apps/iot/src/http_handler/device_handler.erl rename to apps/iot/src/http/device_handler.erl diff --git a/apps/iot/src/http_handler/endpoint_handler.erl b/apps/iot/src/http/endpoint_handler.erl similarity index 100% rename from apps/iot/src/http_handler/endpoint_handler.erl rename to apps/iot/src/http/endpoint_handler.erl diff --git a/apps/iot/src/http_handler/host_handler.erl b/apps/iot/src/http/host_handler.erl similarity index 100% rename from apps/iot/src/http_handler/host_handler.erl rename to apps/iot/src/http/host_handler.erl diff --git a/apps/iot/src/http_handler/http_protocol.erl b/apps/iot/src/http/http_protocol.erl similarity index 100% rename from apps/iot/src/http_handler/http_protocol.erl rename to apps/iot/src/http/http_protocol.erl diff --git a/apps/iot/src/http/http_server.erl b/apps/iot/src/http/http_server.erl new file mode 100644 index 0000000..d24c592 --- /dev/null +++ b/apps/iot/src/http/http_server.erl @@ -0,0 +1,40 @@ +%%%------------------------------------------------------------------- +%%% @author anlicheng +%%% @copyright (C) 2025, +%%% @doc +%%% +%%% @end +%%% Created : 08. 5月 2025 13:00 +%%%------------------------------------------------------------------- +-module(http_server). +-author("anlicheng"). + +%% API +-export([start/0]). + +%% 启动http服务 +start() -> + {ok, Props} = application:get_env(iot, http_server), + Acceptors = proplists:get_value(acceptors, Props, 50), + MaxConnections = proplists:get_value(max_connections, Props, 10240), + Backlog = proplists:get_value(backlog, Props, 1024), + Port = proplists:get_value(port, Props), + + Dispatcher = cowboy_router:compile([ + {'_', [ + {"/host/[...]", http_protocol, [host_handler]}, + {"/device/[...]", http_protocol, [device_handler]}, + {"/service/[...]", http_protocol, [service_handler]} + ]} + ]), + + TransOpts = [ + {port, Port}, + {num_acceptors, Acceptors}, + {backlog, Backlog}, + {max_connections, MaxConnections} + ], + + {ok, Pid} = cowboy:start_clear(http_listener, TransOpts, #{env => #{dispatch => Dispatcher}}), + + lager:debug("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]). \ No newline at end of file diff --git a/apps/iot/src/http_handler/service_handler.erl b/apps/iot/src/http/service_handler.erl similarity index 100% rename from apps/iot/src/http_handler/service_handler.erl rename to apps/iot/src/http/service_handler.erl diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index 0298e07..e49b895 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -17,9 +17,11 @@ start(_StartType, _StartArgs) -> %% 启动mnesia数据库 start_mnesia(), - start_http_server(), + %% 启动http服务 + http_server:start(), - start_tcp_server(), + %% 启动tcp服务 + tcp_server:start(), iot_sup:start_link(). @@ -28,57 +30,6 @@ stop(_State) -> %% internal functions -%% 启动http服务 -start_http_server() -> - {ok, Props} = application:get_env(iot, http_server), - Acceptors = proplists:get_value(acceptors, Props, 50), - MaxConnections = proplists:get_value(max_connections, Props, 10240), - Backlog = proplists:get_value(backlog, Props, 1024), - Port = proplists:get_value(port, Props), - - Dispatcher = cowboy_router:compile([ - {'_', [ - {"/host/[...]", http_protocol, [host_handler]}, - {"/device/[...]", http_protocol, [device_handler]}, - {"/service/[...]", http_protocol, [service_handler]} - ]} - ]), - - TransOpts = [ - {port, Port}, - {num_acceptors, Acceptors}, - {backlog, Backlog}, - {max_connections, MaxConnections} - ], - - {ok, Pid} = cowboy:start_clear(http_listener, TransOpts, #{env => #{dispatch => Dispatcher}}), - - lager:debug("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]). - -%% 启动tcp服务 -start_tcp_server() -> - {ok, Props} = application:get_env(iot, tcp_server), - Acceptors = proplists:get_value(acceptors, Props, 50), - MaxConnections = proplists:get_value(max_connections, Props, 10240), - Backlog = proplists:get_value(backlog, Props, 1024), - Port = proplists:get_value(port, Props), - - TransOpts = [ - {tcp_options, [ - binary, - {reuseaddr, true}, - {active, false}, - {packet, 4}, - {nodelay, false}, - {backlog, Backlog} - ]}, - {acceptors, Acceptors}, - {max_connections, MaxConnections} - ], - {ok, _} = esockd:open('iot/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}), - - lager:debug("[iot_app] the tcp server start at: ~p", [Port]). - %% 启动内存数据库 start_mnesia() -> %% 启动数据库 diff --git a/apps/iot/src/iot_tcp_channel.erl b/apps/iot/src/tcp/tcp_channel.erl similarity index 96% rename from apps/iot/src/iot_tcp_channel.erl rename to apps/iot/src/tcp/tcp_channel.erl index 7cce57a..2e58ac3 100644 --- a/apps/iot/src/iot_tcp_channel.erl +++ b/apps/iot/src/tcp/tcp_channel.erl @@ -6,7 +6,7 @@ %%% @end %%% Created : 11. 1月 2021 上午12:17 %%%------------------------------------------------------------------- --module(iot_tcp_channel). +-module(tcp_channel). -author("licheng5"). -include("iot.hrl"). -include("message_pb.hrl"). @@ -150,11 +150,6 @@ handle_info({tcp, Socket, <>}, State = #state{socket = Socket, host_pid = HostPid}) when is_pid(HostPid) -> - AIEvent = message_pb:decode_msg(AIEventData, ai_event), - iot_host:handle(HostPid, {ai_event, AIEvent}), - {noreply, State}; - %% 主机端的消息响应 handle_info({tcp, Socket, <>}, State = #state{socket = Socket, uuid = UUID, inflight = Inflight}) when PacketId > 0 -> lager:debug("[ws_channel] uuid: ~p, get publish response message: ~p, packet_id: ~p", [UUID, Body, PacketId]), diff --git a/apps/iot/src/tcp/tcp_server.erl b/apps/iot/src/tcp/tcp_server.erl new file mode 100644 index 0000000..95ec474 --- /dev/null +++ b/apps/iot/src/tcp/tcp_server.erl @@ -0,0 +1,37 @@ +%%%------------------------------------------------------------------- +%%% @author anlicheng +%%% @copyright (C) 2025, +%%% @doc +%%% +%%% @end +%%% Created : 08. 5月 2025 12:58 +%%%------------------------------------------------------------------- +-module(tcp_server). +-author("anlicheng"). + +%% API +-export([start/0]). + +%% 启动tcp服务 +start() -> + {ok, Props} = application:get_env(iot, tcp_server), + Acceptors = proplists:get_value(acceptors, Props, 50), + MaxConnections = proplists:get_value(max_connections, Props, 10240), + Backlog = proplists:get_value(backlog, Props, 1024), + Port = proplists:get_value(port, Props), + + TransOpts = [ + {tcp_options, [ + binary, + {reuseaddr, true}, + {active, false}, + {packet, 4}, + {nodelay, false}, + {backlog, Backlog} + ]}, + {acceptors, Acceptors}, + {max_connections, MaxConnections} + ], + {ok, _} = esockd:open('iot/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}), + + lager:debug("[iot_app] the tcp server start at: ~p", [Port]). \ No newline at end of file