Skip to content

Commit

Permalink
add dgiot_license
Browse files Browse the repository at this point in the history
  • Loading branch information
lsxredrain committed Jun 11, 2021
1 parent 6668b11 commit f6fad90
Show file tree
Hide file tree
Showing 63 changed files with 2,636 additions and 408 deletions.
8 changes: 0 additions & 8 deletions apps/dgiot/include/dgiot.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,3 @@
ekka
]).

%%--------------------------------------------------------------------
%% Route
%%--------------------------------------------------------------------

-record(mnesia, {
key :: binary(),
value :: node() | {binary(), node()} | {map(), node()} | {list(), node()}
}).
93 changes: 7 additions & 86 deletions apps/dgiot/include/dgiot_mnesia.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,92 +17,13 @@


%% ==== 全局缓存表 ========
-define(MCACHE, mnesia_dgiotcache).
-record(dgiot_mcache, {
key,
value
}).

%% ==== 设备路由表 ========
-define(DGIOT_ROUTE, mnesia_route).
-record(dgiot_route, {
key, % [ProductId, DevAddr], [产品ID, 设备地址]
status, % 设备状态
node % 节点
}).

%% ==== 设备表 ========
-define(SMART_DEV, mnesia_smartdev).
-record(dgiot_device, {
key, % [ProductId, DevAddr], [产品ID, 设备地址]
basedata % 设备基本数据,map类型
}).

-define(SMART_PROD, mnesia_smartprod).
-record(dgiot_prod, {
key, % [ProductId], [产品ID]
product % 产品基本数据,map类型
}).
-define(MNESIA_TAB, mnesia).

-define(SMART_HUB, mnesia_smarthub).
-record(dgiot_hub, {
key, % [vcaddr,pn], [网关设备地址,子设备子网地址]
subdev % [ProductId,DevAddr], [产品ID, 设备地址]
}).
%%--------------------------------------------------------------------
%% Route
%%--------------------------------------------------------------------

