From 3a4834520282e82363f8d0d2ef63b6bcaeca7d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Tue, 14 Feb 2023 20:31:29 +0800 Subject: [PATCH] fix --- apps/iot/include/iot.hrl | 57 +++++++++++++++++++ apps/iot/src/iot.app.src | 2 +- apps/iot/src/model/host_model.erl | 78 ++++++++++++++++++++++++++ apps/iot/src/model/terminal_model.erl | 80 +++++++++++++++++++++++++++ 4 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 apps/iot/include/iot.hrl create mode 100644 apps/iot/src/model/host_model.erl create mode 100644 apps/iot/src/model/terminal_model.erl diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl new file mode 100644 index 0000000..f3ee775 --- /dev/null +++ b/apps/iot/include/iot.hrl @@ -0,0 +1,57 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @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() +}). \ No newline at end of file diff --git a/apps/iot/src/iot.app.src b/apps/iot/src/iot.app.src index a1ada88..4fa9965 100644 --- a/apps/iot/src/iot.app.src +++ b/apps/iot/src/iot.app.src @@ -9,7 +9,7 @@ ranch, cowboy, lager, - %jiffy, + jiffy, parse_trans, hackney, poolboy, diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl new file mode 100644 index 0000000..44889ce --- /dev/null +++ b/apps/iot/src/model/host_model.erl @@ -0,0 +1,78 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2021, +%%% @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 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ No newline at end of file diff --git a/apps/iot/src/model/terminal_model.erl b/apps/iot/src/model/terminal_model.erl new file mode 100644 index 0000000..48ef377 --- /dev/null +++ b/apps/iot/src/model/terminal_model.erl @@ -0,0 +1,80 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2021, +%%% @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 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ No newline at end of file