diff --git a/apps/iot/src/iot_device.erl b/apps/iot/src/iot_device.erl index 2ebdd86..ebbfa2b 100644 --- a/apps/iot/src/iot_device.erl +++ b/apps/iot/src/iot_device.erl @@ -90,13 +90,13 @@ init([DeviceInfo]) when is_map(DeviceInfo) -> init0(DeviceInfo). init0(#{<<"device_uuid">> := DeviceUUID, <<"status">> := Status, <<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId}) -> case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of + true -> + lager:debug("[iot_device] started device: ~p, state_name: ~p, status: ~p", [DeviceUUID, ?STATE_ACTIVATED, Status]), + {ok, ?STATE_ACTIVATED, #state{device_id = DeviceId, device_uuid = DeviceUUID, status = Status}}; false -> {ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE), lager:debug("[iot_device] started device: ~p, state_name: ~p, status: ~p", [DeviceUUID, ?STATE_DENIED, ?DEVICE_OFFLINE]), - {ok, ?STATE_DENIED, #state{device_id = DeviceId, device_uuid = DeviceUUID, status = ?DEVICE_OFFLINE}}; - true -> - lager:debug("[iot_device] started device: ~p, state_name: ~p, status: ~p", [DeviceUUID, ?STATE_ACTIVATED, Status]), - {ok, ?STATE_ACTIVATED, #state{device_id = DeviceId, device_uuid = DeviceUUID, status = Status}} + {ok, ?STATE_DENIED, #state{device_id = DeviceId, device_uuid = DeviceUUID, status = ?DEVICE_OFFLINE}} end. %% @private @@ -141,13 +141,13 @@ handle_event(cast, {change_status, _}, _, State = #state{}) -> handle_event(cast, reload, _, State = #state{device_uuid = DeviceUUID}) -> lager:debug("[iot_device] will reload: ~p", [DeviceUUID]), case device_bo:get_device_by_uuid(DeviceUUID) of - {ok, #{<<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId}} -> + {ok, #{<<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId, <<"status">> := Status}} -> case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of + true -> + {next_state, ?STATE_ACTIVATED, State#state{device_id = DeviceId, status = Status}}; false -> {ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE), - {next_state, ?STATE_DENIED, State#state{device_id = DeviceId}}; - true -> - {next_state, ?STATE_ACTIVATED, State#state{device_id = DeviceId}} + {next_state, ?STATE_DENIED, State#state{device_id = DeviceId, status = ?DEVICE_OFFLINE}} end; undefined -> lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]), @@ -155,17 +155,20 @@ handle_event(cast, reload, _, State = #state{device_uuid = DeviceUUID}) -> end; %% 处理授权 -handle_event(cast, {auth, false}, ?STATE_DENIED, State = #state{device_uuid = DeviceUUID}) -> +handle_event(cast, {auth, false}, ?STATE_DENIED, State = #state{device_id = DeviceId, device_uuid = DeviceUUID}) -> lager:debug("[iot_device] device_uuid: ~p, auth: false, will keep state_name: ~p", [DeviceUUID, ?STATE_DENIED]), + {ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE), {keep_state, State}; + handle_event(cast, {auth, true}, ?STATE_DENIED, State) -> {next_state, ?STATE_ACTIVATED, State}; handle_event(cast, {auth, true}, ?STATE_ACTIVATED, State = #state{device_uuid = DeviceUUID}) -> lager:debug("[iot_device] device_uuid: ~p, auth: true, will keep state_name: ~p", [DeviceUUID, ?STATE_ACTIVATED]), {keep_state, State}; -handle_event(cast, {auth, false}, ?STATE_ACTIVATED, State = #state{device_uuid = DeviceUUID}) -> +handle_event(cast, {auth, false}, ?STATE_ACTIVATED, State = #state{device_id = DeviceId, device_uuid = DeviceUUID}) -> lager:debug("[iot_device] device_uuid: ~p, auth: false, state_name from: ~p, to: ~p", [DeviceUUID, ?STATE_ACTIVATED, ?STATE_DENIED]), + {ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE), {next_state, ?STATE_DENIED, State#state{status = ?DEVICE_OFFLINE}}. %% @private diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 1d3cc41..42760ab 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -266,27 +266,20 @@ handle_event({call, From}, {create_session, PubKey}, StateName, State = #state{h EncReply = iot_cipher_rsa:encode(Reply, PubKey), {ok, AffectedRow} = host_bo:change_status(UUID, ?HOST_ONLINE), lager:debug("[iot_host] host_id(~p) uuid: ~p, create_session, will change status, affected_row: ~p", [StateName, UUID, AffectedRow]), - change_devices_status(HostId, ?DEVICE_ONLINE), {next_state, ?STATE_SESSION, State, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]}; -%% 设备相关 -handle_event({call, From}, {reload_device, DeviceUUID}, StateName, State) -> +%% 重新加载设备信息 +handle_event({call, From}, {reload_device, DeviceUUID}, _, State) -> case iot_device_sup:ensured_device_started(DeviceUUID) of {ok, DevicePid} -> iot_device:reload(DevicePid), - case StateName =:= ?STATE_SESSION of - true -> - iot_device:change_status(DevicePid, ?DEVICE_ONLINE); - false -> - iot_device:change_status(DevicePid, ?DEVICE_OFFLINE) - end, - {keep_state, State, [{reply, From, ok}]}; {error, Reason} -> {keep_state, State, [{reply, From, {error, Reason}}]} end; +%% 删除设备 handle_event({call, From}, {delete_device, DeviceUUID}, _, State) -> case iot_device:get_pid(DeviceUUID) of undefined -> @@ -296,16 +289,11 @@ handle_event({call, From}, {delete_device, DeviceUUID}, _, State) -> end, {keep_state, State, [{reply, From, ok}]}; -handle_event({call, From}, {activate_device, DeviceUUID, Auth}, StateName, State) -> +%% 激活设备 +handle_event({call, From}, {activate_device, DeviceUUID, Auth}, _, State) -> case iot_device_sup:ensured_device_started(DeviceUUID) of {ok, DevicePid} -> iot_device:auth(DevicePid, Auth), - case StateName =:= ?STATE_SESSION of - true -> - iot_device:change_status(DevicePid, ?DEVICE_ONLINE); - false -> - iot_device:change_status(DevicePid, ?DEVICE_OFFLINE) - end, {keep_state, State, [{reply, From, ok}]}; {error, Reason} -> @@ -450,6 +438,7 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) -> %%% Internal functions %%%=================================================================== +%% 修改设备的状态 change_devices_status(HostId, NewStatus) when is_integer(HostId), is_integer(NewStatus) -> {ok, Devices} = device_bo:get_host_devices(HostId), lists:foreach(fun(DeviceUUID) -> @@ -474,7 +463,7 @@ handle_data(#{<<"device_uuid">> := DeviceUUID, <<"service_name">> := ServiceName NTags = Tags#{<<"uuid">> => UUID, <<"service_name">> => ServiceName, <<"device_uuid">> => DeviceUUID}, influx_client:write_data(DeviceUUID, NTags, FieldsList, Timestamp), - iot_device:change_status(DevicePid, 1); + iot_device:change_status(DevicePid, ?DEVICE_ONLINE); false -> lager:notice("[iot_host] uuid: ~p, device_uuid: ~p, device is not activated", [UUID, DeviceUUID]) end