diff --git a/MMM-Jast.js b/MMM-Jast.js index 4a01aef..5fcbd8a 100644 --- a/MMM-Jast.js +++ b/MMM-Jast.js @@ -63,6 +63,7 @@ Module.register("MMM-Jast", { this.exchangeData = []; this.getExchangeRate(); this.getStocks(); + this.getCrypto(); this.scheduleUpdate(); }, @@ -71,6 +72,7 @@ Module.register("MMM-Jast", { setInterval(function () { self.getExchangeRate(); self.getStocks(); + self.getCrypto(); }, this.config.requestIntervalInSeconds * 1000); }, @@ -78,6 +80,10 @@ Module.register("MMM-Jast", { this.sendSocketNotification("GET_STOCKS", this.config); }, + getCrypto() { + this.sendSocketNotification("GET_CRYPTO", this.config); + }, + getExchangeRate() { this.sendSocketNotification("GET_EXCHANGE", { config: this.config, @@ -97,6 +103,17 @@ Module.register("MMM-Jast", { currentStock.lastUpdate = Date.now(); this.updateDom(); } + } else if (notification === "CRYPTO_RESULT") { + const { symbol, current, last } = payload; + const currentCrypto = this.config.crypto.find( + (crypto) => crypto.symbol === symbol + ); + if (currentCrypto) { + currentCrypto.current = current; + currentCrypto.last = last; + currentCrypto.lastUpdate = Date.now(); + this.updateDom(); + } } else if (notification === "EXCHANGE_RESULT") { let { from, to, rate } = payload; this.exchangeData.push({ from, to, rate, lastUpdate: Date.now() }); diff --git a/node_helper.js b/node_helper.js index 528e99a..303e624 100644 --- a/node_helper.js +++ b/node_helper.js @@ -62,6 +62,40 @@ module.exports = NodeHelper.create({ }); }, + sendCryptoRequest: function (config) { + const self = this; + if (config.debug) { + self.sendSocketNotification("CRYPTO_RESULT", { + symbol: "BTC", + current: 30000, + last: 32000 + }); + return; + } + + config.crypto.forEach((crypto) => { + if (!crypto.lastUpdate || Date.now() - crypto.lastUpdate >= config.updateIntervalInSeconds * 1000) { + const url = `${config.baseURL}query?function=DIGITAL_CURRENCY_DAILY&symbol=${crypto.symbol}&market=USD&apikey=${config.apiKey}`; + request(url, { json: true }, (err, res, body) => { + if (err) { + console.error(`Error requesting Crypto data`); + } + try { + const symbol = body["Meta Data"]["2. Digital Currency Code"]; + const values = Object.values(body["Time Series (Digital Currency Daily)"]); + const current = parseFloat(values[0]["4a. close (USD)"]); + const last = parseFloat(values[1]["4a. close (USD)"]); + + console.log("Sending Crypto result:", { symbol, current, last }); + self.sendSocketNotification("CRYPTO_RESULT", { symbol, current, last }); + } catch (err) { + console.error(`Error processing Crypto response`, body); + } + }); + } + }); + }, + sendExchangeRequest(payload) { const self = this; const { config, rates } = payload; @@ -127,6 +161,8 @@ module.exports = NodeHelper.create({ this.sendStocksRequest(payload); } else if (notification === "GET_EXCHANGE") { this.sendExchangeRequest(payload); + } else if (notification === "GET_CRYPTO") { + this.sendCryptoRequest(payload); } else { console.warn(`${notification} is invalid notification`); } diff --git a/templates/HorizontalStockList.njk b/templates/HorizontalStockList.njk index 7f75282..36fd266 100755 --- a/templates/HorizontalStockList.njk +++ b/templates/HorizontalStockList.njk @@ -21,6 +21,24 @@ {% endfor %} + {% for crypto in config.crypto %} + {{ crypto.name }}: + {% if utils.getStockChange(crypto) > 0 %} + {% set colorClass = "high" %} + {% elif utils.getStockChange(crypto) < 0 %} + {% set colorClass = "low " %} + {% else %} + {% set colorClass = "" %} + {% endif %} + + {{ utils.getCurrentValue(crypto, exchangeData) }} + {{ utils.getCurrency(crypto, exchangeData, config) }} + {% if colorClass %} + ({{ utils.getStockChange(crypto) }}%) + {% endif %} + + + {% endfor %} {% if config.showDepotGrowth %} {% set depotGrowth = utils.getDepotGrowth(config, exchangeData) %} {% if depotGrowth.value > 0 %} diff --git a/templates/VerticalScrollStyle.njk b/templates/VerticalScrollStyle.njk index b5b5efb..9fb4fd4 100755 --- a/templates/VerticalScrollStyle.njk +++ b/templates/VerticalScrollStyle.njk @@ -1,8 +1,8 @@ {% macro render(config) %} {% if config.showDepotGrowth %} - {% set itemCount = config.stocks.length + 1 %} + {% set itemCount = config.stocks.length + config.crypto.length + 1 %} {% else %} - {% set itemCount = config.stocks.length %} + {% set itemCount = config.stocks.length + config.crypto.length %} {% endif %} {% set percentPerItem = 100 / itemCount %}