From f2cc99427dda81fda91e74d8eac79f22686436c2 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Tue, 5 Sep 2023 17:18:55 +0800 Subject: [PATCH] fix endpoint --- apps/iot/src/iot_endpoint_sup.erl | 1 + apps/iot/src/mnesia/mnesia_data_importer.erl | 49 -------------------- apps/iot/src/mnesia/mnesia_endpoint.erl | 37 +++++++++++++++ 3 files changed, 38 insertions(+), 49 deletions(-) delete mode 100644 apps/iot/src/mnesia/mnesia_data_importer.erl diff --git a/apps/iot/src/iot_endpoint_sup.erl b/apps/iot/src/iot_endpoint_sup.erl index 0fd35e2..2c79c1d 100644 --- a/apps/iot/src/iot_endpoint_sup.erl +++ b/apps/iot/src/iot_endpoint_sup.erl @@ -15,6 +15,7 @@ start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) -> + mnesia_endpoint:import_static_endpoints(), Specs = lists:map(fun child_spec/1, mnesia_endpoint:get_all_endpoints()), {ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, Specs}}. diff --git a/apps/iot/src/mnesia/mnesia_data_importer.erl b/apps/iot/src/mnesia/mnesia_data_importer.erl deleted file mode 100644 index 329f961..0000000 --- a/apps/iot/src/mnesia/mnesia_data_importer.erl +++ /dev/null @@ -1,49 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @author aresei -%%% @copyright (C) 2023, -%%% @doc -%%% -%%% @end -%%% Created : 18. 8月 2023 09:28 -%%%------------------------------------------------------------------- --module(mnesia_data_importer). --author("aresei"). --include("iot.hrl"). - -%% API --export([import_endpoints/0]). - -import_endpoints() -> - zhongdian_mqtt_endpoint(). - -zhongdian_mqtt_endpoint() -> - Mapper0 = "fun(LocationCode, Fields, Timestamp) -> - Data = #{ - <<\"version\">> => <<\"1.0\">>, - <<\"location_code\">> => LocationCode, - <<\"ts\">> => Timestamp, - <<\"properties\">> => Fields - }, - {ok, iolist_to_binary(jiffy:encode(Data, [force_utf8]))} - end.", - Mapper = list_to_binary(Mapper0), - - {ok, MapperFun} = iot_util:parse_mapper(Mapper), - - mnesia_endpoint:insert(#endpoint{ - name = <<"zhongdian_mqtt">>, - title = <<"中电mqtt北向数据"/utf8>>, - matcher = <<".*">>, - mapper = Mapper, - mapper_fun = MapperFun, - config = #mqtt_endpoint{ - % host = <<"172.30.6.161">>, - host = <<"115.236.153.181">>, - port = 36980, - username = <<"admin">>, - password = <<"123456">>, - topic = <<"CET/NX/upload">>, - qos = 2 - }, - created_at = iot_util:timestamp_of_seconds() - }). \ No newline at end of file diff --git a/apps/iot/src/mnesia/mnesia_endpoint.erl b/apps/iot/src/mnesia/mnesia_endpoint.erl index 9c89c9f..a3e2ea5 100644 --- a/apps/iot/src/mnesia/mnesia_endpoint.erl +++ b/apps/iot/src/mnesia/mnesia_endpoint.erl @@ -14,6 +14,43 @@ %% API -export([get_keys/0, get_all_endpoints/0, get_endpoint/1, insert/1, delete/1]). -export([to_map/1, config_equals/2]). +-export([import_static_endpoints/0]). + +%% 导入静态的配置信息 +-spec import_static_endpoints() -> no_return(). +import_static_endpoints() -> + zhongdian_mqtt_endpoint(). + +zhongdian_mqtt_endpoint() -> + Mapper0 = "fun(LocationCode, Fields, Timestamp) -> + Data = #{ + <<\"version\">> => <<\"1.0\">>, + <<\"location_code\">> => LocationCode, + <<\"ts\">> => Timestamp, + <<\"properties\">> => Fields + }, + {ok, iolist_to_binary(jiffy:encode(Data, [force_utf8]))} + end.", + Mapper = list_to_binary(Mapper0), + + {ok, MapperFun} = iot_util:parse_mapper(Mapper), + + mnesia_endpoint:insert(#endpoint{ + name = <<"zhongdian_mqtt">>, + title = <<"中电mqtt北向数据"/utf8>>, + matcher = <<".*">>, + mapper = Mapper, + mapper_fun = MapperFun, + config = #mqtt_endpoint{ + host = <<"172.30.6.161">>, + port = 18083, + username = <<"admin">>, + password = <<"123456">>, + topic = <<"CET/NX/upload">>, + qos = 2 + }, + created_at = iot_util:timestamp_of_seconds() + }). -spec get_keys() -> [Name :: binary()]. get_keys() ->