mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-17 00:59:29 -07:00
Improve error handling and API efficiency
- Switch to safe*() methods for centralized error handling - Add error handling and validation - Optimize radar API usage by only fetching yesterday's data when needed - Use centralized URL rewriting for caching proxy support - Add debug logging throughout radar processing pipeline - Improve canvas context validation and error recovery - Handle worker errors gracefully by setting totalScreens = 0 to skip in animation - Remove unused OVERRIDES parameter passing to workers
This commit is contained in:
@@ -2,56 +2,82 @@ import { removeDopplerRadarImageNoise } from './radar-utils.mjs';
|
||||
import { RADAR_FULL_SIZE, RADAR_FINAL_SIZE } from './radar-constants.mjs';
|
||||
|
||||
onmessage = async (e) => {
|
||||
const {
|
||||
url, RADAR_HOST, OVERRIDES, radarSourceXY,
|
||||
} = e.data;
|
||||
try {
|
||||
const {
|
||||
url, radarSourceXY, debug,
|
||||
} = e.data;
|
||||
|
||||
// get the image
|
||||
const modifiedRadarUrl = OVERRIDES.RADAR_HOST ? url.replace(RADAR_HOST, OVERRIDES.RADAR_HOST) : url;
|
||||
const radarResponsePromise = fetch(modifiedRadarUrl);
|
||||
if (debug) {
|
||||
console.log('[RADAR-WORKER] Message received at:', new Date().toISOString(), 'File:', url.split('/').pop());
|
||||
}
|
||||
|
||||
// calculate offsets and sizes
|
||||
const radarSource = {
|
||||
width: 240,
|
||||
height: 163,
|
||||
x: Math.round(radarSourceXY.x / 2),
|
||||
y: Math.round(radarSourceXY.y / 2),
|
||||
};
|
||||
// get the image (URL is already rewritten for caching by radar.mjs)
|
||||
const radarResponsePromise = fetch(url);
|
||||
|
||||
// create radar context for manipulation
|
||||
const radarCanvas = new OffscreenCanvas(RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
|
||||
const radarContext = radarCanvas.getContext('2d');
|
||||
radarContext.imageSmoothingEnabled = false;
|
||||
// calculate offsets and sizes
|
||||
const radarSource = {
|
||||
width: 240,
|
||||
height: 163,
|
||||
x: Math.round(radarSourceXY.x / 2),
|
||||
y: Math.round(radarSourceXY.y / 2),
|
||||
};
|
||||
|
||||
// test response
|
||||
const radarResponse = await radarResponsePromise;
|
||||
if (!radarResponse.ok) throw new Error(`Unable to fetch radar error ${radarResponse.status} ${radarResponse.statusText} from ${radarResponse.url}`);
|
||||
// create radar context for manipulation
|
||||
const radarCanvas = new OffscreenCanvas(RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
|
||||
const radarContext = radarCanvas.getContext('2d');
|
||||
if (!radarContext) {
|
||||
throw new Error('Failed to get radar canvas context');
|
||||
}
|
||||
|
||||
// get the blob
|
||||
const radarImgBlob = await radarResponse.blob();
|
||||
radarContext.imageSmoothingEnabled = false;
|
||||
|
||||
// assign to an html image element
|
||||
const radarImgElement = await createImageBitmap(radarImgBlob);
|
||||
// draw the entire image
|
||||
radarContext.clearRect(0, 0, RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
|
||||
radarContext.drawImage(radarImgElement, 0, 0, RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
|
||||
// test response
|
||||
const radarResponse = await radarResponsePromise;
|
||||
if (!radarResponse.ok) throw new Error(`Unable to fetch radar image: got ${radarResponse.status} ${radarResponse.statusText} from ${radarResponse.url}`);
|
||||
|
||||
// crop the radar image without scaling
|
||||
const croppedRadarCanvas = new OffscreenCanvas(radarSource.width, radarSource.height);
|
||||
const croppedRadarContext = croppedRadarCanvas.getContext('2d');
|
||||
croppedRadarContext.imageSmoothingEnabled = false;
|
||||
croppedRadarContext.drawImage(radarCanvas, radarSource.x, radarSource.y, croppedRadarCanvas.width, croppedRadarCanvas.height, 0, 0, croppedRadarCanvas.width, croppedRadarCanvas.height);
|
||||
// get the blob
|
||||
const radarImgBlob = await radarResponse.blob();
|
||||
|
||||
// clean the image
|
||||
removeDopplerRadarImageNoise(croppedRadarContext);
|
||||
// assign to an html image element
|
||||
const radarImgElement = await createImageBitmap(radarImgBlob);
|
||||
// draw the entire image
|
||||
radarContext.clearRect(0, 0, RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
|
||||
radarContext.drawImage(radarImgElement, 0, 0, RADAR_FULL_SIZE.width, RADAR_FULL_SIZE.height);
|
||||
|
||||
// stretch the radar image
|
||||
const stretchCanvas = new OffscreenCanvas(RADAR_FINAL_SIZE.width, RADAR_FINAL_SIZE.height);
|
||||
const stretchContext = stretchCanvas.getContext('2d', { willReadFrequently: true });
|
||||
stretchContext.imageSmoothingEnabled = false;
|
||||
stretchContext.drawImage(croppedRadarCanvas, 0, 0, radarSource.width, radarSource.height, 0, 0, RADAR_FINAL_SIZE.width, RADAR_FINAL_SIZE.height);
|
||||
// crop the radar image without scaling
|
||||
const croppedRadarCanvas = new OffscreenCanvas(radarSource.width, radarSource.height);
|
||||
const croppedRadarContext = croppedRadarCanvas.getContext('2d');
|
||||
if (!croppedRadarContext) {
|
||||
throw new Error('Failed to get cropped radar canvas context');
|
||||
}
|
||||
|
||||
const stretchedRadar = stretchCanvas.transferToImageBitmap();
|
||||
croppedRadarContext.imageSmoothingEnabled = false;
|
||||
croppedRadarContext.drawImage(radarCanvas, radarSource.x, radarSource.y, croppedRadarCanvas.width, croppedRadarCanvas.height, 0, 0, croppedRadarCanvas.width, croppedRadarCanvas.height);
|
||||
|
||||
postMessage(stretchedRadar, [stretchedRadar]);
|
||||
// clean the image
|
||||
removeDopplerRadarImageNoise(croppedRadarContext);
|
||||
|
||||
// stretch the radar image
|
||||
const stretchCanvas = new OffscreenCanvas(RADAR_FINAL_SIZE.width, RADAR_FINAL_SIZE.height);
|
||||
const stretchContext = stretchCanvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!stretchContext) {
|
||||
throw new Error('Failed to get stretch canvas context');
|
||||
}
|
||||
|
||||
stretchContext.imageSmoothingEnabled = false;
|
||||
stretchContext.drawImage(croppedRadarCanvas, 0, 0, radarSource.width, radarSource.height, 0, 0, RADAR_FINAL_SIZE.width, RADAR_FINAL_SIZE.height);
|
||||
|
||||
const stretchedRadar = stretchCanvas.transferToImageBitmap();
|
||||
|
||||
if (debug) {
|
||||
console.log('[RADAR-WORKER] Sending processed radar at:', new Date().toISOString(), 'Canvas size:', stretchCanvas.width, 'x', stretchCanvas.height, 'File:', url.split('/').pop());
|
||||
}
|
||||
|
||||
postMessage(stretchedRadar, [stretchedRadar]);
|
||||
} catch (error) {
|
||||
console.warn('[RADAR-WORKER] Error at:', new Date().toISOString(), 'Error:', error.message);
|
||||
// Handle radar fetch errors by indicating failure to the main thread
|
||||
// This allows the radar display to set totalScreens = 0 and skip in animation
|
||||
postMessage({ error: true, message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user