add request service config

This commit is contained in:
anlicheng 2025-05-21 14:42:25 +08:00
parent 55982e831c
commit 2be6f8040e
3 changed files with 24 additions and 15 deletions

View File

@ -51,6 +51,7 @@
-define(METHOD_INFORM, 16#04). -define(METHOD_INFORM, 16#04).
-define(METHOD_EVENT, 16#05). -define(METHOD_EVENT, 16#05).
-define(METHOD_PHASE, 16#06). -define(METHOD_PHASE, 16#06).
-define(METHOD_REQUEST_SERVICE_CONFIG, 16#07).
%%%% , %%%% ,

View File

@ -337,9 +337,9 @@ handle_event({call, From}, {activate_device, DeviceUUID, Auth}, _, State = #stat
{keep_state, State#state{device_map = maps:put(DeviceUUID, NDevice, DeviceMap)}, [{reply, From, ok}]} {keep_state, State#state{device_map = maps:put(DeviceUUID, NDevice, DeviceMap)}, [{reply, From, ok}]}
end; end;
%% json格式然后再处理, host进程里面处理, props %% todo
handle_event(cast, {handle, {data, DataBin}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true, device_map = DeviceMap}) -> handle_event(cast, {handle, {data, #data{service_id = ServiceId, device_uuid = DeviceUUID, metric = Metric}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true, device_map = DeviceMap}) ->
#data{service_id = ServiceId, device_uuid = DeviceUUID, metric = Metric} = message_pb:decode_msg(DataBin, data), lager:debug("[iot_host] metric_data host: ~p, service_id: ~p, device_uuid: ~p, metric: ~p", [UUID, ServiceId, DeviceUUID, Metric]),
case DeviceUUID =/= <<"">> of case DeviceUUID =/= <<"">> of
true -> true ->
case maps:find(DeviceUUID, DeviceMap) of case maps:find(DeviceUUID, DeviceMap) of
@ -365,19 +365,15 @@ handle_event(cast, {handle, {data, DataBin}}, ?STATE_ACTIVATED, State = #state{u
%% ping的数据是通过aes加密后的 %% ping的数据是通过aes加密后的
handle_event(cast, {handle, {ping, Metrics}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) -> handle_event(cast, {handle, {ping, Metrics}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) ->
lager:debug("[iot_host] host_id uuid: ~p, get ping: ~p", [UUID, Metrics]), lager:debug("[iot_host] ping host_id uuid: ~p, get ping: ~p", [UUID, Metrics]),
{keep_state, State#state{metrics = Metrics}}; {keep_state, State#state{metrics = Metrics}};
handle_event(cast, {handle, {inform, Inform}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, host_id = HostId, has_session = true}) -> handle_event(cast, {handle, {inform, #service_inform{service_id = ServiceId, status = Status, timestamp = Timestamp}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) ->
#service_inform{service_id = ServiceId, status = Status, timestamp = Timestamp} = Inform, lager:debug("[iot_host] inform host: ~p, service_id: ~p, status: ~p, timestamp: ~p", [UUID, ServiceId, Status, Timestamp]),
lager:debug("[iot_host] host: ~p, service infos is: ~p", [UUID, Inform]),
%% props id:id:id
{keep_state, State}; {keep_state, State};
handle_event(cast, {handle, {event, Event}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) -> handle_event(cast, {handle, {event, #event{service_id = ServiceId, event_type = EventType, params = Params}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) ->
lager:debug("[iot_host] uuid: ~p, get event: ~p", [UUID, Event]), lager:debug("[iot_host] event uuid: ~p, service_id: ~p, event_type: ~p, params: ~p", [UUID, ServiceId, EventType, Params]),
#event{service_id = ServiceId, event_type = EventType, params = Params} = Event,
%DevicePid = iot_device:get_pid(DeviceUUID), %DevicePid = iot_device:get_pid(DeviceUUID),
%iot_device:change_status(DevicePid, Status), %iot_device:change_status(DevicePid, Status),

View File

@ -9,6 +9,7 @@
-module(tcp_channel). -module(tcp_channel).
-author("licheng5"). -author("licheng5").
-include("iot.hrl"). -include("iot.hrl").
-include("iot_tables.hrl").
-include("message_pb.hrl"). -include("message_pb.hrl").
%% API %% API
@ -112,20 +113,20 @@ handle_info({tcp, Socket, <<?PACKET_REQUEST, PacketId:32, ?METHOD_AUTH:8, AuthRe
%% host的monitor %% host的monitor
erlang:monitor(process, HostPid), erlang:monitor(process, HostPid),
AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 0, message = <<"ok">>}), AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 0, message = <<"ok">>}),
Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, ?METHOD_AUTH:8, AuthReplyBin/binary>>), Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, AuthReplyBin/binary>>),
{noreply, State#state{uuid = UUID, host_pid = HostPid}}; {noreply, State#state{uuid = UUID, host_pid = HostPid}};
{denied, Reason} when is_binary(Reason) -> {denied, Reason} when is_binary(Reason) ->
erlang:monitor(process, HostPid), erlang:monitor(process, HostPid),
AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 1, message = Reason}), AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 1, message = Reason}),
Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, ?METHOD_AUTH:8, AuthReplyBin/binary>>), Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, AuthReplyBin/binary>>),
lager:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]), lager:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]),
{noreply, State#state{uuid = UUID, host_pid = HostPid}}; {noreply, State#state{uuid = UUID, host_pid = HostPid}};
{error, Reason} when is_binary(Reason) -> {error, Reason} when is_binary(Reason) ->
AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 2, message = Reason}), AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 2, message = Reason}),
Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, ?METHOD_AUTH:8, AuthReplyBin/binary>>), Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, AuthReplyBin/binary>>),
lager:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]), lager:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]),
{stop, State} {stop, State}
@ -136,6 +137,17 @@ handle_info({tcp, Socket, <<?PACKET_REQUEST, PacketId:32, ?METHOD_AUTH:8, AuthRe
{stop, State} {stop, State}
end; end;
%%
handle_info({tcp, Socket, <<?PACKET_REQUEST, PacketId:32, ?METHOD_REQUEST_SERVICE_CONFIG:8, ServiceId/binary>>}, State = #state{transport = Transport, socket = Socket}) ->
lager:debug("[ws_channel] service_config request service_id: ~p", [ServiceId]),
case service_config_model:get_config(ServiceId) of
error ->
Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32>>);
{ok, #service_config{config_json = ConfigJson}} when is_binary(ConfigJson) ->
Transport:send(Socket, <<?PACKET_RESPONSE, PacketId:32, ConfigJson/binary>>)
end,
{noreply, State};
handle_info({tcp, Socket, <<?PACKET_REQUEST, ?METHOD_DATA:8, Data0/binary>>}, State = #state{socket = Socket, host_pid = HostPid}) when is_pid(HostPid) -> handle_info({tcp, Socket, <<?PACKET_REQUEST, ?METHOD_DATA:8, Data0/binary>>}, State = #state{socket = Socket, host_pid = HostPid}) when is_pid(HostPid) ->
Data = message_pb:decode_msg(Data0, data), Data = message_pb:decode_msg(Data0, data),
iot_host:handle(HostPid, {data, Data}), iot_host:handle(HostPid, {data, Data}),