fix
This commit is contained in:
parent
29a9f46340
commit
9e32c0b196
@ -100,6 +100,8 @@
|
||||
model :: binary(),
|
||||
%% 单元网格编号
|
||||
cell_id :: integer(),
|
||||
%% rsa公钥
|
||||
public_rsa :: binary(),
|
||||
%% aes的key, 后续通讯需要基于这个加密
|
||||
aes :: binary(),
|
||||
metric = #host_metric{},
|
||||
|
||||
59
apps/iot/src/http_handler/http_iot_handler.erl
Normal file
59
apps/iot/src/http_handler/http_iot_handler.erl
Normal file
@ -0,0 +1,59 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @author licheng5
|
||||
%%% @copyright (C) 2023, <COMPANY>
|
||||
%%% @doc
|
||||
%%%
|
||||
%%% @end
|
||||
%%% Created : 06. 3月 2023 14:29
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(http_iot_handler).
|
||||
-author("licheng5").
|
||||
-include("iot.hrl").
|
||||
|
||||
%% API
|
||||
-export([handle_request/4]).
|
||||
|
||||
handle_request("GET", "/api/booking", _, _Params) ->
|
||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||
|
||||
%% 处理命令下发
|
||||
handle_request("POST", "/iot/send_command", _, Params = #{<<"host_id">> := HostId}) ->
|
||||
lager:debug("body is: ~p", [Params]),
|
||||
|
||||
case host_model:activate(HostId) of
|
||||
{error, Reason} when is_binary(Reason) ->
|
||||
{ok, 200, iot_util:json_error(404, Reason)};
|
||||
{ok, #host{aes = Aes, public_rsa = PubKey}} ->
|
||||
Reply = #{
|
||||
<<"a">> => true,
|
||||
<<"aes">> => Aes,
|
||||
<<"reply">> => <<"client.reply.", HostId/binary>>
|
||||
},
|
||||
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
|
||||
|
||||
%% TODO 发送消息到终端
|
||||
lager:debug("enc_reply is: ~p", [EncReply]),
|
||||
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
end;
|
||||
|
||||
%% 处理主机的授权
|
||||
handle_request("POST", "/iot/host_auth", _, Params = #{<<"host_id">> := HostId}) ->
|
||||
lager:debug("body is: ~p", [Params]),
|
||||
|
||||
case host_model:activate(HostId) of
|
||||
{error, Reason} when is_binary(Reason) ->
|
||||
{ok, 200, iot_util:json_error(404, Reason)};
|
||||
{ok, #host{aes = Aes, public_rsa = PubKey}} ->
|
||||
Reply = #{
|
||||
<<"a">> => true,
|
||||
<<"aes">> => Aes,
|
||||
<<"reply">> => <<"client.reply.", HostId/binary>>
|
||||
},
|
||||
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
|
||||
|
||||
%% TODO 发送消息到终端
|
||||
lager:debug("enc_reply is: ~p", [EncReply]),
|
||||
|
||||
{ok, 200, iot_util:json_data(<<"success">>)}
|
||||
end.
|
||||
@ -12,7 +12,7 @@
|
||||
-include_lib("stdlib/include/qlc.hrl").
|
||||
|
||||
%% API
|
||||
-export([get_host/1, get_hosts/3, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]).
|
||||
-export([get_host/1, get_hosts/3, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3, activate/1]).
|
||||
-export([to_map/1]).
|
||||
|
||||
get_host(HostId) when is_binary(HostId) ->
|
||||
@ -97,6 +97,36 @@ change_status(HostId, Status) when is_binary(HostId), is_integer(Status) ->
|
||||
{error, Reason}
|
||||
end.
|
||||
|
||||
-spec activate(HostId :: binary()) -> ok | {error, Reason}.
|
||||
activate(HostId) when is_binary(HostId) ->
|
||||
Fun = fun() ->
|
||||
case mnesia:read(host, HostId) of
|
||||
[] ->
|
||||
mnesia:abort(<<"host not found">>);
|
||||
[Host = #host{status = Status}] ->
|
||||
case Status =:= ?HOST_STATUS_INACTIVE of
|
||||
true ->
|
||||
Aes = iot_util:rand_bytes(16),
|
||||
NHost = Host#host{
|
||||
aes = Aes,
|
||||
activated_ts = iot_util:current_time(),
|
||||
update_ts = iot_util:current_time(),
|
||||
status = ?HOST_STATUS_ONLINE
|
||||
},
|
||||
ok = mnesia:write(host, NHost, write),
|
||||
NHost;
|
||||
false ->
|
||||
mnesia:abort(<<"host status invalid">>)
|
||||
end
|
||||
end
|
||||
end,
|
||||
case mnesia:transaction(Fun) of
|
||||
{atomic, Host} ->
|
||||
{ok, Host};
|
||||
{aborted, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
|
||||
-spec delete(HostId :: binary()) -> ok | {error, Reason :: any()}.
|
||||
delete(HostId) when is_binary(HostId) ->
|
||||
case mnesia:transaction(fun() -> mnesia:delete(host, HostId, write) end) of
|
||||
|
||||
@ -81,7 +81,7 @@ handle(<<"server.register">>, Msg = #{<<"c_id">> := ClientId, <<"r">> := PubKey,
|
||||
handle(<<"server.data">>, #{<<"c_id">> := HostId, <<"d">> := Data}) ->
|
||||
case host_model:get_host(HostId) of
|
||||
{ok, #host{aes = Aes}} ->
|
||||
Services = service_model:get_services(HostId),
|
||||
Services = service_model:get_host_services(HostId),
|
||||
PlainData = iot_cipher_aes:decrypt(Aes, Aes, Data),
|
||||
case jiffy:decode(PlainData, [return_maps]) of
|
||||
Infos when is_list(Infos) ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user