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) ->
{reply, pong, State};
websocket_handle({binary, Data}, State) ->
websocket_handle({text, Data}, State) ->
Request = jiffy:decode(Data, [return_maps]),
lager:debug("[ws_channle] get request: ~p", [Request]),
handle_request(Request, State);
@ -48,8 +48,12 @@ websocket_handle(Info, State) ->
%%
websocket_info({topic_broadcast, Topic, Content}, State = #state{}) ->
Req = jiffy:encode(#{<<"method">> => <<"publish">>, <<"params">> => #{<<"topic">> => Topic, <<"content">> => Content}}, [force_utf8]),
{reply, {binary, Req}, State};
Req = iolist_to_binary(jiffy:encode(#{
<<"method">> => <<"publish">>,
<<"params">> => #{<<"topic">> => Topic, <<"content">> => Content}
}, [force_utf8])),
{reply, {text, Req}, State};
%% service进程关闭
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">>),
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} ->
lager:warning("[ws_channel] service_id: ~p, attach_channel get error: ~p", [ServiceId, Error]),
{stop, State}
@ -97,16 +101,16 @@ handle_request(#{<<"id">> := Id, <<"method">> := <<"subscribe">>, <<"params">> :
{error, Reason} ->
json_error(Id, -1, Reason)
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}) ->
efka_service:metric_data(ServicePid, DeviceUUID, RouteKey, Metric),
{ok, State};
%% 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),
{ok, State}.