-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-MiBiciTuBici.js
121 lines (101 loc) · 4.04 KB
/
MMM-MiBiciTuBici.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* global Module */
/* Magic Mirror
* Module: MMM-MiBiciTuBici
*
* By Jose Forte
* MIT Licensed.
*/
Module.register("MMM-MiBiciTuBici", {
stations: {},
defaults: {
header: 'Mi Bici Tu Bici',
stationsList: [2, 4, 5, 6, 10, 16, 20, 22],
updateInterval: 300000, // update interval in milliseconds
fadeSpeed: 4000,
infoClass: 'medium' // small, medium or big
},
getStyles: function() {
return ["MMM-MiBiciTuBici.css"]
},
start: function() {
Log.info("Starting module: " + this.name);
this.getInfo()
this.scheduleUpdate()
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay
}
var self = this
setInterval(function() {
self.getInfo()
}, nextLoad)
},
getInfo: function () {
this.sendSocketNotification('GET_STATIONS')
},
socketNotificationReceived: function(notification, payload) {
var self = this
if (notification === "STATIONS_RESULT") {
this.stations = payload
this.updateDom(self.config.fadeSpeed)
}
},
getHeader: function() {
return this.config.header
},
getDom: function() {
var wrapper = document.createElement("table")
if (Object.entries(this.stations).length === 0) return wrapper
var stationsList = this.config.stationsList
var stations = this.stations.data.stations
wrapper.className = 'mibicitubici'
// header row
var headerRow = document.createElement("tr"),
headerStationNameCell = document.createElement("td"),
// headerAddressCell = document.createElement("td"),
// headerAnchorCell = document.createElement("td"), // amount of anchors per station
headerBikesCell = document.createElement("td"), // amount of free bikes per station
headerTandemCell = document.createElement("td"); // amount of free tandem bikes per station
// headerAnchorCell.innerHTML = '<i class="fas fa-anchor"></i>'
// headerAnchorCell.className = 'info ' + (this.config.infoClass === 'small' ? 'medium': '')
headerBikesCell.innerHTML = '<i class="fas fa-bicycle"></i>'
headerBikesCell.className = 'info' + ' icon ' + (this.config.infoClass === 'small' ? 'medium': '')
headerTandemCell.innerHTML = '<i class="far fa-handshake"></i>'
headerTandemCell.className = 'info' + ' icon ' + (this.config.infoClass === 'small' ? 'medium': '')
headerRow.appendChild(headerStationNameCell)
// headerRow.appendChild(headerAnchorCell)
headerRow.appendChild(headerBikesCell)
headerRow.appendChild(headerTandemCell)
wrapper.appendChild(headerRow)
// stations rows
for (let key in stations) {
let value = stations[key]
if (stationsList.indexOf(value["station_code"]) != -1) {
let hasBikes = value["bikes"] > 0 ? 'has-many' : 'has-not'
let hasTandem = value["tandem"] > 0 ? 'has-many' : 'has-not'
let stationRow = document.createElement("tr"),
stationNameCell = document.createElement("td"),
// anchorMCell = document.createElement("td"),
bikesCell = document.createElement("td")
tandemCell = document.createElement("td");
stationNameCell.innerHTML = value["station_code"] + '- ' + '<span class="inactive">' +
value["name"] + '</span>'
stationNameCell.className = this.config.infoClass
// anchorMCell.innerHTML = value["anchor"]
// anchorMCell.className = 'info ' + this.config.infoClass
bikesCell.innerHTML = value["bikes"]
bikesCell.className = hasBikes + ' ' + 'info' + ' ' + this.config.infoClass
tandemCell.innerHTML = value["tandem"]
tandemCell.className = hasTandem + ' ' + 'info' + ' ' + this.config.infoClass
stationRow.appendChild(stationNameCell)
// stationRow.appendChild(anchorMCell)
stationRow.appendChild(bikesCell)
stationRow.appendChild(tandemCell)
wrapper.appendChild(stationRow)
}
}
return wrapper
}
})