Skip to content

Commit 8680251

Browse files
Fix same preview-not-updating bug in PrusaLink and OctoPrint handlers
Same root cause as the Klipper fix: the job-preview channel was only pushed a state the moment the file/thumbnail reference changed, so an item linked afterward never received a value until the next print job. Cache the last fetched thumbnail and re-push it every refresh cycle. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Scott Hanson <scooter_seh@yahoo.com>
1 parent c08ea0a commit 8680251

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

bundles/org.openhab.binding.threedprinter/src/main/java/org/openhab/binding/threedprinter/internal/handler/OctoPrintHandler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class OctoPrintHandler extends AbstractPrinterHandler {
5757

5858
private @Nullable OctoPrintConfiguration config;
5959
private String lastPreviewFilename = "";
60+
private @Nullable RawType lastPreviewState;
6061

6162
public OctoPrintHandler(Thing thing, HttpClient httpClient) {
6263
super(thing, httpClient);
@@ -164,12 +165,22 @@ protected void refresh() {
164165
byte @Nullable [] bytes = httpGetBytes(
165166
baseUrl + "/plugin/prusaslicerthumbnails/thumbnail/" + encodedName, cfg.apiKey);
166167
if (bytes != null && bytes.length > 0) {
167-
updateState(CHANNEL_JOB_PREVIEW, new RawType(bytes, "image/png"));
168+
RawType state = new RawType(bytes, "image/png");
169+
updateState(CHANNEL_JOB_PREVIEW, state);
168170
lastPreviewFilename = filename;
171+
lastPreviewState = state;
172+
}
173+
} else {
174+
// Re-push the cached image every cycle rather than only on change, so a channel
175+
// linked to an item after the initial fetch (or a UI reconnecting) still gets it.
176+
RawType cached = lastPreviewState;
177+
if (cached != null) {
178+
updateState(CHANNEL_JOB_PREVIEW, cached);
169179
}
170180
}
171181
} else {
172182
lastPreviewFilename = "";
183+
lastPreviewState = null;
173184
}
174185
}
175186
}

bundles/org.openhab.binding.threedprinter/src/main/java/org/openhab/binding/threedprinter/internal/handler/PrusaLinkHandler.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class PrusaLinkHandler extends AbstractPrinterHandler {
5353

5454
private @Nullable PrusaLinkConfiguration config;
5555
private String lastPreviewFilename = "";
56+
private @Nullable RawType lastPreviewState;
5657

5758
public PrusaLinkHandler(Thing thing, HttpClient httpClient) {
5859
super(thing, httpClient);
@@ -139,20 +140,32 @@ private void updateJobFile(String baseUrl, String apiKey) {
139140
if (file == null) {
140141
updateState(CHANNEL_JOB_NAME, new StringType(""));
141142
lastPreviewFilename = "";
143+
lastPreviewState = null;
142144
return;
143145
}
144146

145147
String name = file.displayName.isBlank() ? file.name : file.displayName;
146148
updateState(CHANNEL_JOB_NAME, new StringType(name));
147149

148150
String thumbnailRef = file.refs != null ? file.refs.thumbnail : "";
149-
if (thumbnailRef.isBlank() || thumbnailRef.equals(lastPreviewFilename)) {
151+
if (thumbnailRef.isBlank()) {
152+
return;
153+
}
154+
if (thumbnailRef.equals(lastPreviewFilename)) {
155+
// Re-push the cached image every cycle rather than only on change, so a channel linked to an
156+
// item after the initial fetch (or a UI reconnecting) still gets the preview.
157+
RawType cached = lastPreviewState;
158+
if (cached != null) {
159+
updateState(CHANNEL_JOB_PREVIEW, cached);
160+
}
150161
return;
151162
}
152163
byte[] bytes = httpGetBytes(baseUrl + thumbnailRef, apiKey);
153164
if (bytes != null && bytes.length > 0) {
154-
updateState(CHANNEL_JOB_PREVIEW, new RawType(bytes, "image/png"));
165+
RawType state = new RawType(bytes, "image/png");
166+
updateState(CHANNEL_JOB_PREVIEW, state);
155167
lastPreviewFilename = thumbnailRef;
168+
lastPreviewState = state;
156169
}
157170
}
158171

0 commit comments

Comments
 (0)