diff --git a/apps/iot/src/iot_mock.erl b/apps/iot/src/iot_mock.erl index b2dfdcd..98ed593 100644 --- a/apps/iot/src/iot_mock.erl +++ b/apps/iot/src/iot_mock.erl @@ -13,9 +13,9 @@ -export([insert_hosts/0]). insert_hosts() -> - lists:foreach(fun(_) -> - Id0 = iot_util:rand_bytes(16), - Id = list_to_binary(Id0), + lists:foreach(fun(Id0) -> + %Id0 = iot_util:rand_bytes(16), + Id = integer_to_binary(Id0), Name = <<"N1000_0001">>, Model = <<"N1000">>, CellId = <<"公共教学楼"/utf8>>, diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl index f991a03..5787f1c 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_hosts/2, insert/4, change_status/2, delete/1, table_size/0]). +-export([get_hosts/2, insert/4, change_status/2, delete/1, table_size/0, filter_hosts/3]). %% 获取app信息 get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> @@ -38,6 +38,25 @@ get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, L Error end. +filter_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> + Fun = fun() -> + Q = qlc:q([E || E <- mnesia:table(host)]), + qlc:fold(fun(Host, Acc) -> + case Pred(Host) of + true -> [Host|Acc]; + false -> Acc + end + end, [], Q) + + end, + case mnesia:transaction(Fun) of + {atomic, Items} when is_list(Items) -> + Items1 = lists:reverse(Items), + {ok, lists:sublist(Items1, Start + 1, Limit)}; + {aborted, Error} -> + Error + end. + insert(Id, Name, Model, CellId) -> Host = #host{ id = Id, diff --git a/rebar.config b/rebar.config index 0374bc8..adfa6eb 100644 --- a/rebar.config +++ b/rebar.config @@ -14,8 +14,9 @@ sasl]}, % {mode, dev}, + {mode, prod}, - %% automatically picked up if the files + %% automatically picked up if the files %% exist but can be set manually, which %% is required if the names aren't exactly %% sys.config and vm.args