diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index 90c48bc..adc46ce 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -51,6 +51,7 @@ -define(METHOD_INFORM, 16#04). -define(METHOD_EVENT, 16#05). -define(METHOD_PHASE, 16#06). +-define(METHOD_REQUEST_SERVICE_CONFIG, 16#07). %%%% 命令类型子分类, 不需要返回值 diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index d631030..2db65b3 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -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}]} end; -%% 需要将消息转换成json格式然后再处理, 需要在host进程里面处理, 数据带有props,服务端暂时未用到 -handle_event(cast, {handle, {data, DataBin}}, ?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), +%% todo +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}) -> + lager:debug("[iot_host] metric_data host: ~p, service_id: ~p, device_uuid: ~p, metric: ~p", [UUID, ServiceId, DeviceUUID, Metric]), case DeviceUUID =/= <<"">> of true -> case maps:find(DeviceUUID, DeviceMap) of @@ -365,19 +365,15 @@ handle_event(cast, {handle, {data, DataBin}}, ?STATE_ACTIVATED, State = #state{u %% ping的数据是通过aes加密后的,因此需要在有会话的情况下才行 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}}; -handle_event(cast, {handle, {inform, Inform}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, host_id = HostId, has_session = true}) -> - #service_inform{service_id = ServiceId, status = Status, timestamp = Timestamp} = Inform, - lager:debug("[iot_host] host: ~p, service infos is: ~p", [UUID, Inform]), - %% props 主机id:场景id:微服务id +handle_event(cast, {handle, {inform, #service_inform{service_id = ServiceId, status = Status, timestamp = Timestamp}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) -> + lager:debug("[iot_host] inform host: ~p, service_id: ~p, status: ~p, timestamp: ~p", [UUID, ServiceId, Status, Timestamp]), {keep_state, State}; -handle_event(cast, {handle, {event, Event}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true}) -> - lager:debug("[iot_host] uuid: ~p, get event: ~p", [UUID, Event]), - #event{service_id = ServiceId, event_type = EventType, params = Params} = Event, - +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] event uuid: ~p, service_id: ~p, event_type: ~p, params: ~p", [UUID, ServiceId, EventType, Params]), %DevicePid = iot_device:get_pid(DeviceUUID), %iot_device:change_status(DevicePid, Status), diff --git a/apps/iot/src/tcp/tcp_channel.erl b/apps/iot/src/tcp/tcp_channel.erl index f859ae6..0cae4e9 100644 --- a/apps/iot/src/tcp/tcp_channel.erl +++ b/apps/iot/src/tcp/tcp_channel.erl @@ -9,6 +9,7 @@ -module(tcp_channel). -author("licheng5"). -include("iot.hrl"). +-include("iot_tables.hrl"). -include("message_pb.hrl"). %% API @@ -112,20 +113,20 @@ handle_info({tcp, Socket, <>}), - Transport:send(Socket, <>), + Transport:send(Socket, <>), {noreply, State#state{uuid = UUID, host_pid = HostPid}}; {denied, Reason} when is_binary(Reason) -> erlang:monitor(process, HostPid), AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 1, message = Reason}), - Transport:send(Socket, <>), + Transport:send(Socket, <>), lager:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]), {noreply, State#state{uuid = UUID, host_pid = HostPid}}; {error, Reason} when is_binary(Reason) -> AuthReplyBin = message_pb:encode_msg(#auth_reply{code = 2, message = Reason}), - Transport:send(Socket, <>), + Transport:send(Socket, <>), lager:debug("[ws_channel] uuid: ~p, attach channel get error: ~p, stop channel", [UUID, Reason]), {stop, State} @@ -136,6 +137,17 @@ handle_info({tcp, Socket, <>}, 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, <>); + {ok, #service_config{config_json = ConfigJson}} when is_binary(ConfigJson) -> + Transport:send(Socket, <>) + end, + {noreply, State}; + handle_info({tcp, Socket, <>}, State = #state{socket = Socket, host_pid = HostPid}) when is_pid(HostPid) -> Data = message_pb:decode_msg(Data0, data), iot_host:handle(HostPid, {data, Data}),