diff --git a/apps/iot/src/endpoint/iot_donghuoliren_endpoint.erl b/apps/iot/src/endpoint/iot_donghuoliren_endpoint.erl index 5f49965..07e179e 100644 --- a/apps/iot/src/endpoint/iot_donghuoliren_endpoint.erl +++ b/apps/iot/src/endpoint/iot_donghuoliren_endpoint.erl @@ -25,6 +25,8 @@ %% 签名用的token值 token :: binary(), url :: string(), + %% 保存了 #{location_code => "具体地址信息"}, 配置时是字符串,需要转换成binary + location_map :: #{}, succ_counter = 0, fail_counter = 0, logger_pid :: pid() @@ -67,10 +69,12 @@ init([]) -> {ok, Props} = application:get_env(iot, donghuoliren), Token = proplists:get_value(token, Props), Url = proplists:get_value(url, Props), + Locations0 = proplists:get_value(locations, Props), + Locations = lists:map(fun({K, V}) -> {list_to_binary(K), unicode:characters_to_binary(V)} end, Locations0), {ok, LoggerPid} = iot_logger:start_link("donghuoliren_data"), - {ok, #state{token = Token, url = Url, logger_pid = LoggerPid}}. + {ok, #state{token = Token, url = Url, location_map = maps:from_list(Locations), logger_pid = LoggerPid}}. %% @private %% @doc Handling call messages @@ -92,10 +96,10 @@ handle_call(get_status, _From, State = #state{succ_counter = SuccCounter, fail_c {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). handle_cast({forward, LocationCode, DynamicLocationCode, EventType, Params}, - State = #state{url = Url, token = Token, logger_pid = LoggerPid, succ_counter = SuccCounter, fail_counter = FailCounter}) -> - - Body = format_event(LocationCode, DynamicLocationCode, EventType, Params), + State = #state{url = Url, token = Token, location_map = LocationMap, logger_pid = LoggerPid, succ_counter = SuccCounter, fail_counter = FailCounter}) -> + Location = maps:get(LocationCode, LocationMap, <<"">>), + Body = format_event(Location, DynamicLocationCode, EventType, Params), %% 签名信息 Sign = iot_util:md5(iolist_to_binary([Token, Body, Token])), Url1 = Url ++ "?sign=" ++ binary_to_list(Sign), @@ -162,9 +166,9 @@ do_post(Url, Body) when is_binary(Body) -> %% 格式话要发送的数据,避免多次格式化处理 -spec format_event(LocationCode :: binary(), DynamicLocationCode :: binary(), EventType :: integer(), Params :: map()) -> Body :: binary(). -format_event(LocationCode, DynamicLocationCode, EventType, +format_event(Location, DynamicLocationCode, EventType, #{<<"datetime">> := Datetime, <<"attachments">> := Attachments0}) - when is_binary(LocationCode), is_binary(DynamicLocationCode), is_integer(EventType) -> + when is_binary(Location), is_binary(DynamicLocationCode), is_integer(EventType) -> %Attachments = lists:map(fun(#{<<"filename">> := Filename}) -> % {ok, FileUrl} = iot_util:file_uri(Filename), @@ -180,7 +184,7 @@ format_event(LocationCode, DynamicLocationCode, EventType, %% 对端有key的顺序限制 Params = #{ <<"attachments">> => Attachments, - <<"eventLocation">> => <<"事件地点测试值"/utf8>>, + <<"eventLocation">> => Location, <<"eventTime">> => Datetime, <<"eventType">> => <<"动火离人"/utf8>>, <<"videoJkAddr">> => <<"rtsp://admin:admin@123@192.168.111.147/cam/realmonitor?channel=1&subtype=0">> diff --git a/config/sys-dev.config b/config/sys-dev.config index 49cb86f..374c090 100644 --- a/config/sys-dev.config +++ b/config/sys-dev.config @@ -89,7 +89,11 @@ %% 智慧监控平台 {donghuoliren, [ {url, "https://xsdc.njau.edu.cn/hq-cyaqjg/rest/rgkSmart/push"}, - {token, <<"aB3$dEfGhiJkLmNoPqRsTuVwXyZ!@#4f5e6d7c8b9a0f1e2d">>} + {token, <<"aB3$dEfGhiJkLmNoPqRsTuVwXyZ!@#4f5e6d7c8b9a0f1e2d">>}, + {locations, [ + {"32981253076969472017320929999562", "诚苑餐厅一楼1F"}, + {"32981271601113088017320930441218", "诚苑餐厅一楼1F"} + ]} ]}, {pools, [ diff --git a/config/sys-prod.config b/config/sys-prod.config index 9ab9948..1b33417 100644 --- a/config/sys-prod.config +++ b/config/sys-prod.config @@ -66,7 +66,11 @@ %% 智慧监控平台 {donghuoliren, [ {url, "https://xsdc.njau.edu.cn/hq-cyaqjg/rest/rgkSmart/push"}, - {token, <<"aB3$dEfGhiJkLmNoPqRsTuVwXyZ!@#4f5e6d7c8b9a0f1e2d">>} + {token, <<"aB3$dEfGhiJkLmNoPqRsTuVwXyZ!@#4f5e6d7c8b9a0f1e2d">>}, + {locations, [ + {"32981253076969472017320929999562", "诚苑餐厅一楼1F"}, + {"32981271601113088017320930441218", "诚苑餐厅一楼1F"} + ]} ]}, %% influxdb数据库配置