Skip to content

Commit

Permalink
fix(simplePositioner): make handle function synchronised
Browse files Browse the repository at this point in the history
  • Loading branch information
NuttyShrimp committed Apr 13, 2024
1 parent affe45c commit 606bc40
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/main/java/telraam/logic/positioner/simple/SimplePositioner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Team> batonIdToTeam;
private final Map<Integer, CircularQueue<Detection>> teamDetections;
Expand All @@ -46,14 +45,14 @@ public SimplePositioner(Jdbi jdbi) {

TeamDAO teamDAO = jdbi.onDemand(TeamDAO.class);
List<Team> 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<BatonSwitchover> 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());
}

Expand All @@ -64,13 +63,13 @@ public SimplePositioner(Jdbi jdbi) {

public void calculatePositions() {
logger.info("SimplePositioner: Calculating positions...");
for (Map.Entry<Integer, CircularQueue<Detection>> entry: teamDetections.entrySet()) {
for (Map.Entry<Integer, CircularQueue<Detection>> entry : teamDetections.entrySet()) {
List<Detection> 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();
Expand All @@ -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 {
Expand All @@ -101,5 +100,4 @@ public void handle(Detection detection) {
}, DEBOUNCE_TIMEOUT, TimeUnit.SECONDS);
}
}

}

0 comments on commit 606bc40

Please sign in to comment.