fix host
This commit is contained in:
parent
0b698356c9
commit
cdb15ec8f0
@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
%% 主机状态
|
%% 主机状态
|
||||||
-define(STATE_DENIED, denied).
|
-define(STATE_DENIED, denied).
|
||||||
-define(STATE_ACTIVATED, activated).
|
|
||||||
-define(STATE_SESSION, session).
|
-define(STATE_SESSION, session).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
@ -136,30 +135,14 @@ init([UUID]) ->
|
|||||||
{ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE),
|
{ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE),
|
||||||
|
|
||||||
case host_bo:get_host_by_uuid(UUID) of
|
case host_bo:get_host_by_uuid(UUID) of
|
||||||
{ok, #{<<"authorize_status">> := AuthorizeStatus, <<"id">> := HostId}} ->
|
{ok, #{<<"id">> := HostId}} ->
|
||||||
Aes = list_to_binary(iot_util:rand_bytes(32)),
|
|
||||||
StateName = case AuthorizeStatus =:= 1 of
|
|
||||||
true ->
|
|
||||||
?STATE_ACTIVATED;
|
|
||||||
false ->
|
|
||||||
?STATE_DENIED
|
|
||||||
end,
|
|
||||||
%% 启动主机相关的device,此时device的状态为离线状态
|
|
||||||
{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),
|
|
||||||
|
|
||||||
%% 通过host_id注册别名, 可以避免通过查询数据库获取HostPid
|
%% 通过host_id注册别名, 可以避免通过查询数据库获取HostPid
|
||||||
AliasName = get_alias_name(HostId),
|
AliasName = get_alias_name(HostId),
|
||||||
global:register_name(AliasName, self()),
|
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 ->
|
undefined ->
|
||||||
lager:warning("[iot_host] host uuid: ~p, loaded from mysql failed", [UUID]),
|
lager:warning("[iot_host] host uuid: ~p, loaded from mysql failed", [UUID]),
|
||||||
ignore
|
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}) ->
|
handle_event({call, From}, {activate, Auth}, StateName, State = #state{host_id = HostId, uuid = UUID, monitor_ref = MRef, channel_pid = ChannelPid}) ->
|
||||||
case {StateName, Auth} of
|
case {StateName, Auth} of
|
||||||
{?STATE_DENIED, false} ->
|
{?STATE_DENIED, _} ->
|
||||||
{keep_state, State, [{reply, From, ok}]};
|
{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} ->
|
{?STATE_SESSION, false} ->
|
||||||
%% 取消之前的monitor
|
%% 取消之前的monitor
|
||||||
erlang:demonitor(MRef),
|
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}]};
|
{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}) ->
|
handle_event({call, From}, {create_session, PubKey}, _, State = #state{uuid = UUID, aes = Aes, host_id = HostId}) ->
|
||||||
lager:debug("[iot_host] host_id(denied) uuid: ~p, create_session, will not change host status", [UUID]),
|
{ok, #{<<"authorize_status">> := AuthorizeStatus}} = host_bo:get_host_by_uuid(UUID),
|
||||||
Reply = #{<<"a">> => false, <<"aes">> => <<"">>},
|
case AuthorizeStatus =:= 1 of
|
||||||
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
|
true ->
|
||||||
{keep_state, State, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]};
|
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}) ->
|
%% 启动主机相关的device
|
||||||
Reply = #{<<"a">> => true, <<"aes">> => Aes},
|
start_devices(HostId),
|
||||||
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]),
|
|
||||||
|
|
||||||
{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) ->
|
handle_event({call, From}, {reload_device, DeviceUUID}, _, State) ->
|
||||||
@ -437,6 +417,18 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
|||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
|
start_devices(HostId) when is_integer(HostId) ->
|
||||||
|
%% 启动主机相关的device,此时device的状态为离线状态
|
||||||
|
{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) ->
|
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),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user