simple code

This commit is contained in:
anlicheng 2023-08-16 16:03:33 +08:00
parent 5ddcc66f0d
commit 0bb6883ac6
2 changed files with 3 additions and 58 deletions

View File

@ -29,7 +29,7 @@ handle_request("POST", "/device/reload", _, #{<<"device_uuid">> := DeviceUUID})
lager:debug("[device_handler] reload device: ~p, get error: ~p", [DeviceUUID, Reason]),
{ok, 200, iot_util:json_error(404, <<"reload device failed">>)}
end;
{ok, DevicePid} when is_pid(DevicePid) ->
DevicePid when is_pid(DevicePid) ->
iot_device:reload(DevicePid),
{ok, 200, iot_util:json_data(<<"success">>)}
end;
@ -39,7 +39,7 @@ handle_request("POST", "/device/delete", _, #{<<"device_uuid">> := DeviceUUID})
case iot_device:get_pid(DeviceUUID) of
undefined ->
{ok, 200, iot_util:json_data(<<"success">>)};
{ok, DevicePid} when is_pid(DevicePid) ->
DevicePid when is_pid(DevicePid) ->
iot_device_sup:delete_device(DeviceUUID),
{ok, 200, iot_util:json_data(<<"success">>)}
end;

View File

@ -21,7 +21,7 @@
-define(HOST_AUTHED, 1).
%% 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([create_session/2, attach_channel/2]).
@ -65,20 +65,6 @@ handle(Pid, Packet) when is_pid(Pid) ->
reload(Pid) when is_pid(Pid) ->
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()}.
get_aes(Pid) when is_pid(Pid) ->
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>>}}]};
%%
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
handle_event(cast, {handle, {data, Data}}, session, State = #state{aes = AES}) ->
PlainData = iot_cipher_aes:decrypt(AES, Data),