This commit is contained in:
anlicheng 2023-08-17 16:10:03 +08:00
parent b7b25aa41f
commit d6d3b9d718
2 changed files with 20 additions and 28 deletions

View File

@ -90,13 +90,13 @@ init([DeviceInfo]) when is_map(DeviceInfo) ->
init0(DeviceInfo). init0(DeviceInfo).
init0(#{<<"device_uuid">> := DeviceUUID, <<"status">> := Status, <<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId}) -> init0(#{<<"device_uuid">> := DeviceUUID, <<"status">> := Status, <<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId}) ->
case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of 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 -> false ->
{ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE), {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]), 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}}; {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}}
end. end.
%% @private %% @private
@ -141,13 +141,13 @@ handle_event(cast, {change_status, _}, _, State = #state{}) ->
handle_event(cast, reload, _, State = #state{device_uuid = DeviceUUID}) -> handle_event(cast, reload, _, State = #state{device_uuid = DeviceUUID}) ->
lager:debug("[iot_device] will reload: ~p", [DeviceUUID]), lager:debug("[iot_device] will reload: ~p", [DeviceUUID]),
case device_bo:get_device_by_uuid(DeviceUUID) of 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 case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of
true ->
{next_state, ?STATE_ACTIVATED, State#state{device_id = DeviceId, status = Status}};
false -> false ->
{ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE), {ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE),
{next_state, ?STATE_DENIED, State#state{device_id = DeviceId}}; {next_state, ?STATE_DENIED, State#state{device_id = DeviceId, status = ?DEVICE_OFFLINE}}
true ->
{next_state, ?STATE_ACTIVATED, State#state{device_id = DeviceId}}
end; end;
undefined -> undefined ->
lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]), 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; 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]), 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}; {keep_state, State};
handle_event(cast, {auth, true}, ?STATE_DENIED, State) -> handle_event(cast, {auth, true}, ?STATE_DENIED, State) ->
{next_state, ?STATE_ACTIVATED, State}; {next_state, ?STATE_ACTIVATED, State};
handle_event(cast, {auth, true}, ?STATE_ACTIVATED, State = #state{device_uuid = DeviceUUID}) -> 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]), lager:debug("[iot_device] device_uuid: ~p, auth: true, will keep state_name: ~p", [DeviceUUID, ?STATE_ACTIVATED]),
{keep_state, State}; {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]), 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}}. {next_state, ?STATE_DENIED, State#state{status = ?DEVICE_OFFLINE}}.
%% @private %% @private

View File

@ -266,27 +266,20 @@ handle_event({call, From}, {create_session, PubKey}, StateName, State = #state{h
EncReply = iot_cipher_rsa:encode(Reply, PubKey), EncReply = iot_cipher_rsa:encode(Reply, PubKey),
{ok, AffectedRow} = host_bo:change_status(UUID, ?HOST_ONLINE), {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]), 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>>}}]}; {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 case iot_device_sup:ensured_device_started(DeviceUUID) of
{ok, DevicePid} -> {ok, DevicePid} ->
iot_device:reload(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}]}; {keep_state, State, [{reply, From, ok}]};
{error, Reason} -> {error, Reason} ->
{keep_state, State, [{reply, From, {error, Reason}}]} {keep_state, State, [{reply, From, {error, Reason}}]}
end; end;
%%
handle_event({call, From}, {delete_device, DeviceUUID}, _, State) -> handle_event({call, From}, {delete_device, DeviceUUID}, _, State) ->
case iot_device:get_pid(DeviceUUID) of case iot_device:get_pid(DeviceUUID) of
undefined -> undefined ->
@ -296,16 +289,11 @@ handle_event({call, From}, {delete_device, DeviceUUID}, _, State) ->
end, end,
{keep_state, State, [{reply, From, ok}]}; {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 case iot_device_sup:ensured_device_started(DeviceUUID) of
{ok, DevicePid} -> {ok, DevicePid} ->
iot_device:auth(DevicePid, Auth), 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}]}; {keep_state, State, [{reply, From, ok}]};
{error, Reason} -> {error, Reason} ->
@ -450,6 +438,7 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
%%
change_devices_status(HostId, NewStatus) when is_integer(HostId), is_integer(NewStatus) -> change_devices_status(HostId, NewStatus) when is_integer(HostId), is_integer(NewStatus) ->
{ok, Devices} = device_bo:get_host_devices(HostId), {ok, Devices} = device_bo:get_host_devices(HostId),
lists:foreach(fun(DeviceUUID) -> 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}, NTags = Tags#{<<"uuid">> => UUID, <<"service_name">> => ServiceName, <<"device_uuid">> => DeviceUUID},
influx_client:write_data(DeviceUUID, NTags, FieldsList, Timestamp), influx_client:write_data(DeviceUUID, NTags, FieldsList, Timestamp),
iot_device:change_status(DevicePid, 1); iot_device:change_status(DevicePid, ?DEVICE_ONLINE);
false -> false ->
lager:notice("[iot_host] uuid: ~p, device_uuid: ~p, device is not activated", [UUID, DeviceUUID]) lager:notice("[iot_host] uuid: ~p, device_uuid: ~p, device is not activated", [UUID, DeviceUUID])
end end