diff --git a/apps/iot/include/endpoint.hrl b/apps/iot/include/endpoint.hrl index 1a48ad2..1825578 100644 --- a/apps/iot/include/endpoint.hrl +++ b/apps/iot/include/endpoint.hrl @@ -36,7 +36,8 @@ username = <<>> :: binary(), password = <<>> :: binary(), database = <<>> :: binary(), - table_name = <<>> :: binary() + table_name = <<>> :: binary(), + fields_map = #{} :: map() }). -record(endpoint, { diff --git a/apps/iot/include/message_pb.hrl b/apps/iot/include/message_pb.hrl index a632950..461a809 100644 --- a/apps/iot/include/message_pb.hrl +++ b/apps/iot/include/message_pb.hrl @@ -83,7 +83,9 @@ -record(data, {service_id = <<>> :: unicode:chardata() | undefined, % = 1, optional device_uuid = <<>> :: unicode:chardata() | undefined, % = 2, optional - metric = <<>> :: unicode:chardata() | undefined % = 3, optional + route_key = <<>> :: unicode:chardata() | undefined, % = 3, optional + format = <<>> :: unicode:chardata() | undefined, % = 4, optional + metric = <<>> :: unicode:chardata() | undefined % = 5, optional }). -endif. @@ -125,4 +127,12 @@ }). -endif. +-ifndef('ALARM_PB_H'). +-define('ALARM_PB_H', true). +-record(alarm, + {service_id = <<>> :: unicode:chardata() | undefined, % = 1, optional + params = <<>> :: unicode:chardata() | undefined % = 2, optional + }). +-endif. + -endif. diff --git a/apps/iot/src/data_format/line_format.erl b/apps/iot/src/data_format/line_format.erl new file mode 100644 index 0000000..7f949a9 --- /dev/null +++ b/apps/iot/src/data_format/line_format.erl @@ -0,0 +1,82 @@ +%%%------------------------------------------------------------------- +%%% @author anlicheng +%%% @copyright (C) 2025, +%%% @doc +%%% +%%% @end +%%% Created : 14. 8月 2025 17:32 +%%%------------------------------------------------------------------- +-module(line_format). +-author("anlicheng"). + +%% API +-export([parse/1]). + +%% <<"cpu,name=xyz,uuid=\"this,=isx=test,\" key=345,key1=23.5,key2=yes 123457">>, +-spec parse(Metric :: binary()) -> map(). +parse(Metric) when is_binary(Metric) -> + case lexer(Metric) of + [Measurement, Fields, Timestamp] -> + Map = case binary:split(Measurement, <<",">>) of + [Measurement] -> + #{<<"measurement">> => Measurement, <<"tags">> => #{}}; + [Measurement0, Tags0] -> + Tags = maps:from_list(parser_keys(Tags0)), + #{<<"measurement">> => Measurement0, <<"tags">> => Tags} + end, + FieldsMap = maps:from_list(parser_keys(Fields)), + {ok, Map#{<<"fields">> => FieldsMap, <<"timestamp">> => binary_to_integer(Timestamp)}}; + _ -> + error + end. + +-spec lexer(Input :: binary()) -> [Token :: binary()]. +lexer(Input) when is_binary(Input) -> + lexer(Input, false, [], []). +lexer(<<>>, _Quotes, Current, Acc) -> + Last = list_to_binary(lists:reverse(Current)), + lists:reverse([Last|Acc]); +lexer(<<$\s, Rest/binary>>, Quotes, Current, Acc) -> + case Quotes of + true -> + lexer(Rest, Quotes, [" "|Current], Acc); + false -> + Part = list_to_binary(lists:reverse(Current)), + lexer(Rest, Quotes, [], [Part|Acc]) + end; +lexer(<<$\", Rest/binary>>, Quotes, Current, Acc) -> + lexer(Rest, not Quotes, [$\"|Current], Acc); +lexer(<>, Quotes, Current, Acc) -> + lexer(Rest, Quotes, [Char|Current], Acc). + +-spec parser_keys(Input :: binary()) -> [{Key :: binary(), Val :: binary()}]. +parser_keys(Input) when is_binary(Input) -> + Parts = split_keys(Input), + lists:flatmap(fun(Item) -> + case binary:split(Item, <<"=">>) of + [Key, Val] -> + [{Key, Val}]; + _ -> + [] + end + end, Parts). + +%% 将 "key=val,key1=val1"的格式转换成 ["key=val", "key1=val1"] +-spec split_keys(Input :: binary()) -> [binary()]. +split_keys(Input) when is_binary(Input) -> + split_keys(Input, false, [], []). +split_keys(<<>>, _Quotes, Current, Acc) -> + Last = list_to_binary(lists:reverse(Current)), + lists:reverse([Last|Acc]); +split_keys(<<$,, Rest/binary>>, Quotes, Current, Acc) -> + case Quotes of + true -> + split_keys(Rest, Quotes, [$,|Current], Acc); + false -> + Part = list_to_binary(lists:reverse(Current)), + split_keys(Rest, Quotes, [], [Part|Acc]) + end; +split_keys(<<$\", Rest/binary>>, Quotes, Current, Acc) -> + split_keys(Rest, not Quotes, [$\"|Current], Acc); +split_keys(<>, Quotes, Current, Acc) -> + split_keys(Rest, Quotes, [Char|Current], Acc). \ No newline at end of file diff --git a/apps/iot/src/endpoint/endpoint.erl b/apps/iot/src/endpoint/endpoint.erl index b6697c5..8a47ac1 100644 --- a/apps/iot/src/endpoint/endpoint.erl +++ b/apps/iot/src/endpoint/endpoint.erl @@ -19,29 +19,29 @@ %%%=================================================================== -spec start_link(Endpoint :: #endpoint{}) -> {'ok', pid()} | 'ignore' | {'error', term()}. -start_link(Endpoint = #endpoint{id = Id, config = #http_endpoint{}}) -> - Name = get_name(Id), +start_link(Endpoint = #endpoint{name = Name0, config = #http_endpoint{}}) -> + Name = get_name(Name0), endpoint_http:start_link(Name, Endpoint); -start_link(Endpoint = #endpoint{id = Id, config = #mqtt_endpoint{}}) -> - Name = get_name(Id), +start_link(Endpoint = #endpoint{name = Name0, config = #mqtt_endpoint{}}) -> + Name = get_name(Name0), endpoint_mqtt:start_link(Name, Endpoint); -start_link(Endpoint = #endpoint{id = Id, config = #mysql_endpoint{}}) -> - Name = get_name(Id), +start_link(Endpoint = #endpoint{name = Name0, config = #mysql_endpoint{}}) -> + Name = get_name(Name0), endpoint_mysql:start_link(Name, Endpoint). --spec get_name(Id :: integer()) -> atom(). -get_name(Id) when is_integer(Id) -> - list_to_atom("endpoint:" ++ integer_to_list(Id)). +-spec get_name(Name :: binary()) -> atom(). +get_name(Name) when is_binary(Name) -> + list_to_atom("endpoint:" ++ binary_to_list(Name)). --spec get_pid(Id :: integer()) -> undefined | pid(). -get_pid(Id) when is_integer(Id) -> - whereis(get_name(Id)). +-spec get_pid(Name :: binary()) -> undefined | pid(). +get_pid(Name) when is_binary(Name) -> + whereis(get_name(Name)). --spec forward(Pid :: undefined | pid(), LocationCode :: binary(), Fields :: list(), Timestamp :: integer()) -> no_return(). +-spec forward(Pid :: undefined | pid(), ServiceId :: binary(), Format :: binary(), Metric :: integer()) -> no_return(). forward(undefined, _, _, _) -> ok; -forward(Pid, LocationCode, Fields, Timestamp) when is_pid(Pid), is_binary(LocationCode), is_list(Fields); is_binary(Fields), is_integer(Timestamp) -> - gen_server:cast(Pid, {forward, LocationCode, Fields, Timestamp}). +forward(Pid, ServiceId, Format, Metric) when is_pid(Pid), is_binary(ServiceId), is_binary(Format), is_binary(Metric) -> + gen_server:cast(Pid, {forward, ServiceId, Format, Metric}). reload(Pid, NEndpoint = #endpoint{}) when is_pid(Pid) -> gen_statem:cast(Pid, {reload, NEndpoint}). diff --git a/apps/iot/src/endpoint/endpoint_buffer.erl b/apps/iot/src/endpoint/endpoint_buffer.erl index 3ccc24b..59d4400 100644 --- a/apps/iot/src/endpoint/endpoint_buffer.erl +++ b/apps/iot/src/endpoint/endpoint_buffer.erl @@ -36,9 +36,7 @@ -record(north_data, { id :: integer(), - location_code, - fields, - timestamp :: integer() + tuple :: any() }). -type buffer() :: #buffer{}. @@ -54,8 +52,8 @@ new(Endpoint = #endpoint{id = Id}, WindowSize) when is_integer(WindowSize), Wind #buffer{cursor = 0, tid = Tid, timer_pid = TimerPid, endpoint = Endpoint, window_size = WindowSize}. -spec append(tuple(), Buffer :: #buffer{}) -> NBuffer :: #buffer{}. -append({LocationCode, Fields, Timestamp}, Buffer = #buffer{tid = Tid, next_id = NextId, window_size = WindowSize, flight_num = FlightNum}) -> - NorthData = #north_data{id = NextId, location_code = LocationCode, fields = Fields, timestamp = Timestamp}, +append(Tuple, Buffer = #buffer{tid = Tid, next_id = NextId, window_size = WindowSize, flight_num = FlightNum}) -> + NorthData = #north_data{id = NextId, tuple = Tuple}, true = ets:insert(Tid, NorthData), NBuffer = Buffer#buffer{next_id = NextId + 1}, case FlightNum < WindowSize of @@ -72,34 +70,20 @@ trigger_n(Buffer = #buffer{window_size = WindowSize}) -> %% 触发读取下一条数据 -spec trigger_next(Buffer :: #buffer{}) -> NBuffer :: #buffer{}. -trigger_next(Buffer = #buffer{tid = Tid, cursor = Cursor, timer_pid = TimerPid, flight_num = FlightNum, endpoint = #endpoint{title = Title, mapper_fun = MapperFun}}) -> +trigger_next(Buffer = #buffer{tid = Tid, cursor = Cursor, timer_pid = TimerPid, flight_num = FlightNum}) -> case ets:next(Tid, Cursor) of '$end_of_table' -> Buffer; NKey -> - [NorthData = #north_data{id = Id, location_code = LocationCode}] = ets:lookup(Tid, NKey), - %lager:debug("[iot_endpoint] endpoint: ~p, fetch_next success, north data is: ~p", [Name, NorthData]), - %% 重发机制, 在发送的过程中mapper可能会改变 - case safe_invoke_mapper(MapperFun, NorthData) of - {ok, Body} -> - ReceiverPid = self(), - ReceiverPid ! {next_data, Id, LocationCode, Body}, - endpoint_timer:task(TimerPid, Id, fun() -> ReceiverPid ! {next_data, Id, LocationCode, Body} end), - - Buffer#buffer{flight_num = FlightNum + 1}; - {error, Error} -> - lager:debug("[iot_endpoint] forward endpoint: ~p, mapper get error: ~p, message discard", [Title, Error]), - ets:delete(Tid, Id), - Buffer; - ignore -> - lager:debug("[iot_endpoint] forward endpoint: ~p, mapper ignore, messge discard", [Title]), - ets:delete(Tid, Id), - Buffer - end + [#north_data{id = Id, tuple = Tuple}] = ets:lookup(Tid, NKey), + ReceiverPid = self(), + ReceiverPid ! {next_data, Id, Tuple}, + endpoint_timer:task(TimerPid, Id, fun() -> ReceiverPid ! {next_data, Id, Tuple} end), + Buffer#buffer{flight_num = FlightNum + 1} end. -spec ack(Id :: integer(), Buffer :: #buffer{}) -> NBuffer :: #buffer{}. -ack(Id, Buffer = #buffer{tid = Tid, timer_pid = TimerPid, acc_num = AccNum, flight_num = FlightNum}) when is_integer(Id) -> +ack(Id, Buffer = #buffer{tid = Tid, timer_pid = TimerPid, acc_num = AccNum, flight_num = FlightNum}) when is_integer(Id) -> true = ets:delete(Tid, Id), endpoint_timer:ack(TimerPid, Id), trigger_next(Buffer#buffer{acc_num = AccNum + 1, flight_num = FlightNum - 1}). @@ -119,18 +103,4 @@ cleanup(#buffer{timer_pid = TimerPid}) -> %%%=================================================================== %%% Internal functions -%%%=================================================================== - --spec safe_invoke_mapper(MapperFun :: fun(), NorthData :: #north_data{}) -> - {ok, Body :: any()} | ignore | {error, Reason :: any()}. -safe_invoke_mapper(MapperFun, #north_data{location_code = LocationCode, fields = Fields, timestamp = Timestamp}) -> - try - if - is_function(MapperFun, 2) -> - MapperFun(LocationCode, Fields); - is_function(MapperFun, 3) -> - MapperFun(LocationCode, Fields, Timestamp) - end - catch _:Error -> - {error, Error} - end. \ No newline at end of file +%%%=================================================================== \ No newline at end of file diff --git a/apps/iot/src/endpoint/endpoint_http.erl b/apps/iot/src/endpoint/endpoint_http.erl index 8da36da..5121d0c 100644 --- a/apps/iot/src/endpoint/endpoint_http.erl +++ b/apps/iot/src/endpoint/endpoint_http.erl @@ -68,8 +68,8 @@ handle_call(get_stat, _From, State = #state{buffer = Buffer}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_cast({forward, LocationCode, Fields, Timestamp}, State = #state{buffer = Buffer}) -> - NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer), +handle_cast({forward, ServiceId, Format, Metric}, State = #state{buffer = Buffer}) -> + NBuffer = endpoint_buffer:append({ServiceId, Format, Metric}, Buffer), {noreply, State#state{buffer = NBuffer}}; handle_cast(cleanup, State = #state{buffer = Buffer}) -> @@ -82,11 +82,12 @@ handle_cast(cleanup, State = #state{buffer = Buffer}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_info({next_data, Id, _LocationCode, Body}, State = #state{buffer = Buffer, endpoint = #endpoint{config = #http_endpoint{url = Url}}}) -> +handle_info({next_data, Id, {ServiceId, Format, Metric}}, State = #state{buffer = Buffer, endpoint = #endpoint{config = #http_endpoint{url = Url}}}) -> Headers = [ - {<<"content-type">>, <<"application/json">>} + {<<"Content-Type">>, <<"text/", Format/binary>>}, + {<<"Service-Id">>, ServiceId} ], - case hackney:request(post, Url, Headers, Body) of + case hackney:request(post, Url, Headers, Metric) of {ok, 200, _, ClientRef} -> {ok, RespBody} = hackney:body(ClientRef), hackney:close(ClientRef), diff --git a/apps/iot/src/endpoint/endpoint_mqtt.erl b/apps/iot/src/endpoint/endpoint_mqtt.erl index cb50c17..787fefc 100644 --- a/apps/iot/src/endpoint/endpoint_mqtt.erl +++ b/apps/iot/src/endpoint/endpoint_mqtt.erl @@ -81,8 +81,8 @@ handle_call(get_stat, _From, State = #state{buffer = Buffer}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_cast({forward, LocationCode, Fields, Timestamp}, State = #state{buffer = Buffer}) -> - NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer), +handle_cast({forward, ServiceId, Format, Metric}, State = #state{buffer = Buffer}) -> + NBuffer = endpoint_buffer:append({ServiceId, Format, Metric}, Buffer), {noreply, State#state{buffer = NBuffer}}. %% @private @@ -123,13 +123,15 @@ handle_info({timeout, _, create_postman}, State = #state{buffer = Buffer, status end; %% 离线时,忽略数据发送逻辑 -handle_info({next_data, _Id, _LocationCode, _Message}, State = #state{status = ?DISCONNECTED}) -> +handle_info({next_data, _Id, _Tuple}, State = #state{status = ?DISCONNECTED}) -> {keep_state, State}; %% 发送数据到mqtt服务器 -handle_info({next_data, Id, LocationCode, Message}, State = #state{status = ?CONNECTED, conn_pid = ConnPid, buffer = Buffer, inflight = InFlight, endpoint = #endpoint{config = #mqtt_endpoint{topic = Topic0, qos = Qos}}}) -> - Topic = re:replace(Topic0, <<"\\${location_code}">>, LocationCode, [global, {return, binary}]), - lager:debug("[mqtt_postman] will publish topic: ~p, message: ~p, qos: ~p", [Topic, Message, Qos]), - case emqtt:publish(ConnPid, Topic, #{}, Message, [{qos, Qos}, {retain, true}]) of +handle_info({next_data, Id, {ServiceId, Format, Metric}}, State = #state{status = ?CONNECTED, conn_pid = ConnPid, buffer = Buffer, inflight = InFlight, + endpoint = #endpoint{config = #mqtt_endpoint{topic = Topic0, qos = Qos}}}) -> + + Topic = re:replace(Topic0, <<"\\${service_id}">>, ServiceId, [global, {return, binary}]), + lager:debug("[mqtt_postman] will publish topic: ~p, format: ~p, metric: ~p, qos: ~p", [Topic, Format, Metric, Qos]), + case emqtt:publish(ConnPid, Topic, #{}, Metric, [{qos, Qos}, {retain, true}]) of ok -> NBuffer = endpoint_buffer:ack(Id, Buffer), {noreply, State#state{buffer = NBuffer}}; diff --git a/apps/iot/src/endpoint/endpoint_mysql.erl b/apps/iot/src/endpoint/endpoint_mysql.erl index 3baa49a..6689690 100644 --- a/apps/iot/src/endpoint/endpoint_mysql.erl +++ b/apps/iot/src/endpoint/endpoint_mysql.erl @@ -79,8 +79,8 @@ handle_call(get_stat, _From, State = #state{buffer = Buffer}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_cast({forward, LocationCode, Fields, Timestamp}, State = #state{buffer = Buffer}) -> - NBuffer = endpoint_buffer:append({LocationCode, Fields, Timestamp}, Buffer), +handle_cast({forward, ServiceId, Format, Metric}, State = #state{buffer = Buffer}) -> + NBuffer = endpoint_buffer:append({ServiceId, Format, Metric}, Buffer), {noreply, State#state{buffer = NBuffer}}; handle_cast(cleanup, State = #state{buffer = Buffer}) -> @@ -93,7 +93,9 @@ handle_cast(cleanup, State = #state{buffer = Buffer}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_info({timeout, _, create_postman}, State = #state{status = ?DISCONNECTED, buffer = Buffer, endpoint = #endpoint{title = Title, config = #mysql_endpoint{host = Host, port = Port, username = Username, password = Password, database = Database, table_name = TableName, pool_size = PoolSize}}}) -> +handle_info({timeout, _, create_postman}, State = #state{status = ?DISCONNECTED, buffer = Buffer, + endpoint = #endpoint{title = Title, config = #mysql_endpoint{host = Host, port = Port, username = Username, password = Password, database = Database}}}) -> + lager:debug("[iot_endpoint] endpoint: ~p, create postman", [Title]), WorkerArgs = [ {host, binary_to_list(Host)}, @@ -106,6 +108,7 @@ handle_info({timeout, _, create_postman}, State = #state{status = ?DISCONNECTED, ], %% 启动工作的线程池 + PoolSize = 5, case poolboy:start_link([{size, PoolSize}, {max_overflow, PoolSize}, {worker_module, mysql}], WorkerArgs) of {ok, PoolPid} -> NBuffer = endpoint_buffer:trigger_n(Buffer), @@ -120,13 +123,13 @@ handle_info({timeout, _, create_postman}, State = #state{status = ?DISCONNECTED, end; %% 离线时,忽略数据发送逻辑 -handle_info({next_data, _Id, _LocationCode, _Message}, State = #state{status = ?DISCONNECTED}) -> +handle_info({next_data, _Id, _Tuple}, State = #state{status = ?DISCONNECTED}) -> {noreply, State}; %% 发送数据到mqtt服务器 -handle_info({next_data, Id, _LocationCode, Fields}, State = #state{status = ?CONNECTED, pool_pid = PoolPid, buffer = Buffer, +handle_info({next_data, Id, ServiceId, Format, Metric}, State = #state{status = ?CONNECTED, pool_pid = PoolPid, buffer = Buffer, endpoint = #endpoint{title = Title, config = #mysql_endpoint{table_name = Table}}}) -> - {ok, InsertSql, Values} = insert_sql(Table, Fields), + {ok, InsertSql, Values} = insert_sql(Table, ServiceId, Format, Metric), case poolboy:transaction(PoolPid, fun(ConnPid) -> mysql:query(ConnPid, InsertSql, Values) end) of ok -> NBuffer = endpoint_buffer:ack(Id, Buffer), @@ -170,8 +173,8 @@ code_change(_OldVsn, State = #state{}, _Extra) -> %%% Internal functions %%%=================================================================== --spec insert_sql(Table :: binary(), Fields :: list()) -> {ok, Sql :: binary(), Values :: list()}. -insert_sql(Table, Fields) when is_binary(Table), is_list(Fields) -> +-spec insert_sql(Table :: binary(), ServiceId :: binary(), Format :: binary(), FieldsMap :: map(), Metric :: binary()) -> {ok, Sql :: binary(), Values :: list()}. +insert_sql(Table, ServiceId, <<"line">>, FieldsMap, Metric) when is_binary(Table), is_binary(ServiceId), is_binary(Metric) -> {Keys, Values} = kvs(Fields), FieldSql = iolist_to_binary(lists:join(<<", ">>, Keys)), diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 2db65b3..45052d4 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -338,8 +338,8 @@ handle_event({call, From}, {activate_device, DeviceUUID, Auth}, _, State = #stat end; %% todo -handle_event(cast, {handle, {data, #data{service_id = ServiceId, device_uuid = DeviceUUID, metric = Metric}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true, device_map = DeviceMap}) -> - lager:debug("[iot_host] metric_data host: ~p, service_id: ~p, device_uuid: ~p, metric: ~p", [UUID, ServiceId, DeviceUUID, Metric]), +handle_event(cast, {handle, {data, #data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey0, format = Format, metric = Metric}}}, ?STATE_ACTIVATED, State = #state{uuid = UUID, has_session = true, device_map = DeviceMap}) -> + lager:debug("[iot_host] metric_data host: ~p, service_id: ~p, device_uuid: ~p, route_key: ~p, metric: ~p", [UUID, ServiceId, DeviceUUID, RouteKey0, Metric]), case DeviceUUID =/= <<"">> of true -> case maps:find(DeviceUUID, DeviceMap) of @@ -349,9 +349,13 @@ handle_event(cast, {handle, {data, #data{service_id = ServiceId, device_uuid = D {ok, Device} -> case iot_device:is_activated(Device) of true -> - %EndpointPid = endpoint:get_pid(1), - %endpoint:forward(EndpointPid, ServiceId, DeviceMap, Metric), - + RouteKey = get_route_key(RouteKey0), + case endpoint:get_pid(RouteKey) of + undefined -> + ok; + EndpointPid -> + endpoint:forward(EndpointPid, ServiceId, Format, Metric) + end, NDevice = iot_device:change_status(Device, ?DEVICE_ONLINE), {keep_state, State#state{device_map = maps:put(DeviceUUID, NDevice, DeviceMap)}}; false -> @@ -439,6 +443,11 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) -> %%% Internal functions %%%=================================================================== +get_route_key(<<"">>) -> + <<"default">>; +get_route_key(RouteKey) when is_binary(RouteKey) -> + RouteKey. + -spec report_event(UUID :: binary(), NewStatus :: integer()) -> no_return(). report_event(UUID, NewStatus) when is_binary(UUID), is_integer(NewStatus) -> TextMap = #{ diff --git a/apps/iot/src/proto/message_pb.erl b/apps/iot/src/proto/message_pb.erl index 41a870e..c6dc024 100644 --- a/apps/iot/src/proto/message_pb.erl +++ b/apps/iot/src/proto/message_pb.erl @@ -79,9 +79,11 @@ -type event() :: #event{}. --export_type(['auth_request'/0, 'auth_reply'/0, 'pub'/0, 'async_call_reply'/0, 'deploy'/0, 'fetch_task_log'/0, 'invoke'/0, 'push_service_config'/0, 'data'/0, 'ping'/0, 'service_inform'/0, 'event'/0]). --type '$msg_name'() :: auth_request | auth_reply | pub | async_call_reply | deploy | fetch_task_log | invoke | push_service_config | data | ping | service_inform | event. --type '$msg'() :: auth_request() | auth_reply() | pub() | async_call_reply() | deploy() | fetch_task_log() | invoke() | push_service_config() | data() | ping() | service_inform() | event(). +-type alarm() :: #alarm{}. + +-export_type(['auth_request'/0, 'auth_reply'/0, 'pub'/0, 'async_call_reply'/0, 'deploy'/0, 'fetch_task_log'/0, 'invoke'/0, 'push_service_config'/0, 'data'/0, 'ping'/0, 'service_inform'/0, 'event'/0, 'alarm'/0]). +-type '$msg_name'() :: auth_request | auth_reply | pub | async_call_reply | deploy | fetch_task_log | invoke | push_service_config | data | ping | service_inform | event | alarm. +-type '$msg'() :: auth_request() | auth_reply() | pub() | async_call_reply() | deploy() | fetch_task_log() | invoke() | push_service_config() | data() | ping() | service_inform() | event() | alarm(). -export_type(['$msg_name'/0, '$msg'/0]). -if(?OTP_RELEASE >= 24). @@ -119,7 +121,8 @@ encode_msg(Msg, MsgName, Opts) -> data -> encode_msg_data(id(Msg, TrUserData), TrUserData); ping -> encode_msg_ping(id(Msg, TrUserData), TrUserData); service_inform -> encode_msg_service_inform(id(Msg, TrUserData), TrUserData); - event -> encode_msg_event(id(Msg, TrUserData), TrUserData) + event -> encode_msg_event(id(Msg, TrUserData), TrUserData); + alarm -> encode_msg_alarm(id(Msg, TrUserData), TrUserData) end. @@ -379,7 +382,7 @@ encode_msg_push_service_config(#push_service_config{service_id = F1, config_json encode_msg_data(Msg, TrUserData) -> encode_msg_data(Msg, <<>>, TrUserData). -encode_msg_data(#data{service_id = F1, device_uuid = F2, metric = F3}, Bin, TrUserData) -> +encode_msg_data(#data{service_id = F1, device_uuid = F2, route_key = F3, format = F4, metric = F5}, Bin, TrUserData) -> B1 = if F1 == undefined -> Bin; true -> begin @@ -400,13 +403,33 @@ encode_msg_data(#data{service_id = F1, device_uuid = F2, metric = F3}, Bin, TrUs end end end, - if F3 == undefined -> B2; + B3 = if F3 == undefined -> B2; + true -> + begin + TrF3 = id(F3, TrUserData), + case is_empty_string(TrF3) of + true -> B2; + false -> e_type_string(TrF3, <>, TrUserData) + end + end + end, + B4 = if F4 == undefined -> B3; + true -> + begin + TrF4 = id(F4, TrUserData), + case is_empty_string(TrF4) of + true -> B3; + false -> e_type_string(TrF4, <>, TrUserData) + end + end + end, + if F5 == undefined -> B4; true -> begin - TrF3 = id(F3, TrUserData), - case is_empty_string(TrF3) of - true -> B2; - false -> e_type_string(TrF3, <>, TrUserData) + TrF5 = id(F5, TrUserData), + case is_empty_string(TrF5) of + true -> B4; + false -> e_type_string(TrF5, <>, TrUserData) end end end. @@ -607,6 +630,31 @@ encode_msg_event(#event{service_id = F1, event_type = F2, params = F3}, Bin, TrU end end. +encode_msg_alarm(Msg, TrUserData) -> encode_msg_alarm(Msg, <<>>, TrUserData). + + +encode_msg_alarm(#alarm{service_id = F1, params = F2}, Bin, TrUserData) -> + B1 = if F1 == undefined -> Bin; + true -> + begin + TrF1 = id(F1, TrUserData), + case is_empty_string(TrF1) of + true -> Bin; + false -> e_type_string(TrF1, <>, TrUserData) + end + end + end, + if F2 == undefined -> B1; + true -> + begin + TrF2 = id(F2, TrUserData), + case is_empty_string(TrF2) of + true -> B1; + false -> e_type_string(TrF2, <>, TrUserData) + end + end + end. + e_field_ping_ips([Elem | Rest], Bin, TrUserData) -> Bin2 = <>, Bin3 = e_type_string(id(Elem, TrUserData), Bin2, TrUserData), @@ -786,7 +834,8 @@ decode_msg_2_doit(push_service_config, Bin, TrUserData) -> id(decode_msg_push_se decode_msg_2_doit(data, Bin, TrUserData) -> id(decode_msg_data(Bin, TrUserData), TrUserData); decode_msg_2_doit(ping, Bin, TrUserData) -> id(decode_msg_ping(Bin, TrUserData), TrUserData); decode_msg_2_doit(service_inform, Bin, TrUserData) -> id(decode_msg_service_inform(Bin, TrUserData), TrUserData); -decode_msg_2_doit(event, Bin, TrUserData) -> id(decode_msg_event(Bin, TrUserData), TrUserData). +decode_msg_2_doit(event, Bin, TrUserData) -> id(decode_msg_event(Bin, TrUserData), TrUserData); +decode_msg_2_doit(alarm, Bin, TrUserData) -> id(decode_msg_alarm(Bin, TrUserData), TrUserData). @@ -1240,63 +1289,77 @@ skip_32_push_service_config(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, skip_64_push_service_config(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_push_service_config(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). -decode_msg_data(Bin, TrUserData) -> dfp_read_field_def_data(Bin, 0, 0, 0, id(<<>>, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). +decode_msg_data(Bin, TrUserData) -> dfp_read_field_def_data(Bin, 0, 0, 0, id(<<>>, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). -dfp_read_field_def_data(<<10, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_data_service_id(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -dfp_read_field_def_data(<<18, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_data_device_uuid(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -dfp_read_field_def_data(<<26, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_data_metric(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -dfp_read_field_def_data(<<>>, 0, 0, _, F@_1, F@_2, F@_3, _) -> #data{service_id = F@_1, device_uuid = F@_2, metric = F@_3}; -dfp_read_field_def_data(Other, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dg_read_field_def_data(Other, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +dfp_read_field_def_data(<<10, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> d_field_data_service_id(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +dfp_read_field_def_data(<<18, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> d_field_data_device_uuid(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +dfp_read_field_def_data(<<26, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> d_field_data_route_key(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +dfp_read_field_def_data(<<34, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> d_field_data_format(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +dfp_read_field_def_data(<<42, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> d_field_data_metric(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +dfp_read_field_def_data(<<>>, 0, 0, _, F@_1, F@_2, F@_3, F@_4, F@_5, _) -> #data{service_id = F@_1, device_uuid = F@_2, route_key = F@_3, format = F@_4, metric = F@_5}; +dfp_read_field_def_data(Other, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> dg_read_field_def_data(Other, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData). -dg_read_field_def_data(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 32 - 7 -> dg_read_field_def_data(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -dg_read_field_def_data(<<0:1, X:7, Rest/binary>>, N, Acc, _, F@_1, F@_2, F@_3, TrUserData) -> +dg_read_field_def_data(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) when N < 32 - 7 -> dg_read_field_def_data(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +dg_read_field_def_data(<<0:1, X:7, Rest/binary>>, N, Acc, _, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> Key = X bsl N + Acc, case Key of - 10 -> d_field_data_service_id(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); - 18 -> d_field_data_device_uuid(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); - 26 -> d_field_data_metric(Rest, 0, 0, 0, F@_1, F@_2, F@_3, TrUserData); + 10 -> d_field_data_service_id(Rest, 0, 0, 0, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 18 -> d_field_data_device_uuid(Rest, 0, 0, 0, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 26 -> d_field_data_route_key(Rest, 0, 0, 0, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 34 -> d_field_data_format(Rest, 0, 0, 0, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 42 -> d_field_data_metric(Rest, 0, 0, 0, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); _ -> case Key band 7 of - 0 -> skip_varint_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 1 -> skip_64_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 2 -> skip_length_delimited_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 3 -> skip_group_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData); - 5 -> skip_32_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, TrUserData) + 0 -> skip_varint_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 1 -> skip_64_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 2 -> skip_length_delimited_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 3 -> skip_group_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); + 5 -> skip_32_data(Rest, 0, 0, Key bsr 3, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) end end; -dg_read_field_def_data(<<>>, 0, 0, _, F@_1, F@_2, F@_3, _) -> #data{service_id = F@_1, device_uuid = F@_2, metric = F@_3}. +dg_read_field_def_data(<<>>, 0, 0, _, F@_1, F@_2, F@_3, F@_4, F@_5, _) -> #data{service_id = F@_1, device_uuid = F@_2, route_key = F@_3, format = F@_4, metric = F@_5}. -d_field_data_service_id(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_data_service_id(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -d_field_data_service_id(<<0:1, X:7, Rest/binary>>, N, Acc, F, _, F@_2, F@_3, TrUserData) -> +d_field_data_service_id(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) when N < 57 -> d_field_data_service_id(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +d_field_data_service_id(<<0:1, X:7, Rest/binary>>, N, Acc, F, _, F@_2, F@_3, F@_4, F@_5, TrUserData) -> {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, - dfp_read_field_def_data(RestF, 0, 0, F, NewFValue, F@_2, F@_3, TrUserData). + dfp_read_field_def_data(RestF, 0, 0, F, NewFValue, F@_2, F@_3, F@_4, F@_5, TrUserData). -d_field_data_device_uuid(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_data_device_uuid(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -d_field_data_device_uuid(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, _, F@_3, TrUserData) -> +d_field_data_device_uuid(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) when N < 57 -> d_field_data_device_uuid(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +d_field_data_device_uuid(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, _, F@_3, F@_4, F@_5, TrUserData) -> {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, - dfp_read_field_def_data(RestF, 0, 0, F, F@_1, NewFValue, F@_3, TrUserData). + dfp_read_field_def_data(RestF, 0, 0, F, F@_1, NewFValue, F@_3, F@_4, F@_5, TrUserData). -d_field_data_metric(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> d_field_data_metric(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -d_field_data_metric(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, _, TrUserData) -> +d_field_data_route_key(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) when N < 57 -> d_field_data_route_key(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +d_field_data_route_key(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, _, F@_4, F@_5, TrUserData) -> {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, - dfp_read_field_def_data(RestF, 0, 0, F, F@_1, F@_2, NewFValue, TrUserData). + dfp_read_field_def_data(RestF, 0, 0, F, F@_1, F@_2, NewFValue, F@_4, F@_5, TrUserData). -skip_varint_data(<<1:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> skip_varint_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); -skip_varint_data(<<0:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +d_field_data_format(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) when N < 57 -> d_field_data_format(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +d_field_data_format(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, _, F@_5, TrUserData) -> + {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, + dfp_read_field_def_data(RestF, 0, 0, F, F@_1, F@_2, F@_3, NewFValue, F@_5, TrUserData). -skip_length_delimited_data(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) when N < 57 -> skip_length_delimited_data(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, TrUserData); -skip_length_delimited_data(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, TrUserData) -> +d_field_data_metric(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) when N < 57 -> d_field_data_metric(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +d_field_data_metric(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, _, TrUserData) -> + {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, + dfp_read_field_def_data(RestF, 0, 0, F, F@_1, F@_2, F@_3, F@_4, NewFValue, TrUserData). + +skip_varint_data(<<1:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> skip_varint_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +skip_varint_data(<<0:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> dfp_read_field_def_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData). + +skip_length_delimited_data(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) when N < 57 -> skip_length_delimited_data(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData); +skip_length_delimited_data(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> Length = X bsl N + Acc, <<_:Length/binary, Rest2/binary>> = Rest, - dfp_read_field_def_data(Rest2, 0, 0, F, F@_1, F@_2, F@_3, TrUserData). + dfp_read_field_def_data(Rest2, 0, 0, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData). -skip_group_data(Bin, _, Z2, FNum, F@_1, F@_2, F@_3, TrUserData) -> +skip_group_data(Bin, _, Z2, FNum, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> {_, Rest} = read_group(Bin, FNum), - dfp_read_field_def_data(Rest, 0, Z2, FNum, F@_1, F@_2, F@_3, TrUserData). + dfp_read_field_def_data(Rest, 0, Z2, FNum, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData). -skip_32_data(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +skip_32_data(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> dfp_read_field_def_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData). -skip_64_data(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +skip_64_data(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData) -> dfp_read_field_def_data(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, F@_4, F@_5, TrUserData). decode_msg_ping(Bin, TrUserData) -> dfp_read_field_def_ping(Bin, @@ -1641,6 +1704,57 @@ skip_32_event(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> skip_64_event(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> dfp_read_field_def_event(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData). +decode_msg_alarm(Bin, TrUserData) -> dfp_read_field_def_alarm(Bin, 0, 0, 0, id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). + +dfp_read_field_def_alarm(<<10, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> d_field_alarm_service_id(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData); +dfp_read_field_def_alarm(<<18, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> d_field_alarm_params(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData); +dfp_read_field_def_alarm(<<>>, 0, 0, _, F@_1, F@_2, _) -> #alarm{service_id = F@_1, params = F@_2}; +dfp_read_field_def_alarm(Other, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dg_read_field_def_alarm(Other, Z1, Z2, F, F@_1, F@_2, TrUserData). + +dg_read_field_def_alarm(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 32 - 7 -> dg_read_field_def_alarm(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +dg_read_field_def_alarm(<<0:1, X:7, Rest/binary>>, N, Acc, _, F@_1, F@_2, TrUserData) -> + Key = X bsl N + Acc, + case Key of + 10 -> d_field_alarm_service_id(Rest, 0, 0, 0, F@_1, F@_2, TrUserData); + 18 -> d_field_alarm_params(Rest, 0, 0, 0, F@_1, F@_2, TrUserData); + _ -> + case Key band 7 of + 0 -> skip_varint_alarm(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 1 -> skip_64_alarm(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 2 -> skip_length_delimited_alarm(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 3 -> skip_group_alarm(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 5 -> skip_32_alarm(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData) + end + end; +dg_read_field_def_alarm(<<>>, 0, 0, _, F@_1, F@_2, _) -> #alarm{service_id = F@_1, params = F@_2}. + +d_field_alarm_service_id(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 57 -> d_field_alarm_service_id(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +d_field_alarm_service_id(<<0:1, X:7, Rest/binary>>, N, Acc, F, _, F@_2, TrUserData) -> + {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, + dfp_read_field_def_alarm(RestF, 0, 0, F, NewFValue, F@_2, TrUserData). + +d_field_alarm_params(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 57 -> d_field_alarm_params(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +d_field_alarm_params(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, _, TrUserData) -> + {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, + dfp_read_field_def_alarm(RestF, 0, 0, F, F@_1, NewFValue, TrUserData). + +skip_varint_alarm(<<1:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> skip_varint_alarm(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData); +skip_varint_alarm(<<0:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_alarm(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). + +skip_length_delimited_alarm(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 57 -> skip_length_delimited_alarm(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +skip_length_delimited_alarm(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) -> + Length = X bsl N + Acc, + <<_:Length/binary, Rest2/binary>> = Rest, + dfp_read_field_def_alarm(Rest2, 0, 0, F, F@_1, F@_2, TrUserData). + +skip_group_alarm(Bin, _, Z2, FNum, F@_1, F@_2, TrUserData) -> + {_, Rest} = read_group(Bin, FNum), + dfp_read_field_def_alarm(Rest, 0, Z2, FNum, F@_1, F@_2, TrUserData). + +skip_32_alarm(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_alarm(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). + +skip_64_alarm(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_alarm(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). + read_group(Bin, FieldNum) -> {NumBytes, EndTagLen} = read_gr_b(Bin, 0, 0, 0, 0, FieldNum), <> = Bin, @@ -1718,7 +1832,8 @@ merge_msgs(Prev, New, MsgName, Opts) -> data -> merge_msg_data(Prev, New, TrUserData); ping -> merge_msg_ping(Prev, New, TrUserData); service_inform -> merge_msg_service_inform(Prev, New, TrUserData); - event -> merge_msg_event(Prev, New, TrUserData) + event -> merge_msg_event(Prev, New, TrUserData); + alarm -> merge_msg_alarm(Prev, New, TrUserData) end. -compile({nowarn_unused_function,merge_msg_auth_request/3}). @@ -1834,7 +1949,8 @@ merge_msg_push_service_config(#push_service_config{service_id = PFservice_id, co end}. -compile({nowarn_unused_function,merge_msg_data/3}). -merge_msg_data(#data{service_id = PFservice_id, device_uuid = PFdevice_uuid, metric = PFmetric}, #data{service_id = NFservice_id, device_uuid = NFdevice_uuid, metric = NFmetric}, _) -> +merge_msg_data(#data{service_id = PFservice_id, device_uuid = PFdevice_uuid, route_key = PFroute_key, format = PFformat, metric = PFmetric}, + #data{service_id = NFservice_id, device_uuid = NFdevice_uuid, route_key = NFroute_key, format = NFformat, metric = NFmetric}, _) -> #data{service_id = if NFservice_id =:= undefined -> PFservice_id; true -> NFservice_id @@ -1843,6 +1959,14 @@ merge_msg_data(#data{service_id = PFservice_id, device_uuid = PFdevice_uuid, met if NFdevice_uuid =:= undefined -> PFdevice_uuid; true -> NFdevice_uuid end, + route_key = + if NFroute_key =:= undefined -> PFroute_key; + true -> NFroute_key + end, + format = + if NFformat =:= undefined -> PFformat; + true -> NFformat + end, metric = if NFmetric =:= undefined -> PFmetric; true -> NFmetric @@ -1944,6 +2068,17 @@ merge_msg_event(#event{service_id = PFservice_id, event_type = PFevent_type, par true -> NFparams end}. +-compile({nowarn_unused_function,merge_msg_alarm/3}). +merge_msg_alarm(#alarm{service_id = PFservice_id, params = PFparams}, #alarm{service_id = NFservice_id, params = NFparams}, _) -> + #alarm{service_id = + if NFservice_id =:= undefined -> PFservice_id; + true -> NFservice_id + end, + params = + if NFparams =:= undefined -> PFparams; + true -> NFparams + end}. + verify_msg(Msg) when tuple_size(Msg) >= 1 -> verify_msg(Msg, element(1, Msg), []); verify_msg(X) -> mk_type_error(not_a_known_message, X, []). @@ -1967,6 +2102,7 @@ verify_msg(Msg, MsgName, Opts) -> ping -> v_msg_ping(Msg, [MsgName], TrUserData); service_inform -> v_msg_service_inform(Msg, [MsgName], TrUserData); event -> v_msg_event(Msg, [MsgName], TrUserData); + alarm -> v_msg_alarm(Msg, [MsgName], TrUserData); _ -> mk_type_error(not_a_known_message, Msg, []) end. @@ -2087,7 +2223,7 @@ v_msg_push_service_config(X, Path, _TrUserData) -> mk_type_error({expected_msg, -compile({nowarn_unused_function,v_msg_data/3}). -dialyzer({nowarn_function,v_msg_data/3}). -v_msg_data(#data{service_id = F1, device_uuid = F2, metric = F3}, Path, TrUserData) -> +v_msg_data(#data{service_id = F1, device_uuid = F2, route_key = F3, format = F4, metric = F5}, Path, TrUserData) -> if F1 == undefined -> ok; true -> v_type_string(F1, [service_id | Path], TrUserData) end, @@ -2095,7 +2231,13 @@ v_msg_data(#data{service_id = F1, device_uuid = F2, metric = F3}, Path, TrUserDa true -> v_type_string(F2, [device_uuid | Path], TrUserData) end, if F3 == undefined -> ok; - true -> v_type_string(F3, [metric | Path], TrUserData) + true -> v_type_string(F3, [route_key | Path], TrUserData) + end, + if F4 == undefined -> ok; + true -> v_type_string(F4, [format | Path], TrUserData) + end, + if F5 == undefined -> ok; + true -> v_type_string(F5, [metric | Path], TrUserData) end, ok; v_msg_data(X, Path, _TrUserData) -> mk_type_error({expected_msg, data}, X, Path). @@ -2184,6 +2326,18 @@ v_msg_event(#event{service_id = F1, event_type = F2, params = F3}, Path, TrUserD ok; v_msg_event(X, Path, _TrUserData) -> mk_type_error({expected_msg, event}, X, Path). +-compile({nowarn_unused_function,v_msg_alarm/3}). +-dialyzer({nowarn_function,v_msg_alarm/3}). +v_msg_alarm(#alarm{service_id = F1, params = F2}, Path, TrUserData) -> + if F1 == undefined -> ok; + true -> v_type_string(F1, [service_id | Path], TrUserData) + end, + if F2 == undefined -> ok; + true -> v_type_string(F2, [params | Path], TrUserData) + end, + ok; +v_msg_alarm(X, Path, _TrUserData) -> mk_type_error({expected_msg, alarm}, X, Path). + -compile({nowarn_unused_function,v_type_int32/3}). -dialyzer({nowarn_function,v_type_int32/3}). v_type_int32(N, _Path, _TrUserData) when is_integer(N), -2147483648 =< N, N =< 2147483647 -> ok; @@ -2282,7 +2436,9 @@ get_msg_defs() -> {{msg, data}, [#field{name = service_id, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = device_uuid, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, - #field{name = metric, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]}, + #field{name = route_key, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}, + #field{name = format, fnum = 4, rnum = 5, type = string, occurrence = optional, opts = []}, + #field{name = metric, fnum = 5, rnum = 6, type = string, occurrence = optional, opts = []}]}, {{msg, ping}, [#field{name = adcode, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = boot_time, fnum = 2, rnum = 3, type = uint32, occurrence = optional, opts = []}, @@ -2305,16 +2461,17 @@ get_msg_defs() -> {{msg, event}, [#field{name = service_id, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = event_type, fnum = 2, rnum = 3, type = uint32, occurrence = optional, opts = []}, - #field{name = params, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]}]. + #field{name = params, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]}, + {{msg, alarm}, [#field{name = service_id, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = params, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]}]. -get_msg_names() -> [auth_request, auth_reply, pub, async_call_reply, deploy, fetch_task_log, invoke, push_service_config, data, ping, service_inform, event]. +get_msg_names() -> [auth_request, auth_reply, pub, async_call_reply, deploy, fetch_task_log, invoke, push_service_config, data, ping, service_inform, event, alarm]. get_group_names() -> []. -get_msg_or_group_names() -> [auth_request, auth_reply, pub, async_call_reply, deploy, fetch_task_log, invoke, push_service_config, data, ping, service_inform, event]. +get_msg_or_group_names() -> [auth_request, auth_reply, pub, async_call_reply, deploy, fetch_task_log, invoke, push_service_config, data, ping, service_inform, event, alarm]. get_enum_names() -> []. @@ -2359,7 +2516,9 @@ find_msg_def(push_service_config) -> find_msg_def(data) -> [#field{name = service_id, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = device_uuid, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, - #field{name = metric, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]; + #field{name = route_key, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}, + #field{name = format, fnum = 4, rnum = 5, type = string, occurrence = optional, opts = []}, + #field{name = metric, fnum = 5, rnum = 6, type = string, occurrence = optional, opts = []}]; find_msg_def(ping) -> [#field{name = adcode, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = boot_time, fnum = 2, rnum = 3, type = uint32, occurrence = optional, opts = []}, @@ -2383,6 +2542,7 @@ find_msg_def(event) -> [#field{name = service_id, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = event_type, fnum = 2, rnum = 3, type = uint32, occurrence = optional, opts = []}, #field{name = params, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]; +find_msg_def(alarm) -> [#field{name = service_id, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = params, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]; find_msg_def(_) -> error. @@ -2453,6 +2613,7 @@ fqbin_to_msg_name(<<"Data">>) -> data; fqbin_to_msg_name(<<"Ping">>) -> ping; fqbin_to_msg_name(<<"ServiceInform">>) -> service_inform; fqbin_to_msg_name(<<"Event">>) -> event; +fqbin_to_msg_name(<<"Alarm">>) -> alarm; fqbin_to_msg_name(E) -> error({gpb_error, {badmsg, E}}). @@ -2468,6 +2629,7 @@ msg_name_to_fqbin(data) -> <<"Data">>; msg_name_to_fqbin(ping) -> <<"Ping">>; msg_name_to_fqbin(service_inform) -> <<"ServiceInform">>; msg_name_to_fqbin(event) -> <<"Event">>; +msg_name_to_fqbin(alarm) -> <<"Alarm">>; msg_name_to_fqbin(E) -> error({gpb_error, {badmsg, E}}). @@ -2506,7 +2668,7 @@ get_all_source_basenames() -> ["message_pb.proto"]. get_all_proto_names() -> ["message_pb"]. -get_msg_containment("message_pb") -> [async_call_reply, auth_reply, auth_request, data, deploy, event, fetch_task_log, invoke, ping, pub, push_service_config, service_inform]; +get_msg_containment("message_pb") -> [alarm, async_call_reply, auth_reply, auth_request, data, deploy, event, fetch_task_log, invoke, ping, pub, push_service_config, service_inform]; get_msg_containment(P) -> error({gpb_error, {badproto, P}}). @@ -2538,6 +2700,7 @@ get_proto_by_msg_name_as_fqbin(<<"Deploy">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"AuthReply">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"AsyncCallReply">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"ServiceInform">>) -> "message_pb"; +get_proto_by_msg_name_as_fqbin(<<"Alarm">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(E) -> error({gpb_error, {badmsg, E}}).