This commit is contained in:
安礼成 2023-02-14 20:59:03 +08:00
parent 3a48345202
commit 357c908612
5 changed files with 54 additions and 13 deletions

View File

@ -14,6 +14,7 @@
hackney,
poolboy,
mnesia,
crypto,
kernel,
stdlib
]},

View File

@ -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.
%%

23
apps/iot/src/iot_mock.erl Normal file
View File

@ -0,0 +1,23 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2023, <COMPANY>
%%% @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)).

View File

@ -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) || <<E:4>> <= Bytes]),
string:to_lower(S).

View File

@ -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) ->