fix
This commit is contained in:
parent
3a48345202
commit
357c908612
@ -14,6 +14,7 @@
|
||||
hackney,
|
||||
poolboy,
|
||||
mnesia,
|
||||
crypto,
|
||||
kernel,
|
||||
stdlib
|
||||
]},
|
||||
|
||||
@ -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
23
apps/iot/src/iot_mock.erl
Normal 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)).
|
||||
@ -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).
|
||||
@ -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) ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user