Skip to content

Commit

Permalink
auto calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Litzenburger authored and Jan Litzenburger committed Jan 24, 2021
1 parent a962bda commit 9e7fe79
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MMM-Jast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<none, vertical, horizontal>",
defaultCurrency: "EUR",
Expand Down
38 changes: 35 additions & 3 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -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...`);
},
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 9e7fe79

Please sign in to comment.