diff --git a/apps/dgiot_device/src/dgiot_device.erl b/apps/dgiot_device/src/dgiot_device.erl index e5174f06..e1be470b 100644 --- a/apps/dgiot_device/src/dgiot_device.erl +++ b/apps/dgiot_device/src/dgiot_device.erl @@ -23,28 +23,52 @@ -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. +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) -> 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()}). + 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), @@ -54,26 +78,10 @@ put(Device) -> #{<<"latitude">> := Latitude, <<"longitude">> := Logitude} = maps:get(<<"location">>, Device, #{<<"latitude">> => OldLatitude, <<"longitude">> => OldLogitude}), 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), 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 diff --git a/apps/dgiot_device/src/dgiot_device_channel.erl b/apps/dgiot_device/src/dgiot_device_channel.erl index 2db42ab0..ecf77323 100644 --- a/apps/dgiot_device/src/dgiot_device_channel.erl +++ b/apps/dgiot_device/src/dgiot_device_channel.erl @@ -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) -> diff --git a/apps/dgiot_device/src/dgiot_product.erl b/apps/dgiot_device/src/dgiot_product.erl index 2915e061..ec36564e 100644 --- a/apps/dgiot_device/src/dgiot_product.erl +++ b/apps/dgiot_device/src/dgiot_product.erl @@ -130,7 +130,6 @@ synchronize_device(ProductId) -> {error, Reason} end. - save(Product) -> Product1 = format_product(Product), #{<<"productId">> := ProductId} = Product1, diff --git a/apps/dgiot_parse/src/dgiot_parse.erl b/apps/dgiot_parse/src/dgiot_parse.erl index e6d9a6c6..3bc34552 100644 --- a/apps/dgiot_parse/src/dgiot_parse.erl +++ b/apps/dgiot_parse/src/dgiot_parse.erl @@ -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, diff --git a/apps/dgiot_parse/src/sever/dgiot_parse_channel.erl b/apps/dgiot_parse/src/sever/dgiot_parse_channel.erl index 8366ea6c..edf6a8a2 100644 --- a/apps/dgiot_parse/src/sever/dgiot_parse_channel.erl +++ b/apps/dgiot_parse/src/sever/dgiot_parse_channel.erl @@ -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) ->