From b487fa5af13d9d5ffd628f2977a259748b1712ed Mon Sep 17 00:00:00 2001 From: lsxredrain <34489690@qq.com> Date: Tue, 20 Jul 2021 18:01:37 +0800 Subject: [PATCH] add device cache --- apps/dgiot_device/src/dgiot_device.erl | 48 ++++++++++--------- .../dgiot_device/src/dgiot_device_channel.erl | 15 +++++- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/apps/dgiot_device/src/dgiot_device.erl b/apps/dgiot_device/src/dgiot_device.erl index 83b46fd9e4..e5174f06d7 100644 --- a/apps/dgiot_device/src/dgiot_device.erl +++ b/apps/dgiot_device/src/dgiot_device.erl @@ -33,20 +33,26 @@ init_ets() -> 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}), + #{<<"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))), - dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl], node()}). + 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()}). put(Device) -> DeviceId = maps:get(<<"objectId">>, Device), case lookup(DeviceId) of - {ok, {[_Ts, {OldLatitude, OldLogitude}, OldProduct, OldAcl], Node}} -> + {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}), + #{<<"latitude">> := Latitude, <<"longitude">> := Logitude} = + maps:get(<<"location">>, Device, #{<<"latitude">> => OldLatitude, <<"longitude">> => OldLogitude}), Product = binary_to_atom(ProductId), NewAcl = case maps:find(<<"ACL">>, Device) of @@ -64,50 +70,48 @@ put(Device) -> _ -> NewAcl end, - dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, LastAcl], Node}); + 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}); _ -> - #{<<"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))), - dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl], node()}) + pass end. save(Device) -> - DeviceId = maps:get(<<"objectId">>, Device), #{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device), - #{<<"latitude">> := Latitude, <<"longitude">> := Logitude} = maps:get(<<"location">>, Device, #{<<"latitude">> => 0, <<"longitude">> => 0}), + #{<<"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))), - dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl], node()}). + 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()}). save(DeviceId, Data) -> case lookup(DeviceId) of - {ok, {[_Ts, {OldLatitude, OldLogitude}, Product, Acl], Node}} -> + {ok, {[_Ts, {OldLatitude, OldLogitude}, Product, Acl,Enable, Name, DevAddr], Node}} -> Latitude = maps:get(<<"latitude">>, Data, OldLatitude), Logitude = maps:get(<<"longitude">>, Data, OldLogitude), - dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl], Node}); + dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl,Enable, Name, DevAddr], Node}); _ -> pass end. save(ProductId, DevAddr, Data) -> DeviceId = dgiot_parse:get_deviceid(ProductId, DevAddr), case lookup(DeviceId) of - {ok, {[_Ts, {OldLatitude, OldLogitude}, Product, Acl], Node}} -> + {ok, {[_Ts, {OldLatitude, OldLogitude}, Product, Acl,Enable, Name, DevAddr], Node}} -> Latitude = maps:get(<<"latitude">>, Data, OldLatitude), Logitude = maps:get(<<"longitude">>, Data, OldLogitude), - dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl], Node}); + dgiot_mnesia:insert(DeviceId, {[dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl, Enable, Name, DevAddr], Node}); _ -> pass end. - - lookup(DeviceId) -> case dgiot_mnesia:lookup(DeviceId) of {atomic, []} -> diff --git a/apps/dgiot_device/src/dgiot_device_channel.erl b/apps/dgiot_device/src/dgiot_device_channel.erl index 3246615639..2db42ab0bd 100644 --- a/apps/dgiot_device/src/dgiot_device_channel.erl +++ b/apps/dgiot_device/src/dgiot_device_channel.erl @@ -54,6 +54,18 @@ zh => <<"排序"/utf8>> } }, + <<"offline">> => #{ + order => 2, + type => integer, + required => true, + default => 60, + title => #{ + zh => <<"离线超时时间(秒)"/utf8>> + }, + description => #{ + zh => <<"离线超时时间(秒)"/utf8>> + } + }, <<"ico">> => #{ order => 102, type => string, @@ -82,7 +94,8 @@ init(?TYPE, ChannelId, Args) -> }, {ok, State, []}. -handle_init(#state{env = #{<<"order">> := Order}} = 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)