This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
257 lines (241 loc) · 10.1 KB
/
index.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* RoutEasy Example Project v.1.0
* Last updated: 09/29/2016
*/
var req = require('request');
var async = require('async');
var config = require('./config');
var request = req.defaults(
{
json: true,
jar: true
});
async.waterfall(
[
/**
* 1. LOGIN
*
* Routeasy currently supports plain-text credentials over HTTP.
*
*/
function(callback)
{
request(
{
json: true,
url: config.host + '/auth/signin',
method: 'POST',
body: config.credentials
}, function(err, response)
{
console.log('1. user display name: ' + response.body.displayName);
callback();
});
},
/**
* 2. VEHICLE LISTING
*
*/
function(callback)
{
request(
{
url: config.host + '/vehicles',
method: 'GET'
}, function(err, response)
{
if(err) { console.error(err); }
console.log('2. first vehicle name: ' + response.body[0].name);
callback();
});
},
/**
* 3. DEPOTS LISTING
*
*/
function(callback)
{
request(
{
url: config.host + '/depots',
method: 'GET'
}, function(err, response)
{
if(err) { console.error(err); }
console.log('3. first depot name: ' + response.body[0].name);
callback();
});
},
/**
* 4. GEOCODING
*
* Routeasy offers a handy endpoint for geocoding batch deliveries
*
*/
function(callback)
{
request(
{
url: config.host + '/geoinfo/geocode',
method: 'POST',
body: require('./samples/deliveries_geocoding')
}, function(err, response)
{
if(err) { console.error(err); }
console.log('4. first delivery latitude: ' + response.body[0].address.geocode.lat);
callback(null, response.body);
});
},
// ---------------------- ORDER CREATION IS NOW DEPRECATED ----------------------
/**
* 5. ORDER CREATION
*
* Before creating a routing project, one must create an order (delivery set)
function(callback)
{
request(
{
url: config.host + '/orders',
method: 'POST',
body: require('./samples/new_order')
}, function(err, response)
{
if(err) { console.error(err); }
console.log('5. order name: ' + response.body.name);
callback();
});
},
*/
// ---------------------- ORDER CREATION IS NOW DEPRECATED ----------------------
/**
* 6. ROUTING CREATION AND EXECUTION
*
* Currently the API client must send raw data into the 'data' member of the new routing
*
*/
function(deliveries, callback)
{
var vehicle, depot;
var createEntity = function(url, src, handle) { return function(callback) { request({url: config.host + url, method: 'POST', body: require(src)}, function(err, response) { handle(response.body); callback() }); }; };
async.series(
[
createEntity('/vehicles', './samples/new_routing_vehicle', function(v) { vehicle = v; }),
createEntity('/depots', './samples/new_routing_depot', function(d)
{
/* RoutEasy Core Update: Depots must have window daily restriction defined */
d.constraints = { window_daily: { start_time: new Date(1970,1,1,8,0,0), end_time: new Date(1970,1,1,20,0,0) } } ;
depot = d;
}),
], function(err)
{
var routing = require('./samples/new_routing');
routing.data.order = {};
routing.data.order.deliveries = deliveries;
routing.data.vehicles = [vehicle];
routing.data.depots = [depot];
routing.vehicles = [vehicle._id];
routing.depots = [depot._id];
request(
{
url: config.host + '/routings',
method: 'POST',
body: routing
}, function(err, response)
{
if(err) { console.error(err); }
var r = response.body;
console.log('6a. routing name: ' + r.name);
console.log('6b. routing vehicle name: ' + r.data.vehicles[0].name);
console.log('6c. routing depot name: ' + r.data.depots[0].name);
request(
{
url: config.host + '/routings/' + r._id + '/versions/starred',
method: 'GET',
body: routing
}, function(err, response)
{
if(err) { console.error(err); }
var v = response.body;
console.log('6e. routing starred version: ' + v._id);
request(
{
url: config.host + '/versions/' + v._id + '/go',
method: 'POST',
}, function(err, response)
{
if(err) { console.error(err); }
var v = response.body;
var update = function()
{
request(config.host + '/versions/' + v._id, function(err, res)
{
if(err) { console.log(err); }
v = res.body;
if(['terminated', 'completed'].indexOf(v.state.status) >= 0)
{
if(v.state.status === 'terminated')
console.log('\tfinished with ERROR! check: ' + v._id + ' | ' + JSON.stringify(v.state));
else
console.log('\tfinished with SUCCESS!');
callback();
return;
}
console.log('\t' + v.state.completion.toFixed(0) + '% completed');
setTimeout(update, 500);
});
};
update();
});
});
});
});
},
/**
* 7. ROUTINGS LISTING
*
*/
function(callback)
{
request(
{
url: config.host + '/routings',
method: 'GET'
}, function(err, response)
{
if(err) { console.error(err); }
console.log('7. first routing name: ' + response.body[0].name);
callback();
});
},
/**
* 8. ROUTING VERSIONS LISTING
*
*/
function(callback)
{
request(
{
url: config.host + '/routings',
method: 'GET'
}, function(err, response)
{
if(err) { console.error(err); }
response.body.sort(function(a, b) { return a.date < b.date ? 1 : -1; });
var lastRoutingId = response.body[0]._id;
console.log('8a. last routing id: ' + response.body[0]._id);
console.log('8a. last routing name: ' + response.body[0].name);
request(
{
url: config.host + '/routings/' + lastRoutingId + '/versions',
method: 'GET'
}, function(err, response)
{
if(err) { console.error(err); }
response.body.sort(function(a, b) { return a.date < b.date ? 1 : -1; });
console.log('8c. last version of ' + lastRoutingId + ' id: ' + response.body[0]._id);
console.log('8d. last version of ' + lastRoutingId + ' distance: ' + response.body[0].summary.results_total_distance);
callback();
});
});
},
]);