diff --git a/appinfo.json b/appinfo.json index 477ad12..33915c2 100644 --- a/appinfo.json +++ b/appinfo.json @@ -7,7 +7,7 @@ "configurable" ], "companyName": "WasabitLabs", - "longName": "Bitstamp BTC Price", + "longName": "BTC Price from different providers", "projectType": "native", "resources": { "media": [ @@ -22,7 +22,7 @@ "shortName": "Btc Simple", "uuid": "cea47b89-6378-47ca-b9f4-c3bb9de3286e", "versionCode": 3, - "versionLabel": "1.2", + "versionLabel": "1.3", "watchapp": { "watchface": false } diff --git a/src/js/pebble-js-app.js b/src/js/pebble-js-app.js index 3dba1f3..f3cdb70 100644 --- a/src/js/pebble-js-app.js +++ b/src/js/pebble-js-app.js @@ -1,8 +1,10 @@ +var DEFAULT_CONFIGURATION = {"provider": "bitstamp"}; + function fetchData() { var response; var req = new XMLHttpRequest(); - var provider = new Bitstamp(); + var provider = getProvider(); console.log(provider.url); req.open('GET', provider.url); @@ -35,44 +37,101 @@ function fetchData() { req.send(null); } -function BtcE(){ +function BtcE() { this.url = "https://btc-e.com/api/3/ticker/btc_usd"; this.name = "BTC-e"; - - this.data = function(response){ + this.data = function(response) { return {price: response.btc_usd.last, timestamp: response.btc_usd.updated}; }; } -function Bitstamp(){ +function Bitstamp() { this.url = "https://www.bitstamp.net/api/ticker/"; this.name = "Bitstamp"; - this.data = function(response) { return {price: response.last, timestamp: response.timestamp}; }; } +function Coindesk() { + this.url = "http://api.coindesk.com/v1/bpi/currentprice.json"; + this.name = "Coindesk"; + this.data = function(response) { + return {price: response.bpi.USD.rate_float, timestamp: Date.parse(response.time.updatedISO) / 1000}; + }; +} + function Bitfinex() { this.url = "https://api.bitfinex.com/v1/pubticker/btcusd"; this.name = "Bitfinex"; - this.data = function(response) { return {price: response.last_price, timestamp: response.timestamp}; }; } -Pebble.addEventListener("ready", - function(e) { - console.log("connect! " + e.ready); - fetchData(); - console.log(e.type); - }); - -Pebble.addEventListener("appmessage", - function(e) { - fetchData(); - console.log(e.type); - console.log(e.payload); - console.log("message!"); - }); +function getConfiguration() { + var configuration = localStorage.getItem("configuration"); + if (configuration) { + try { + return JSON.parse(configuration); + } catch(e) { + console.log("WARN: error parsing configuration from localstorage: " + e); + return DEFAULT_CONFIGURATION; + } + } else { + return DEFAULT_CONFIGURATION; + } +} + +function getProvider() { + var configuration = getConfiguration(); + var provider = configuration.provider; + var objProvider; + + console.log("Getting provider..."); + console.log(getConfiguration()); + console.log(provider); + + switch (provider) { + case "bitstamp": + objProvider = new Bitstamp(); + break; + case "btc_e": + objProvider = new BtcE(); + break; + case "coindesk": + objProvider = new Coindesk(); + break; + case "bitfinex": + objProvider = new Bitfinex(); + break; + default: + objProvider = new Bitstamp(); + } + + return objProvider; +} + +Pebble.addEventListener("ready", function(e) { + console.log("connect! " + e.ready); + fetchData(); + console.log(e.type); +}); + +Pebble.addEventListener("appmessage", function(e) { + fetchData(); + console.log(e.type); + console.log(e.payload); + console.log("message!"); +}); + +Pebble.addEventListener("showConfiguration", function(e) { + Pebble.openURL("http://wasabitlabs.com/en/btc-simple"); +}); + +Pebble.addEventListener("webviewclosed", function(e) { + var configuration = JSON.parse(decodeURIComponent(e.response)); + localStorage.setItem("configuration", JSON.stringify(configuration)); + console.log("Configuration window returned: " + JSON.stringify(configuration)); + fetchData(); +});