This commit is contained in:
anlicheng 2023-08-28 13:50:22 +08:00
parent 0b698356c9
commit cdb15ec8f0

View File

@ -14,7 +14,6 @@
%%
-define(STATE_DENIED, denied).
-define(STATE_ACTIVATED, activated).
-define(STATE_SESSION, session).
%% API
@ -136,30 +135,14 @@ init([UUID]) ->
{ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE),
case host_bo:get_host_by_uuid(UUID) of
{ok, #{<<"authorize_status">> := AuthorizeStatus, <<"id">> := HostId}} ->
Aes = list_to_binary(iot_util:rand_bytes(32)),
StateName = case AuthorizeStatus =:= 1 of
true ->
?STATE_ACTIVATED;
false ->
?STATE_DENIED
end,
%% devicedevice的状态为离线状态
{ok, Devices} = device_bo:get_host_devices(HostId),
lists:foreach(fun(DeviceUUID) ->
case iot_device_sup:ensured_device_started(DeviceUUID) of
{ok, DevicePid} ->
iot_device:change_status(DevicePid, ?DEVICE_OFFLINE);
{error, Reason} ->
lager:notice("[iot_host] host: ~p, start_device: ~p, get error: ~p", [UUID, DeviceUUID, Reason])
end
end, Devices),
{ok, #{<<"id">> := HostId}} ->
%% host_id注册别名, HostPid
AliasName = get_alias_name(HostId),
global:register_name(AliasName, self()),
{ok, StateName, #state{host_id = HostId, uuid = UUID, aes = Aes}};
Aes = list_to_binary(iot_util:rand_bytes(32)),
{ok, ?STATE_DENIED, #state{host_id = HostId, uuid = UUID, aes = Aes}};
undefined ->
lager:warning("[iot_host] host uuid: ~p, loaded from mysql failed", [UUID]),
ignore
@ -212,18 +195,8 @@ handle_event({call, From}, {publish_message, _, _, _}, _, State) ->
%%
handle_event({call, From}, {activate, Auth}, StateName, State = #state{host_id = HostId, uuid = UUID, monitor_ref = MRef, channel_pid = ChannelPid}) ->
case {StateName, Auth} of
{?STATE_DENIED, false} ->
{?STATE_DENIED, _} ->
{keep_state, State, [{reply, From, ok}]};
{?STATE_DENIED, true} ->
{next_state, ?STATE_ACTIVATED, State, [{reply, From, ok}]};
{?STATE_ACTIVATED, false} ->
{ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE),
change_devices_status(HostId, ?DEVICE_OFFLINE),
{next_state, ?STATE_DENIED, State, [{reply, From, ok}]};
{?STATE_ACTIVATED, true} ->
{keep_state, State, [{reply, From, ok}]};
{?STATE_SESSION, false} ->
%% monitor
erlang:demonitor(MRef),
@ -253,19 +226,26 @@ handle_event({call, From}, {attach_channel, ChannelPid}, _, State = #state{uuid
{keep_state, State#state{channel_pid = ChannelPid, monitor_ref = MRef}, [{reply, From, ok}]};
%% 线
handle_event({call, From}, {create_session, PubKey}, ?STATE_DENIED, State = #state{uuid = UUID}) ->
lager:debug("[iot_host] host_id(denied) uuid: ~p, create_session, will not change host status", [UUID]),
Reply = #{<<"a">> => false, <<"aes">> => <<"">>},
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
{keep_state, State, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]};
handle_event({call, From}, {create_session, PubKey}, _, State = #state{uuid = UUID, aes = Aes, host_id = HostId}) ->
{ok, #{<<"authorize_status">> := AuthorizeStatus}} = host_bo:get_host_by_uuid(UUID),
case AuthorizeStatus =:= 1 of
true ->
Reply = #{<<"a">> => true, <<"aes">> => Aes},
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", [?STATE_SESSION, UUID, AffectedRow]),
handle_event({call, From}, {create_session, PubKey}, StateName, State = #state{uuid = UUID, aes = Aes}) ->
Reply = #{<<"a">> => true, <<"aes">> => Aes},
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]),
%% device
start_devices(HostId),
{next_state, ?STATE_SESSION, State, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]};
{next_state, ?STATE_SESSION, State, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]};
false ->
lager:debug("[iot_host] host_id(denied) uuid: ~p, create_session, will not change host status", [UUID]),
Reply = #{<<"a">> => false, <<"aes">> => <<"">>},
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
{next_state, ?STATE_DENIED, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]}
end;
%%
handle_event({call, From}, {reload_device, DeviceUUID}, _, State) ->
@ -437,6 +417,18 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
%%% Internal functions
%%%===================================================================
start_devices(HostId) when is_integer(HostId) ->
%% devicedevice的状态为离线状态
{ok, Devices} = device_bo:get_host_devices(HostId),
lists:foreach(fun(DeviceUUID) ->
case iot_device_sup:ensured_device_started(DeviceUUID) of
{ok, DevicePid} ->
iot_device:change_status(DevicePid, ?DEVICE_OFFLINE);
{error, Reason} ->
lager:notice("[iot_host] start_device: ~p, get error: ~p", [DeviceUUID, Reason])
end
end, Devices).
%%
change_devices_status(HostId, NewStatus) when is_integer(HostId), is_integer(NewStatus) ->
{ok, Devices} = device_bo:get_host_devices(HostId),