mirror of
https://gitee.com/dgiiot/dgiot.git
synced 2024-12-03 20:59:41 +08:00
add device cache
This commit is contained in:
parent
2a3606dd8d
commit
2818b8872d
@ -23,28 +23,52 @@
|
|||||||
-dgiot_data("ets").
|
-dgiot_data("ets").
|
||||||
-export([init_ets/0]).
|
-export([init_ets/0]).
|
||||||
-export([create_device/1, create_device/2, get_sub_device/1, get_sub_device/2, get/2]).
|
-export([create_device/1, create_device/2, get_sub_device/1, get_sub_device/2, get/2]).
|
||||||
-export([post/1, put/1, save/1, save/2, save/3, lookup/1, lookup/2, delete/1, delete/2, save_prod/2, lookup_prod/1]).
|
-export([load_device/1, post/1, put/1, save/1, save/2, save/3, lookup/1, lookup/2, delete/1, delete/2, save_prod/2, lookup_prod/1]).
|
||||||
-export([encode/1, decode/3]).
|
-export([encode/1, decode/3]).
|
||||||
|
|
||||||
init_ets() ->
|
init_ets() ->
|
||||||
dgiot_data:init(?DGIOT_PRODUCT),
|
dgiot_data:init(?DGIOT_PRODUCT),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
load_device(Order) ->
|
||||||
|
Success = fun(Page) ->
|
||||||
|
lists:map(fun(Device) ->
|
||||||
|
dgiot_device:save(Device)
|
||||||
|
end, Page)
|
||||||
|
end,
|
||||||
|
Query = #{
|
||||||
|
<<"order">> => Order,
|
||||||
|
<<"where">> => #{}
|
||||||
|
},
|
||||||
|
dgiot_parse_loader:start(<<"Device">>, Query, 0, 100, 1000000, Success).
|
||||||
|
|
||||||
|
get_Acl(Device,OldAcl) ->
|
||||||
|
NewAcl =
|
||||||
|
case maps:find(<<"ACL">>, Device) of
|
||||||
|
error ->
|
||||||
|
OldAcl;
|
||||||
|
{ok, Acl} ->
|
||||||
|
lists:foldl(fun(X, Acc) ->
|
||||||
|
Acc ++ [binary_to_atom(X)]
|
||||||
|
end, [], maps:keys(Acl))
|
||||||
|
end,
|
||||||
|
case length(NewAcl) of
|
||||||
|
0 ->
|
||||||
|
OldAcl;
|
||||||
|
_ ->
|
||||||
|
NewAcl
|
||||||
|
end.
|
||||||
post(Device) ->
|
post(Device) ->
|
||||||
DeviceId = maps:get(<<"objectId">>, Device),
|
DeviceId = maps:get(<<"objectId">>, Device),
|
||||||
#{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device),
|
#{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device),
|
||||||
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
|
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
|
||||||
maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}),
|
maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}),
|
||||||
Product = binary_to_atom(ProductId),
|
Product = binary_to_atom(ProductId),
|
||||||
Acl =
|
|
||||||
lists:foldl(fun(X, Acc) ->
|
|
||||||
Acc ++ [binary_to_atom(X)]
|
|
||||||
end, [], maps:keys(maps:get(<<"ACL">>, Device))),
|
|
||||||
DeviceId = maps:get(<<"objectId">>, Device),
|
DeviceId = maps:get(<<"objectId">>, Device),
|
||||||
Name = maps:get(<<"name">>, Device,<<"">>),
|
Name = maps:get(<<"name">>, Device,<<"">>),
|
||||||
DevAddr = maps:get(<<"devaddr">>, Device,<<"">>),
|
DevAddr = maps:get(<<"devaddr">>, Device,<<"">>),
|
||||||
Enable = maps:get(<<"isEnable">>, Device, false),
|
Enable = maps:get(<<"isEnable">>, Device, false),
|
||||||
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl,Enable, Name, DevAddr], node()}).
|
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, get_Acl(Device,#{}), Enable, Name, DevAddr], node()}).
|
||||||
|
|
||||||
put(Device) ->
|
put(Device) ->
|
||||||
DeviceId = maps:get(<<"objectId">>, Device),
|
DeviceId = maps:get(<<"objectId">>, Device),
|
||||||
@ -54,26 +78,10 @@ put(Device) ->
|
|||||||
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
|
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
|
||||||
maps:get(<<"location">>, Device, #{<<"latitude">> => OldLatitude, <<"longitude">> => OldLogitude}),
|
maps:get(<<"location">>, Device, #{<<"latitude">> => OldLatitude, <<"longitude">> => OldLogitude}),
|
||||||
Product = binary_to_atom(ProductId),
|
Product = binary_to_atom(ProductId),
|
||||||
NewAcl =
|
|
||||||
case maps:find(<<"ACL">>, Device) of
|
|
||||||
error ->
|
|
||||||
OldAcl;
|
|
||||||
{ok, Acl} ->
|
|
||||||
lists:foldl(fun(X, Acc) ->
|
|
||||||
Acc ++ [binary_to_atom(X)]
|
|
||||||
end, [], maps:keys(Acl))
|
|
||||||
end,
|
|
||||||
LastAcl =
|
|
||||||
case length(NewAcl) of
|
|
||||||
0 ->
|
|
||||||
OldAcl;
|
|
||||||
_ ->
|
|
||||||
NewAcl
|
|
||||||
end,
|
|
||||||
Name = maps:get(<<"name">>, Device,OldName),
|
Name = maps:get(<<"name">>, Device,OldName),
|
||||||
DevAddr = maps:get(<<"devaddr">>, Device,OldDevAddr),
|
DevAddr = maps:get(<<"devaddr">>, Device,OldDevAddr),
|
||||||
Enable = maps:get(<<"isEnable">>, Device, OldEnable),
|
Enable = maps:get(<<"isEnable">>, Device, OldEnable),
|
||||||
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, LastAcl,Enable, Name, DevAddr], Node});
|
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, get_Acl(Device,OldAcl),Enable, Name, DevAddr], Node});
|
||||||
_ ->
|
_ ->
|
||||||
pass
|
pass
|
||||||
end.
|
end.
|
||||||
@ -83,15 +91,11 @@ save(Device) ->
|
|||||||
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
|
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
|
||||||
maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}),
|
maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}),
|
||||||
Product = binary_to_atom(ProductId),
|
Product = binary_to_atom(ProductId),
|
||||||
Acl =
|
|
||||||
lists:foldl(fun(X, Acc) ->
|
|
||||||
Acc ++ [binary_to_atom(X)]
|
|
||||||
end, [], maps:keys(maps:get(<<"ACL">>, Device))),
|
|
||||||
DeviceId = maps:get(<<"objectId">>, Device),
|
DeviceId = maps:get(<<"objectId">>, Device),
|
||||||
Name = maps:get(<<"name">>, Device,<<"">>),
|
Name = maps:get(<<"name">>, Device,<<"">>),
|
||||||
DevAddr = maps:get(<<"devaddr">>, Device,<<"">>),
|
DevAddr = maps:get(<<"devaddr">>, Device,<<"">>),
|
||||||
Enable = maps:get(<<"isEnable">>, Device, false),
|
Enable = maps:get(<<"isEnable">>, Device, false),
|
||||||
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl, Enable, Name, DevAddr], node()}).
|
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, get_Acl(Device,#{}), Enable, Name, DevAddr], node()}).
|
||||||
|
|
||||||
save(DeviceId, Data) ->
|
save(DeviceId, Data) ->
|
||||||
case lookup(DeviceId) of
|
case lookup(DeviceId) of
|
||||||
|
@ -94,18 +94,8 @@ init(?TYPE, ChannelId, Args) ->
|
|||||||
},
|
},
|
||||||
{ok, State, []}.
|
{ok, State, []}.
|
||||||
|
|
||||||
handle_init(#state{env = #{<<"order">> := Order, <<"offline">>:= OffLine}} = State) ->
|
handle_init(State) ->
|
||||||
dgiot_data:insert({device, offline},OffLine),
|
erlang:send_after(300, self(), {message, <<"_Pool">>, load}),
|
||||||
Success = fun(Page) ->
|
|
||||||
lists:map(fun(Device) ->
|
|
||||||
dgiot_device:save(Device)
|
|
||||||
end, Page)
|
|
||||||
end,
|
|
||||||
Query = #{
|
|
||||||
<<"order">> => Order,
|
|
||||||
<<"where">> => #{}
|
|
||||||
},
|
|
||||||
dgiot_parse_loader:start(<<"Device">>, Query, 0, 100, 1000000, Success),
|
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
%% 通道消息处理,注意:进程池调用
|
%% 通道消息处理,注意:进程池调用
|
||||||
@ -113,7 +103,13 @@ handle_event(_EventId, Event, State) ->
|
|||||||
?LOG(info, "Channel ~p", [Event]),
|
?LOG(info, "Channel ~p", [Event]),
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
handle_message(_Message, State) ->
|
handle_message(load, #state{env = #{<<"order">> := Order, <<"offline">>:= OffLine}} = State) ->
|
||||||
|
dgiot_data:insert({device, offline}, OffLine),
|
||||||
|
dgiot_device:load_device(Order),
|
||||||
|
dgiot_parse:load_role(),
|
||||||
|
{ok, State};
|
||||||
|
|
||||||
|
handle_message(_, State) ->
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
stop(ChannelType, ChannelId, _State) ->
|
stop(ChannelType, ChannelId, _State) ->
|
||||||
|
@ -130,7 +130,6 @@ synchronize_device(ProductId) ->
|
|||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
save(Product) ->
|
save(Product) ->
|
||||||
Product1 = format_product(Product),
|
Product1 = format_product(Product),
|
||||||
#{<<"productId">> := ProductId} = Product1,
|
#{<<"productId">> := ProductId} = Product1,
|
||||||
|
@ -86,7 +86,7 @@
|
|||||||
get_maintenanceid/2,
|
get_maintenanceid/2,
|
||||||
subscribe/2,
|
subscribe/2,
|
||||||
send_msg/3,
|
send_msg/3,
|
||||||
load/0,
|
load_role/0,
|
||||||
save_User_Role/2,
|
save_User_Role/2,
|
||||||
del_User_Role/2,
|
del_User_Role/2,
|
||||||
put_User_Role/3
|
put_User_Role/3
|
||||||
@ -1050,7 +1050,7 @@ test_graphql() ->
|
|||||||
%% {"operationName":"Health","variables":{},"query":"query Health {\n health\n}\n"}
|
%% {"operationName":"Health","variables":{},"query":"query Health {\n health\n}\n"}
|
||||||
graphql(Data).
|
graphql(Data).
|
||||||
|
|
||||||
load() ->
|
load_role() ->
|
||||||
Success = fun(Page) ->
|
Success = fun(Page) ->
|
||||||
lists:map(fun(X) ->
|
lists:map(fun(X) ->
|
||||||
#{<<"objectId">> := RoleId, <<"parent">> := #{<<"objectId">> := ParentId}} = X,
|
#{<<"objectId">> := RoleId, <<"parent">> := #{<<"objectId">> := ParentId}} = X,
|
||||||
|
@ -165,10 +165,8 @@ init(?TYPE, Channel, Cfg) ->
|
|||||||
],
|
],
|
||||||
{ok, State, Specs}.
|
{ok, State, Specs}.
|
||||||
|
|
||||||
|
|
||||||
%% 初始化池子
|
%% 初始化池子
|
||||||
handle_init(State) ->
|
handle_init(State) ->
|
||||||
dgiot_parse:load(),
|
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
handle_message(config, #state{cfg = Cfg} = State) ->
|
handle_message(config, #state{cfg = Cfg} = State) ->
|
||||||
|
Loading…
Reference in New Issue
Block a user