diff --git a/config/ct.config b/config/ct.config index 756bff94..b79a524a 100644 --- a/config/ct.config +++ b/config/ct.config @@ -12,6 +12,11 @@ host => "localhost", port => 8085 }}, + {iot_location_service, #{ + transport => http, + host => "localhost", + port => 8085 + }}, {downlink_service, #{ transport => http, host => "localhost", diff --git a/config/sys.config b/config/sys.config index 07306632..96f3fea6 100644 --- a/config/sys.config +++ b/config/sys.config @@ -12,6 +12,11 @@ host => "localhost", port => 50051 }}, + {iot_location_service, #{ + transport => http, + host => "localhost", + port => 50051 + }}, {downlink_service, #{ transport => http, host => "localhost", diff --git a/config/sys.config.src b/config/sys.config.src index 3e01fa60..82f392d8 100644 --- a/config/sys.config.src +++ b/config/sys.config.src @@ -14,6 +14,11 @@ host => "${HPR_IOT_CONFIG_HOST}", port => "${HPR_IOT_CONFIG_PORT}" }}, + {iot_location_service, #{ + transport => "${HPR_IOT_LOCATION_TRANSPORT}", + host => "${HPR_IOT_LOCATION_HOST}", + port => "${HPR_IOT_LOCATION_PORT}" + }}, {downlink_service, #{ transport => "${HPR_DOWNLINK_SERVICE_TRANSPORT}", host => "${HPR_DOWNLINK_SERVICE_HOST}", diff --git a/src/grpc/iot_config/hpr_gateway_location.erl b/src/grpc/iot_config/hpr_gateway_location.erl index 848a05c0..f2d230c2 100644 --- a/src/grpc/iot_config/hpr_gateway_location.erl +++ b/src/grpc/iot_config/hpr_gateway_location.erl @@ -5,31 +5,18 @@ %%%------------------------------------------------------------------- -module(hpr_gateway_location). --behaviour(gen_server). - -include("hpr.hrl"). -include("../autogen/iot_config_pb.hrl"). -%% ------------------------------------------------------------------ -%% API Function Exports -%% ------------------------------------------------------------------ -export([ - start_link/1, init/0, get/1, - update_location/1, expire_locations/0 ]). -%% ------------------------------------------------------------------ -%% gen_server Function Exports -%% ------------------------------------------------------------------ +%% This is exported for "just in case" -export([ - init/1, - handle_call/3, - handle_cast/2, - handle_info/2, - terminate/2 + get_location_from_ics/1 ]). -define(SERVER, ?MODULE). @@ -42,8 +29,6 @@ -define(NOT_FOUND, not_found). -define(REQUESTED, requested). --record(state, {}). - -record(location, { status :: ok | ?NOT_FOUND | error | ?REQUESTED, gateway :: libp2p_crypto:pubkey_bin(), @@ -53,19 +38,10 @@ long = undefined :: float() | undefined }). --type state() :: #state{}. -type loc() :: {h3:h3index(), float(), float()} | undefined. -export_type([loc/0]). -%% ------------------------------------------------------------------ -%%% API Function Definitions -%% ------------------------------------------------------------------ - --spec start_link(map()) -> any(). -start_link(Args) -> - gen_server:start_link({local, ?SERVER}, ?SERVER, Args, []). - -spec init() -> ok. init() -> ?ETS = ets:new(?ETS, [ @@ -92,25 +68,25 @@ get(PubKeyBin) -> LastHour = Now - ?ERROR_CACHE_TIME, case ets:lookup(?ETS, PubKeyBin) of [] -> - ok = ?MODULE:update_location(PubKeyBin), + ok = update_location(PubKeyBin), {error, ?NOT_FOUND}; [#location{status = ok, timestamp = T, h3_index = H3Index, lat = Lat, long = Long}] when T < Yesterday -> - ok = ?MODULE:update_location(PubKeyBin), + ok = update_location(PubKeyBin), {ok, H3Index, Lat, Long}; [#location{status = _, timestamp = T}] when T < Yesterday -> - ok = ?MODULE:update_location(PubKeyBin), + ok = update_location(PubKeyBin), {error, ?NOT_FOUND}; [#location{status = error, timestamp = T}] when T < LastHour -> - ok = ?MODULE:update_location(PubKeyBin), + ok = update_location(PubKeyBin), {error, undefined}; [#location{status = error}] -> {error, undefined}; [#location{status = requested, timestamp = T, gateway = PubKeyBin}] when T < LastHour -> GatewayName = hpr_utils:gateway_name(PubKeyBin), lager:warning("got an old request for ~p ~s", [PubKeyBin, GatewayName]), - ok = ?MODULE:update_location(PubKeyBin), + ok = update_location(PubKeyBin), {error, ?REQUESTED}; [#location{status = requested}] -> {error, ?REQUESTED}; @@ -120,16 +96,6 @@ get(PubKeyBin) -> {ok, H3Index, Lat, Long} end. --spec update_location(libp2p_crypto:pubkey_bin()) -> ok. -update_location(PubKeyBin) -> - NewLoc = #location{ - status = ?REQUESTED, - gateway = PubKeyBin, - timestamp = erlang:system_time(millisecond) - }, - true = ets:insert(?ETS, NewLoc), - gen_server:cast(?SERVER, {update_location, NewLoc}). - -spec expire_locations() -> ok. expire_locations() -> Time = erlang:system_time(millisecond) - ?CACHE_TIME, @@ -138,18 +104,18 @@ expire_locations() -> ]), lager:info("expiring ~w dets keys", [DETSDeleted]). -% ------------------------------------------------------------------ -%%% gen_server Function Definitions %% ------------------------------------------------------------------ --spec init(map()) -> {ok, state()}. -init(_Args) -> - {ok, #state{}}. - -handle_call(_Msg, _From, State) -> - {reply, ok, State}. +%% Internal Function Definitions +%% ------------------------------------------------------------------ -handle_cast({update_location, Loc}, State) -> - PubKeyBin = Loc#location.gateway, +-spec update_location(libp2p_crypto:pubkey_bin()) -> ok. +update_location(PubKeyBin) -> + Loc = #location{ + status = ?REQUESTED, + gateway = PubKeyBin, + timestamp = erlang:system_time(millisecond) + }, + true = ets:insert(?ETS, Loc), Start = erlang:system_time(millisecond), case get_location_from_ics(PubKeyBin) of {error, ?NOT_FOUND} -> @@ -178,20 +144,8 @@ handle_cast({update_location, Loc}, State) -> lat = Lat, long = Long }) - end, - {noreply, State}; -handle_cast(_Msg, State) -> - {noreply, State}. - -handle_info(_Info, State) -> - {noreply, State}. - -terminate(_Reason, _state) -> - ok. + end. -%% ------------------------------------------------------------------ -%% Internal Function Definitions -%% ------------------------------------------------------------------ -spec insert(Loc :: #location{}) -> ok. insert(Loc) -> true = ets:insert(?ETS, Loc), diff --git a/src/hpr_sup.erl b/src/hpr_sup.erl index 769b9bcf..6e231a2b 100644 --- a/src/hpr_sup.erl +++ b/src/hpr_sup.erl @@ -63,13 +63,14 @@ init([]) -> PacketReporterConfig = application:get_env(?APP, packet_reporter, #{}), ConfigServiceConfig = application:get_env(?APP, iot_config_service, #{}), + LocationServiceConfig = application:get_env(?APP, iot_location_service, #{}), DownlinkServiceConfig = application:get_env(?APP, downlink_service, #{}), MultiBuyServiceConfig = application:get_env(?APP, multi_buy_service, #{}), %% Starting config service client channel here because of the way we get %% .env vars into the app. _ = maybe_start_channel(ConfigServiceConfig, ?IOT_CONFIG_CHANNEL), - _ = maybe_start_channel(ConfigServiceConfig, ?LOCATION_CHANNEL), + _ = maybe_start_channel(LocationServiceConfig, ?LOCATION_CHANNEL), _ = maybe_start_channel(DownlinkServiceConfig, ?DOWNLINK_CHANNEL), _ = maybe_start_channel(MultiBuyServiceConfig, ?MULTI_BUY_CHANNEL), @@ -85,8 +86,6 @@ init([]) -> ?WORKER(hpr_packet_reporter, [PacketReporterConfig]), - ?WORKER(hpr_gateway_location, [#{}]), - ?WORKER(hpr_route_stream_worker, [#{}]), ?WORKER(hpr_protocol_router, [#{}]), diff --git a/src/hpr_utils.erl b/src/hpr_utils.erl index f390510a..03114d6f 100644 --- a/src/hpr_utils.erl +++ b/src/hpr_utils.erl @@ -303,7 +303,6 @@ get_device_traces(Data) -> format_coord(Coord) -> erlang:float_to_binary(Coord, [{decimals, 15}]). - %% ------------------------------------------------------------------ %% EUNIT Tests %% ------------------------------------------------------------------