-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll-infoconnect.js
198 lines (165 loc) · 6.3 KB
/
poll-infoconnect.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
exports.pollJourney = function(reqUrl, callback) {
var url = require('url');
var uuid = require('node-uuid');
var repo = require('./journey-repository.js');
var query = url.parse(reqUrl, true).query;
var ref = query["ref"];
if (!ref || ref == 'undefined') {
callback(new Error("/pollJourney expects ?ref=REF"));
return;
}
// get the data
getNztaData(ref, function(err, journey) {
if (err) {
callback(err);
return;
}
// check that this poll has not already been saved
checkHasJourneyBeenSaved(ref, journey.pollDateTime, function (error, saved) {
if (error) {
callback(error);
return;
}
if (saved) {
// Already an instance of this journey in storage. quit.
console.log("journey ref " + ref + " pollDateTime " + journey.pollDateTime + " has already been saved to storage.");
callback(null, journey);
return;
}
// save latest journey
journey.PartitionKey = ref;
journey.RowKey = uuid.v4();
repo.insert(journey, function (error) {
if (error) {
callback(error);
return;
}
// clean up journey history
deleteOldPollData(ref, journey.pollDateTime, function (error) {
if (error) {
callback(error);
return;
}
callback(null, journey);
});
});
});
});
};
// Checks if a journey with ref and pollDateTime already exists in storage. If so, returns true.
var checkHasJourneyBeenSaved = function (ref, pollDateTime, callback) {
var azure = require('azure');
var repo = require('./journey-repository.js');
var tableQuery = azure.TableQuery
.select()
.from('journey')
.where('PartitionKey eq ?', ref)
.and('pollDateTime eq ?', pollDateTime);
repo.get(tableQuery, function(error, entities) {
if (error) {
callback(error);
return;
}
callback(null, entities.length > 0);
});
}
var deleteOldPollData = function (ref, pollDateTime, callback) {
var azure = require('azure');
var repo = require('./journey-repository.js');
var secondsBetweenPolls = 300;
var numberOfPollsToKeep = 10;
// delete all journeys > 10 * secondsBetweenPolls
var oldestJourneyDateToKeep = new Date(pollDateTime);
oldestJourneyDateToKeep.setSeconds(oldestJourneyDateToKeep.getSeconds() - (secondsBetweenPolls * numberOfPollsToKeep));
var tableQuery = azure.TableQuery
.select()
.from('journey')
.where('PartitionKey eq ?', ref)
.and('pollDateTime lt ?', oldestJourneyDateToKeep);
repo.get(tableQuery, function(error, entities) {
if (error) {
callback(error);
return;
}
deleteEntities(repo, entities, function(error) {
if (error) {
callback(error);
return;
}
callback(null);
});
});
};
// recursively delete expired entities - good example of async loop problem.
var deleteEntities = function(repo, entities, callback) {
console.log("deleting " + entities.length + " journeys");
var entity = entities.pop();
if (entity == undefined) {
// no more
callback(null);
return;
}
console.log("deleting journey " + entity.PartitionKey + " " + entity.RowKey);
// delete the entity. The callback calls this function recursively.
repo.delete(entity, function (error) {
if (error) {
callback(error);
return;
}
deleteEntities(repo, entities, callback);
});
}
var getNztaData = function (ref, callback) {
var https = require('https');
var xml2js = require('xml2js');
// if no appsettings, then we must be debugging locally, mock out the data call and return stub data
if (process.env.NztaUsername === undefined) {
var fakeData = {
name: "This is a TEST - " + ref,
averageSpeed: Math.round(Math.random() * 50),
minutes: Math.round(Math.random() * 30),
pollDateTime: new Date(),
};
callback(null, fakeData);
return;
}
// request API data from NZTA
var nztaOptions = {
host: 'infoconnect1.highwayinfo.govt.nz',
port: 443,
path: '/ic/jbi/SsdfJourney2/REST/FeedService/journey/' + ref,
headers: {
"username": process.env.NztaUsername,
"password": process.env.NztaPassword
}
};
https.get(nztaOptions, function(nztaResponse) {
var nztaData = '';
nztaResponse.on('data', function(chunk) {
// chunk response from NZTA
nztaData += chunk;
var message = "" + chunk;
console.log('Received response of ' + message.length + ' bytes from nzta.');
});
nztaResponse.on('end', function() {
// final response, now process data
console.log('response end.');
xml2js.parseString(nztaData, function(err, result) {
// result = json
if (result == null || result["tns:findJourneyByReferenceResponse"] == null || result["tns:findJourneyByReferenceResponse"]["tns:return"] == null
|| result["tns:findJourneyByReferenceResponse"]["tns:return"].length == 0) {
callback(new Error("Call to NZTA returned no Data."));
return;
}
var data = {
name: result["tns:findJourneyByReferenceResponse"]["tns:return"][0]["tns:name"][0],
averageSpeed: new Number(result["tns:findJourneyByReferenceResponse"]["tns:return"][0]["tns:averageSpeed"][0]),
minutes: new Number(result["tns:findJourneyByReferenceResponse"]["tns:return"][0]["tns:lastEstimate"][0]),
pollDateTime: new Date(result["tns:findJourneyByReferenceResponse"]["tns:return"][0]["tns:lastEstimateTime"][0])
};
callback(null, data);
return;
});
});
});
}