fix
This commit is contained in:
parent
24c16d12f5
commit
dabb9aee2f
5
apps/iot/priv/env.config
Normal file
5
apps/iot/priv/env.config
Normal file
@ -0,0 +1,5 @@
|
||||
[
|
||||
{super_device_uuids, [
|
||||
{name, <<"test">>}
|
||||
]}
|
||||
].
|
||||
@ -81,7 +81,8 @@ write_data(Measurement, Tags, FieldsList, Timestamp) when is_binary(Measurement)
|
||||
|
||||
-spec get_bucket(DeviceUUID :: binary()) -> binary().
|
||||
get_bucket(DeviceUUID) when is_binary(DeviceUUID) ->
|
||||
case iot_env:exists(DeviceUUID) of
|
||||
SuperDeviceUUIDs = iot_env:get_value(super_device_uuids, []),
|
||||
case lists:member(DeviceUUID, SuperDeviceUUIDs) of
|
||||
true ->
|
||||
<<"metric_", DeviceUUID/binary>>;
|
||||
false ->
|
||||
|
||||
@ -9,39 +9,50 @@
|
||||
-module(iot_env).
|
||||
-author("anlicheng").
|
||||
|
||||
%% 定义变量
|
||||
-record(env, {
|
||||
key,
|
||||
val
|
||||
}).
|
||||
|
||||
%% API
|
||||
-export([new/0, reload/0]).
|
||||
-export([exists/1]).
|
||||
-export([new/0, reload/0, get_value/1, get_value/2]).
|
||||
|
||||
-spec new() -> ok.
|
||||
new() ->
|
||||
ets:new(iot_env, [public, set, named_table, {read_concurrency, true}]),
|
||||
DeviceUUIDs = load_env(),
|
||||
true = ets:insert(iot_env, {device_uuids, DeviceUUIDs}),
|
||||
ets:new(iot_env, [public, set, named_table, {read_concurrency, true}, {keypos, 2}]),
|
||||
Envs = load_env(),
|
||||
[ets:insert(iot_env, #env{key = Key, val = Val}) || {Key, Val} <- Envs],
|
||||
ok.
|
||||
|
||||
-spec reload() -> ok.
|
||||
reload() ->
|
||||
DeviceUUIDs = load_env(),
|
||||
true = ets:insert(iot_env, {device_uuids, DeviceUUIDs}),
|
||||
Envs = load_env(),
|
||||
[ets:insert(iot_env, #env{key = Key, val = Val}) || {Key, Val} <- Envs],
|
||||
ok.
|
||||
|
||||
-spec exists(DeviceUUID :: binary()) -> boolean().
|
||||
exists(DeviceUUID) when is_binary(DeviceUUID) ->
|
||||
case ets:lookup(iot_env, device_uuids) of
|
||||
-spec get_value(Key :: atom()) -> any().
|
||||
get_value(Key) when is_atom(Key) ->
|
||||
get_value(Key, undefined).
|
||||
|
||||
-spec get_value(Key :: atom(), Default :: any()) -> any().
|
||||
get_value(Key, Default) when is_atom(Key) ->
|
||||
case ets:lookup(iot_env, Key) of
|
||||
[] ->
|
||||
false;
|
||||
[{device_uuids, DeviceUUIDs}|_] ->
|
||||
lists:member(DeviceUUID, DeviceUUIDs)
|
||||
Default;
|
||||
[#env{val = Val}|_] ->
|
||||
Val
|
||||
end.
|
||||
|
||||
-spec load_env() -> [binary()].
|
||||
load_env() ->
|
||||
RootDir = code:root_dir(),
|
||||
EnvFile = RootDir ++ "/.env",
|
||||
Dir = code:priv_dir(iot),
|
||||
EnvFile = Dir ++ "/env.config",
|
||||
case file:read_file(EnvFile) of
|
||||
{ok, Content} ->
|
||||
binary:split(string:trim(Content), <<"\n">>, [global, trim]);
|
||||
{ok, Tokens, _} = erl_scan:string(binary_to_list(Content)),
|
||||
{ok, Forms} = erl_parse:parse_term(Tokens),
|
||||
Forms;
|
||||
{error, Reason} ->
|
||||
lager:warning("[iot] read .env file: ~p, get error: ~p", [EnvFile, Reason]),
|
||||
[]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user