-
Notifications
You must be signed in to change notification settings - Fork 1
/
traffic.js
41 lines (37 loc) · 1.32 KB
/
traffic.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
// Traffic Functionality
var trafficSetup = function(deviceClient) {
// Self Referentiation
var self = this;
// Variables for Limiting the Quantity of Traffic Based Information Responses
var timesGetTrafficCalled = 0;
self.trafficIntervalID = 0;
// Function to publish traffic information to IOT Device
self.getTraffic = function(request, response)
{
// Disconnect from Device after N iterations
if (timesGetTrafficCalled > 3)
{
clearInterval(self.trafficIntervalID);
timesGetTrafficCalled = 0;
}
else
{
var locationString = "";
//According to bing api documentation schema is (South Latitude, West Longitude, North Latitude, East Longitude)
locationString += response.lat2 + "," + response.long1 + "," + response.lat1 + "," + response.long2;
console.log("TRAFFIC!!!");
var trafficURL = "http://dev.virtualearth.net/REST/v1/Traffic/Incidents/"+locationString+"?key=AuYVzEN-Wy-0UMikqHd2SdOvKFWunzHF6NMTf3k3sc_2yeL95PecboET7X3QmLPv"
request.get(trafficURL, {
json: true
},
function (error, response, body) {
// Note: for just traffic data use body.resourceSets
// New api key is required
console.log("traffic: " + JSON.stringify(body));
deviceClient.publish("status", "json", JSON.stringify(body));
});
}
++timesGetTrafficCalled;
}
}
module.exports = trafficSetup;