From 357c9086129d8dd38808bc5c37fb187380eaeae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Tue, 14 Feb 2023 20:59:03 +0800 Subject: [PATCH] fix --- apps/iot/src/iot.app.src | 1 + apps/iot/src/iot_mnesia.erl | 23 +++++++++++++++-------- apps/iot/src/iot_mock.erl | 23 +++++++++++++++++++++++ apps/iot/src/iot_util.erl | 7 ++++++- apps/iot/src/model/host_model.erl | 13 +++++++++---- 5 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 apps/iot/src/iot_mock.erl diff --git a/apps/iot/src/iot.app.src b/apps/iot/src/iot.app.src index 4fa9965..5df4b41 100644 --- a/apps/iot/src/iot.app.src +++ b/apps/iot/src/iot.app.src @@ -14,6 +14,7 @@ hackney, poolboy, mnesia, + crypto, kernel, stdlib ]}, diff --git a/apps/iot/src/iot_mnesia.erl b/apps/iot/src/iot_mnesia.erl index ecee50d..db2247b 100644 --- a/apps/iot/src/iot_mnesia.erl +++ b/apps/iot/src/iot_mnesia.erl @@ -8,6 +8,7 @@ %%%------------------------------------------------------------------- -module(iot_mnesia). -author("licheng5"). +-include("iot.hrl"). %% API -export([init_database/0, copy_database/1, join/1]). @@ -23,14 +24,20 @@ init_database() -> ok = mnesia:start(), %% 创建数据库表 - %% 订阅关系表 -% mnesia:create_table(booking, [ -% {attributes, record_info(fields, booking)}, -% {record_name, booking}, -% {disc_copies, [node()]}, -% {type, set} -% ]), -% + mnesia:create_table(host, [ + {attributes, record_info(fields, host)}, + {record_name, host}, + {disc_copies, [node()]}, + {type, set} + ]), + + mnesia:create_table(terminal, [ + {attributes, record_info(fields, terminal)}, + {record_name, host}, + {disc_copies, [node()]}, + {type, set} + ]), + ok. %% 加入集群 diff --git a/apps/iot/src/iot_mock.erl b/apps/iot/src/iot_mock.erl new file mode 100644 index 0000000..b2dfdcd --- /dev/null +++ b/apps/iot/src/iot_mock.erl @@ -0,0 +1,23 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 14. 2月 2023 20:32 +%%%------------------------------------------------------------------- +-module(iot_mock). +-author("licheng5"). + +%% API +-export([insert_hosts/0]). + +insert_hosts() -> + lists:foreach(fun(_) -> + Id0 = iot_util:rand_bytes(16), + Id = list_to_binary(Id0), + Name = <<"N1000_0001">>, + Model = <<"N1000">>, + CellId = <<"公共教学楼"/utf8>>, + host_model:insert(Id, Name, Model, CellId) + end, lists:seq(1, 100)). \ No newline at end of file diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index 0157c0e..21ce0ff 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -11,7 +11,7 @@ %% API -export([timestamp/0, number_format/2, current_time/0]). --export([step/3, chunks/2]). +-export([step/3, chunks/2, rand_bytes/1]). -export([json_data/1, json_error/2]). %% 时间,精确到毫秒 @@ -62,3 +62,8 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) <<"message">> => ErrMessage } }, [force_utf8]). + +rand_bytes(Size) when is_integer(Size) -> + Bytes = crypto:strong_rand_bytes(Size), + S = lists:flatten([integer_to_list(E, 16) || <> <= Bytes]), + string:to_lower(S). \ 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 44889ce..f9590df 100644 --- a/apps/iot/src/model/host_model.erl +++ b/apps/iot/src/model/host_model.erl @@ -12,6 +12,7 @@ -include_lib("stdlib/include/qlc.hrl"). %% API +-export([get_all_hosts/0, insert/4, change_status/2]). %% 获取app信息 get_all_hosts() -> @@ -20,10 +21,14 @@ get_all_hosts() -> qlc:e(Q) end, case mnesia:transaction(Fun) of - Items when is_list(Items) -> - {ok, Items}; - {error, _, Reason} -> - {error, Reason} + {atomic, Items} when is_list(Items) -> + Stat = lists:foldl(fun(#host{status = Status}, Acc) -> + Num = maps:get(Status, Acc, 0), + Acc#{Status => Num + 1} + end, #{}, Items), + {ok, Items, Stat}; + {aborted, Error} -> + Error end. insert(Id, Name, Model, CellId) ->