Skip to content

Commit

Permalink
Formatting and README changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryck committed Mar 11, 2018
1 parent 4e0a042 commit 4c3d2e1
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 106 deletions.
3 changes: 0 additions & 3 deletions MMM-AQI.css
Expand Up @@ -8,17 +8,14 @@
content: '\f062';
padding-right: 5px;
}

.city.unhealthy-sensitive:before,
.city.unhealthy:before,
.city.very-unhealthy:before {
content: '\f074';
}

.city.hazardous:before {
content: '\f071';
}

.aqi.good {
color: #009966;
}
Expand Down
202 changes: 100 additions & 102 deletions MMM-AQI.js
Expand Up @@ -5,50 +5,50 @@
* MIT Licensed.
*/
Module.register("MMM-AQI", {
defaults: {
defaults: {
token: "",
city: "here",
iaqi: true,
updateInterval: 30 * 60 * 1000, // Every half hour.
initialLoadDelay: 0, // No delay/
animationSpeed: 1000, // One second.
debug: false
debug: false

},
start: function() {
},
start: function () {
Log.info('Starting module: ' + this.name);
this.loaded = false;
this.result = null;
this.scheduleUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
this.apiBase = "https://api.waqi.info/feed/" + this.config.city + "/";
this.url = encodeURI(this.apiBase + this.getParams());
if(this.config.debug) {
if (this.config.debug) {
Log.info(this.url);
}

this.updateAQI(this);

},
// updateAQI
updateAQI: function(self) {
self.sendSocketNotification("GET_AQI", {"url":self.url});
},

getStyles: function() {
return ["MMM-AQI.css", "weather-icons.css"];
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
//Define header for module.
getHeader: function() {
return this.config.header;
},

// Override dom generator.
getDom: function() {
// updateAQI
updateAQI: function (self) {
self.sendSocketNotification("GET_AQI", { "url": self.url });
},

getStyles: function () {
return ["MMM-AQI.css", "weather-icons.css"];
},
// Define required scripts.
getScripts: function () {
return ["moment.js"];
},
//Define header for module.
getHeader: function () {
return this.config.header;
},

// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");

if (this.config.sensorPin === "") {
Expand All @@ -70,51 +70,51 @@ Module.register("MMM-AQI", {


if (this.result.data != null) {
var aqiRow = document.createElement("tr");
var aqi = this.result.data.aqi;
var city = this.result.data.city.name;
var aqiRow = document.createElement("tr");
var aqi = this.result.data.aqi;
var city = this.result.data.city.name;

// Asign aqi class name.
var aqiClass = "";
switch (true) {
case aqi <= 0:
if(this.config.debug) {
if (this.config.debug) {
Log.info("empty" + " - " + aqi);
}
aqiClass += "empty";
break;
case aqi > 0 && aqi < 51:
if(this.config.debug) {
if (this.config.debug) {
Log.info("good" + " - " + aqi);
}
aqiClass += "good";
break;
case aqi >= 51 && aqi < 101:
if(this.config.debug) {
if (this.config.debug) {
Log.info("moderate" + " - " + aqi);
}
aqiClass += "moderate";
break;
case aqi >= 101 && aqi < 151:
if(this.config.debug) {
if (this.config.debug) {
Log.info("unhealthy-sensitive" + " - " + aqi);
}
aqiClass += "unhealthy-sensitive";
break;
case aqi >= 151 && aqi < 201:
if(this.config.debug) {
if (this.config.debug) {
Log.info("unhealthy" + " - " + aqi);
}
aqiClass += "unhealthy";
break;
case aqi >= 201 && aqi < 300:
if(this.config.debug) {
if (this.config.debug) {
Log.info("very-unhealthy" + " - " + aqi);
}
aqiClass += "very-unhealthy";
break;
case aqi >= 300:
if(this.config.debug) {
if (this.config.debug) {
Log.info("hazardous" + " - " + aqi);
}
aqiClass += "hazardous";
Expand All @@ -133,29 +133,27 @@ Module.register("MMM-AQI", {

dataTable.appendChild(aqiRow);


if (this.config.iaqi) {
var iaqi = this.result.data.iaqi;
var iaqiRow = "";
var iaqiCityCell = "";
var iaqiAQICell = "";

Object.keys(iaqi).forEach(function(key){
console.log(key + '=' + iaqi[key].v);
iaqiRow = document.createElement("tr");

iaqiCityCell = document.createElement("td");
iaqiCityCell.className = "xsmall iaqi key " + aqiClass;
iaqiCityCell.innerHTML = key;
iaqiRow.appendChild(iaqiCityCell);

iaqiAQICell = document.createElement("td");
iaqiAQICell.className = "xsmall iaqi value " + aqiClass;
iaqiAQICell.innerHTML = iaqi[key].v;
iaqiRow.appendChild(iaqiAQICell);
dataTable.appendChild(iaqiRow);
});
}
var iaqi = this.result.data.iaqi;
var iaqiRow = "";
var iaqiCityCell = "";
var iaqiAQICell = "";

Object.keys(iaqi).forEach(function (key) {
iaqiRow = document.createElement("tr");

iaqiCityCell = document.createElement("td");
iaqiCityCell.className = "xsmall iaqi key " + aqiClass;
iaqiCityCell.innerHTML = key.toUpperCase();
iaqiRow.appendChild(iaqiCityCell);

iaqiAQICell = document.createElement("td");
iaqiAQICell.className = "xsmall iaqi value " + aqiClass;
iaqiAQICell.innerHTML = iaqi[key].v;
iaqiRow.appendChild(iaqiAQICell);
dataTable.appendChild(iaqiRow);
});
}
} else {
var row1 = document.createElement("tr");
dataTable.appendChild(row1);
Expand All @@ -165,66 +163,66 @@ Module.register("MMM-AQI", {
messageCell.className = "bright";
row1.appendChild(messageCell);

var row2 = document.createElement("tr");
dataTable.appendChild(row2);
var row2 = document.createElement("tr");
dataTable.appendChild(row2);

var timeCell = document.createElement("td");
timeCell.innerHTML = this.result.timestamp;
timeCell.className = "bright xsmall";
row2.appendChild(timeCell);
var timeCell = document.createElement("td");
timeCell.innerHTML = this.result.timestamp;
timeCell.className = "bright xsmall";
row2.appendChild(timeCell);
}
wrapper.appendChild(dataTable);
return wrapper;
},
processAQI: function(result) {
this.result = {};
this.result.timestamp = moment().format("LLL");
processAQI: function (result) {
this.result = {};
this.result.timestamp = moment().format("LLL");
if (typeof result !== "undefined" && result != null) {
if(this.config.debug) {
if (this.config.debug) {
Log.info(result);
}
this.result.data = result.data;
} else {
//No data returned - set error message
this.result.message = "No data returned";
this.result.data = null;
if(this.config.debug) {
Log.error("No data returned");
Log.error(this.result);
}
}
} else {
//No data returned - set error message
this.result.message = "No data returned";
this.result.data = null;
if (this.config.debug) {
Log.error("No data returned");
Log.error(this.result);
}
}
this.updateDom(this.config.animationSpeed);
this.loaded = true;
},
getParams: function() {
var params = "?";
params += "token=" + this.config.token;
if(this.config.debug) {
Log.info(params);
}
return params;
},
getParams: function () {
var params = "?";
params += "token=" + this.config.token;
if (this.config.debug) {
Log.info(params);
}
return params;
},
/* scheduleUpdate()
* Schedule next update.
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}

var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.updateAQI(self);
}, nextLoad);
},
// Process data returned
socketNotificationReceived: function(notification, payload) {
if (notification === "AQI_DATA" && payload.url === this.url) {
this.processAQI(payload.data);
this.scheduleUpdate(this.config.updateInterval);
}
}
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}

var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function () {
self.updateAQI(self);
}, nextLoad);
},
// Process data returned
socketNotificationReceived: function (notification, payload) {
if (notification === "AQI_DATA" && payload.url === this.url) {
this.processAQI(payload.data);
this.scheduleUpdate(this.config.updateInterval);
}
}
});
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -35,7 +35,7 @@ Here is an example of an entry in `config.js`
position: 'bottom_left',
header: 'Air Quality Index (AQI)',
config: {
token: "demo",
token: "",
city: "here",
iaqi: true,
updateInterval: 30 * 60 * 1000, // Every half hour.
Expand All @@ -46,6 +46,14 @@ Here is an example of an entry in `config.js`
},
```

## Find you city
The API is quite picky with the cities, so yoiur best option is to use the keyword _here_ or an id.
To find your id, just enter this URL in your browser:

[https://api.waqi.info/search/?token=TOKEN&keyword=CITY](https://api.waqi.info/search/?token=TOKEN&keyword=CITY)

**Note:** You need to replace _TOKEN_ and _CITY_ for your token (the same one you are using in the module works) and the city you are looking for, repectively.

## Dependencies
- [request](https://www.npmjs.com/package/request) (installed via `npm install`)

Expand Down

0 comments on commit 4c3d2e1

Please sign in to comment.