From 1ef763da8d76991eb2f5d5687c1d3994d776195d Mon Sep 17 00:00:00 2001 From: martinkooij Date: Wed, 30 Jun 2021 20:37:39 +0200 Subject: [PATCH 1/8] Update MMM-MyCommute.js --- MMM-MyCommute.js | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/MMM-MyCommute.js b/MMM-MyCommute.js index e2f217d..22beed4 100644 --- a/MMM-MyCommute.js +++ b/MMM-MyCommute.js @@ -197,6 +197,7 @@ Module.register('MMM-MyCommute', { this.sendSocketNotification("GOOGLE_TRAFFIC_GET", {destinations: destinations, instanceId: this.identifier}); } else { this.hide(1000, {lockString: this.identifier}); + this.sendNotification('SET_LCD_BACKLIGHT', {command: -1} ); // remove module backlighting this.inWindow = false; this.isHidden = true; } @@ -204,6 +205,7 @@ Module.register('MMM-MyCommute', { } else { this.hide(1000, {lockString: this.identifier}); + this.sendNotification('SET_LCD_BACKLIGHT', {command: -1} ); // remove module backlighting this.inWindow = false; this.isHidden = true; } @@ -284,27 +286,31 @@ Module.register('MMM-MyCommute', { formatTime: function(time, timeInTraffic) { var timeEl = document.createElement("span"); + var trafficStatus = 0 ; //0 = good, 1 = moderate, 2 = poor timeEl.classList.add("travel-time"); if (timeInTraffic != null) { timeEl.innerHTML = moment.duration(Number(timeInTraffic), "seconds").format(this.config.travelTimeFormat, {trim: this.config.travelTimeFormatTrim}); var variance = timeInTraffic / time; - if (this.config.colorCodeTravelTime) { - if (variance > this.config.poorTimeThreshold) { - timeEl.classList.add("status-poor"); - } else if (variance > this.config.moderateTimeThreshold) { - timeEl.classList.add("status-moderate"); - } else { + if (variance > this.config.poorTimeThreshold) { + if (this.config.colorCodeTravelTime) { timeEl.classList.add("status-poor");}; + trafficStatus = 2; + } else if (variance > this.config.moderateTimeThreshold) { + if (this.config.colorCodeTravelTime) {timeEl.classList.add("status-moderate");}; + trafficStatus = 1 ; + } else { timeEl.classList.add("status-good"); - } + trafficStatus = 0 ; } + } else { timeEl.innerHTML = moment.duration(Number(time), "seconds").format(this.config.travelTimeFormat, {trim: this.config.travelTimeFormatTrim}); timeEl.classList.add("status-good"); + trafficStatus = 0 ; } - return timeEl; + return [timeEl,trafficStatus]; }, @@ -360,6 +366,8 @@ Module.register('MMM-MyCommute', { wrapper.appendChild(loading); return wrapper } + + var worstTrafficStatus = 0 ; // 0 = good, 1 = moderate, 2 = poor for (var i = 0; i < this.predictions.length; i++) { @@ -396,8 +404,13 @@ Module.register('MMM-MyCommute', { } else if (p.routes.length == 1 || !this.config.showSummary) { var r = p.routes[0]; - - row.appendChild( this.formatTime(r.time, r.timeInTraffic) ); + var trafficStatus ; + var timeFormat; + + [timeFormat,trafficStatus] = this.formatTime(r.time, r.timeInTraffic); + + row.appendChild( timeFormat ); + worstTrafficStatus = (worstTrafficStatus>trafficStatus)?worstTrafficStatus:trafficStatus ; //summary? if (this.config.showSummary) { @@ -459,7 +472,14 @@ Module.register('MMM-MyCommute', { wrapper.appendChild(row); } - + if (worstTrafficStatus = 0 ) { + this.sendNotification('SET_LCD_BACKLIGHT', {command: "GOOD"} ); // good traffic situation + } else if (worstTrafficStatus = 1) { + this.sendNotification('SET_LCD_BACKLIGHT', {command: "MODERATE"} ); // moderate traffic situation + } else { + this.sendNotification('SET_LCD_BACKLIGHT', {command: "POOR"} ); // poor traffic situation + } + return wrapper; }, From d027302ff19508a8dbf05348c72483b9106ff834 Mon Sep 17 00:00:00 2001 From: martinkooij Date: Wed, 30 Jun 2021 20:45:38 +0200 Subject: [PATCH 2/8] Update MMM-MyCommute.js --- MMM-MyCommute.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MMM-MyCommute.js b/MMM-MyCommute.js index 22beed4..6fa055d 100644 --- a/MMM-MyCommute.js +++ b/MMM-MyCommute.js @@ -472,9 +472,9 @@ Module.register('MMM-MyCommute', { wrapper.appendChild(row); } - if (worstTrafficStatus = 0 ) { + if (worstTrafficStatus == 0 ) { this.sendNotification('SET_LCD_BACKLIGHT', {command: "GOOD"} ); // good traffic situation - } else if (worstTrafficStatus = 1) { + } else if (worstTrafficStatus == 1) { this.sendNotification('SET_LCD_BACKLIGHT', {command: "MODERATE"} ); // moderate traffic situation } else { this.sendNotification('SET_LCD_BACKLIGHT', {command: "POOR"} ); // poor traffic situation From e26cad436b5dd27f81a73b8ba5352cf24eaa60d3 Mon Sep 17 00:00:00 2001 From: martinkooij Date: Fri, 2 Jul 2021 09:58:56 +0200 Subject: [PATCH 3/8] Create node_helper-commute.js --- node_helper-commute.js | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 node_helper-commute.js diff --git a/node_helper-commute.js b/node_helper-commute.js new file mode 100644 index 0000000..66ce502 --- /dev/null +++ b/node_helper-commute.js @@ -0,0 +1,98 @@ +/* Magic Mirror + * Module: mrx-work-traffic + * + * By Dominic Marx + * MIT Licensed. + */ + +var NodeHelper = require("node_helper"); +var fetch = require('node-fetch'); +var moment = require('moment'); + +module.exports = NodeHelper.create({ + + start: function() { + console.log("====================== Starting node_helper for module [" + this.name + "]"); + }, + + + // subclass socketNotificationReceived + socketNotificationReceived: function(notification, payload){ + if (notification === 'GOOGLE_TRAFFIC_GET') { + + //first data opull after new config + (async () => {await this.getPredictions(payload);})(); + } + }, + + getPredictions: async function(payload) { + + var returned = 0; + var predictions = new Array(); + + payload.destinations.forEach(function(dest, index) { + try { + var body = await fetch(dest.url); + var data = body.json() ; + + var prediction = new Object({ + config: dest.config + }); + + if (data.error_message) { + console.log("MMM-MyCommute: " + data.error_message); + prediction.error = true; + } else { + console.log("MM-MyCommute-DEBUG: "+ JSON.stringify(data,undefined,2)); + var routeList = new Array(); + for (var i = 0; i < data.routes.length; i++) { + var r = data.routes[i]; + var routeObj = new Object({ + summary: r.summary, + time: r.legs[0].duration.value + }); + + if (r.legs[0].duration_in_traffic) { + routeObj.timeInTraffic = r.legs[0].duration_in_traffic.value; + } + if (dest.config.mode && dest.config.mode == 'transit') { + var transitInfo = new Array(); + var gotFirstTransitLeg = false; + for (var j = 0; j < r.legs[0].steps.length; j++) { + var s = r.legs[0].steps[j]; + + if (s.transit_details) { + var arrivalTime = ''; + if (!gotFirstTransitLeg && dest.config.showNextVehicleDeparture) { + gotFirstTransitLeg = true; + // arrivalTime = ' (next at ' + s.transit_details.departure_time.text + ')'; + arrivalTime = moment(s.transit_details.departure_time.value * 1000); + } + transitInfo.push({routeLabel: s.transit_details.line.short_name ? s.transit_details.line.short_name : s.transit_details.line.name, vehicle: s.transit_details.line.vehicle.type, arrivalTime: arrivalTime}); + } + routeObj.transitInfo = transitInfo; + } + } + routeList.push(routeObj); + } + prediction.routes = routeList; + + } + + } catch(error) { + console.log( "Error getting traffic prediction: " + error.response.body ); + prediction.error = true; + + }; + + predictions[index] = prediction; + returned++; + + if (returned == payload.destinations.length) { + this.sendSocketNotification('GOOGLE_TRAFFIC_RESPONSE' + payload.instanceId, predictions); + }; + }); + } + + +}); \ No newline at end of file From 4dbfeb5656a352c73090429210b035b2f1c3855e Mon Sep 17 00:00:00 2001 From: martinkooij Date: Fri, 2 Jul 2021 10:01:36 +0200 Subject: [PATCH 4/8] Update package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index c7943fc..0a814b9 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,5 @@ "dependencies": { "moment": "^2.18.1", "moment-duration-format": "^1.3.0", - "request": "^2.81.0" } } From 31c6f3506dc734758d101af775707c9c9e5aa48d Mon Sep 17 00:00:00 2001 From: martinkooij Date: Fri, 2 Jul 2021 10:02:11 +0200 Subject: [PATCH 5/8] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0a814b9..b6181af 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ "homepage": "https://github.com/jclarke0000/MMM-MyCommute#readme", "dependencies": { "moment": "^2.18.1", - "moment-duration-format": "^1.3.0", + "moment-duration-format": "^1.3.0" } } From 8e88ee55a9735c2c93ff3ec3420173abc760081f Mon Sep 17 00:00:00 2001 From: martinkooij Date: Fri, 2 Jul 2021 10:36:45 +0200 Subject: [PATCH 6/8] change to node-fetch --- node_helper-commute.js | 98 ------------------------------------------ node_helper.js | 64 +++++++++++++-------------- 2 files changed, 29 insertions(+), 133 deletions(-) delete mode 100644 node_helper-commute.js diff --git a/node_helper-commute.js b/node_helper-commute.js deleted file mode 100644 index 66ce502..0000000 --- a/node_helper-commute.js +++ /dev/null @@ -1,98 +0,0 @@ -/* Magic Mirror - * Module: mrx-work-traffic - * - * By Dominic Marx - * MIT Licensed. - */ - -var NodeHelper = require("node_helper"); -var fetch = require('node-fetch'); -var moment = require('moment'); - -module.exports = NodeHelper.create({ - - start: function() { - console.log("====================== Starting node_helper for module [" + this.name + "]"); - }, - - - // subclass socketNotificationReceived - socketNotificationReceived: function(notification, payload){ - if (notification === 'GOOGLE_TRAFFIC_GET') { - - //first data opull after new config - (async () => {await this.getPredictions(payload);})(); - } - }, - - getPredictions: async function(payload) { - - var returned = 0; - var predictions = new Array(); - - payload.destinations.forEach(function(dest, index) { - try { - var body = await fetch(dest.url); - var data = body.json() ; - - var prediction = new Object({ - config: dest.config - }); - - if (data.error_message) { - console.log("MMM-MyCommute: " + data.error_message); - prediction.error = true; - } else { - console.log("MM-MyCommute-DEBUG: "+ JSON.stringify(data,undefined,2)); - var routeList = new Array(); - for (var i = 0; i < data.routes.length; i++) { - var r = data.routes[i]; - var routeObj = new Object({ - summary: r.summary, - time: r.legs[0].duration.value - }); - - if (r.legs[0].duration_in_traffic) { - routeObj.timeInTraffic = r.legs[0].duration_in_traffic.value; - } - if (dest.config.mode && dest.config.mode == 'transit') { - var transitInfo = new Array(); - var gotFirstTransitLeg = false; - for (var j = 0; j < r.legs[0].steps.length; j++) { - var s = r.legs[0].steps[j]; - - if (s.transit_details) { - var arrivalTime = ''; - if (!gotFirstTransitLeg && dest.config.showNextVehicleDeparture) { - gotFirstTransitLeg = true; - // arrivalTime = ' (next at ' + s.transit_details.departure_time.text + ')'; - arrivalTime = moment(s.transit_details.departure_time.value * 1000); - } - transitInfo.push({routeLabel: s.transit_details.line.short_name ? s.transit_details.line.short_name : s.transit_details.line.name, vehicle: s.transit_details.line.vehicle.type, arrivalTime: arrivalTime}); - } - routeObj.transitInfo = transitInfo; - } - } - routeList.push(routeObj); - } - prediction.routes = routeList; - - } - - } catch(error) { - console.log( "Error getting traffic prediction: " + error.response.body ); - prediction.error = true; - - }; - - predictions[index] = prediction; - returned++; - - if (returned == payload.destinations.length) { - this.sendSocketNotification('GOOGLE_TRAFFIC_RESPONSE' + payload.instanceId, predictions); - }; - }); - } - - -}); \ No newline at end of file diff --git a/node_helper.js b/node_helper.js index b9c0e5c..0593fbb 100644 --- a/node_helper.js +++ b/node_helper.js @@ -6,7 +6,7 @@ */ var NodeHelper = require("node_helper"); -var request = require('request'); +var fetch = require('node-fetch'); var moment = require('moment'); module.exports = NodeHelper.create({ @@ -21,36 +21,34 @@ module.exports = NodeHelper.create({ if (notification === 'GOOGLE_TRAFFIC_GET') { //first data opull after new config - this.getPredictions(payload); - - } + (async () => {await this.getPredictions(payload);})(); + } }, - - - getPredictions: function(payload) { - var self = this; + + getPredictions: async function(payload) { var returned = 0; var predictions = new Array(); - - payload.destinations.forEach(function(dest, index) { - request({url: dest.url, method: 'GET'}, function(error, response, body) { +// console.log("MM-MyCommute-DEBUG PAYLOAD GET PREDECTIONS: "+ JSON.stringify(payload,undefined,2)); + + for (let index = 0 ; index < payload.destinations.length ; index++) { + var dest = payload.destinations[index] ; + try { + var body = await fetch(dest.url); + var data = await body.json() ; + +// console.log("MM-MyCommute-DEBUG ("+ index + "): "+ JSON.stringify(data,undefined,2)); - var prediction = new Object({ - config: dest.config - }); - - if(!error && response.statusCode == 200){ - - var data = JSON.parse(body); + var prediction = new Object({ + config: dest.config + }); - - if (data.error_message) { + if (data.error_message) { console.log("MMM-MyCommute: " + data.error_message); prediction.error = true; - } else { - + } else { + var routeList = new Array(); for (var i = 0; i < data.routes.length; i++) { var r = data.routes[i]; @@ -86,21 +84,17 @@ module.exports = NodeHelper.create({ } - } else { - console.log( "Error getting traffic prediction: " + response.statusCode ); + } catch(error) { + console.log( "Error getting traffic prediction: " + error ); prediction.error = true; - } - - predictions[index] = prediction; - returned++; - - if (returned == payload.destinations.length) { - self.sendSocketNotification('GOOGLE_TRAFFIC_RESPONSE' + payload.instanceId, predictions); }; + predictions[index] = prediction; + }; + + this.sendSocketNotification('GOOGLE_TRAFFIC_RESPONSE' + payload.instanceId, predictions); - }); - }); - } - + } + + }); \ No newline at end of file From 93427a2c939925a37baf6ffe0e578c5cd2bf7f46 Mon Sep 17 00:00:00 2001 From: martinkooij Date: Fri, 2 Jul 2021 10:51:43 +0200 Subject: [PATCH 7/8] Update MMM-MyCommute.js --- MMM-MyCommute.js | 42 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/MMM-MyCommute.js b/MMM-MyCommute.js index 6fa055d..e2f217d 100644 --- a/MMM-MyCommute.js +++ b/MMM-MyCommute.js @@ -197,7 +197,6 @@ Module.register('MMM-MyCommute', { this.sendSocketNotification("GOOGLE_TRAFFIC_GET", {destinations: destinations, instanceId: this.identifier}); } else { this.hide(1000, {lockString: this.identifier}); - this.sendNotification('SET_LCD_BACKLIGHT', {command: -1} ); // remove module backlighting this.inWindow = false; this.isHidden = true; } @@ -205,7 +204,6 @@ Module.register('MMM-MyCommute', { } else { this.hide(1000, {lockString: this.identifier}); - this.sendNotification('SET_LCD_BACKLIGHT', {command: -1} ); // remove module backlighting this.inWindow = false; this.isHidden = true; } @@ -286,31 +284,27 @@ Module.register('MMM-MyCommute', { formatTime: function(time, timeInTraffic) { var timeEl = document.createElement("span"); - var trafficStatus = 0 ; //0 = good, 1 = moderate, 2 = poor timeEl.classList.add("travel-time"); if (timeInTraffic != null) { timeEl.innerHTML = moment.duration(Number(timeInTraffic), "seconds").format(this.config.travelTimeFormat, {trim: this.config.travelTimeFormatTrim}); var variance = timeInTraffic / time; - if (variance > this.config.poorTimeThreshold) { - if (this.config.colorCodeTravelTime) { timeEl.classList.add("status-poor");}; - trafficStatus = 2; - } else if (variance > this.config.moderateTimeThreshold) { - if (this.config.colorCodeTravelTime) {timeEl.classList.add("status-moderate");}; - trafficStatus = 1 ; - } else { + if (this.config.colorCodeTravelTime) { + if (variance > this.config.poorTimeThreshold) { + timeEl.classList.add("status-poor"); + } else if (variance > this.config.moderateTimeThreshold) { + timeEl.classList.add("status-moderate"); + } else { timeEl.classList.add("status-good"); - trafficStatus = 0 ; + } } - } else { timeEl.innerHTML = moment.duration(Number(time), "seconds").format(this.config.travelTimeFormat, {trim: this.config.travelTimeFormatTrim}); timeEl.classList.add("status-good"); - trafficStatus = 0 ; } - return [timeEl,trafficStatus]; + return timeEl; }, @@ -366,8 +360,6 @@ Module.register('MMM-MyCommute', { wrapper.appendChild(loading); return wrapper } - - var worstTrafficStatus = 0 ; // 0 = good, 1 = moderate, 2 = poor for (var i = 0; i < this.predictions.length; i++) { @@ -404,13 +396,8 @@ Module.register('MMM-MyCommute', { } else if (p.routes.length == 1 || !this.config.showSummary) { var r = p.routes[0]; - var trafficStatus ; - var timeFormat; - - [timeFormat,trafficStatus] = this.formatTime(r.time, r.timeInTraffic); - - row.appendChild( timeFormat ); - worstTrafficStatus = (worstTrafficStatus>trafficStatus)?worstTrafficStatus:trafficStatus ; + + row.appendChild( this.formatTime(r.time, r.timeInTraffic) ); //summary? if (this.config.showSummary) { @@ -472,14 +459,7 @@ Module.register('MMM-MyCommute', { wrapper.appendChild(row); } - if (worstTrafficStatus == 0 ) { - this.sendNotification('SET_LCD_BACKLIGHT', {command: "GOOD"} ); // good traffic situation - } else if (worstTrafficStatus == 1) { - this.sendNotification('SET_LCD_BACKLIGHT', {command: "MODERATE"} ); // moderate traffic situation - } else { - this.sendNotification('SET_LCD_BACKLIGHT', {command: "POOR"} ); // poor traffic situation - } - + return wrapper; }, From 301554850cb0b6324b3a07a7d98331c07a141587 Mon Sep 17 00:00:00 2001 From: martinkooij Date: Mon, 9 Aug 2021 20:55:14 +0200 Subject: [PATCH 8/8] Update node_helper.js --- node_helper.js | 1 + 1 file changed, 1 insertion(+) diff --git a/node_helper.js b/node_helper.js index 0593fbb..cb7c61d 100644 --- a/node_helper.js +++ b/node_helper.js @@ -37,6 +37,7 @@ module.exports = NodeHelper.create({ try { var body = await fetch(dest.url); var data = await body.json() ; + await new Promise(resolve => setTimeout(resolve, 200)); // wait 200ms to prevent overlading the interface. // console.log("MM-MyCommute-DEBUG ("+ index + "): "+ JSON.stringify(data,undefined,2));