diff --git a/src/main/java/telraam/logic/positioner/simple/SimplePositioner.java b/src/main/java/telraam/logic/positioner/simple/SimplePositioner.java index 90d0ec9..9958d1e 100644 --- a/src/main/java/telraam/logic/positioner/simple/SimplePositioner.java +++ b/src/main/java/telraam/logic/positioner/simple/SimplePositioner.java @@ -13,23 +13,22 @@ import telraam.logic.positioner.PositionSender; import telraam.logic.positioner.Positioner; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; import java.util.logging.Logger; public class SimplePositioner implements Positioner { + private static final Logger logger = Logger.getLogger(SimplePositioner.class.getName()); private final int QUEUE_SIZE = 50; private final int MIN_RSSI = -85; private final int DEBOUNCE_TIMEOUT = 1; private boolean debounceScheduled; private final ScheduledExecutorService scheduler; - private static final Logger logger = Logger.getLogger(SimplePositioner.class.getName()); private final PositionSender positionSender; private final Map batonIdToTeam; private final Map> teamDetections; @@ -46,14 +45,14 @@ public SimplePositioner(Jdbi jdbi) { TeamDAO teamDAO = jdbi.onDemand(TeamDAO.class); List teams = teamDAO.getAll(); - for (Team team: teams) { + for (Team team : teams) { teamDetections.put(team.getId(), new CircularQueue<>(QUEUE_SIZE)); teamPositions.put(team.getId(), new Position(team.getId())); } List switchovers = jdbi.onDemand(BatonSwitchoverDAO.class).getAll(); switchovers.sort(Comparator.comparing(BatonSwitchover::getTimestamp)); - for (BatonSwitchover switchover: switchovers) { + for (BatonSwitchover switchover : switchovers) { batonIdToTeam.put(switchover.getNewBatonId(), teamDAO.getById(switchover.getTeamId()).get()); } @@ -64,13 +63,13 @@ public SimplePositioner(Jdbi jdbi) { public void calculatePositions() { logger.info("SimplePositioner: Calculating positions..."); - for (Map.Entry> entry: teamDetections.entrySet()) { + for (Map.Entry> entry : teamDetections.entrySet()) { List detections = teamDetections.get(entry.getKey()); detections.sort(Comparator.comparing(Detection::getTimestamp)); int currentStationRssi = MIN_RSSI; int currentStationPosition = 0; - for (Detection detection: detections) { + for (Detection detection : detections) { if (detection.getRssi() > currentStationRssi) { currentStationRssi = detection.getRssi(); currentStationPosition = detection.getStationId(); @@ -85,11 +84,11 @@ public void calculatePositions() { logger.info("SimplePositioner: Done calculating positions"); } - public void handle(Detection detection) { + public synchronized void handle(Detection detection) { Team team = batonIdToTeam.get(detection.getBatonId()); teamDetections.get(team.getId()).add(detection); - if (! debounceScheduled) { + if (!debounceScheduled) { debounceScheduled = true; scheduler.schedule(() -> { try { @@ -101,5 +100,4 @@ public void handle(Detection detection) { }, DEBOUNCE_TIMEOUT, TimeUnit.SECONDS); } } - }