From 7a5c92b4f92d94a0102a757409ec968dbfeeb902 Mon Sep 17 00:00:00 2001 From: anlicheng Date: Fri, 18 Aug 2023 09:40:20 +0800 Subject: [PATCH] fix --- apps/iot/src/iot_app.erl | 2 + apps/iot/src/mnesia/mnesia_data_importer.erl | 49 ++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 apps/iot/src/mnesia/mnesia_data_importer.erl diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index 59acba0..acb30c8 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -15,6 +15,8 @@ start(_StartType, _StartArgs) -> io:setopts([{encoding, unicode}]), %% 启动数据库 start_mnesia(), + %% 导入固定数据到mnesia + mnesia_data_importer:import_endpoints(), %% 加速内存的回收 erlang:system_flag(fullsweep_after, 16), diff --git a/apps/iot/src/mnesia/mnesia_data_importer.erl b/apps/iot/src/mnesia/mnesia_data_importer.erl new file mode 100644 index 0000000..a43a481 --- /dev/null +++ b/apps/iot/src/mnesia/mnesia_data_importer.erl @@ -0,0 +1,49 @@ +%%%------------------------------------------------------------------- +%%% @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(), + ok. + +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 = 1883, + username = <<"admin">>, + password = <<"123456">>, + topic = <<"CET/NX/upload">>, + qos = 2 + }, + created_at = iot_util:timestamp_of_seconds() + }). \ No newline at end of file