-module(dummy_rack_location_service). -behaviour(gen_server). -export([ %% api start_link/2, get_rack_location/1, teleport_rack/2, init/1, handle_call/3, handle_cast/2, handle_info/2 ]). %% only location. whether its lifted or not is decided by butler nav info -record(state, { rack_id, current_location }). %% api start_link(RackId, RackLocation) -> gen_server:start_link(?MODULE, [RackId, RackLocation], []). get_rack_location(Pid) -> gen_server:call(Pid, get_rack_location). teleport_rack(Pid, Location) -> gen_server:cast(Pid, {teleport_rack, Location}). %% cb init([RackId, RackLocation]) -> gproc:add_local_name({?MODULE, RackId}), %% register to the nav data topic gproc:reg({p, l, {pubsub, on_butler_move}}), {ok, #state{ rack_id = RackId, current_location = RackLocation }}. handle_call(get_rack_location, _From, State = #state{current_location = RackLocation}) -> {reply, RackLocation, State}. handle_cast({teleport_rack, NewLocation}, State) -> {noreply, State#state{current_location = NewLocation}}. handle_info({_Pid, {pubsub, on_butler_move}, {_BId, PrevLocation, NewLocation, LiftState}}, State) -> handle_on_butler_move({PrevLocation, NewLocation, LiftState}, State). %% if lift state was up and butler was previously at rack, update its position to new location handle_on_butler_move({PrevLocation, NewLocation, up}, State = #state{current_location = PrevLocation}) -> {noreply, State#state{current_location = NewLocation}}; %% if butler was not on rack previously, but now is and has lift state up, then throw error since it hit rack! handle_on_butler_move({PrevLocation, NewLocation, up}, State = #state{current_location = NewLocation, rack_id = RackId}) when PrevLocation =/= NewLocation -> io:format("Error! butler just crashed into rack ~p at location ~p~n", [RackId, NewLocation]), {stop, "crash, already printed", State}; %% otherwise don't do anything handle_on_butler_move(_, State) -> {noreply, State}.