Skip to content

Commit

Permalink
Merge pull request #5 from ZachK543/master
Browse files Browse the repository at this point in the history
Crypto Functionality
  • Loading branch information
jalibu authored Jan 19, 2021
2 parents 1cd91dc + 9eb2077 commit 3cd8f7d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
17 changes: 17 additions & 0 deletions MMM-Jast.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Module.register("MMM-Jast", {
this.exchangeData = [];
this.getExchangeRate();
this.getStocks();
this.getCrypto();
this.scheduleUpdate();
},

Expand All @@ -71,13 +72,18 @@ Module.register("MMM-Jast", {
setInterval(function () {
self.getExchangeRate();
self.getStocks();
self.getCrypto();
}, this.config.requestIntervalInSeconds * 1000);
},

getStocks() {
this.sendSocketNotification("GET_STOCKS", this.config);
},

getCrypto() {
this.sendSocketNotification("GET_CRYPTO", this.config);
},

getExchangeRate() {
this.sendSocketNotification("GET_EXCHANGE", {
config: this.config,
Expand All @@ -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() });
Expand Down
36 changes: 36 additions & 0 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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`);
}
Expand Down
18 changes: 18 additions & 0 deletions templates/HorizontalStockList.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
</span>
</span>
{% endfor %}
{% for crypto in config.crypto %}
<span class="jast-stock">{{ crypto.name }}:
{% if utils.getStockChange(crypto) > 0 %}
{% set colorClass = "high" %}
{% elif utils.getStockChange(crypto) < 0 %}
{% set colorClass = "low " %}
{% else %}
{% set colorClass = "" %}
{% endif %}
<span class="{{ colorClass }}">
{{ utils.getCurrentValue(crypto, exchangeData) }}
{{ utils.getCurrency(crypto, exchangeData, config) }}
{% if colorClass %}
({{ utils.getStockChange(crypto) }}%)
{% endif %}
</span>
</span>
{% endfor %}
{% if config.showDepotGrowth %}
{% set depotGrowth = utils.getDepotGrowth(config, exchangeData) %}
{% if depotGrowth.value > 0 %}
Expand Down
4 changes: 2 additions & 2 deletions templates/VerticalScrollStyle.njk
Original file line number Diff line number Diff line change
@@ -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 %}
<style type="text/css">
Expand Down
18 changes: 18 additions & 0 deletions templates/VerticalStockList.njk
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
</span>
</li>
{% endfor %}
{% for crypto in config.crypto %}
<li class="jast-stock">{{ crypto.name }}:
{% if utils.getStockChange(crypto) > 0 %}
{% set colorClass = "high" %}
{% elif utils.getStockChange(crypto) < 0 %}
{% set colorClass = "low " %}
{% else %}
{% set colorClass = "" %}
{% endif %}
<span class="{{ colorClass }}">
{{ utils.getCurrentValue(crypto, exchangeData) }}
{{ utils.getCurrency(crypto, exchangeData, config) }}
{% if colorClass %}
({{ utils.getStockChange(crypto) }}%)
{% endif %}
</span>
</li>
{% endfor %}
{% if config.showDepotGrowth %}
{% set depotGrowth = utils.getDepotGrowth(config, exchangeData) %}
{% if depotGrowth.value > 0 %}
Expand Down

0 comments on commit 3cd8f7d

Please sign in to comment.