From dabb9aee2feec01892d2ca98af975176fe494055 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 30 Aug 2024 14:57:44 +0800 Subject: [PATCH] fix --- apps/iot/priv/env.config | 5 +++ apps/iot/src/influxdb/influx_client.erl | 3 +- apps/iot/src/iot_env.erl | 47 +++++++++++++++---------- 3 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 apps/iot/priv/env.config diff --git a/apps/iot/priv/env.config b/apps/iot/priv/env.config new file mode 100644 index 0000000..663b93e --- /dev/null +++ b/apps/iot/priv/env.config @@ -0,0 +1,5 @@ +[ + {super_device_uuids, [ + {name, <<"test">>} + ]} +]. \ No newline at end of file diff --git a/apps/iot/src/influxdb/influx_client.erl b/apps/iot/src/influxdb/influx_client.erl index 6741811..30bbc24 100644 --- a/apps/iot/src/influxdb/influx_client.erl +++ b/apps/iot/src/influxdb/influx_client.erl @@ -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 -> diff --git a/apps/iot/src/iot_env.erl b/apps/iot/src/iot_env.erl index d98980a..d5e4462 100644 --- a/apps/iot/src/iot_env.erl +++ b/apps/iot/src/iot_env.erl @@ -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 - [] -> - false; - [{device_uuids, DeviceUUIDs}|_] -> - lists:member(DeviceUUID, DeviceUUIDs) - end. +-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 + [] -> + 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]), []