add device cache

This commit is contained in:
lsxredrain 2021-07-09 12:40:15 +08:00
parent 195e43ccf2
commit 5fe3a9062f
2 changed files with 37 additions and 4 deletions

View File

@ -122,7 +122,7 @@ register_service(#state{}) ->
%% dgiot_metrics:gauge(dgiot_global, <<"node">>, [Key], Value)
ok
end, no, Info),
dgiot_mnesia:insert({node(), node}, Info#{pid => self(), update => Now})
dgiot_data:insert({node(), node}, Info#{pid => self(), update => Now})
catch
Err:Reason ->
?LOG(error, "~p:~p", [Err, Reason])

View File

@ -23,15 +23,48 @@
-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([save/1, lookup/2, save_prod/2, lookup_prod/1]).
-export([save/1, save/2, save/3, lookup/1, lookup/2, save_prod/2, lookup_prod/1]).
-export([encode/1, decode/3]).
init_ets() ->
dgiot_data:init(?DGIOT_PRODUCT),
ok.
save(_Device) ->
ok.
save(Device) ->
DeviceId = maps:get(<<"objectId">>, Device),
#{<<"objectId">> := ProductId} = maps:get(<<"product">>, Device),
#{<<"latitude">> := Latitude, <<"longitude">> := Logitude} = maps:get(<<"location">>, Device),
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, [node(), dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl]).
save(ProductId, DevAddr) ->
DeviceId = dgiot_parse:get_deviceid(ProductId, DevAddr),
case lookup(DeviceId) of
{ok, [Node, _Ts, Location, Product, Acl]} ->
dgiot_mnesia:insert(DeviceId, [Node, dgiot_datetime:now_ms(), Location, Product, Acl]);
_ -> pass
end.
save(DeviceId, Latitude, Logitude) ->
case lookup(DeviceId) of
{ok, [Node, _Ts, _, Product, Acl]} ->
dgiot_mnesia:insert(DeviceId, [Node, dgiot_datetime:now_ms(), {Latitude, Logitude}, Product, Acl]);
_ -> pass
end.
lookup(DeviceId) ->
case dgiot_mnesia:lookup(DeviceId) of
{atomic, []} ->
{error, not_find};
{aborted, Reason} ->
{error, Reason};
{ok, [{mnesia, _K, V}]} ->
{ok, V}
end.
lookup(ProductId, DevAddr) ->
DeviceId = dgiot_parse:get_deviceid(ProductId, DevAddr),