Skip to content

Commit

Permalink
Restyled by prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Feb 5, 2021
1 parent f065566 commit 6c54ae7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 52 deletions.
51 changes: 30 additions & 21 deletions MMM-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,49 @@

Module.register("MMM-json", {
// Default module config.
defaults : {
url : "https://jsonplaceholder.typicode.com/users",
refreshInterval : 1000 * 60 * 5 // 5 minutes
defaults: {
url: "https://jsonplaceholder.typicode.com/users",
refreshInterval: 1000 * 60 * 5 // 5 minutes
},

start : function() {
start: function () {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.getData();

var self = this;
// Schedule updates
setInterval(function() {
setInterval(function () {
self.getData();
self.updateDom();
}, this.config.refreshInterval);
},

// Import additional CSS Styles
getStyles : function() { return [ "mmm-json.css" ]; },
getStyles: function () {
return ["mmm-json.css"];
},

// Contact node helper for data
getData : function() {
getData: function () {
Log.info("MMM-json: getting data");

this.sendSocketNotification(
"MMM_JSON_GET_REQUEST",
{config : this.config, identifier : this.identifier});
this.sendSocketNotification("MMM_JSON_GET_REQUEST", {
config: this.config,
identifier: this.identifier
});
},

// Handle node helper response
socketNotificationReceived : function(notification, payload) {
if (notification === "MMM_JSON_GET_RESPONSE" &&
payload.identifier == this.identifier) {
socketNotificationReceived: function (notification, payload) {
if (
notification === "MMM_JSON_GET_RESPONSE" &&
payload.identifier == this.identifier
) {
if (payload.error === true) {
console.error(
"MMM-JSON: An Error occured while fetching your response. Please have a look at the server log.");
"MMM-JSON: An Error occured while fetching your response. Please have a look at the server log."
);
this.loaded = false;
} else {
this.loaded = true;
Expand All @@ -52,7 +58,7 @@ Module.register("MMM-json", {
}
},
// Override the Header generator
getHeader : function() {
getHeader: function () {
// If an Icon should be displayed we need our own header
if (this.config.headerIcon) {
return "";
Expand All @@ -64,7 +70,7 @@ Module.register("MMM-json", {
},

// Override dom generator.
getDom : function() {
getDom: function () {
var wrapper = document.createElement("div");
if (this.config.url === "") {
wrapper.innerHTML = "Missing configuration.";
Expand All @@ -84,8 +90,9 @@ Module.register("MMM-json", {
var imgDiv = document.createElement("div");

var sTitle = document.createElement("p");
sTitle.innerHTML =
this.data.header ? this.data.header : this.config.header;
sTitle.innerHTML = this.data.header
? this.data.header
: this.config.header;
sTitle.className += "normal";

var icon = document.createElement("i");
Expand All @@ -108,9 +115,11 @@ Module.register("MMM-json", {

titleTr.innerHTML = this.response[i].title + ":";
dataTr.innerHTML =
(this.response[i].prefix ? this.response[i].prefix : "") + " " +
this.response[i].value + " " +
(this.response[i].suffix ? this.response[i].suffix : "");
(this.response[i].prefix ? this.response[i].prefix : "") +
" " +
this.response[i].value +
" " +
(this.response[i].suffix ? this.response[i].suffix : "");

titleTr.className += " small regular bright";
dataTr.className += " small light bright";
Expand Down
72 changes: 41 additions & 31 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var request = require("request");
var jp = require("jsonpath");
const {jq} = require("jq.node");
const { jq } = require("jq.node");
var NodeHelper = require("node_helper");

function asPromise(context, callbackFunction, ...args) {
Expand All @@ -23,73 +23,83 @@ function asPromise(context, callbackFunction, ...args) {
async function do_jq(filter, data) {
if (filter) {
try {
data = await asPromise(null, jq, JSON.stringify(data), filter, {})
data = JSON.parse(data)
data = await asPromise(null, jq, JSON.stringify(data), filter, {});
data = JSON.parse(data);
} catch (e) {
console.error("Error handling jq.node filter '" + filter + "':", e)
console.error("Error handling jq.node filter '" + filter + "':", e);
}
}
return data
return data;
}

module.exports = NodeHelper.create({
start : function() { console.log("Starting node helper: " + this.name); },
start: function () {
console.log("Starting node helper: " + this.name);
},

socketNotificationReceived : function(notification, payload) {
socketNotificationReceived: function (notification, payload) {
var self = this;
console.log("Notification: " + notification + " Payload:", payload);

if (notification === "MMM_JSON_GET_REQUEST") {
req_params = {
url : payload.config.url,
json : true,
url: payload.config.url,
json: true,
...payload.config.request
};
console.debug(self.name + " req_params:", req_params);
request(req_params, async function(error, response, jsonData) {
request(req_params, async function (error, response, jsonData) {
if (!error && Math.floor(response.statusCode / 100) === 2) {
var responseObject;

jsonData = await do_jq(payload.config.jq, jsonData)
if (payload.config.values == undefined ||
payload.config.values.length == 0) {
jsonData = await do_jq(payload.config.jq, jsonData);
if (
payload.config.values == undefined ||
payload.config.values.length == 0
) {
// Values are not defined fetch first properties
var firstObject = jsonData;
if (Array.isArray(jsonData)) {
firstObject = jsonData[0];
}
responseObject = {
identifier : payload.identifier,
data : Object.keys(firstObject).map((prop) => {
return {title : prop, value : firstObject[prop]};
identifier: payload.identifier,
data: Object.keys(firstObject).map((prop) => {
return { title: prop, value: firstObject[prop] };
})
};
}
else {
} else {
// Values are defined, get what the user wants
responseObject = {
identifier : payload.identifier,
data : payload.config.values.map((val) => {
identifier: payload.identifier,
data: payload.config.values.map((val) => {
return {
...val,
value : val.numberDevisor != undefined
? (jp.query(jsonData, val.query)[0] /
val.numberDevisor)
.toFixed(3)
: jp.query(jsonData, val.query)[0]
value:
val.numberDevisor != undefined
? (
jp.query(jsonData, val.query)[0] / val.numberDevisor
).toFixed(3)
: jp.query(jsonData, val.query)[0]
};
})
};
}

self.sendSocketNotification("MMM_JSON_GET_RESPONSE", responseObject);
} else {
self.sendSocketNotification(
"MMM_JSON_GET_RESPONSE",
{identifier : payload.identifier, error : true});
console.error(self.name + " error:", error,
"statusCode:", response && response.statusCode,
"statusMessage:", response && response.statusMessage);
self.sendSocketNotification("MMM_JSON_GET_RESPONSE", {
identifier: payload.identifier,
error: true
});
console.error(
self.name + " error:",
error,
"statusCode:",
response && response.statusCode,
"statusMessage:",
response && response.statusMessage
);
}
});
}
Expand Down

0 comments on commit 6c54ae7

Please sign in to comment.