fix
This commit is contained in:
parent
ad98d9d695
commit
3a48345202
57
apps/iot/include/iot.hrl
Normal file
57
apps/iot/include/iot.hrl
Normal 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()
|
||||
}).
|
||||
@ -9,7 +9,7 @@
|
||||
ranch,
|
||||
cowboy,
|
||||
lager,
|
||||
%jiffy,
|
||||
jiffy,
|
||||
parse_trans,
|
||||
hackney,
|
||||
poolboy,
|
||||
|
||||
78
apps/iot/src/model/host_model.erl
Normal file
78
apps/iot/src/model/host_model.erl
Normal 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
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
80
apps/iot/src/model/terminal_model.erl
Normal file
80
apps/iot/src/model/terminal_model.erl
Normal 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
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
Loading…
x
Reference in New Issue
Block a user