From 9e7fe794d4de7eee20895b0744510d95687f5817 Mon Sep 17 00:00:00 2001 From: Jan Litzenburger Date: Sun, 24 Jan 2021 18:57:34 +0100 Subject: [PATCH] auto calculation --- MMM-Jast.js | 2 +- README.md | 2 +- node_helper.js | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/MMM-Jast.js b/MMM-Jast.js index 5426270..c4db841 100644 --- a/MMM-Jast.js +++ b/MMM-Jast.js @@ -6,7 +6,7 @@ Module.register("MMM-Jast", { defaults: { debug: false, header: null, - updateIntervalInSeconds: 1800, + updateIntervalInSeconds: 0, requestIntervalInSeconds: 62, fadeSpeedInSeconds: 3.5, // Higher value: vertical -> faster // horizontal -> slower stocks: [ diff --git a/README.md b/README.md index ce9214b..bd2cd79 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ To use this module, add it to the modules array in the `config/config.js` file: position: "top_left", config: { maxWidth: "100%", - updateIntervalInSeconds: 1800, + updateIntervalInSeconds: 0, // 0 = Auto calculation to get a maximum number of 500 requests per day. fadeSpeedInSeconds: 3.5, // Higher value: vertical -> faster // horizontal -> slower scroll: "", defaultCurrency: "EUR", diff --git a/node_helper.js b/node_helper.js index d427696..c1b8c73 100644 --- a/node_helper.js +++ b/node_helper.js @@ -1,7 +1,39 @@ +const e = require("express"); const NodeHelper = require("node_helper"); const request = require("request"); module.exports = NodeHelper.create({ + updateInterval: null, + getUpdateInterval(config) { + if (!this.updateInterval) { + if (config.updateIntervalInSeconds === 0) { + const exchangeRates = []; + config.stocks.forEach((stock) => { + if ( + stock.tradeCurrency && + stock.displayCurrency && + stock.tradeCurrency !== stock.displayCurrency + ) { + const currentChange = exchangeRates.find( + (rate) => + rate.from === stock.tradeCurrency && + rate.to === stock.displayCurrency + ); + if (!currentChange) { + exchangeRates.push(currentChange); + } + } + }); + const numEntities = + config.stocks.length + config.crypto.length + exchangeRates.length; + const delayPerEntity = 86400 / 500; + this.updateInterval = delayPerEntity * numEntities * 1000; + } else { + this.updateInterval = config.updateIntervalInSeconds * 1000; + } + } + return this.updateInterval; + }, start() { console.log(`${this.name} helper method started...`); }, @@ -35,7 +67,7 @@ module.exports = NodeHelper.create({ config.stocks.forEach((stock) => { if ( !stock.lastUpdate || - Date.now() - stock.lastUpdate >= config.updateIntervalInSeconds * 1000 + Date.now() - stock.lastUpdate >= this.getUpdateInterval(config) ) { const url = `${config.baseURL}query?function=TIME_SERIES_DAILY&outputsize=compact&apikey=${config.apiKey}&symbol=${stock.symbol}`; request(url, { json: true }, (err, _res, body) => { @@ -76,7 +108,7 @@ module.exports = NodeHelper.create({ config.crypto.forEach((crypto) => { if ( !crypto.lastUpdate || - Date.now() - crypto.lastUpdate >= config.updateIntervalInSeconds * 1000 + Date.now() - crypto.lastUpdate >= this.getUpdateInterval(config) ) { const url = `${config.baseURL}query?function=DIGITAL_CURRENCY_DAILY&symbol=${crypto.symbol}&market=USD&apikey=${config.apiKey}`; request(url, { json: true }, (err, _res, body) => { @@ -132,7 +164,7 @@ module.exports = NodeHelper.create({ !currentChange || !currentChange.lastUpdate || Date.now() - currentChange.lastUpdate >= - config.updateIntervalInSeconds * 1000 + this.getUpdateInterval(config) ) { const url = `${config.baseURL}query?function=CURRENCY_EXCHANGE_RATE&from_currency=${stock.tradeCurrency}&to_currency=${stock.displayCurrency}&apikey=${config.apiKey}`; request(url, { json: true }, (err, res, body) => {