This commit is contained in:
安礼成 2023-02-14 20:31:29 +08:00
parent ad98d9d695
commit 3a48345202
4 changed files with 216 additions and 1 deletions

57
apps/iot/include/iot.hrl Normal file
View File

@ -0,0 +1,57 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 14. 2 2023 19:48
%%%-------------------------------------------------------------------
-author("licheng5").
%%
-define(STATUS_INACTIVE, 0).
-define(STATUS_ONLINE, 1).
-define(STATUS_OFFLINE, 2).
-define(STATUS_DELETE, 3).
%%
-record(host, {
id :: binary(),
name :: binary(),
model :: binary(),
cell_id :: integer(),
terminal_num = 0 :: integer(),
update_ts = 0 :: integer(),
status = 0 :: integer()
}).
%%
-record(terminal, {
id :: binary(),
name :: binary(),
product_id :: integer(),
vendor_id :: integer(),
model :: binary(),
cell_id :: integer(),
host_id :: binary(),
update_ts = 0 :: integer(),
status = 0 :: integer()
}).
%%
-record(cell, {
id :: integer(),
name :: binary()
}).
%%
-record(product, {
id :: integer(),
name :: binary()
}).
%%
-record(vendor, {
id :: integer(),
name :: binary()
}).

View File

@ -9,7 +9,7 @@
ranch, ranch,
cowboy, cowboy,
lager, lager,
%jiffy, jiffy,
parse_trans, parse_trans,
hackney, hackney,
poolboy, poolboy,

View File

@ -0,0 +1,78 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2021, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 27. 4 2021 4:38
%%%-------------------------------------------------------------------
-module(host_model).
-author("licheng5").
-include("iot.hrl").
-include_lib("stdlib/include/qlc.hrl").
%% API
%% app信息
get_all_hosts() ->
Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(host)]),
qlc:e(Q)
end,
case mnesia:transaction(Fun) of
Items when is_list(Items) ->
{ok, Items};
{error, _, Reason} ->
{error, Reason}
end.
insert(Id, Name, Model, CellId) ->
Host = #host{
id = Id,
name = Name,
model = Model,
cell_id = CellId,
terminal_num = 0,
update_ts = 0,
status = ?STATUS_INACTIVE
},
case mnesia:transaction(fun() -> mnesia:write(host, Host, write) end) of
{atomic, _} ->
ok;
{aborted, Error} ->
{error, Error}
end.
change_status(Id, Status) ->
Fun = fun() ->
case mnesia:read(host, Id) of
[] ->
mnesia:abort(<<"appinfo not found">>);
[Host] ->
NHost = Host#host{status = Status},
mnesia:write(host, NHost, write)
end
end,
case mnesia:transaction(Fun) of
{atomic, ok} ->
ok;
{aborted, Reason} ->
{error, Reason}
end.
delete(Id) ->
case mnesia:transaction(fun() -> mnesia:delete(host, Id, write) end) of
{atomic, ok} ->
ok;
{aborted, Reason} ->
{error, Reason}
end.
%% app表的数据大小
table_size() ->
mnesia:table_info(host, size).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -0,0 +1,80 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2021, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 27. 4 2021 4:38
%%%-------------------------------------------------------------------
-module(terminal_model).
-author("licheng5").
-include("iot.hrl").
-include_lib("stdlib/include/qlc.hrl").
%% API
%% app信息
get_all_terminals() ->
Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(terminal)]),
qlc:e(Q)
end,
case mnesia:transaction(Fun) of
Items when is_list(Items) ->
{ok, Items};
{error, _, Reason} ->
{error, Reason}
end.
insert(Id, HostId, Name, ProductId, VendorId, Model, CellId) ->
Terminal = #terminal{
id = Id,
name = Name,
product_id = ProductId,
vendor_id = VendorId,
model = Model,
cell_id = CellId,
host_id = HostId,
update_ts = 0,
status = ?STATUS_INACTIVE
},
case mnesia:transaction(fun() -> mnesia:write(terminal, Terminal, write) end) of
{atomic, _} ->
ok;
{aborted, Error} ->
{error, Error}
end.
change_status(Id, Status) ->
Fun = fun() ->
case mnesia:read(host, Id) of
[] ->
mnesia:abort(<<"appinfo not found">>);
[Host] ->
NHost = Host#host{status = Status},
mnesia:write(host, NHost, write)
end
end,
case mnesia:transaction(Fun) of
{atomic, ok} ->
ok;
{aborted, Reason} ->
{error, Reason}
end.
delete(Id) ->
case mnesia:transaction(fun() -> mnesia:delete(host, Id, write) end) of
{atomic, ok} ->
ok;
{aborted, Reason} ->
{error, Reason}
end.
%% app表的数据大小
table_size() ->
mnesia:table_info(host, size).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%