Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): lazy load baton statuses #105

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/telraam/monitoring/BatonStatusHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ public class BatonStatusHolder {

private BatonDAO batonDAO;
private DetectionDAO detectionDAO;
private boolean initialized = false;

public BatonStatusHolder(BatonDAO BDAO, DetectionDAO DDAO) {
batonDAO = BDAO;
detectionDAO = DDAO;
this.initStatus();
}

private void initStatus() {
if (initialized) return;
NuttyShrimp marked this conversation as resolved.
Show resolved Hide resolved
initialized = true;
var batons = batonDAO.getAll();
for (Baton baton : batons) {
BatonStatus batonStatus = new BatonStatus(
Expand All @@ -45,6 +47,7 @@ private void initStatus() {
}

public List<BatonStatus> GetAllBatonStatuses() {
this.initStatus();
// For each baton, fetch latest detection
var batons = batonDAO.getAll();
for (Baton baton : batons) {
Expand Down Expand Up @@ -78,6 +81,7 @@ private void updateState(Detection msg) {
}

public BatonStatus GetBatonStatus(Integer batonId) {
this.initStatus();
if (!batonIdToMac.containsKey(batonId)) {
var baton = batonDAO.getById(batonId);
baton.ifPresent(value -> batonIdToMac.put(batonId, value.getMac().toLowerCase()));
Expand All @@ -87,6 +91,7 @@ public BatonStatus GetBatonStatus(Integer batonId) {
}

public BatonStatus createBatonStatus(Integer batonId) {
this.initStatus();
String batonMac = batonIdToMac.get(batonId);
if (batonMac != null) {
return batonStatusMap.get(batonMac);
Expand All @@ -112,6 +117,7 @@ public BatonStatus createBatonStatus(Integer batonId) {
}

public void resetRebooted(int batonId) {
this.initStatus();
var batonStatus = GetBatonStatus(batonId);
if (batonStatus == null) {
return;
Expand Down