From 9e32c0b196ff6e56a2820adafb4d855f234e56b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Mon, 6 Mar 2023 15:29:24 +0800 Subject: [PATCH] fix --- apps/iot/include/iot.hrl | 2 + .../iot/src/http_handler/http_iot_handler.erl | 59 +++++++++++++++++++ apps/iot/src/model/host_model.erl | 32 +++++++++- .../src/mqtt_handler/iot_message_handler.erl | 2 +- 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 apps/iot/src/http_handler/http_iot_handler.erl diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index 75c4f80..9374ba4 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -100,6 +100,8 @@ model :: binary(), %% 单元网格编号 cell_id :: integer(), + %% rsa公钥 + public_rsa :: binary(), %% aes的key, 后续通讯需要基于这个加密 aes :: binary(), metric = #host_metric{}, diff --git a/apps/iot/src/http_handler/http_iot_handler.erl b/apps/iot/src/http_handler/http_iot_handler.erl new file mode 100644 index 0000000..c4de7a4 --- /dev/null +++ b/apps/iot/src/http_handler/http_iot_handler.erl @@ -0,0 +1,59 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @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. \ No newline at end of file diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl index bad464b..9c940d1 100644 --- a/apps/iot/src/model/host_model.erl +++ b/apps/iot/src/model/host_model.erl @@ -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 diff --git a/apps/iot/src/mqtt_handler/iot_message_handler.erl b/apps/iot/src/mqtt_handler/iot_message_handler.erl index 7c01562..25e2449 100644 --- a/apps/iot/src/mqtt_handler/iot_message_handler.erl +++ b/apps/iot/src/mqtt_handler/iot_message_handler.erl @@ -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) ->