From 54f7acc3f7a4792b29fb7ecdd57056c8d226cc59 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Mon, 21 Aug 2023 18:00:05 +0800 Subject: [PATCH] fix device auth status --- apps/iot/src/iot_device.erl | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/apps/iot/src/iot_device.erl b/apps/iot/src/iot_device.erl index 8f607d7..b174c2f 100644 --- a/apps/iot/src/iot_device.erl +++ b/apps/iot/src/iot_device.erl @@ -153,21 +153,22 @@ handle_event(cast, reload, _, State = #state{device_uuid = DeviceUUID}) -> end; %% 处理授权 -handle_event(cast, {auth, false}, ?STATE_DENIED, State = #state{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(DeviceUUID, ?DEVICE_OFFLINE), - {keep_state, State}; +handle_event(cast, {auth, Auth}, StateName, State = #state{device_uuid = DeviceUUID}) -> + case {StateName, Auth} of + {?STATE_DENIED, false} -> + lager:debug("[iot_device] device_uuid: ~p, auth: false, will keep state_name: ~p", [DeviceUUID, ?STATE_DENIED]), + {keep_state, State}; + {?STATE_DENIED, true} -> + {next_state, ?STATE_ACTIVATED, 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}) -> - 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(DeviceUUID, ?DEVICE_OFFLINE), - {next_state, ?STATE_DENIED, State#state{status = ?DEVICE_OFFLINE}}. + {?STATE_ACTIVATED, false} -> + 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(DeviceUUID, ?DEVICE_OFFLINE), + {next_state, ?STATE_DENIED, State#state{status = ?DEVICE_OFFLINE}}; + {?STATE_ACTIVATED, true} -> + lager:debug("[iot_device] device_uuid: ~p, auth: true, will keep state_name: ~p", [DeviceUUID, ?STATE_ACTIVATED]), + {keep_state, State} + end. %% @private %% @doc This function is called by a gen_statem when it is about to