From bb20e03bba79a4e8fa033ad903c3c86f0a6a7e1c Mon Sep 17 00:00:00 2001 From: Clawd Date: Wed, 11 Feb 2026 20:45:28 +0000 Subject: [PATCH] Add periodic traffic refresh (every 5 min) with cache-busting --- MMM-CombinedMap.js | 48 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/MMM-CombinedMap.js b/MMM-CombinedMap.js index ef28a34..3ba955f 100644 --- a/MMM-CombinedMap.js +++ b/MMM-CombinedMap.js @@ -64,6 +64,8 @@ Module.register("MMM-CombinedMap", { this.currentRadarIndex = 0; this.animationTimer = null; this.availableTimestamps = []; + this.trafficFlowLayer = null; + this.trafficIncidentsLayer = null; }, getDom: function() { @@ -153,25 +155,45 @@ Module.register("MMM-CombinedMap", { }, addTrafficLayer: function() { - // TomTom Traffic Flow tiles - const trafficLayer = L.tileLayer( - `https://api.tomtom.com/traffic/map/4/tile/flow/relative0/{z}/{x}/{y}.png?key=${this.config.tomtomApiKey}&tileSize=256`, - { - maxZoom: 18, - opacity: this.config.trafficOpacity - } - ); - trafficLayer.addTo(this.map); + this.refreshTrafficLayers(); - // Also add traffic incidents overlay - const incidentsLayer = L.tileLayer( - `https://api.tomtom.com/traffic/map/4/tile/incidents/s3/{z}/{x}/{y}.png?key=${this.config.tomtomApiKey}&tileSize=256`, + // Set up periodic traffic refresh (every 5 minutes) + setInterval(() => this.refreshTrafficLayers(), 5 * 60 * 1000); + }, + + refreshTrafficLayers: function() { + // Remove existing traffic layers + if (this.trafficFlowLayer) { + this.map.removeLayer(this.trafficFlowLayer); + } + if (this.trafficIncidentsLayer) { + this.map.removeLayer(this.trafficIncidentsLayer); + } + + // Cache-buster timestamp (rounded to minute for some caching benefit) + const cacheBust = Math.floor(Date.now() / 60000); + + // TomTom Traffic Flow tiles + this.trafficFlowLayer = L.tileLayer( + `https://api.tomtom.com/traffic/map/4/tile/flow/relative0/{z}/{x}/{y}.png?key=${this.config.tomtomApiKey}&tileSize=256&t=${cacheBust}`, { maxZoom: 18, opacity: this.config.trafficOpacity } ); - incidentsLayer.addTo(this.map); + this.trafficFlowLayer.addTo(this.map); + + // Traffic incidents overlay + this.trafficIncidentsLayer = L.tileLayer( + `https://api.tomtom.com/traffic/map/4/tile/incidents/s3/{z}/{x}/{y}.png?key=${this.config.tomtomApiKey}&tileSize=256&t=${cacheBust}`, + { + maxZoom: 18, + opacity: this.config.trafficOpacity + } + ); + this.trafficIncidentsLayer.addTo(this.map); + + Log.info("MMM-CombinedMap: Traffic layers refreshed"); }, fetchRadarCapabilities: function() {