diff --git a/api/nest/.credentials.json b/api/nest/.credentials.json index d90b917..c27efea 100644 --- a/api/nest/.credentials.json +++ b/api/nest/.credentials.json @@ -1,5 +1,4 @@ { "username": "NestUsername", - "password": "NestPassword", - "wolframAlphaApiKey": "WolframAlphaAPIKey" + "password": "NestPassword" } diff --git a/api/nest/index.js b/api/nest/index.js index 4898e62..ad0eac4 100644 --- a/api/nest/index.js +++ b/api/nest/index.js @@ -56,18 +56,41 @@ NestTask.prototype.setTemperature = function(temp) { }; NestTask.prototype.convertTemperature = function(temp, callback) { - var self = this; - var wolfram = require('wolfram-alpha') - var client = wolfram.createClient(self.credentials.wolframAlphaApiKey, null); - client.query(temp, function(err, results) { - if(err) { - throw err; + var words = temp.toLowerCase().split(' '), + placeCount = 0, + digits = 0, + noNumber = true, + negative = words[0] == 'negative' ? true : false, + numberNames = { + ones: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'], + tens: ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'], + large: ['thousand', 'million', 'billion', 'trillion', 'quadrillion'] // Number.MAX_VALUE is around 9 quadrillion + }; + for(var i = 0; i < words.length; i++) { + var ones = numberNames.ones.indexOf(words[i]), + tens = numberNames.tens.indexOf(words[i]), + large = numberNames.large.indexOf(words[i]); + if(ones > -1) + placeCount += ones; + if(tens > -1) + placeCount += 10*(tens+2); + if(words[i] == 'hundred') + placeCount *= 100; + if(large > -1) { + placeCount *= Math.pow(1000, large+1), + digits += placeCount, + placeCount = 0; } - // filter results to find input - var text = results[0].subpods[0].text; - var number = parseInt(text); - callback(number); - }); + noNumber = ones > -1 || tens > -1 || words[i] == 'hundred' || large > -1 ? false : true; + } + if(placeCount > 0 && placeCount < 1000) + digits += placeCount; + if(negative) + digits *= -1; + if(!noNumber) + callback(digits); + else + console.log('Couldn\'t understand temperature!'); }; NestTask.prototype.setHome = function() {