This commit is contained in:
anlicheng 2025-09-22 16:31:22 +08:00
parent a690925a28
commit a8aeeb9c68

View File

@ -37,7 +37,7 @@ websocket_init(_State) ->
websocket_handle(ping, State) -> websocket_handle(ping, State) ->
{reply, pong, State}; {reply, pong, State};
websocket_handle({binary, Data}, State) -> websocket_handle({text, Data}, State) ->
Request = jiffy:decode(Data, [return_maps]), Request = jiffy:decode(Data, [return_maps]),
lager:debug("[ws_channle] get request: ~p", [Request]), lager:debug("[ws_channle] get request: ~p", [Request]),
handle_request(Request, State); handle_request(Request, State);
@ -48,8 +48,12 @@ websocket_handle(Info, State) ->
%% %%
websocket_info({topic_broadcast, Topic, Content}, State = #state{}) -> websocket_info({topic_broadcast, Topic, Content}, State = #state{}) ->
Req = jiffy:encode(#{<<"method">> => <<"publish">>, <<"params">> => #{<<"topic">> => Topic, <<"content">> => Content}}, [force_utf8]), Req = iolist_to_binary(jiffy:encode(#{
{reply, {binary, Req}, State}; <<"method">> => <<"publish">>,
<<"params">> => #{<<"topic">> => Topic, <<"content">> => Content}
}, [force_utf8])),
{reply, {text, Req}, State};
%% service进程关闭 %% service进程关闭
websocket_info({'DOWN', _Ref, process, ServicePid, Reason}, State = #state{service_pid = ServicePid}) -> websocket_info({'DOWN', _Ref, process, ServicePid, Reason}, State = #state{service_pid = ServicePid}) ->
@ -83,7 +87,7 @@ handle_request(#{<<"id">> := Id, <<"method">> := <<"register">>, <<"params">> :=
Reply = json_result(Id, <<"ok">>), Reply = json_result(Id, <<"ok">>),
erlang:monitor(process, ServicePid), erlang:monitor(process, ServicePid),
{reply, {binary, Reply}, State#state{service_pid = ServicePid, is_registered = true}}; {reply, {text, Reply}, State#state{service_pid = ServicePid, is_registered = true}};
{error, Error} -> {error, Error} ->
lager:warning("[ws_channel] service_id: ~p, attach_channel get error: ~p", [ServiceId, Error]), lager:warning("[ws_channel] service_id: ~p, attach_channel get error: ~p", [ServiceId, Error]),
{stop, State} {stop, State}
@ -97,16 +101,16 @@ handle_request(#{<<"id">> := Id, <<"method">> := <<"subscribe">>, <<"params">> :
{error, Reason} -> {error, Reason} ->
json_error(Id, -1, Reason) json_error(Id, -1, Reason)
end, end,
{reply, {binary, Reply}, State}; {reply, {text, Reply}, State};
%% %%
handle_request(#{<<"id">> := 0, <<"method">> := <<"metric_data">>, handle_request(#{<<"method">> := <<"metric_data">>,
<<"params">> := #{<<"device_uuid">> := DeviceUUID, <<"route_key">> := RouteKey, <<"metric">> := Metric}}, State = #state{service_pid = ServicePid, is_registered = true}) -> <<"params">> := #{<<"device_uuid">> := DeviceUUID, <<"route_key">> := RouteKey, <<"metric">> := Metric}}, State = #state{service_pid = ServicePid, is_registered = true}) ->
efka_service:metric_data(ServicePid, DeviceUUID, RouteKey, Metric), efka_service:metric_data(ServicePid, DeviceUUID, RouteKey, Metric),
{ok, State}; {ok, State};
%% Event事件 %% Event事件
handle_request(#{<<"id">> := 0, <<"method">> := <<"event">>, <<"params">> := #{<<"event_type">> := EventType, <<"body">> := Body}}, State = #state{service_pid = ServicePid, is_registered = true}) -> handle_request(#{<<"method">> := <<"event">>, <<"params">> := #{<<"event_type">> := EventType, <<"body">> := Body}}, State = #state{service_pid = ServicePid, is_registered = true}) ->
efka_service:send_event(ServicePid, EventType, Body), efka_service:send_event(ServicePid, EventType, Body),
{ok, State}. {ok, State}.