add device cache

This commit is contained in:
lsxredrain 2021-07-21 16:53:54 +08:00
parent 2a3606dd8d
commit 2818b8872d
5 changed files with 43 additions and 46 deletions

View File

@ -23,37 +23,26 @@
-dgiot_data("ets").
-export([init_ets/0]).
-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]).
init_ets() ->
dgiot_data:init(?DGIOT_PRODUCT),
ok.
post(Device) ->
DeviceId = maps:get(<<"objectId">>, Device),
#{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device),
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}),
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),
Name = maps:get(<<"name">>, Device,<<"">>),
DevAddr = maps:get(<<"devaddr">>, Device,<<"">>),
Enable = maps:get(<<"isEnable">>, Device, false),
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl,Enable, Name, DevAddr], node()}).
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).
put(Device) ->
DeviceId = maps:get(<<"objectId">>, Device),
case lookup(DeviceId) of
{ok, {[_Ts, {OldLatitude, OldLogitude}, OldProduct, OldAcl, OldEnable, OldName, OldDevAddr], Node}} ->
#{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device, #{<<"objectId">> => atom_to_binary(OldProduct)}),
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
maps:get(<<"location">>, Device, #{<<"latitude">> => OldLatitude, <<"longitude">> => OldLogitude}),
Product = binary_to_atom(ProductId),
get_Acl(Device,OldAcl) ->
NewAcl =
case maps:find(<<"ACL">>, Device) of
error ->
@ -63,17 +52,36 @@ put(Device) ->
Acc ++ [binary_to_atom(X)]
end, [], maps:keys(Acl))
end,
LastAcl =
case length(NewAcl) of
0 ->
OldAcl;
_ ->
NewAcl
end,
end.
post(Device) ->
DeviceId = maps:get(<<"objectId">>, Device),
#{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device),
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}),
Product = binary_to_atom(ProductId),
DeviceId = maps:get(<<"objectId">>, Device),
Name = maps:get(<<"name">>, Device,<<"">>),
DevAddr = maps:get(<<"devaddr">>, Device,<<"">>),
Enable = maps:get(<<"isEnable">>, Device, false),
dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, get_Acl(Device,#{}), Enable, Name, DevAddr], node()}).
put(Device) ->
DeviceId = maps:get(<<"objectId">>, Device),
case lookup(DeviceId) of
{ok, {[_Ts, {OldLatitude, OldLogitude}, OldProduct, OldAcl, OldEnable, OldName, OldDevAddr], Node}} ->
#{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device, #{<<"objectId">> => atom_to_binary(OldProduct)}),
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
maps:get(<<"location">>, Device, #{<<"latitude">> => OldLatitude, <<"longitude">> => OldLogitude}),
Product = binary_to_atom(ProductId),
Name = maps:get(<<"name">>, Device,OldName),
DevAddr = maps:get(<<"devaddr">>, Device,OldDevAddr),
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
end.
@ -83,15 +91,11 @@ save(Device) ->
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} =
maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}),
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),
Name = maps:get(<<"name">>, Device,<<"">>),
DevAddr = maps:get(<<"devaddr">>, Device,<<"">>),
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) ->
case lookup(DeviceId) of

View File

@ -94,18 +94,8 @@ init(?TYPE, ChannelId, Args) ->
},
{ok, State, []}.
handle_init(#state{env = #{<<"order">> := Order, <<"offline">>:= OffLine}} = State) ->
dgiot_data:insert({device, offline},OffLine),
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),
handle_init(State) ->
erlang:send_after(300, self(), {message, <<"_Pool">>, load}),
{ok, State}.
%% ,
@ -113,7 +103,13 @@ handle_event(_EventId, Event, State) ->
?LOG(info, "Channel ~p", [Event]),
{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}.
stop(ChannelType, ChannelId, _State) ->

View File

@ -130,7 +130,6 @@ synchronize_device(ProductId) ->
{error, Reason}
end.
save(Product) ->
Product1 = format_product(Product),
#{<<"productId">> := ProductId} = Product1,

View File

@ -86,7 +86,7 @@
get_maintenanceid/2,
subscribe/2,
send_msg/3,
load/0,
load_role/0,
save_User_Role/2,
del_User_Role/2,
put_User_Role/3
@ -1050,7 +1050,7 @@ test_graphql() ->
%% {"operationName":"Health","variables":{},"query":"query Health {\n health\n}\n"}
graphql(Data).
load() ->
load_role() ->
Success = fun(Page) ->
lists:map(fun(X) ->
#{<<"objectId">> := RoleId, <<"parent">> := #{<<"objectId">> := ParentId}} = X,

View File

@ -165,10 +165,8 @@ init(?TYPE, Channel, Cfg) ->
],
{ok, State, Specs}.
%%
handle_init(State) ->
dgiot_parse:load(),
{ok, State}.
handle_message(config, #state{cfg = Cfg} = State) ->