Skip to content

Commit

Permalink
Insert new location before casting (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
macpie authored Dec 6, 2023
1 parent af9e3af commit fcbc79e
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/grpc/iot_config/hpr_gateway_location.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ get(PubKeyBin) ->

-spec update_location(libp2p_crypto:pubkey_bin()) -> ok.
update_location(PubKeyBin) ->
gen_server:cast(?SERVER, {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() ->
Expand All @@ -142,13 +148,8 @@ init(_Args) ->
handle_call(_Msg, _From, State) ->
{reply, ok, State}.

handle_cast({update_location, PubKeyBin}, State) ->
NewLoc = #location{
status = ?REQUESTED,
gateway = PubKeyBin,
timestamp = erlang:system_time(millisecond)
},
true = ets:insert(?ETS, NewLoc),
handle_cast({update_location, Loc}, State) ->
PubKeyBin = Loc#location.gateway,
Start = erlang:system_time(millisecond),
case get_location_from_ics(PubKeyBin) of
{error, ?NOT_FOUND} ->
Expand All @@ -158,20 +159,20 @@ handle_cast({update_location, PubKeyBin}, State) ->
"fail to get_location_from_ics ~p for ~s",
[?NOT_FOUND, GatewayName]
),
ok = insert(NewLoc#location{status = ?NOT_FOUND});
ok = insert(Loc#location{status = ?NOT_FOUND});
{error, Reason} ->
hpr_metrics:observe_gateway_location(Start, error),
GatewayName = hpr_utils:gateway_name(PubKeyBin),
lager:warning(
"fail to get_location_from_ics ~p for ~s",
[Reason, GatewayName]
),
ok = insert(NewLoc#location{status = error});
ok = insert(Loc#location{status = error});
{ok, H3IndexString} ->
hpr_metrics:observe_gateway_location(Start, ok),
H3Index = h3:from_string(H3IndexString),
{Lat, Long} = h3:to_geo(H3Index),
ok = insert(NewLoc#location{
ok = insert(Loc#location{
status = ok,
h3_index = H3Index,
lat = Lat,
Expand Down

0 comments on commit fcbc79e

Please sign in to comment.