|
| 1 | +/* Magic Mirror |
| 2 | + * Module: Ruter |
| 3 | + * |
| 4 | + * By Cato Antonsen (https://github.com/CatoAntonsen) |
| 5 | + * MIT Licensed. |
| 6 | + */ |
| 7 | + |
| 8 | +Module.register("MMM-Ruter",{ |
| 9 | + |
| 10 | + // Default module config. |
| 11 | + defaults: { |
| 12 | + showHeader: false, // Set this to true to show header above the journeys (default is false) |
| 13 | + showPlatform: false, // Set this to true to get the names of the platforms (default is false) |
| 14 | + maxItems: 5, // Number of journeys to display (default is 5) |
| 15 | + humanizeTimeTreshold: 15, // If time to next journey is below this value, it will be displayed as "x minutes" instead of time (default is 15 minutes) |
| 16 | + serviceReloadInterval: 30000, // Refresh rate in MS for how often we call Ruter's web service. NB! Don't set it too low! (default is 30 seconds) |
| 17 | + timeReloadInterval: 1000, // Refresh rate how often we check if we need to update the time shown on the mirror (default is every second) |
| 18 | + animationSpeed: 1000, // How fast the animation changes when updating mirror (default is 1 second) |
| 19 | + fade: true, // Set this to true to fade list from light to dark. (default is true) |
| 20 | + fadePoint: 0.25 // Start on 1/4th of the list. |
| 21 | + }, |
| 22 | + |
| 23 | + getStyles: function () { |
| 24 | + return ["ruter.css"]; |
| 25 | + }, |
| 26 | + |
| 27 | + getScripts: function() { |
| 28 | + return ["moment.js"]; |
| 29 | + }, |
| 30 | + |
| 31 | + getTranslations: function() { |
| 32 | + return { |
| 33 | + en: "translations/en.json", |
| 34 | + nb: "translations/nb.json" |
| 35 | + } |
| 36 | + }, |
| 37 | + |
| 38 | + start: function() { |
| 39 | + console.log(this.translate("STARINGMODULE") + ": " + this.name); |
| 40 | + |
| 41 | + this.journeys = []; |
| 42 | + this.previousTime = []; |
| 43 | + |
| 44 | + this.sendSocketNotification("CONFIG", this.config); |
| 45 | + |
| 46 | + var self = this; |
| 47 | + |
| 48 | + setInterval(function() { |
| 49 | + self.updateDomIfNeeded(); |
| 50 | + }, this.config.timeReloadInterval); |
| 51 | + |
| 52 | + }, |
| 53 | + |
| 54 | + socketNotificationReceived: function(notification, payload) { |
| 55 | + if (notification === "RUTER_UPDATE") { |
| 56 | + this.journeys = payload; |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + getDom: function() { |
| 61 | + if (this.journeys.length > 0) { |
| 62 | + |
| 63 | + var table = document.createElement("table"); |
| 64 | + table.className = "ruter small"; |
| 65 | + |
| 66 | + if (this.config.showHeader) { |
| 67 | + table.appendChild(this.getTableHeaderRow()); |
| 68 | + } |
| 69 | + |
| 70 | + for(var i = 0; i < this.journeys.length; i++) { |
| 71 | + |
| 72 | + var tr = this.getTableRow(this.journeys[i]); |
| 73 | + |
| 74 | + // Create fade effect. <-- stolen from default "calendar" module |
| 75 | + if (this.config.fade && this.config.fadePoint < 1) { |
| 76 | + if (this.config.fadePoint < 0) { |
| 77 | + this.config.fadePoint = 0; |
| 78 | + } |
| 79 | + var startingPoint = this.journeys.length * this.config.fadePoint; |
| 80 | + var steps = this.journeys.length - startingPoint; |
| 81 | + if (i >= startingPoint) { |
| 82 | + var currentStep = i - startingPoint; |
| 83 | + tr.style.opacity = 1 - (1 / steps * currentStep); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + table.appendChild(tr); |
| 88 | + } |
| 89 | + |
| 90 | + return table; |
| 91 | + } else { |
| 92 | + var wrapper = document.createElement("div"); |
| 93 | + wrapper.innerHTML = this.translate("LOADING"); |
| 94 | + wrapper.className = "small dimmed"; |
| 95 | + } |
| 96 | + |
| 97 | + return wrapper; |
| 98 | + }, |
| 99 | + |
| 100 | + getTableHeaderRow: function() { |
| 101 | + var thLine = document.createElement("th"); |
| 102 | + thLine.className = "light"; |
| 103 | + thLine.appendChild(document.createTextNode(this.translate("LINEHEADER"))); |
| 104 | + |
| 105 | + var thDestination = document.createElement("th"); |
| 106 | + thDestination.className = "light"; |
| 107 | + thDestination.appendChild(document.createTextNode(this.translate("DESTINATIONHEADER"))); |
| 108 | + |
| 109 | + var thPlatform = document.createElement("th"); |
| 110 | + thPlatform.className = "light"; |
| 111 | + thPlatform.appendChild(document.createTextNode(this.translate("PLATFORMHEADER"))); |
| 112 | + |
| 113 | + var thTime = document.createElement("th"); |
| 114 | + thTime.className = "light time" |
| 115 | + thTime.appendChild(document.createTextNode(this.translate("TIMEHEADER"))); |
| 116 | + |
| 117 | + var thead = document.createElement("thead"); |
| 118 | + thead.addClass = "xsmall dimmed"; |
| 119 | + thead.appendChild(thLine); |
| 120 | + thead.appendChild(thDestination); |
| 121 | + if (this.config.showPlatform) { thead.appendChild(thPlatform); } |
| 122 | + thead.appendChild(thTime); |
| 123 | + |
| 124 | + return thead; |
| 125 | + }, |
| 126 | + |
| 127 | + getTableRow: function(journey) { |
| 128 | + var tdLine = document.createElement("td"); |
| 129 | + var txtLine = document.createTextNode(journey.lineName); |
| 130 | + tdLine.appendChild(txtLine); |
| 131 | + |
| 132 | + var tdDestination = document.createElement("td"); |
| 133 | + tdDestination.className = "destination bright"; |
| 134 | + tdDestination.appendChild(document.createTextNode(journey.destinationName)); |
| 135 | + |
| 136 | + if (this.config.showPlatform) { |
| 137 | + var tdPlatform = document.createElement("td"); |
| 138 | + tdPlatform.className = "platform"; |
| 139 | + tdPlatform.appendChild(document.createTextNode(journey.platform)); |
| 140 | + } |
| 141 | + |
| 142 | + var tdTime = document.createElement("td"); |
| 143 | + tdTime.className = "time light"; |
| 144 | + tdTime.appendChild(document.createTextNode(this.formatTime(journey.time))); |
| 145 | + |
| 146 | + var tr = document.createElement("tr"); |
| 147 | + tr.appendChild(tdLine); |
| 148 | + tr.appendChild(tdDestination); |
| 149 | + if (this.config.showPlatform) { tr.appendChild(tdPlatform); } |
| 150 | + tr.appendChild(tdTime); |
| 151 | + |
| 152 | + return tr; |
| 153 | + }, |
| 154 | + |
| 155 | + updateDomIfNeeded: function() { |
| 156 | + var needUpdate = false; |
| 157 | + |
| 158 | + for(var i=0; i < this.journeys.length; i++) { |
| 159 | + var time = this.formatTime(this.journeys[i].time); |
| 160 | + if (this.previousTime[i] != time) { |
| 161 | + needUpdate = true; |
| 162 | + this.previousTime[i] = time; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (needUpdate) { |
| 167 | + this.updateDom(this.config.animationSpeed); |
| 168 | + } |
| 169 | + }, |
| 170 | + |
| 171 | + formatTime: function(t) { |
| 172 | + var min = moment.duration(moment(t) - moment.now()).minutes(); |
| 173 | + if (min == 0) { |
| 174 | + return this.translate("NOW") |
| 175 | + } else if (min == 1) { |
| 176 | + return this.translate("1MIN"); |
| 177 | + } else if (min < this.config.humanizeTimeTreshold) { |
| 178 | + return min + " " + this.translate("MINUTES"); |
| 179 | + } else { |
| 180 | + return moment(t).format("LT"); |
| 181 | + } |
| 182 | + } |
| 183 | +}); |
0 commit comments