diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index 53e214f..efeef44 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -97,6 +97,7 @@ -record(event_data, { id = 0 :: integer(), location_code :: binary(), + real_location_code :: binary(), event_type :: integer(), params :: map() }). \ 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 8565a17..d68d38a 100644 --- a/apps/iot/src/endpoint/iot_http_endpoint.erl +++ b/apps/iot/src/endpoint/iot_http_endpoint.erl @@ -14,7 +14,7 @@ %% API -export([start_link/2]). --export([get_pid/1, forward/4, get_stat/0]). +-export([get_pid/1, forward/5, get_stat/0]). %% gen_statem callbacks -export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]). @@ -42,9 +42,9 @@ get_pid(Name) when is_atom(Name) -> whereis(Name). --spec forward(Pid :: pid(), LocationCode :: binary(), EventType :: integer(), Params :: map()) -> no_return(). -forward(Pid, LocationCode, EventType, Params) when is_pid(Pid), is_binary(LocationCode), is_integer(EventType), is_map(Params) -> - gen_statem:cast(Pid, {forward, LocationCode, EventType, Params}). +-spec forward(Pid :: pid(), LocationCode :: binary(), RealLocationCode :: binary(), EventType :: integer(), Params :: map()) -> no_return(). +forward(Pid, LocationCode, RealLocationCode, EventType, Params) when is_pid(Pid), is_binary(LocationCode), is_binary(RealLocationCode), is_integer(EventType), is_map(Params) -> + gen_statem:cast(Pid, {forward, LocationCode, RealLocationCode, EventType, Params}). -spec get_stat() -> {ok, Stat :: #{}}. get_stat() -> @@ -82,8 +82,8 @@ callback_mode() -> %% functions is called when gen_statem receives and event from %% call/2, cast/2, or as a normal process message. -handle_event(cast, {forward, LocationCode, EventType, Params}, _, State = #state{id = Id, flight_num = FlightNum, pool_size = PoolSize, queue = Q}) -> - EventData = #event_data{id = Id, location_code = LocationCode, event_type = EventType, params = Params}, +handle_event(cast, {forward, LocationCode, RealLocationCode, EventType, Params}, _, State = #state{id = Id, flight_num = FlightNum, pool_size = PoolSize, queue = Q}) -> + EventData = #event_data{id = Id, location_code = LocationCode, real_location_code = RealLocationCode, event_type = EventType, params = Params}, %% 避免不必要的内部消息 Actions = case FlightNum < PoolSize of true -> [{next_event, info, fetch_next}]; diff --git a/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl b/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl index fd1a5d9..83a4cff 100644 --- a/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl +++ b/apps/iot/src/endpoint/iot_jinzhi_endpoint.erl @@ -14,7 +14,7 @@ %% API -export([start_link/0]). --export([get_pid/0, forward/3, get_stat/0]). +-export([get_pid/0, forward/4, get_stat/0]). %% gen_statem callbacks -export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]). @@ -46,9 +46,9 @@ get_pid() -> whereis(?MODULE). --spec forward(LocationCode :: binary(), EventType :: integer(), Params :: map()) -> no_return(). -forward(LocationCode, EventType, Params) when is_binary(LocationCode), is_integer(EventType), is_map(Params) -> - gen_statem:cast(?MODULE, {forward, LocationCode, EventType, Params}). +-spec forward(LocationCode :: binary(), RealLocationCode :: binary(), EventType :: integer(), Params :: map()) -> no_return(). +forward(LocationCode, RealLocationCode, EventType, Params) when is_binary(LocationCode), is_binary(RealLocationCode), is_integer(EventType), is_map(Params) -> + gen_statem:cast(?MODULE, {forward, LocationCode, RealLocationCode, EventType, Params}). -spec get_stat() -> {ok, Stat :: #{}}. get_stat() -> @@ -92,8 +92,8 @@ callback_mode() -> %% functions is called when gen_statem receives and event from %% call/2, cast/2, or as a normal process message. -handle_event(cast, {forward, LocationCode, EventType, Params}, _, State = #state{id = Id, flight_num = FlightNum, pool_size = PoolSize, queue = Q}) -> - EventData = #event_data{id = Id, location_code = LocationCode, event_type = EventType, params = Params}, +handle_event(cast, {forward, LocationCode, RealLocationCode, EventType, Params}, _, State = #state{id = Id, flight_num = FlightNum, pool_size = PoolSize, queue = Q}) -> + EventData = #event_data{id = Id, location_code = LocationCode, real_location_code = RealLocationCode, event_type = EventType, params = Params}, %% 避免不必要的内部消息 Actions = case FlightNum < PoolSize of true -> [{next_event, info, fetch_next}]; @@ -176,7 +176,7 @@ code_change(_OldVsn, StateName, State = #state{}, _Extra) -> %%%=================================================================== -spec do_post(EventData :: #event_data{}, State :: #state{}) -> no_return(). -do_post(#event_data{id = Id, location_code = LocationCode, event_type = EventType, +do_post(#event_data{id = Id, location_code = LocationCode, real_location_code = RealLocationCode, event_type = EventType, params = Params = #{<<"event_code">> := EventCode, <<"description">> := Description, <<"datetime">> := Datetime, <<"attachments">> := Attachments0}}, #state{pri_key = PriKey, url = Url, logger_pid = LoggerPid}) -> @@ -190,6 +190,7 @@ do_post(#event_data{id = Id, location_code = LocationCode, event_type = EventTyp DeviceInfo = #{ <<"location">> => LocationCode, + <<"real_location">> => RealLocationCode, <<"category">> => EventCode, <<"description">> => Description, <<"occurrenceTime">> => Datetime, diff --git a/apps/iot/src/iot_ai_router.erl b/apps/iot/src/iot_ai_router.erl index 964dba3..b095c23 100644 --- a/apps/iot/src/iot_ai_router.erl +++ b/apps/iot/src/iot_ai_router.erl @@ -16,11 +16,11 @@ -spec route_uuid(RouterUUID :: binary(), EventType :: integer(), Params :: map()) -> no_return(). route_uuid(RouterUUID, EventType, Params) when is_binary(RouterUUID), is_integer(EventType), is_map(Params) -> %% 查找终端设备对应的点位信息 - case redis_client:hget(RouterUUID, <<"location_code">>) of - {ok, undefined} -> + case redis_client:hgetall(RouterUUID) of + {ok, #{<<"location_code">> := LocationCode, <<"real_location_code">> := RealLocationCode}} when is_binary(LocationCode), is_binary(RealLocationCode) -> + iot_jinzhi_endpoint:forward(LocationCode, RealLocationCode, EventType, Params); + {ok, _} -> lager:debug("[iot_ai_router] the event_data hget location_code, uuid: ~p, not found", [RouterUUID]); - {ok, LocationCode} when is_binary(LocationCode) -> - iot_jinzhi_endpoint:forward(LocationCode, EventType, Params); {error, Reason} -> lager:debug("[iot_ai_router] the event_data hget location_code uuid: ~p, get error: ~p", [RouterUUID, Reason]) end. \ No newline at end of file