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, hackney,
poolboy, poolboy,
mnesia, mnesia,
crypto,
kernel, kernel,
stdlib stdlib
]}, ]},

View File

@ -8,6 +8,7 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(iot_mnesia). -module(iot_mnesia).
-author("licheng5"). -author("licheng5").
-include("iot.hrl").
%% API %% API
-export([init_database/0, copy_database/1, join/1]). -export([init_database/0, copy_database/1, join/1]).
@ -23,14 +24,20 @@ init_database() ->
ok = mnesia:start(), ok = mnesia:start(),
%% %%
%% mnesia:create_table(host, [
% mnesia:create_table(booking, [ {attributes, record_info(fields, host)},
% {attributes, record_info(fields, booking)}, {record_name, host},
% {record_name, booking}, {disc_copies, [node()]},
% {disc_copies, [node()]}, {type, set}
% {type, set} ]),
% ]),
% mnesia:create_table(terminal, [
{attributes, record_info(fields, terminal)},
{record_name, host},
{disc_copies, [node()]},
{type, set}
]),
ok. 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 %% API
-export([timestamp/0, number_format/2, current_time/0]). -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]). -export([json_data/1, json_error/2]).
%% %%
@ -62,3 +62,8 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage)
<<"message">> => ErrMessage <<"message">> => ErrMessage
} }
}, [force_utf8]). }, [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"). -include_lib("stdlib/include/qlc.hrl").
%% API %% API
-export([get_all_hosts/0, insert/4, change_status/2]).
%% app信息 %% app信息
get_all_hosts() -> get_all_hosts() ->
@ -20,10 +21,14 @@ get_all_hosts() ->
qlc:e(Q) qlc:e(Q)
end, end,
case mnesia:transaction(Fun) of case mnesia:transaction(Fun) of
Items when is_list(Items) -> {atomic, Items} when is_list(Items) ->
{ok, Items}; Stat = lists:foldl(fun(#host{status = Status}, Acc) ->
{error, _, Reason} -> Num = maps:get(Status, Acc, 0),
{error, Reason} Acc#{Status => Num + 1}
end, #{}, Items),
{ok, Items, Stat};
{aborted, Error} ->
Error
end. end.
insert(Id, Name, Model, CellId) -> insert(Id, Name, Model, CellId) ->