-define(SMART_GATEWAY, mnesia_smartgateway).
-record(dgiot_gateway, {
key, % [ProductId, DevAddr], [产品ID, 设备地址]
gatway % [DtuProductId, vcaddr,pn], [网关产品ID, 网关设备地址,子设备子网地址]
-record(mnesia, {
key :: binary() | list() | tuple(),
value :: node() | {binary(), node()} | {map(), node()} | {list(), node()}
}).

-define(TABLES, [
{?MCACHE, [
{ram_copies, [node()]},
{record_name, dgiot_mcache},
{attributes, record_info(fields, dgiot_mcache)},
{storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
]},

{?DGIOT_ROUTE, [
{ram_copies, [node()]},
{record_name, dgiot_route},
{type, set},
{index, [node]},
{attributes, record_info(fields, dgiot_route)},
{storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
]},

{?SMART_DEV, [
{disc_copies, [node()]},
{local_content, true},
{type, set},
{record_name, dgiot_device},
{attributes, record_info(fields, dgiot_device)},
{storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
]},

{?SMART_PROD, [
{ram_copies, [node()]},
{record_name, dgiot_prod},
{type, set},
{attributes, record_info(fields, dgiot_prod)},
{storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
]},

{?SMART_HUB, [
{ram_copies, [node()]},
{record_name, dgiot_hub},
{type, set},
{attributes, record_info(fields, dgiot_hub)},
{storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
]},

{?SMART_GATEWAY, [
{ram_copies, [node()]},
{record_name, dgiot_gateway},
{type, set},
{attributes, record_info(fields, dgiot_gateway)},
{storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
]}
]).
5 changes: 4 additions & 1 deletion apps/dgiot/src/dgiot_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ init([]) ->
CMSup = child_spec(dgiot_cm_sup, supervisor, []),
Childs = [KernelSup]
++ [MnesiaSup]
++ [CMSup],
++ [CMSup]
++ [child_spec(dgiot_dcache, worker, [?DCACHE]),
dgiot_channelx:spec(channelx_mgr)],

SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1
Expand Down
12 changes: 1 addition & 11 deletions apps/dgiot/src/otp/dgiot_cron.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
-include_lib("stdlib/include/ms_transform.hrl").
-include("dgiot_cron.hrl").
-include_lib("dgiot/include/logger.hrl").
-export([test/0, init_ets/0, init/1, childspec/1, childspec/2, do_task/2, change_time/2]).
-export([test/0, childspec/1, childspec/2, do_task/2, change_time/2]).
-export([start/1, start/2, start_timer/2, start_timer/3, handle_call/3, handle_info/2, get_next_time/3]).
-export([save/2, match_task/2,now/1,push/1]).

-include_lib("eunit/include/eunit.hrl").
start(Sup) ->
init_ets(),
{ok, _} = start(Sup, ?DEFAULT_CRON).

start(Sup, Name) ->
ChildSpec = childspec(Name),
dgiot_data:init(?DGIOT_CRON),
supervisor:start_child(Sup, ChildSpec).

start_timer(StartTime, Callback) ->
Expand All @@ -53,14 +51,6 @@ start_timer(ID, Secs, Callback) when is_integer(Secs) ->
}).


init_ets() ->
init(?CRON_DB).

%% 初始化数据,可以多共用一个
init(Name) ->
dgiot_data:init(Name, [public, named_table, {write_concurrency, true}, {read_concurrency, true}]).


%% 返回任务进程描述,可以挂到sup下面
childspec(Name) ->
childspec(Name, fun() -> dgiot_datetime:nowstamp() end).
Expand Down
6 changes: 3 additions & 3 deletions apps/dgiot/src/otp/dgiot_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ get_nodes(Sort) ->
fun({{Node, node}, Info}) ->
Info#{node => Node}
end,
case dgiot_mcache:match_object({{'$1', node}, '$2'}, RowFun) of
case dgiot_mnesia:match_object({{'$1', node}, '$2'}, RowFun) of
[] -> [];
Nodes -> lists:sort(Sort, Nodes)
end.
Expand Down Expand Up @@ -103,7 +103,7 @@ handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, _State) ->
dgiot_mcache:delete({node(), node}),
dgiot_mnesia:delete({node(), node}),
ok.

code_change(_OldVsn, State, _Extra) ->
Expand All @@ -121,7 +121,7 @@ register_service(#state{ }) ->
fun(Key, Value, _) ->
dgiot_metrics:gauge(dgiot_global, <<"node">>, [Key], Value)
end, no, Info),
dgiot_mcache:insert({{node(), node}, Info#{pid => self(), update => Now}})
dgiot_mnesia:insert({node(), node}, Info#{pid => self(), update => Now})
catch
Err:Reason ->
?LOG(error,"~p:~p", [Err, Reason])
Expand Down
2 changes: 1 addition & 1 deletion apps/dgiot/src/storage/dgiot_cache_check_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ handle_call({set, Key, TTL}, _From, #checkstate{
Now = calendar:local_time(),
StartTime = calendar:datetime_to_gregorian_seconds(Now),
ets:insert(CheckEts, {Key, {0, StartTime, TTL}}),
%dgiot_mcache:insert({{Key, cache}, {0, StartTime, TTL}}),
%dgiot_mnesia:insert({{Key, cache}, {0, StartTime, TTL}}),
{reply, ok, State};
handle_call(sortkey, _From, #checkstate{
weight = Weight,
Expand Down
6 changes: 4 additions & 2 deletions apps/dgiot/src/storage/dgiot_data.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

-module(dgiot_data).
-author("johnliu").
-include("dgiot_cron.hrl").

-export([init/0, init/1, init/2]).
-export([insert/2, save/2, delete/1, match/1, match_object/2, match_limit/2, match_safe_do/3, match_object/3, match_delete/1, select/2, lookup/1, page/6, destroy/1, update_counter/2]).
Expand All @@ -28,7 +29,9 @@

init() ->
init(?CONSUMER),
init(?DB).
init(?DB),
init(?DGIOT_CRON),
init(?CRON_DB).

init(Name) ->
init(Name, [public, named_table, ordered_set, {write_concurrency, true}, {read_concurrency, true}]).
Expand All @@ -41,7 +44,6 @@ init(Name, Options) ->
Name
end.


insert(Key, Value) ->
insert(?DB, Key, Value).
insert(Name, Key, Value) ->
Expand Down
Loading

0 comments on commit f6fad90

Please sign in to comment.