Skip to content

Commit

Permalink
add crypto info component
Browse files Browse the repository at this point in the history
added a few comments as well
  • Loading branch information
JGrotex committed Dec 28, 2022
1 parent 8ecb01d commit 02d8d95
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ Local Weather display based on GPS longitude and latitude using a free service A
[more Details here](docs/weather-component.md)

### Crypto Currency Component
coming soon!
Displays currenet Crypto Currency exchange rates using a free service API from https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates#http-request

![](docs/img/crypto-info-component.png)

[more Details here](docs/crypto-info-component.md)

### more Components
soon more here ...
23 changes: 23 additions & 0 deletions docs/crypto-info-component.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Crypto Info Component

## Overview
Displays current Crypto Currency exchange rates.

![](img/crypto-info-component.png)

## Component Interface
This Crypto Component can be configured to use any target currency, in case nothing is specified the default is used. (USD)

<!-- Crypto Currency Info showcase -->
<a-entity crypto-info-component="currency:EUR" position="4 -2 -5"></a-entity>

## API
following REST API call is executed all ~15 minutes to refresh the currency status from this fee API (no API key needed)

https://api.coinbase.com/v2/exchange-rates?currency=USD

Sample Result:

{"data":{"currency":"EUR","rates":{"AED":"3.90788","AFN":"94.2399999999998372","ALL":"112.982","AMD":"418.7199999999963287" ....

> full Coinbase API Docs here: https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates#http-request
Binary file added docs/img/crypto-info-component.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<!-- Component loader area -->
<script src="js/components/clock-component.js"></script>
<script src="js/components/weather-component.js"></script>
<script src="js/components/crypto-info-component.js"></script>

<style>
a-scene {
Expand Down Expand Up @@ -70,6 +71,9 @@
<a-entity gltf-model="#ts" position="6 -2 -5"></a-entity>
<a-entity gltf-model="#tsrain" position="7 -2 -5"></a-entity>
-->

<!-- Crypto Currency Info showcase -->
<a-entity crypto-info-component="currency:EUR" position="4 -2 -5"></a-entity>

<!-- Lights -->
<a-entity light="type: ambient; intensity: 0.3;" position="0 3 0"></a-entity>
Expand Down
5 changes: 2 additions & 3 deletions src/js/components/clock-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ AFRAME.registerComponent('clock-component',{

init: function(){

var el = this.el;
var data = this.data;

this.createComponent();

},

// *** render Component
createComponent: function(){

this.timeEl = document.createElement('a-text');
Expand All @@ -30,6 +28,7 @@ AFRAME.registerComponent('clock-component',{

},

// *** TICKS
tick: function(){

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
Expand Down
112 changes: 112 additions & 0 deletions src/js/components/crypto-info-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
AFRAME.registerComponent('crypto-info-component',{
schema : {
currency : {default:'USD'}
},

init: function(){

this.currentTimestamp = new Date().getMinutes();

this.createComponent();
this.getCryptoRatesREST();

},

createComponent: function() {

this.lastupdate = document.createElement('a-text');
this.lastupdate.setAttribute('id','lastupdate');
this.lastupdate.setAttribute('text',{ color: '#fff', align: 'left', width: "3", value: "Status: " + new Date().toLocaleTimeString(), opacity:'0.8'});
this.lastupdate.object3D.position.set(0.2, -1.2, -0.1);
this.el.appendChild(this.lastupdate);

this.textEl_ETH = document.createElement('a-text');
this.textEl_ETH.setAttribute('id','ethtxt');
this.textEl_ETH.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Ethereum:", opacity:'1'});
this.textEl_ETH.object3D.position.set(0, 0.6, 0);
this.el.appendChild(this.textEl_ETH);

this.textEl_BTC = document.createElement('a-text');
this.textEl_BTC.setAttribute('id','btctxt');
this.textEl_BTC.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Bitcoin:", opacity:'1'});
this.textEl_BTC.object3D.position.set(0, 0.3, 0);
this.el.appendChild(this.textEl_BTC);

this.textEl_SOL = document.createElement('a-text');
this.textEl_SOL.setAttribute('id','soltxt');
this.textEl_SOL.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Solana:", opacity:'1'});
this.textEl_SOL.object3D.position.set(0, 0, 0);
this.el.appendChild(this.textEl_SOL);

this.textEl_MATIC = document.createElement('a-text');
this.textEl_MATIC.setAttribute('id','adatxt');
this.textEl_MATIC.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Polygon:", opacity:'1'});
this.textEl_MATIC.object3D.position.set(0, -0.3, 0);
this.el.appendChild(this.textEl_MATIC);

this.textEl_ADA = document.createElement('a-text');
this.textEl_ADA.setAttribute('id','adatxt');
this.textEl_ADA.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Cardano:", opacity:'1'});
this.textEl_ADA.object3D.position.set(0, -0.6, 0);
this.el.appendChild(this.textEl_ADA);

this.textEl_SKL = document.createElement('a-text');
this.textEl_SKL.setAttribute('id','adatxt');
this.textEl_SKL.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Skale:", opacity:'1'});
this.textEl_SKL.object3D.position.set(0, -0.9, 0);
this.el.appendChild(this.textEl_SKL);
},

// *** render Content
showCrypto: function(rates) {

// convert rates
var eth = Math.round(1/rates.ETH *100)/100;
var btc = Math.round(1/rates.BTC *100)/100;
var sol = Math.round(1/rates.SOL *100)/100;
var matic = Math.round(1/rates.MATIC *100)/100;
var ada = Math.round(1/rates.ADA *100)/100;
var skl = Math.round(1/rates.SKL *100)/100;

// show rates
this.textEl_ETH.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Ethereum: " + eth + " " + this.data.currency, opacity:'1'});
this.textEl_BTC.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Bitcoin: " + btc + " " + this.data.currency, opacity:'1'});
this.textEl_SOL.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Solana: " + sol + " " + this.data.currency, opacity:'1'});
this.textEl_MATIC.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Polygon: " + matic + " " + this.data.currency, opacity:'1'});
this.textEl_ADA.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Cardano: " + ada + " " + this.data.currency, opacity:'1'});
this.textEl_SKL.setAttribute('text',{ color: '#fff', align: 'left', width: "6", value: "Skale: " + skl + " " + this.data.currency, opacity:'1'});

this.lastupdate.setAttribute('text',{ color: '#fff', align: 'left', width: "3", value: "Status update: " + new Date().toLocaleTimeString(), opacity:'0.8'});
},

// *** load Crypto Details
getCryptoRatesREST: function() {
// get latest crypto rates via REST
// https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates#http-request
var requestOptions = {
method: 'GET',
redirect: 'follow'
};

fetch("https://api.coinbase.com/v2/exchange-rates?currency=" + this.data.currency, requestOptions)
.then(response => response.text())
.then(result => {
//console.log(JSON.parse( result ).data.rates);
this.showCrypto(JSON.parse( result ).data.rates);
})
.catch(error => console.log('error', error));
},

// *** TICKS
tick: function() {
var newTimestamp = new Date().getMinutes();
difference = Math.abs(this.currentTimestamp - newTimestamp);

if (difference > 15) {
console.log("tick")
this.currentTimestamp= new Date().getMinutes();
this.getCryptoRatesREST();
}

}
});
14 changes: 6 additions & 8 deletions src/js/components/weather-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ AFRAME.registerComponent('weather-component',{
},
init: function(){

var el = this.el;
var data = this.data;
//console.log("weather-component loaded!");
//console.log(data); //data.field
//console.log(el);

this.currentTimestamp = new Date().getMinutes();

this.createComponent();
this.getWeatherREST();

},

// *** render Component
createComponent: function () {

this.lastupdate = document.createElement('a-text');
Expand All @@ -41,6 +36,7 @@ AFRAME.registerComponent('weather-component',{

},

// *** update Details
showWeather: function(dataseries) {

var weathercontainer=this.weathercontainer;
Expand Down Expand Up @@ -100,6 +96,8 @@ AFRAME.registerComponent('weather-component',{
this.textEl_Status.setAttribute('text',{ color: '#fff', align: 'left', width: "4", value: "", opacity:'0.5'});
this.lastupdate.setAttribute('text',{ color: '#fff', align: 'left', width: "3", value: "Status update: " + new Date().toLocaleTimeString(), opacity:'0.8'});
},

// *** load Details
getWeatherREST: function() {
// get weather data via REST
// //https://www.7timer.info/bin/api.pl?lon=7.318&lat=51.819&product=civillight&output=json
Expand All @@ -117,19 +115,19 @@ AFRAME.registerComponent('weather-component',{
.catch(error => console.log('error', error));
},

// *** TICKS
tick: function() {

var newTimestamp = new Date().getMinutes();
difference = Math.abs(this.currentTimestamp - newTimestamp);

if (difference > 30) {
this.currentTimestamp= new Date().getMinutes();
this.getWeatherREST();
}

}
});

// *** common Helpers
function formatDate8digit(x) {
var day = x % 100;
var month = Math.floor(x % 10000 / 100);
Expand Down

0 comments on commit 02d8d95

Please sign in to comment.