-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from 12urenloop/simple_positioner
Simple positioner
- Loading branch information
Showing
18 changed files
with
234 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/telraam/logic/Lapper.java → ...ain/java/telraam/logic/lapper/Lapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...ogic/external/ExternalLapperResource.java → ...pper/external/ExternalLapperResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ic/external/models/ExternalLapperLap.java → ...er/external/models/ExternalLapperLap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../external/models/ExternalLapperStats.java → .../external/models/ExternalLapperStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ternal/models/ExternalLapperTeamLaps.java → ...ternal/models/ExternalLapperTeamLaps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...va/telraam/logic/simple/SimpleLapper.java → ...aam/logic/lapper/simple/SimpleLapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package telraam.logic.positioner; | ||
|
||
import java.util.LinkedList; | ||
|
||
public class CircularQueue<T> extends LinkedList<T> { | ||
|
||
private final int maxSize; | ||
public CircularQueue(int maxSize) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
@Override | ||
public boolean add(T e) { | ||
if (size() >= this.maxSize) { | ||
removeFirst(); | ||
} | ||
|
||
return super.add(e); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package telraam.logic.positioner; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import telraam.database.models.Team; | ||
import telraam.websocket.WebSocketMessageSingleton; | ||
|
||
@Getter @Setter | ||
public class Position { | ||
private int teamId; | ||
private float progress; // Progress of the lap. Between 0-1 | ||
private float speed; // Current speed. Progress / second | ||
|
||
public Position(int teamId) { | ||
this.teamId = teamId; | ||
this.progress = 0; | ||
this.speed = 0; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/telraam/logic/positioner/PositionSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package telraam.logic.positioner; | ||
|
||
import telraam.websocket.WebSocketMessage; | ||
import telraam.websocket.WebSocketMessageSingleton; | ||
|
||
import java.util.List; | ||
|
||
public class PositionSender { | ||
private final WebSocketMessage<List<Position>> message = new WebSocketMessage<>(); | ||
|
||
public PositionSender() { | ||
this.message.setTopic("position"); | ||
} | ||
|
||
public void send(List<Position> positions) { | ||
this.message.setData(positions); | ||
WebSocketMessageSingleton.getInstance().sendToAll(this.message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package telraam.logic.positioner; | ||
|
||
import telraam.database.models.Detection; | ||
|
||
public interface Positioner { | ||
void handle(Detection detection); | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/telraam/logic/positioner/simple/SimplePositioner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package telraam.logic.positioner.simple; | ||
|
||
import org.jdbi.v3.core.Jdbi; | ||
import telraam.database.daos.BatonSwitchoverDAO; | ||
import telraam.database.daos.StationDAO; | ||
import telraam.database.daos.TeamDAO; | ||
import telraam.database.models.BatonSwitchover; | ||
import telraam.database.models.Detection; | ||
import telraam.database.models.Station; | ||
import telraam.database.models.Team; | ||
import telraam.logic.positioner.CircularQueue; | ||
import telraam.logic.positioner.Position; | ||
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.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Logger; | ||
|
||
public class SimplePositioner implements Positioner { | ||
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<Team, CircularQueue<Detection>> teamDetections; | ||
private final List<Integer> stations; | ||
private final Map<Team, Position> teamPositions; | ||
|
||
public SimplePositioner(Jdbi jdbi) { | ||
this.debounceScheduled = false; | ||
this.scheduler = Executors.newScheduledThreadPool(1); | ||
this.positionSender = new PositionSender(); | ||
this.batonIdToTeam = new HashMap<>(); | ||
this.teamDetections = new HashMap<>(); | ||
this.teamPositions = new HashMap<>(); | ||
|
||
TeamDAO teamDAO = jdbi.onDemand(TeamDAO.class); | ||
List<Team> teams = teamDAO.getAll(); | ||
for (Team team: teams) { | ||
teamDetections.put(team, new CircularQueue<>(QUEUE_SIZE)); | ||
teamPositions.put(team, new Position(team.getId())); | ||
} | ||
List<BatonSwitchover> switchovers = jdbi.onDemand(BatonSwitchoverDAO.class).getAll(); | ||
switchovers.sort(Comparator.comparing(BatonSwitchover::getTimestamp)); | ||
|
||
for (BatonSwitchover switchover: switchovers) { | ||
batonIdToTeam.put(switchover.getNewBatonId(), teamDAO.getById(switchover.getTeamId()).get()); | ||
} | ||
|
||
List<Station> stationList = jdbi.onDemand(StationDAO.class).getAll(); | ||
stationList.sort(Comparator.comparing(Station::getDistanceFromStart)); | ||
stations = stationList.stream().map(Station::getId).toList(); | ||
} | ||
|
||
public void calculatePositions() { | ||
logger.info("SimplePositioner: Calculating positions..."); | ||
for (Map.Entry<Team, 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) { | ||
if (detection.getRssi() > currentStationRssi) { | ||
currentStationRssi = detection.getRssi(); | ||
currentStationPosition = detection.getStationId(); | ||
} | ||
} | ||
|
||
float progress = ((float) 100 / stations.size()) * currentStationPosition; | ||
teamPositions.get(entry.getKey()).setProgress(progress); | ||
} | ||
|
||
positionSender.send(teamPositions.values().stream().toList()); | ||
logger.info("SimplePositioner: Done calculating positions"); | ||
} | ||
|
||
public void handle(Detection detection) { | ||
Team team = batonIdToTeam.get(detection.getBatonId()); | ||
teamDetections.get(team).add(detection); | ||
|
||
if (! debounceScheduled) { | ||
debounceScheduled = true; | ||
scheduler.schedule(() -> { | ||
try { | ||
calculatePositions(); | ||
} catch (Exception e) { | ||
logger.severe(e.getMessage()); | ||
} | ||
debounceScheduled = false; | ||
}, DEBOUNCE_TIMEOUT, TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package telraam.websocket; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter @Setter | ||
public class WebSocketMessage<T> { | ||
private String topic; | ||
private T data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.