diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index 8aaf1af..53e214f 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -99,11 +99,4 @@ location_code :: binary(), event_type :: integer(), params :: map() -}). - -%% 发送数据 --record(post_data, { - id = 0 :: integer(), - %% 数据库类型的endpoint, 可以返回list: [{K, V}, {K1, V1}] - body :: binary() | list() }). \ No newline at end of file diff --git a/apps/iot/src/endpoint/iot_http_endpoint.erl b/apps/iot/src/endpoint/iot_http_endpoint.erl index 8a7eeba..8565a17 100644 --- a/apps/iot/src/endpoint/iot_http_endpoint.erl +++ b/apps/iot/src/endpoint/iot_http_endpoint.erl @@ -159,12 +159,12 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) -> %%%=================================================================== -spec do_post(PostmanPid :: pid(), EventData :: #event_data{}) -> no_return(). -do_post(PostmanPid, #event_data{id = Id, location_code = LocationCode, event_type = EventType, params = Params}) when is_pid(PostmanPid) -> +do_post(PostmanPid, #event_data{id = Id, event_type = EventType, params = Params}) when is_pid(PostmanPid) -> Data = #{ <<"version">> => <<"1.0">>, <<"event_type">> => EventType, <<"params">> => Params }, Body = iolist_to_binary(jiffy:encode(Data, [force_utf8])), - PostmanPid ! {post, self(), #post_data{id = Id, body = Body}}, + PostmanPid ! {post, self(), {Id, Body}}, ok. \ No newline at end of file diff --git a/apps/iot/src/endpoint/iot_zd_endpoint.erl b/apps/iot/src/endpoint/iot_zd_endpoint.erl index 799d976..202664a 100644 --- a/apps/iot/src/endpoint/iot_zd_endpoint.erl +++ b/apps/iot/src/endpoint/iot_zd_endpoint.erl @@ -238,7 +238,7 @@ do_post(PostmanPid, #north_data{id = Id, location_code = LocationCode, real_loca }, try Body = iolist_to_binary(jiffy:encode(Data, [force_utf8])), - PostmanPid ! {post, self(), #post_data{id = Id, body = Body}} + PostmanPid ! {post, self(), {Id, Body}} catch _:_ -> self() ! {ack, Id, <<"json error">>} end. \ No newline at end of file diff --git a/apps/iot/src/mocker/iot_mock.erl b/apps/iot/src/mocker/iot_mock.erl index 21a280f..4c2a26b 100644 --- a/apps/iot/src/mocker/iot_mock.erl +++ b/apps/iot/src/mocker/iot_mock.erl @@ -30,7 +30,7 @@ test_influxdb() -> end, lists:seq(1, 100)). test_mqtt() -> - iot_zd_endpoint:forward({<<"location_code_test123">>, <<"location_code_test123">>}, [ + iot_zd_endpoint:forward(<<"location_code_test123">>, <<"location_code_test123">>, [ #{<<"key">> => <<"name">>, <<"value">> => <<"anlicheng">>}, #{<<"key">> => <<"age">>, <<"value">> => 30}, #{<<"key">> => <<"flow">>, <<"value">> => 30} diff --git a/apps/iot/src/postman/http_postman.erl b/apps/iot/src/postman/http_postman.erl index edbe9ba..ef2b095 100644 --- a/apps/iot/src/postman/http_postman.erl +++ b/apps/iot/src/postman/http_postman.erl @@ -74,7 +74,7 @@ handle_cast(_Request, State = #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_info({post, ReceiverPid, #post_data{id = Id, body = Body}}, State = #state{url = Url}) -> +handle_info({post, ReceiverPid, {Id, Body}}, State = #state{url = Url}) -> Headers = [ {<<"content-type">>, <<"application/json">>} ], diff --git a/apps/iot/src/postman/mqtt_postman.erl b/apps/iot/src/postman/mqtt_postman.erl index 55be2cb..e73bb1f 100644 --- a/apps/iot/src/postman/mqtt_postman.erl +++ b/apps/iot/src/postman/mqtt_postman.erl @@ -97,7 +97,7 @@ handle_info({puback, #{packet_id := PacketId}}, State = #state{inflight = Inflig end; %% 转发信息 -handle_info({post, ReceiverPid, #post_data{id = Id, body = Message}}, State = #state{conn_pid = ConnPid, inflight = InFlight, topic = Topic, qos = Qos}) -> +handle_info({post, ReceiverPid, {Id, Message}}, State = #state{conn_pid = ConnPid, inflight = InFlight, topic = Topic, qos = Qos}) -> lager:debug("[mqtt_postman] will publish topic: ~p, message: ~ts, qos: ~p", [Topic, Message, Qos]), case emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, Qos}, {retain, true}]) of ok -> diff --git a/apps/iot/src/postman/mysql_postman.erl b/apps/iot/src/postman/mysql_postman.erl index 9217397..dacd0ee 100644 --- a/apps/iot/src/postman/mysql_postman.erl +++ b/apps/iot/src/postman/mysql_postman.erl @@ -80,7 +80,7 @@ handle_cast(_Request, State = #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_info({post, ReceiverPid, #post_data{id = Id, body = Fields}}, State = #state{mysql_pid = ConnPid, table = Table}) when is_list(Fields) -> +handle_info({post, ReceiverPid, {Id, Fields}}, State = #state{mysql_pid = ConnPid, table = Table}) when is_list(Fields) -> case catch mysql_provider:insert(ConnPid, Table, Fields, false) of ok -> ReceiverPid ! {ack, Id};