|
7 | 7 | Module.register('MMM-AirQuality', { |
8 | 8 | // Default module config. |
9 | 9 | defaults: { |
| 10 | + initialDelay: 0, |
10 | 11 | lang: '', |
11 | 12 | location: '', |
12 | 13 | showLocation: true, |
13 | 14 | showIndex: true, |
14 | 15 | appendLocationNameToHeader: true, |
15 | 16 | updateInterval: 30, // every 30 minutes |
16 | | - animationSpeed: 1000 |
| 17 | + animationSpeed: 1000, |
| 18 | + token: '', |
| 19 | + apiBase: 'api.waqi.info/', |
| 20 | + dataEndpoint: 'feed/', |
17 | 21 | }, |
| 22 | + notifications: { |
| 23 | + DATA: 'AIR_QUALITY_DATA', |
| 24 | + DATA_RESPONSE: 'AIR_QUALITY_DATA_RESPONSE', |
| 25 | + }, |
18 | 26 | start: function(){ |
19 | | - Log.info('Starting module: ' + this.name); |
20 | | - // load data |
21 | | - this.load(); |
22 | | - // schedule refresh |
23 | | - setInterval( |
24 | | - this.load.bind(this), |
25 | | - this.config.updateInterval * 60 * 1000); |
| 27 | + const self = this |
| 28 | + Log.info(`Starting module: ${this.name}`) |
| 29 | + self.loaded = false |
| 30 | + |
| 31 | + setTimeout(function () { |
| 32 | + self.sendSocketNotification(self.notifications.DATA, self.config) |
| 33 | + }, this.config.initialDelay * 1000) |
| 34 | + |
| 35 | + // set auto-update |
| 36 | + setInterval(function () { |
| 37 | + self.sendSocketNotification(self.notifications.DATA, self.config) |
| 38 | + }, this.config.updateInterval * 60 * 1000 + this.config.initialDelay * 1000) |
26 | 39 | }, |
27 | | - load: function(){ |
28 | | - _aqiFeed({ |
29 | | - lang: this.config.lang, |
30 | | - city: this.config.location, |
31 | | - callback: this.render.bind(this) |
32 | | - }); |
33 | | - }, |
34 | | - render: function(data){ |
35 | | - this.data.value = $(data.aqit).find("span").text(); |
36 | | - this.data.impact = data.impact; |
37 | | - this.data.city = data.cityname; |
| 40 | + render: function(response){ |
| 41 | + let data = response.data; |
| 42 | + this.data.value = data.aqi; |
| 43 | + this.data.city = data.city.name; |
38 | 44 | this.loaded = true; |
39 | | - this.updateDom(this.animationSpeed); |
| 45 | + |
| 46 | + if (data.aqi < 51) { |
| 47 | + this.data.color = "#009966"; |
| 48 | + this.data.impact = 'Good'; |
| 49 | + } else if (data.aqi < 101) { |
| 50 | + this.data.color = "#ffde33"; |
| 51 | + this.data.impact = 'Moderate'; |
| 52 | + } else if (data.aqi < 151) { |
| 53 | + this.data.color = '#ff9933'; |
| 54 | + this.data.impact = 'Unhealty for Sensitive Groups'; |
| 55 | + } else if (data.aqi < 201) { |
| 56 | + this.data.color = '#cc0033'; |
| 57 | + this.data.impact = 'Unhealthy'; |
| 58 | + } else if (data.aqi < 301) { |
| 59 | + this.data.color = '#7e0023'; |
| 60 | + this.data.impact = 'Hazardous'; |
| 61 | + } |
40 | 62 | }, |
41 | 63 | html: { |
42 | | - icon: '<i class="fa fa-leaf"></i>', |
| 64 | + icon: '<i class="fa-solid fa-smog"></i>', |
43 | 65 | city: '<div class="xsmall">{0}</div>', |
44 | | - quality: '<div>{0} {1}{2}</div>' |
| 66 | + quality: '<div style="color: {0}">{1} {2}{3}</div>' |
45 | 67 | }, |
46 | 68 | getScripts: function() { |
47 | 69 | return [ |
48 | | - 'aqiFeed.js', |
49 | 70 | '//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js', |
50 | 71 | 'String.format.js' |
51 | 72 | ]; |
52 | 73 | }, |
53 | 74 | getStyles: function() { |
54 | | - return ['https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css']; |
| 75 | + return ['https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css']; |
55 | 76 | }, |
56 | | - // Override getHeader method. |
57 | | - getHeader: function () { |
58 | | - var header = "" |
59 | | - if (this.data.header) |
60 | | - header += this.data.header; |
61 | | - if (this.config.appendLocationNameToHeader) { |
62 | | - if (header != "") { |
63 | | - header += " "; |
64 | | - } |
65 | | - header += this.data.city; |
66 | | - } |
67 | | - return header |
68 | | - }, |
| 77 | + // Override getHeader method. |
| 78 | + getHeader: function () { |
| 79 | + var header = "" |
| 80 | + if (this.data.header) |
| 81 | + header += this.data.header; |
| 82 | + if (this.config.appendLocationNameToHeader) { |
| 83 | + if (header != "") { |
| 84 | + header += " "; |
| 85 | + } |
| 86 | + header += this.data.city; |
| 87 | + } |
| 88 | + return header |
| 89 | + }, |
69 | 90 | // Override dom generator. |
70 | 91 | getDom: function() { |
71 | 92 | var wrapper = document.createElement("div"); |
| 93 | + if (this.config.token === '') { |
| 94 | + wrapper.innerHTML = "Please set the AQICN token for module: " + this.name + ". You can acquire one at <a href='https://aqicn.org/data-platform/token/'>https://aqicn.org/data-platform/token/</a>."; |
| 95 | + wrapper.className = "dimmed light small"; |
| 96 | + return wrapper; |
| 97 | + } |
72 | 98 | if (this.config.location === '') { |
73 | 99 | wrapper.innerHTML = "Please set the air quality index <i>location</i> in the config for module: " + this.name + "."; |
74 | 100 | wrapper.className = "dimmed light small"; |
75 | 101 | return wrapper; |
76 | 102 | } |
| 103 | + |
77 | 104 | if (!this.loaded) { |
78 | 105 | wrapper.innerHTML = "Loading air quality index ..."; |
79 | 106 | wrapper.className = "dimmed light small"; |
80 | 107 | return wrapper; |
81 | 108 | } |
82 | 109 | wrapper.innerHTML = |
83 | 110 | this.html.quality.format( |
| 111 | + this.data.color, |
84 | 112 | this.html.icon, |
85 | 113 | this.data.impact, |
86 | 114 | (this.config.showIndex?' ('+this.data.value+')':''))+ |
87 | 115 | (this.config.showLocation && !this.config.appendLocationNameToHeader?this.html.city.format(this.data.city):''); |
88 | 116 | return wrapper; |
89 | | - } |
| 117 | + }, |
| 118 | + socketNotificationReceived: function (notification, payload) { |
| 119 | + const self = this |
| 120 | + Log.debug('received ' + notification) |
| 121 | + switch (notification) { |
| 122 | + case self.notifications.DATA_RESPONSE: |
| 123 | + if (payload.status === 'OK') { |
| 124 | + console.log('Data %o', payload.payloadReturn) |
| 125 | + self.render(payload.payloadReturn) |
| 126 | + self.updateDom(this.animationSpeed); |
| 127 | + } else { |
| 128 | + console.log('DATA FAILED ' + payload.message) |
| 129 | + } |
| 130 | + break |
| 131 | + } |
| 132 | + }, |
90 | 133 | }); |
0 commit comments