Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1 Fix - Removed the need for Wolfram Alpha #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions api/nest/.credentials.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"username": "NestUsername",
"password": "NestPassword",
"wolframAlphaApiKey": "WolframAlphaAPIKey"
"password": "NestPassword"
}
45 changes: 34 additions & 11 deletions api/nest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down