fix device
This commit is contained in:
parent
97be797566
commit
9c0b87ebcd
@ -18,47 +18,47 @@
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
%% 重新加载对应的主机信息
|
||||
handle_request("POST", "/device/reload", _, #{<<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) ->
|
||||
lager:debug("[device_handler] will reload device uuid: ~p", [DeviceUUID]),
|
||||
case iot_device:get_pid(DeviceUUID) of
|
||||
handle_request("POST", "/device/reload", _, #{<<"host_id">> := HostId, <<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) ->
|
||||
lager:debug("[device_handler] host_id: ~p, will reload device uuid: ~p", [HostId, DeviceUUID]),
|
||||
AliasName = iot_host:get_alias_name(HostId),
|
||||
case whereis(AliasName) of
|
||||
undefined ->
|
||||
case iot_device_sup:start_device(DeviceUUID) of
|
||||
{ok, _} ->
|
||||
{ok, 200, iot_util:json_error(404, <<"reload device failed">>)};
|
||||
HostPid when is_pid(HostPid) ->
|
||||
case iot_host:reload_device(HostPid, DeviceUUID) of
|
||||
ok ->
|
||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||
{error, Reason} ->
|
||||
lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]),
|
||||
{ok, 200, iot_util:json_error(404, <<"reload device failed">>)}
|
||||
end;
|
||||
DevicePid when is_pid(DevicePid) ->
|
||||
iot_device:reload(DevicePid),
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
end
|
||||
end;
|
||||
|
||||
%% 删除对应的主机信息
|
||||
handle_request("POST", "/device/delete", _, #{<<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) ->
|
||||
case iot_device:get_pid(DeviceUUID) of
|
||||
handle_request("POST", "/device/delete", _, #{<<"host_id">> := HostId, <<"device_uuid">> := DeviceUUID}) when is_binary(DeviceUUID) ->
|
||||
AliasName = iot_host:get_alias_name(HostId),
|
||||
case whereis(AliasName) of
|
||||
undefined ->
|
||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||
DevicePid when is_pid(DevicePid) ->
|
||||
iot_device_sup:delete_device(DeviceUUID),
|
||||
{ok, 200, iot_util:json_error(404, <<"delete device failed">>)};
|
||||
HostPid when is_pid(HostPid) ->
|
||||
ok = iot_host:delete_device(HostPid, DeviceUUID),
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
end;
|
||||
|
||||
%% 处理主机的授权的激活
|
||||
handle_request("POST", "/device/activate", _, #{<<"device_uuid">> := DeviceUUID, <<"auth">> := Auth}) when is_binary(DeviceUUID) ->
|
||||
case iot_device:get_pid(DeviceUUID) of
|
||||
handle_request("POST", "/device/activate", _, #{<<"host_id">> := HostId, <<"device_uuid">> := DeviceUUID, <<"auth">> := Auth}) when is_binary(DeviceUUID) ->
|
||||
AliasName = iot_host:get_alias_name(HostId),
|
||||
case whereis(AliasName) of
|
||||
undefined ->
|
||||
case iot_device_sup:start_device(DeviceUUID) of
|
||||
{ok, Pid} ->
|
||||
iot_device:auth(Pid, Auth),
|
||||
{ok, 200, iot_util:json_error(404, <<"activate device failed">>)};
|
||||
HostPid when is_pid(HostPid) ->
|
||||
case iot_host:activate_device(HostPid, DeviceUUID, Auth) of
|
||||
ok ->
|
||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||
{error, Reason} ->
|
||||
lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]),
|
||||
lager:debug("[device_handler] activate device: ~p, get error: ~p", [DeviceUUID, Reason]),
|
||||
{ok, 200, iot_util:json_error(404, <<"activate device failed">>)}
|
||||
end;
|
||||
DevicePid when is_pid(DevicePid) ->
|
||||
iot_device:auth(DevicePid, Auth),
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
end
|
||||
end;
|
||||
|
||||
handle_request(_, Path, _, _) ->
|
||||
|
||||
@ -14,14 +14,11 @@
|
||||
|
||||
%% API
|
||||
-export([get_name/1, get_pid/1]).
|
||||
-export([start_link/2, is_activated/1, change_status/2, reload/1, auth/2, stop/1]).
|
||||
-export([start_link/2, is_activated/1, change_status/2, reload/1, auth/2]).
|
||||
|
||||
%% gen_statem callbacks
|
||||
-export([init/1, format_status/2, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
||||
|
||||
%% 周期性的同步设备状态
|
||||
-define(RELOAD_TICKER, 100 * 1000).
|
||||
|
||||
%% 终端是否授权
|
||||
-define(DEVICE_AUTH_DENIED, 0).
|
||||
-define(DEVICE_AUTH_AUTHED, 1).
|
||||
@ -33,7 +30,7 @@
|
||||
-record(state, {
|
||||
device_id :: integer(),
|
||||
device_uuid :: binary(),
|
||||
status = ?DEVICE_ONLINE
|
||||
status = ?DEVICE_OFFLINE
|
||||
}).
|
||||
|
||||
%%%===================================================================
|
||||
@ -66,10 +63,6 @@ reload(Pid) when is_pid(Pid) ->
|
||||
auth(Pid, Auth) when is_pid(Pid), is_boolean(Auth) ->
|
||||
gen_statem:cast(Pid, {auth, Auth}).
|
||||
|
||||
-spec stop(Pid :: pid()) -> no_return().
|
||||
stop(Pid) when is_pid(Pid) ->
|
||||
gen_statem:stop(Pid).
|
||||
|
||||
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
||||
%% initialize. To ensure a synchronized start-up procedure, this
|
||||
%% function does not return until Module:init/1 has returned.
|
||||
@ -95,17 +88,17 @@ init([DeviceUUID]) when is_binary(DeviceUUID) ->
|
||||
end;
|
||||
init([DeviceInfo]) when is_map(DeviceInfo) ->
|
||||
init0(DeviceInfo).
|
||||
init0(#{<<"device_uuid">> := DeviceUUID, <<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId}) ->
|
||||
%% 定期同步数据库的状态信息
|
||||
erlang:start_timer(?RELOAD_TICKER, self(), reload_ticker),
|
||||
StateName = case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of
|
||||
false -> ?STATE_DENIED;
|
||||
true -> ?STATE_ACTIVATED
|
||||
end,
|
||||
{ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE),
|
||||
lager:debug("[iot_device] started device: ~p, state_name: ~p", [DeviceUUID, StateName]),
|
||||
init0(#{<<"device_uuid">> := DeviceUUID, <<"status">> := Status, <<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId}) ->
|
||||
case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of
|
||||
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}}
|
||||
end.
|
||||
|
||||
{ok, StateName, #state{device_id = DeviceId, device_uuid = DeviceUUID, status = ?DEVICE_OFFLINE}}.
|
||||
%% @private
|
||||
%% @doc This function is called by a gen_statem when it needs to find out
|
||||
%% the callback mode of the callback module.
|
||||
@ -147,37 +140,33 @@ handle_event(cast, {change_status, _}, _, State = #state{}) ->
|
||||
%% 重新加载数据库数据
|
||||
handle_event(cast, reload, _, State = #state{device_uuid = DeviceUUID}) ->
|
||||
lager:debug("[iot_device] will reload: ~p", [DeviceUUID]),
|
||||
reload_database(State);
|
||||
|
||||
%% 处理授权
|
||||
handle_event(cast, {auth, Auth}, ?STATE_DENIED, State = #state{device_id = DeviceId, device_uuid = DeviceUUID}) ->
|
||||
case Auth of
|
||||
true ->
|
||||
%% 需要矫正status字段的值
|
||||
{ok, _} = device_bo:change_status(DeviceId, ?DEVICE_ONLINE),
|
||||
lager:debug("[iot_device] device_uuid: ~p, auth: true, state_name from ~p, to: ~p", [DeviceUUID, ?STATE_DENIED, ?STATE_ACTIVATED]),
|
||||
{next_state, ?STATE_ACTIVATED, State#state{status = ?DEVICE_ONLINE}};
|
||||
false ->
|
||||
lager:debug("[iot_device] device_uuid: ~p, auth: false, will keep state_name: ~p", [DeviceUUID, ?STATE_DENIED]),
|
||||
case device_bo:get_device_by_uuid(DeviceUUID) of
|
||||
{ok, #{<<"authorize_status">> := AuthorizeStatus, <<"id">> := DeviceId}} ->
|
||||
case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of
|
||||
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}}
|
||||
end;
|
||||
undefined ->
|
||||
lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]),
|
||||
{keep_state, State}
|
||||
end;
|
||||
|
||||
handle_event(cast, {auth, Auth}, ?STATE_ACTIVATED, State = #state{device_id = DeviceId, device_uuid = DeviceUUID}) ->
|
||||
case Auth of
|
||||
true ->
|
||||
lager:debug("[iot_device] device_uuid: ~p, auth: true, will keep state_name: ~p", [DeviceUUID, ?STATE_ACTIVATED]),
|
||||
{keep_state, State};
|
||||
false ->
|
||||
%% 需要矫正status字段的值
|
||||
{ok, _} = device_bo:change_status(DeviceId, ?DEVICE_OFFLINE),
|
||||
lager:debug("[iot_device] device_uuid: ~p, auth: false, state_name from: ~p, to: ~p", [DeviceUUID, ?STATE_ACTIVATED, ?STATE_DENIED]),
|
||||
%% 处理授权
|
||||
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]),
|
||||
{keep_state, State};
|
||||
handle_event(cast, {auth, true}, ?STATE_DENIED, State) ->
|
||||
{next_state, ?STATE_ACTIVATED, State};
|
||||
|
||||
{next_state, ?STATE_DENIED, State#state{status = ?DEVICE_OFFLINE}}
|
||||
end;
|
||||
|
||||
handle_event(info, {timeout, _, reload_ticker}, _, State) ->
|
||||
erlang:start_timer(?RELOAD_TICKER + rand:uniform(10) * 1000, self(), reload_ticker),
|
||||
reload_database(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]),
|
||||
{next_state, ?STATE_DENIED, State#state{status = ?DEVICE_OFFLINE}}.
|
||||
|
||||
%% @private
|
||||
%% @doc This function is called by a gen_statem when it is about to
|
||||
@ -195,17 +184,4 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
||||
|
||||
%%%===================================================================
|
||||
%%% Internal functions
|
||||
%%%===================================================================
|
||||
|
||||
reload_database(State = #state{device_uuid = DeviceUUID}) ->
|
||||
case device_bo:get_device_by_uuid(DeviceUUID) of
|
||||
{ok, #{<<"authorize_status">> := AuthorizeStatus, <<"status">> := Status, <<"id">> := DeviceId}} ->
|
||||
StateName = case AuthorizeStatus =:= ?DEVICE_AUTH_AUTHED of
|
||||
false -> ?STATE_DENIED;
|
||||
true -> ?STATE_ACTIVATED
|
||||
end,
|
||||
{next_state, StateName, State#state{device_id = DeviceId, status = Status}};
|
||||
undefined ->
|
||||
lager:warning("[iot_device] device uuid: ~p, loaded from mysql failed", [DeviceUUID]),
|
||||
{keep_state, State}
|
||||
end.
|
||||
%%%===================================================================
|
||||
@ -16,10 +16,16 @@
|
||||
-define(HOST_DENIED, 0).
|
||||
-define(HOST_AUTHED, 1).
|
||||
|
||||
%% 主机状态
|
||||
-define(STATE_DENIED, denied).
|
||||
-define(STATE_ACTIVATED, activated).
|
||||
-define(STATE_SESSION, session).
|
||||
|
||||
%% API
|
||||
-export([start_link/2, get_name/1, get_pid/1, handle/2, reload/1, activate/2]).
|
||||
-export([start_link/2, get_name/1, get_alias_name/1, get_pid/1, handle/2, reload/1, activate/2]).
|
||||
-export([get_metric/1, publish_message/4, get_aes/1]).
|
||||
-export([create_session/2, attach_channel/2]).
|
||||
-export([reload_device/2, delete_device/2, activate_device/3]).
|
||||
|
||||
%% gen_statem callbacks
|
||||
-export([init/1, format_status/2, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
||||
@ -42,6 +48,7 @@
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
%%%===================================================================
|
||||
|
||||
-spec get_pid(UUID :: binary()) -> undefined | pid().
|
||||
get_pid(UUID) when is_binary(UUID) ->
|
||||
Name = get_name(UUID),
|
||||
@ -51,6 +58,11 @@ get_pid(UUID) when is_binary(UUID) ->
|
||||
get_name(UUID) when is_binary(UUID) ->
|
||||
binary_to_atom(<<"iot_host:", UUID/binary>>).
|
||||
|
||||
-spec get_name(HostId :: integer()) -> atom().
|
||||
get_alias_name(HostId0) when is_integer(HostId0) ->
|
||||
HostId = integer_to_binary(HostId0),
|
||||
binary_to_atom(<<"iot_host_id:", HostId/binary>>).
|
||||
|
||||
%% 处理消息
|
||||
-spec handle(Pid :: pid(), Packet :: {atom(), binary()} | {atom(), {binary(), binary()}}) -> no_return().
|
||||
handle(Pid, Packet) when is_pid(Pid) ->
|
||||
@ -100,6 +112,20 @@ publish_message(Pid, CommandType, Params, Timeout) when is_pid(Pid), is_integer(
|
||||
{error, Reason}
|
||||
end.
|
||||
|
||||
%% 设备管理相关
|
||||
|
||||
-spec reload_device(Pid :: pid(), DeviceUUID :: binary()) -> ok | {error, Reason :: any()}.
|
||||
reload_device(Pid, DeviceUUID) when is_pid(Pid), is_binary(DeviceUUID) ->
|
||||
gen_statem:call(Pid, {reload_device, DeviceUUID}).
|
||||
|
||||
-spec delete_device(Pid :: pid(), DeviceUUID :: binary()) -> ok.
|
||||
delete_device(Pid, DeviceUUID) when is_pid(Pid), is_binary(DeviceUUID) ->
|
||||
gen_statem:call(Pid, {delete_device, DeviceUUID}).
|
||||
|
||||
-spec activate_device(Pid :: pid(), DeviceUUID :: binary(), Auth :: boolean()) -> ok | {error, Reason :: any()}.
|
||||
activate_device(Pid, DeviceUUID, Auth) when is_pid(Pid), is_binary(DeviceUUID), is_boolean(Auth) ->
|
||||
gen_statem:call(Pid, {activate_device, DeviceUUID, Auth}).
|
||||
|
||||
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
||||
%% initialize. To ensure a synchronized start-up procedure, this
|
||||
%% function does not return until Module:init/1 has returned.
|
||||
@ -122,8 +148,8 @@ init([UUID]) ->
|
||||
{ok, #{<<"authorize_status">> := AuthorizeStatus, <<"id">> := HostId}} ->
|
||||
Aes = list_to_binary(iot_util:rand_bytes(32)),
|
||||
StateName = case AuthorizeStatus =:= ?HOST_AUTHED of
|
||||
false -> denied;
|
||||
true -> activated
|
||||
false -> ?STATE_DENIED;
|
||||
true -> ?STATE_ACTIVATED
|
||||
end,
|
||||
%% 启动主机相关的device,此时device的状态为离线状态
|
||||
{ok, Devices} = device_bo:get_host_devices(HostId),
|
||||
@ -136,6 +162,10 @@ init([UUID]) ->
|
||||
end
|
||||
end, Devices),
|
||||
|
||||
%% 通过host_id注册别名, 可以避免通过查询数据库获取HostPid
|
||||
AliasName = get_alias_name(HostId),
|
||||
erlang:register(AliasName, self()),
|
||||
|
||||
{ok, StateName, #state{host_id = HostId, uuid = UUID, aes = Aes}};
|
||||
undefined ->
|
||||
lager:warning("[iot_host] host uuid: ~p, loaded from mysql failed", [UUID]),
|
||||
@ -167,7 +197,7 @@ handle_event({call, From}, get_aes, _, State = #state{aes = Aes}) ->
|
||||
{keep_state, State, [{reply, From, {ok, Aes}}]};
|
||||
|
||||
%% 发送普通格式的消息, 激活的时候,会话时创建不成功的; 发送aes类型的命令的时候,必须要求session是存在的
|
||||
handle_event({call, From}, {publish_message, ReceiverPid, CommandType, {aes, Command0}}, session, State = #state{aes = AES, channel_pid = ChannelPid}) ->
|
||||
handle_event({call, From}, {publish_message, ReceiverPid, CommandType, {aes, Command0}}, ?STATE_SESSION, State = #state{aes = AES, channel_pid = ChannelPid}) ->
|
||||
Command = iot_cipher_aes:encrypt(AES, Command0),
|
||||
%% 通过websocket发送请求
|
||||
Ref = ws_channel:publish(ChannelPid, ReceiverPid, <<CommandType:8, Command/binary>>),
|
||||
@ -190,9 +220,9 @@ handle_event({call, From}, reload, StateName, State = #state{uuid = UUID}) ->
|
||||
%% 重新加载主机信息
|
||||
{ok, Host = #{<<"authorize_status">> := AuthorizeStatus}} = host_bo:get_host_by_uuid(UUID),
|
||||
lager:debug("[iot_host] reload host uuid: ~p, successed", [Host]),
|
||||
case StateName == denied andalso AuthorizeStatus =:= ?HOST_AUTHED of
|
||||
case StateName == ?STATE_DENIED andalso AuthorizeStatus =:= ?HOST_AUTHED of
|
||||
true ->
|
||||
{next_state, activated, State, [{reply, From, ok}]};
|
||||
{next_state, ?STATE_ACTIVATED, State, [{reply, From, ok}]};
|
||||
false ->
|
||||
{keep_state, State, [{reply, From, ok}]}
|
||||
end;
|
||||
@ -202,10 +232,10 @@ handle_event({call, From}, {activate, false}, _, State = #state{host_id = HostId
|
||||
{ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE),
|
||||
change_devices_status(HostId, ?DEVICE_OFFLINE),
|
||||
|
||||
{next_state, denied, State, [{reply, From, ok}]};
|
||||
{next_state, ?STATE_DENIED, State, [{reply, From, ok}]};
|
||||
%% 开启授权
|
||||
handle_event({call, From}, {activate, true}, denied, State) ->
|
||||
{next_state, activated, State, [{reply, From, ok}]};
|
||||
handle_event({call, From}, {activate, true}, ?STATE_DENIED, State) ->
|
||||
{next_state, ?STATE_ACTIVATED, State, [{reply, From, ok}]};
|
||||
handle_event({call, From}, {activate, true}, _, State) ->
|
||||
{keep_state, State, [{reply, From, ok}]};
|
||||
|
||||
@ -225,7 +255,7 @@ 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}, denied, State = #state{uuid = UUID}) ->
|
||||
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),
|
||||
@ -238,10 +268,63 @@ handle_event({call, From}, {create_session, PubKey}, StateName, State = #state{h
|
||||
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, 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) ->
|
||||
DeviceStatus = case StateName =:= ?STATE_SESSION of
|
||||
true -> ?DEVICE_ONLINE;
|
||||
false -> ?DEVICE_OFFLINE
|
||||
end,
|
||||
case iot_device:get_pid(DeviceUUID) of
|
||||
undefined ->
|
||||
case iot_device_sup:start_device(DeviceUUID) of
|
||||
{ok, DevicePid} ->
|
||||
iot_device:change_status(DevicePid, DeviceStatus),
|
||||
{keep_state, State, [{reply, From, ok}]};
|
||||
{error, Reason} ->
|
||||
{keep_state, State, [{reply, From, {error, Reason}}]}
|
||||
end;
|
||||
DevicePid when is_pid(DevicePid) ->
|
||||
iot_device:reload(DevicePid),
|
||||
iot_device:change_status(DevicePid, DeviceStatus),
|
||||
|
||||
{keep_state, State, [{reply, From, ok}]}
|
||||
end;
|
||||
|
||||
handle_event({call, From}, {delete_device, DeviceUUID}, _, State) ->
|
||||
case iot_device:get_pid(DeviceUUID) of
|
||||
undefined ->
|
||||
ok;
|
||||
DevicePid when is_pid(DevicePid) ->
|
||||
iot_device_sup:delete_device(DeviceUUID)
|
||||
end,
|
||||
{keep_state, State, [{reply, From, ok}]};
|
||||
|
||||
handle_event({call, From}, {activate_device, DeviceUUID, Auth}, StateName, State) ->
|
||||
DeviceStatus = case StateName =:= ?STATE_SESSION of
|
||||
true -> ?DEVICE_ONLINE;
|
||||
false -> ?DEVICE_OFFLINE
|
||||
end,
|
||||
|
||||
case iot_device:get_pid(DeviceUUID) of
|
||||
undefined ->
|
||||
case iot_device_sup:start_device(DeviceUUID) of
|
||||
{ok, Pid} ->
|
||||
iot_device:auth(Pid, Auth),
|
||||
iot_device:change_status(Pid, DeviceStatus),
|
||||
{keep_state, State, [{reply, From, ok}]};
|
||||
{error, Reason} ->
|
||||
{keep_state, State, [{reply, From, {error, Reason}}]}
|
||||
end;
|
||||
DevicePid when is_pid(DevicePid) ->
|
||||
iot_device:auth(DevicePid, Auth),
|
||||
iot_device:change_status(DevicePid, DeviceStatus),
|
||||
{keep_state, State, [{reply, From, ok}]}
|
||||
end;
|
||||
|
||||
%% 需要将消息转换成json格式然后再处理, 需要在host进程里面处理, 数据带有props,服务端暂时未用到
|
||||
handle_event(cast, {handle, {data, Data}}, session, State = #state{aes = AES}) ->
|
||||
handle_event(cast, {handle, {data, Data}}, ?STATE_SESSION, State = #state{aes = AES}) ->
|
||||
PlainData = iot_cipher_aes:decrypt(AES, Data),
|
||||
case catch jiffy:decode(PlainData, [return_maps]) of
|
||||
Info when is_map(Info) ->
|
||||
@ -267,7 +350,7 @@ handle_event(cast, {handle, {ping, CipherMetric}}, _, State = #state{uuid = UUID
|
||||
{keep_state, State}
|
||||
end;
|
||||
|
||||
handle_event(cast, {handle, {inform, Info0}}, session, State = #state{uuid = UUID, host_id = HostId, aes = AES}) ->
|
||||
handle_event(cast, {handle, {inform, Info0}}, ?STATE_SESSION, State = #state{uuid = UUID, host_id = HostId, aes = AES}) ->
|
||||
Info = iot_cipher_aes:decrypt(AES, Info0),
|
||||
case catch jiffy:decode(Info, [return_maps]) of
|
||||
#{<<"at">> := At, <<"services">> := ServiceInforms} ->
|
||||
@ -294,7 +377,7 @@ handle_event(cast, {handle, {inform, Info0}}, session, State = #state{uuid = UUI
|
||||
end,
|
||||
{keep_state, State};
|
||||
|
||||
handle_event(cast, {handle, {feedback_step, Info0}}, session, State = #state{aes = AES}) ->
|
||||
handle_event(cast, {handle, {feedback_step, Info0}}, ?STATE_SESSION, State = #state{aes = AES}) ->
|
||||
Info = iot_cipher_aes:decrypt(AES, Info0),
|
||||
case catch jiffy:decode(Info, [return_maps]) of
|
||||
Data = #{<<"task_id">> := TaskId, <<"code">> := Code} ->
|
||||
@ -309,7 +392,7 @@ handle_event(cast, {handle, {feedback_step, Info0}}, session, State = #state{aes
|
||||
end,
|
||||
{keep_state, State};
|
||||
|
||||
handle_event(cast, {handle, {feedback_result, Info0}}, session, State = #state{aes = AES}) ->
|
||||
handle_event(cast, {handle, {feedback_result, Info0}}, ?STATE_SESSION, State = #state{aes = AES}) ->
|
||||
Info = iot_cipher_aes:decrypt(AES, Info0),
|
||||
case catch jiffy:decode(Info, [return_maps]) of
|
||||
#{<<"task_id">> := TaskId, <<"time">> := Time, <<"code">> := Code, <<"reason">> := Reason, <<"error">> := Error, <<"type">> := Type} ->
|
||||
@ -326,7 +409,7 @@ handle_event(cast, {handle, {feedback_result, Info0}}, session, State = #state{a
|
||||
end,
|
||||
{keep_state, State};
|
||||
|
||||
handle_event(cast, {handle, {event, Event0}}, session, State = #state{aes = AES}) ->
|
||||
handle_event(cast, {handle, {event, Event0}}, ?STATE_SESSION, State = #state{aes = AES}) ->
|
||||
EventText = iot_cipher_aes:decrypt(AES, Event0),
|
||||
case catch jiffy:decode(EventText, [return_maps]) of
|
||||
#{<<"event_type">> := ?EVENT_DEVICE, <<"params">> := #{<<"device_uuid">> := DeviceUUID, <<"status">> := Status}} ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user