Skip to content

Commit

Permalink
BUG: fix active filament for multi AMS (fixes #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
TFyre committed Feb 17, 2024
1 parent 8bce9df commit 022f47e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
4 changes: 1 addition & 3 deletions bambu/src/main/java/com/tfyre/bambu/view/SdCardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ private void runCallable(final Callable<Boolean> callable) {
callable.call();
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
runInUI(() -> {
showError(ex.getMessage());
});
runInUI(() -> showError(ex.getMessage()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,30 @@ private Images getHumidityImage(final String id) {
}

private void processAms(final com.tfyre.bambu.model.Ams ams) {
final String trayId;
final int amsTrayId;
if (isPrinterIdle() || !ams.hasTrayNow()) {
trayId = "-1";
amsTrayId = -1;
} else {
trayId = ams.getTrayNow();
amsTrayId = parseInt(printer.getName(), ams.getTrayNow(), -1);
}
ams.getAmsList().forEach(single -> {
Optional.ofNullable(amsHeaders.get(getAmsHeaderId(single.getId()))).ifPresent(header -> {
final int amsId = getAmsId(single);
Optional.ofNullable(amsHeaders.get(getAmsHeaderId(amsId))).ifPresent(header -> {
setTemperature(header.temperature(), parseDouble(printer.getName(), single.getTemp(), 0));
header.humidity().setSrc(getHumidityImage(single.getHumidity()).getImage());
});

single.getTrayList().forEach(tray -> {
Optional.ofNullable(amsFilaments.get(getFilamentTrayKey(single, tray))).ifPresent(filament -> {
final int trayId = getTrayId(tray);
Optional.ofNullable(amsFilaments.get(getFilamentTrayKey(amsId, trayId))).ifPresent(filament -> {
if (!tray.hasTrayInfoIdx()) {
filament.type().setText("Empty");
return;
}
filament.type().setText(Filament.getFilamentDescription(tray.getTrayInfoIdx()));
filament.color().getStyle().setBackgroundColor("#%s".formatted(tray.getTrayColor()));
filament.div().removeClassName("active");
if (trayId.equals(tray.getId())) {
if (amsTrayId == filament.amsTrayId()) {
filament.div().addClassName("active");
}
});
Expand All @@ -196,10 +198,11 @@ private void processAms(final com.tfyre.bambu.model.Ams ams) {
}

private void processVtTray(final com.tfyre.bambu.model.VtTray tray) {
Optional.ofNullable(amsHeaders.get(getTrayKey(tray.getId()))).ifPresent(header -> {
final int trayId = getTrayId(tray);
Optional.ofNullable(amsHeaders.get(getTrayKey(trayId))).ifPresent(header -> {
setTemperature(header.temperature(), parseDouble(printer.getName(), tray.getTrayTemp(), 0));
});
Optional.ofNullable(amsFilaments.get(getTrayKey(tray.getId()))).ifPresent(filament -> {
Optional.ofNullable(amsFilaments.get(getTrayKey(trayId))).ifPresent(filament -> {
filament.type().setText(Filament.getFilamentDescription(tray.getTrayInfoIdx()));
filament.color().getStyle().setBackgroundColor("#%s".formatted(tray.getTrayColor()));
});
Expand Down Expand Up @@ -672,29 +675,34 @@ private Div buildAmsFilament(final String key, final int amsId, final int trayId
return wrapAmsFilament(filament);
}

private int getAmsId(final AmsSingle single) {
return parseInt(printer.getName(), single.getId(), BambuConst.AMS_TRAY_UNLOAD);
}

private int getTrayId(final Tray tray) {
return parseInt(printer.getName(), tray.getId(), BambuConst.AMS_TRAY_UNLOAD);
}

private int getTrayId(final com.tfyre.bambu.model.VtTray tray) {
return parseInt(printer.getName(), tray.getId(), BambuConst.AMS_TRAY_UNLOAD);
}

private Div buildAmsFilament(final AmsSingle single, final Tray tray) {
final int amsId = parseInt(printer.getName(), single.getId(), BambuConst.AMS_TRAY_UNLOAD);
final int trayId = parseInt(printer.getName(), tray.getId(), BambuConst.AMS_TRAY_UNLOAD);
final int amsId = getAmsId(single);
final int trayId = getTrayId(tray);
return buildAmsFilament(getFilamentTrayKey(amsId, trayId), amsId, trayId);
}

private String getFilamentTrayKey(final int amsId, final int trayId) {
return "single[%d]tray[%d]".formatted(amsId, trayId);
}

private String getFilamentTrayKey(final AmsSingle single, final Tray tray) {
return getFilamentTrayKey(
parseInt(printer.getName(), single.getId(), BambuConst.AMS_TRAY_UNLOAD),
parseInt(printer.getName(), tray.getId(), BambuConst.AMS_TRAY_UNLOAD)
);
}

private String getAmsHeaderId(final String id) {
return "AMS#%s".formatted(id);
private String getAmsHeaderId(final int id) {
return "AMS#%d".formatted(id);
}

private String getTrayKey(final String id) {
return "Tray#%s".formatted(id);
private String getTrayKey(final int id) {
return "Tray#%d".formatted(id);
}

private Div buildTray(final String amsHeaderId, final boolean hasHumidity, final List<Div> filaments) {
Expand All @@ -712,7 +720,7 @@ private Div buildTray(final String amsHeaderId, final boolean hasHumidity, final
private void buildAms(final Div parent, final com.tfyre.bambu.model.Ams ams) {
ams.getAmsList().forEach(single -> {
parent.add(buildTray(
getAmsHeaderId(single.getId()),
getAmsHeaderId(getAmsId(single)),
true,
single.getTrayList().stream()
.map(tray -> buildAmsFilament(single, tray))
Expand All @@ -722,11 +730,11 @@ private void buildAms(final Div parent, final com.tfyre.bambu.model.Ams ams) {
}

private void buildVtTray(final Div parent, final com.tfyre.bambu.model.VtTray tray) {
final int trayId = parseInt(printer.getName(), tray.getId(), BambuConst.AMS_TRAY_UNLOAD);
final int trayId = getTrayId(tray);
parent.add(buildTray(
getTrayKey(tray.getId()),
getTrayKey(trayId),
false,
List.of(buildAmsFilament(getTrayKey(tray.getId()), BambuConst.AMS_TRAY_UNLOAD, trayId))
List.of(buildAmsFilament(getTrayKey(trayId), BambuConst.AMS_TRAY_UNLOAD, trayId))
));
}

Expand Down

0 comments on commit 022f47e

Please sign in to comment.