增加环境变量
This commit is contained in:
parent
528b7fe8c8
commit
48a2dd40cf
@ -70,13 +70,23 @@ write_data(Measurement, Tags, FieldsList, Timestamp) when is_binary(Measurement)
|
||||
influx_point:new(Measurement, Tags, NFields, Timestamp)
|
||||
end, NFieldsList),
|
||||
Precision = influx_client:get_precision(Timestamp),
|
||||
Bucket = get_bucket(Measurement),
|
||||
|
||||
%poolboy:transaction(influx_pool_backup, fun(Pid) -> influx_client:write(Pid, ?DEFAULT_BUCKET, ?DEFAULT_ORG, Precision, Points) end);
|
||||
poolboy:transaction(influx_pool, fun(Pid) -> influx_client:write(Pid, ?DEFAULT_BUCKET, ?DEFAULT_ORG, Precision, Points) end);
|
||||
poolboy:transaction(influx_pool, fun(Pid) -> influx_client:write(Pid, Bucket, ?DEFAULT_ORG, Precision, Points) end);
|
||||
false ->
|
||||
ok
|
||||
end.
|
||||
|
||||
-spec get_bucket(DeviceUUID :: binary()) -> binary().
|
||||
get_bucket(DeviceUUID) when is_binary(DeviceUUID) ->
|
||||
case iot_env:exists(DeviceUUID) of
|
||||
true ->
|
||||
<<"metric_", DeviceUUID/binary>>;
|
||||
false ->
|
||||
?DEFAULT_BUCKET
|
||||
end.
|
||||
|
||||
-spec write(Pid :: pid(), Bucket :: binary(), Org :: binary(), Points :: list()) -> no_return().
|
||||
write(Pid, Bucket, Org, Points) when is_pid(Pid), is_binary(Bucket), is_binary(Org), is_list(Points) ->
|
||||
write(Pid, Bucket, Org, <<"ms">>, Points).
|
||||
|
||||
@ -15,6 +15,8 @@ start(_StartType, _StartArgs) ->
|
||||
io:setopts([{encoding, unicode}]),
|
||||
%% 加速内存的回收
|
||||
erlang:system_flag(fullsweep_after, 16),
|
||||
%% 加载环境变量
|
||||
ok = iot_env:new(),
|
||||
|
||||
%% 启动数据库
|
||||
% start_mnesia(),
|
||||
|
||||
48
apps/iot/src/iot_env.erl
Normal file
48
apps/iot/src/iot_env.erl
Normal file
@ -0,0 +1,48 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @author anlicheng
|
||||
%%% @copyright (C) 2024, <COMPANY>
|
||||
%%% @doc
|
||||
%%%
|
||||
%%% @end
|
||||
%%% Created : 30. 8月 2024 10:44
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(iot_env).
|
||||
-author("anlicheng").
|
||||
|
||||
%% API
|
||||
-export([new/0, reload/0]).
|
||||
-export([exists/1]).
|
||||
|
||||
-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}),
|
||||
ok.
|
||||
|
||||
-spec reload() -> ok.
|
||||
reload() ->
|
||||
DeviceUUIDs = load_env(),
|
||||
true = ets:insert(iot_env, {device_uuids, DeviceUUIDs}),
|
||||
ok.
|
||||
|
||||
-spec exists(DeviceUUID :: binary()) -> boolean().
|
||||
exists(DeviceUUID) when is_binary(DeviceUUID) ->
|
||||
case ets:lookup(iot_env, device_uuids) of
|
||||
[] ->
|
||||
false;
|
||||
[{device_uuids, DeviceUUIDs}|_] ->
|
||||
lists:member(DeviceUUID, DeviceUUIDs)
|
||||
end.
|
||||
|
||||
-spec load_env() -> [binary()].
|
||||
load_env() ->
|
||||
RootDir = code:root_dir(),
|
||||
EnvFile = RootDir ++ "/.env",
|
||||
case file:read_file(EnvFile) of
|
||||
{ok, Content} ->
|
||||
binary:split(string:trim(Content), <<"\n">>, [global, trim]);
|
||||
{error, Reason} ->
|
||||
lager:warning("[iot] read .env file get error: ~p", [Reason]),
|
||||
[]
|
||||
end.
|
||||
Loading…
x
Reference in New Issue
Block a user