simple code
This commit is contained in:
parent
5ddcc66f0d
commit
0bb6883ac6
@ -29,7 +29,7 @@ handle_request("POST", "/device/reload", _, #{<<"device_uuid">> := DeviceUUID})
|
|||||||
lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]),
|
lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]),
|
||||||
{ok, 200, iot_util:json_error(404, <<"reload device failed">>)}
|
{ok, 200, iot_util:json_error(404, <<"reload device failed">>)}
|
||||||
end;
|
end;
|
||||||
{ok, DevicePid} when is_pid(DevicePid) ->
|
DevicePid when is_pid(DevicePid) ->
|
||||||
iot_device:reload(DevicePid),
|
iot_device:reload(DevicePid),
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||||
end;
|
end;
|
||||||
@ -39,7 +39,7 @@ handle_request("POST", "/device/delete", _, #{<<"device_uuid">> := DeviceUUID})
|
|||||||
case iot_device:get_pid(DeviceUUID) of
|
case iot_device:get_pid(DeviceUUID) of
|
||||||
undefined ->
|
undefined ->
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||||
{ok, DevicePid} when is_pid(DevicePid) ->
|
DevicePid when is_pid(DevicePid) ->
|
||||||
iot_device_sup:delete_device(DeviceUUID),
|
iot_device_sup:delete_device(DeviceUUID),
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||||
end;
|
end;
|
||||||
|
|||||||
@ -21,7 +21,7 @@
|
|||||||
-define(HOST_AUTHED, 1).
|
-define(HOST_AUTHED, 1).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/2, get_name/1, get_pid/1, handle/2, reload/1, activate/2, reload_device/2, delete_device/2, auth_device/3]).
|
-export([start_link/2, get_name/1, get_pid/1, handle/2, reload/1, activate/2]).
|
||||||
-export([get_metric/1, publish_message/4, get_aes/1]).
|
-export([get_metric/1, publish_message/4, get_aes/1]).
|
||||||
-export([create_session/2, attach_channel/2]).
|
-export([create_session/2, attach_channel/2]).
|
||||||
|
|
||||||
@ -65,20 +65,6 @@ handle(Pid, Packet) when is_pid(Pid) ->
|
|||||||
reload(Pid) when is_pid(Pid) ->
|
reload(Pid) when is_pid(Pid) ->
|
||||||
gen_statem:call(Pid, reload).
|
gen_statem:call(Pid, reload).
|
||||||
|
|
||||||
%% 重新加载主机的基本信息
|
|
||||||
-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 | {error, Reason :: any()}.
|
|
||||||
delete_device(Pid, DeviceUUID) when is_pid(Pid), is_binary(DeviceUUID) ->
|
|
||||||
gen_statem:call(Pid, {delete_device, DeviceUUID}).
|
|
||||||
|
|
||||||
-spec auth_device(Pid :: pid(), DeviceUUID :: binary(), Auth :: boolean()) -> ok | {error, Reason :: any()}.
|
|
||||||
auth_device(Pid, DeviceUUID, Auth) when is_pid(Pid), is_binary(DeviceUUID), is_boolean(Auth) ->
|
|
||||||
gen_statem:call(Pid, {auth_device, DeviceUUID, Auth}).
|
|
||||||
|
|
||||||
-spec get_aes(Pid :: pid()) -> {ok, Aes :: binary()}.
|
-spec get_aes(Pid :: pid()) -> {ok, Aes :: binary()}.
|
||||||
get_aes(Pid) when is_pid(Pid) ->
|
get_aes(Pid) when is_pid(Pid) ->
|
||||||
gen_statem:call(Pid, get_aes).
|
gen_statem:call(Pid, get_aes).
|
||||||
@ -244,47 +230,6 @@ handle_event({call, From}, {create_session, PubKey}, StateName, State = #state{u
|
|||||||
|
|
||||||
{next_state, session, State, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]};
|
{next_state, session, State, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]};
|
||||||
|
|
||||||
%% 处理设备相关
|
|
||||||
handle_event({call, From}, {reload_device, DeviceUUID}, _, State) ->
|
|
||||||
case iot_device:get_pid(DeviceUUID) of
|
|
||||||
undefined ->
|
|
||||||
case iot_device_sup:start_device(DeviceUUID) of
|
|
||||||
{ok, DevicePid} when is_pid(DevicePid) ->
|
|
||||||
{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),
|
|
||||||
{keep_state, State, [{reply, From, ok}]}
|
|
||||||
end;
|
|
||||||
|
|
||||||
handle_event({call, From}, {auth_device, DeviceUUID, Auth}, _, State = #state{uuid = UUID}) ->
|
|
||||||
case iot_device:get_pid(DeviceUUID) of
|
|
||||||
undefined ->
|
|
||||||
case iot_device_sup:start_device(DeviceUUID) of
|
|
||||||
{ok, Pid} ->
|
|
||||||
iot_device:auth(Pid, Auth),
|
|
||||||
{keep_state, State, [{reply, From, ok}]};
|
|
||||||
{error, Reason} ->
|
|
||||||
lager:notice("[iot_host] host: ~p, auth_device, try start_device get error: ~p", [UUID, Reason]),
|
|
||||||
{keep_state, State, [{reply, From, {error, Reason}}]}
|
|
||||||
end;
|
|
||||||
DevicePid ->
|
|
||||||
iot_device:auth(DevicePid, Auth),
|
|
||||||
{keep_state, State, [{reply, From, ok}]}
|
|
||||||
end;
|
|
||||||
|
|
||||||
handle_event({call, From}, {delete_device, DeviceUUID}, _, State = #state{uuid = UUID}) ->
|
|
||||||
case iot_device:get_pid(DeviceUUID) of
|
|
||||||
undefined ->
|
|
||||||
lager:notice("[iot_host] uuid: ~p, delete device: ~p, not found", [UUID, DeviceUUID]),
|
|
||||||
{keep_state, State, [{reply, From, ok}]};
|
|
||||||
DevicePid when is_pid(DevicePid) ->
|
|
||||||
iot_device_sup:delete_device(DeviceUUID),
|
|
||||||
{keep_state, State, [{reply, From, ok}]}
|
|
||||||
end;
|
|
||||||
|
|
||||||
%% 需要将消息转换成json格式然后再处理, 需要在host进程里面处理, 数据带有props,服务端暂时未用到
|
%% 需要将消息转换成json格式然后再处理, 需要在host进程里面处理, 数据带有props,服务端暂时未用到
|
||||||
handle_event(cast, {handle, {data, Data}}, session, State = #state{aes = AES}) ->
|
handle_event(cast, {handle, {data, Data}}, session, State = #state{aes = AES}) ->
|
||||||
PlainData = iot_cipher_aes:decrypt(AES, Data),
|
PlainData = iot_cipher_aes:decrypt(AES, Data),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user