-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
203 lines (169 loc) · 6.07 KB
/
app.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
/*var express = require('express');
var request = require('request');
var Twitter = require('twitter');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
var parser = require('json-parser');
var Client = require("ibmiotf");
var config = {
"org": "t82anh",
"id": "MyGeoTweets",
"domain": "internetofthings.ibmcloud.com",
"type": "GeoTweets",
"auth-method": "token",
"auth-token": "F0uO@W!BGM8vHbo0RL"
};
var deviceClient = new Client.IotfDevice(config);
deviceClient.connect();
var twitterClient = new Twitter({
consumer_key: 'uyGpIwSq2QmEjok8qsvAtwCW0',
consumer_secret: 'P1Jona5WwUE54WF8LjQHE8ZJa2zK7wKmLGTj8WPZyGHqeMhxHl',
access_token_key: '132337921-mWzwmPzlpWS9m5B7pw7T3VIWIgkPQQYUVkuGJqTG',
access_token_secret: 'waoDLEZrg14hV2dox0FwTUs4Qv5C3ReajWOtVnlLVigUM'
});
// create a new express server
var app = express();
var timesGetWeatherAndTweetsCalled = 0;
var weatherIntervalID;
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// When the device connects
deviceClient.on("connect", function() {
console.log("Device connected!");
});
// When the device receives an error
deviceClient.on("error", function(err) {
console.log("Error! - " + err);
});
app.get('/process_get', function(req, res)
{
timesGetWeatherAndTweetsCalled = 0;
// Prepare output in JSON format
response = {
lat1: req.query.latitude1,
long1: req.query.longitude1,
lat2: req.query.latitude2,
long2: req.query.longitude2
};
var locationString = "";
locationString += response.lat1 + "," + response.long1 + "," + response.lat2 + "," + response.long2;
intervalID = setInterval(function() {
getWeatherAndTweets(locationString);
}, 10000);
// res.setHeader("Content-Type", "text/html");
// res.end("<form action='https://jpeterkdemoapp.mybluemix.net/process_get' method='GET'>" +
// "Latitude: <input type='text' name='latitude' /><br />" +
// "Longitude: <input type='text' name='longitude' /><br />" +
// "<input type='submit' text='submit' />" +
// "</form>" +
// "<p id='blankSpace'>" + JSON.stringify(body.forecasts) + "</p>");
});
function getWeatherAndTweets(locationString)
{
if (timesGetWeatherAndTweetsCalled >= 5)
{
clearInterval(intervalID);
deviceClient.disconnect();
}
else
{
var callURL = "https://3024c902-44c2-4a90-a124-2fcf95f7a555:[email protected]/api/weather/v1/geocode/" + response.lat1 + "/" + response.long1 + "/forecast/hourly/48hour.json?units=m&language=en-US";
request.get(callURL, {
json: true
},
function (error, response, body) {
console.log("forecast: " + body.forecasts);
deviceClient.publish("status", "json", JSON.stringify(body.forecasts));
});
var stream = twitterClient.stream("statuses/filter", { locations: locationString });
stream.on("data", function(event) {
console.log(event && event.text);
deviceClient.publish("status", "json", '{"d": {"text": ' + event.text + '}}');
});
}
timesGetWeatherAndTweetsCalled++;
}
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});*/
// App Dependencies
var Client = require("ibmiotf");
var express = require('express');
var request = require('request');
var cfenv = require('cfenv');
// IOT Device Configuration and Connection
var config = {
"org": process.env.IOT_ORG,
"id": process.env.IOT_ID,
"domain": process.env.IOT_DOMAIN,
"type": process.env.IOT_TYPE,
"auth-method": process.env.IOT_AUTHMETHOD,
"auth-token": process.env.IOT_AUTHTOKEN
};
/*"org": "t82anh",
"id": "MyGeoTweets",
"domain": "internetofthings.ibmcloud.com",
"type": "GeoTweets",
"auth-method": "token",
"auth-token": "F0uO@W!BGM8vHbo0RL"*/
var deviceClient = new Client.IotfDevice(config);
deviceClient.connect();
// When the device connects
deviceClient.on("connect", function() {
console.log("Device connected!");
});
// When the device receives an error
deviceClient.on("error", function(err) {
console.log("Error! - " + err);
});
// Weather Dependencies and Instance
var weatherVar = require('./weather.js');
var weatherVarInstance = new weatherVar(deviceClient);
var twitterVar = require('./twitter.js');
var twitterVarInstance = new twitterVar(deviceClient);
var trafficVar = require('./traffic.js');
var trafficVarInstance = new trafficVar(deviceClient);
// Create a new express server
var app = express();
// Serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// Get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
app.get('/process_get', function(req, res)
{
// Prepare output in JSON format
response = {
lat1: req.query.latitude1,
long1: req.query.longitude1,
lat2: req.query.latitude2,
long2: req.query.longitude2
};
// Perform Twitter Functionality every N milliseconds
twitterVarInstance.twitterIntervalID = setInterval(function() {
twitterVarInstance.getTwitter(request, response);
}, 10000);
//Perform Weather Functionality every N milliseconds
weatherVarInstance.weatherIntervalID = setInterval(function() {
weatherVarInstance.getWeather(request, response);
}, 10000);
// Perform Traffic Functionality every N milliseconds
trafficVarInstance.trafficIntervalID = setInterval(function() {
trafficVarInstance.getTraffic(request, response);
}, 10000);
});
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});