forked from webofthings/wot-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmashupserver.js
62 lines (50 loc) · 1.88 KB
/
mashupserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var http = require("http"),
request = require('request'),
fs = require('fs');
var location = 'London, GB';
var piRootUrl = 'http://localhost:3000/pi/';
var name = 'Dom'
http.createServer(function(webReq, webResp){
// 1) Get Yahoo Weather
request({
url: prepareYahooWeatherUrl(location),
json: true}, function (err, resp, yahooResult) {
if (!err && resp.statusCode == 200) {
console.log(yahooResult);
var localTemp = yahooResult.query.results.channel.item.condition.temp;
console.log('Local @ ' + location + ': ' + localTemp);
// 2) Call the Pi
request({
url: piRootUrl + 'sensors/temperature',
json: true}, function (err, resp, piResult) {
if (!err && resp.statusCode == 200) {
console.log(piResult);
var piTemp = piResult.value;
console.log('Pi @ London: ' + piTemp);
// 3) Compare and log
var message = prepareMessage(name, location, localTemp, piTemp);
fs.appendFile('log.txt', message, encoding='utf8', function (err) {
if (err) throw err;
webResp.writeHeader(200, {"Content-Type": "text/plain"});
webResp.write(message);
webResp.end();
});
}
});
}
});
}).listen(8080);
function prepareYahooWeatherUrl(location) {
return "https://query.yahooapis.com/v1/public/yql?q=select item from weather.forecast where woeid in (select woeid from geo.places(1) where text='" + location + "') and u='c'&format=json";
}
function prepareMessage(name, location, localTemp, piTemp) {
var diff = localTemp - piTemp;
var qualifier = ' higher ';
if(diff < 0) {
qualifier = ' lower ';
}
var result = 'Hello I\'m ' + name + ' from ' + location;
result += ' my local temperature is ' + Math.abs(diff) + ' degrees';
result += qualifier + 'than yours!';
return result;
}