diff --git a/apps/iot/src/iot_device.erl b/apps/iot/src/iot_device.erl index debd8f9..6f766ea 100644 --- a/apps/iot/src/iot_device.erl +++ b/apps/iot/src/iot_device.erl @@ -96,6 +96,7 @@ init0(#{<<"device_uuid">> := DeviceUUID, <<"status">> := Status, <<"authorize_st false -> {ok, _} = device_bo:change_status(DeviceUUID, ?DEVICE_OFFLINE), report_event(DeviceUUID, ?DEVICE_OFFLINE), + lager:debug("[iot_device] started device: ~p, state_name: ~p, status: ~p", [DeviceUUID, ?STATE_DENIED, ?DEVICE_OFFLINE]), {ok, ?STATE_DENIED, #state{device_uuid = DeviceUUID, status = ?DEVICE_OFFLINE}} end. @@ -185,7 +186,7 @@ report_event(DeviceUUID, NewStatus) when is_binary(DeviceUUID), is_integer(NewSt <<"key">> => <<"device_status">>, <<"value">> => NewStatus, <<"unit">> => <<"">>, - <<"type">> => <<"SOE">>, + <<"type">> => <<"DI">>, <<"name">> => <<"设备状态"/utf8>>, <<"timestamp">> => Timestamp }], diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 4e3dc0b..f195fbe 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -147,6 +147,8 @@ start_link(Name, UUID) when is_atom(Name), is_binary(UUID) -> {stop, Reason :: term()} | ignore). init([UUID]) -> {ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE), + report_event(UUID, ?HOST_OFFLINE), + case host_bo:get_host_by_uuid(UUID) of {ok, #{<<"id">> := HostId}} -> %% 通过host_id注册别名, 可以避免通过查询数据库获取HostPid @@ -234,6 +236,8 @@ handle_call({activate, Auth}, _From, State = #state{uuid = UUID, host_id = HostI ok; false -> {ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE), + report_event(UUID, ?HOST_OFFLINE), + change_devices_status(HostId, ?DEVICE_UNKNOWN) end, {reply, ok, State}; @@ -248,6 +252,8 @@ handle_call({activate, Auth}, _From, State = #state{host_id = HostId, uuid = UUI ws_channel:stop(ChannelPid, closed), {ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE), + report_event(UUID, ?HOST_OFFLINE), + change_devices_status(HostId, ?DEVICE_UNKNOWN) end, {reply, ok, State#state{monitor_ref = undefined, channel_pid = undefined, has_session = false}}; @@ -275,6 +281,8 @@ handle_call({create_session, PubKey}, _From, State = #state{uuid = UUID, aes = A Reply = #{<<"a">> => true, <<"aes">> => Aes}, EncReply = iot_cipher_rsa:encode(Reply, PubKey), {ok, AffectedRow} = host_bo:change_status(UUID, ?HOST_ONLINE), + report_event(UUID, ?HOST_ONLINE), + lager:debug("[iot_host] host_id(session) uuid: ~p, create_session, will change status, affected_row: ~p", [UUID, AffectedRow]), {reply, {ok, <<10:8, EncReply/binary>>}, State#state{has_session = true}}; @@ -434,6 +442,8 @@ handle_cast(heartbeat, State = #state{uuid = UUID, heartbeat_counter = Heartbeat handle_info({timeout, _, heartbeat_ticker}, State = #state{uuid = UUID, host_id = HostId, heartbeat_counter = 0, has_session = false}) -> lager:warning("[iot_host] uuid: ~p, heartbeat lost, devices will unknown", [UUID]), {ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE), + report_event(UUID, ?HOST_OFFLINE), + change_devices_status(HostId, ?DEVICE_UNKNOWN), erlang:start_timer(?HEARTBEAT_INTERVAL, self(), heartbeat_ticker), {noreply, State#state{heartbeat_counter = 0}}; @@ -460,6 +470,8 @@ handle_info(Info, State = #state{has_session = HasSession}) -> terminate(Reason, _State = #state{host_id = HostId, uuid = UUID, has_session = HasSession}) -> lager:debug("[iot_host] host: ~p, terminate with reason: ~p, has_session: ~p", [UUID, Reason, HasSession]), host_bo:change_status(UUID, ?HOST_OFFLINE), + report_event(UUID, ?HOST_OFFLINE), + change_devices_status(HostId, ?DEVICE_UNKNOWN), ok. @@ -512,4 +524,19 @@ handle_data(#{<<"service_name">> := ServiceName, <<"at">> := Timestamp, <<"field %% 数据写入influxdb NTags = Tags#{<<"uuid">> => UUID, <<"service_name">> => ServiceName}, - influx_client:write_data(UUID, NTags, FieldsList, Timestamp). \ No newline at end of file + influx_client:write_data(UUID, NTags, FieldsList, Timestamp). + +-spec report_event(UUID :: binary(), NewStatus :: integer()) -> no_return(). +report_event(UUID, NewStatus) when is_binary(UUID), is_integer(NewStatus) -> + %% 设备的状态信息上报给中电 + Timestamp = iot_util:timestamp_of_seconds(), + FieldsList = [#{ + <<"key">> => <<"host_status">>, + <<"value">> => NewStatus, + <<"unit">> => <<"">>, + <<"type">> => <<"DI">>, + <<"name">> => <<"主机状态"/utf8>>, + <<"timestamp">> => Timestamp + }], + iot_router:route(UUID, FieldsList, Timestamp), + lager:debug("[iot_host] host_uuid: ~p, route fields: ~p", [UUID, FieldsList]). diff --git a/docs/north_data.md b/docs/north_data.md index 91b17be..c03e9fc 100644 --- a/docs/north_data.md +++ b/docs/north_data.md @@ -47,7 +47,7 @@ } ``` -## 设备状态变化(事件:水表或者电表) +## 设备状态变化 ```text @@ -55,13 +55,30 @@ "key": "device_status", "value": NewStatus, 0: 离线, 1: 在线, 2: 未知 "unit": 0, - "type": <<"SOE">>, + "type": <<"DI">>, "name": "设备状态", "timestamp": int(10) } ``` + +## 边缘主机设备状态变化 + +```text + +{ + "key": "host_status", + "value": NewStatus, 0: 离线, 1: 在线 + "unit": 0, + "type": <<"DI">>, + "name": "主机状态", + "timestamp": int(10) +} + +``` + + # 智慧照明 ## 总能耗(数据)