1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Route4Me; |
4
|
|
|
|
5
|
|
|
use Route4Me\Common; |
6
|
|
|
use Route4Me\Address; |
7
|
|
|
use Route4Me\Exception\BadParam; |
8
|
|
|
use Route4Me\RouteParameters; |
9
|
|
|
use Route4Me\Route4Me; |
10
|
|
|
use GuzzleHttp\Client; |
11
|
|
|
|
12
|
|
|
class Route extends Common |
13
|
|
|
{ |
14
|
|
|
static public $apiUrl = '/api.v4/route.php'; |
15
|
|
|
static public $apiUrlAddress = '/api.v4/address.php'; |
16
|
|
|
static public $apiUrlDuplicate='/actions/duplicate_route.php'; |
17
|
|
|
static public $apiUrlDelete='/actions/delete_routes.php'; |
18
|
|
|
static public $apiUrlReseq='/api.v3/route/reoptimize_2.php'; |
19
|
|
|
static public $apiUrlMerge='/actions/merge_routes.php'; |
20
|
|
|
static public $apiUrlShare='/actions/route/share_route.php'; |
21
|
|
|
static public $apiUrlNoteFile='/actions/addRouteNotes.php'; |
22
|
|
|
static public $apiUrlAsset='/api.v4/status.php'; |
23
|
|
|
static public $apiUrlDeviceLocation='/api/track/get_device_location.php'; |
24
|
|
|
//static public $apiUrlMove='/actions/route/move_route_destination.php'; |
25
|
|
|
public $route_id; |
26
|
|
|
public $route_destination_id; |
27
|
|
|
public $optimization_problem_id; |
28
|
|
|
public $vehicle_alias; |
29
|
|
|
public $driver_alias; |
30
|
|
|
public $trip_distance; |
31
|
|
|
public $mpg; |
32
|
|
|
public $gas_price; |
33
|
|
|
public $route_duration_sec; |
34
|
|
|
public $parameters; |
35
|
|
|
public $addresses = array(); |
36
|
|
|
public $links = array(); |
37
|
|
|
public $directions = array(); |
38
|
|
|
public $path = array(); |
39
|
|
|
public $tracking_history = array(); |
40
|
|
|
public $recipient_email; |
41
|
|
|
public $httpheaders; |
42
|
|
|
public $is_unrouted; |
43
|
|
|
|
44
|
|
|
public $dev_lat; |
45
|
|
|
public $dev_lng; |
46
|
|
|
|
47
|
|
|
public static function fromArray(array $params) |
48
|
|
|
{ |
49
|
|
|
$route = new Route(); |
50
|
|
|
$route->route_id = Common::getValue($params, 'route_id'); |
51
|
|
|
$route->optimization_problem_id = Common::getValue($params, 'optimization_problem_id'); |
52
|
|
|
$route->vehicle_alias = Common::getValue($params, 'vehicle_alias'); |
53
|
|
|
$route->driver_alias = Common::getValue($params, 'driver_alias'); |
54
|
|
|
$route->trip_distance = Common::getValue($params, 'trip_distance'); |
55
|
|
|
$route->mpg = Common::getValue($params, 'mpg'); |
56
|
|
|
$route->gas_price = Common::getValue($params, 'gas_price'); |
57
|
|
|
$route->route_duration_sec = Common::getvalue($params, 'route_duration_sec'); |
58
|
|
|
$route->is_unrouted = Common::getvalue($params, 'is_unrouted'); |
59
|
|
|
|
60
|
|
|
// Make RouteParameters |
61
|
|
|
if (isset($params['parameters'])) { |
62
|
|
|
$route->parameters = RouteParameters::fromArray($params['parameters']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
if (isset($params['addresses'])) { |
|
|
|
|
66
|
|
|
$addresses = array(); |
67
|
|
|
foreach ($params['addresses'] as $address) { |
68
|
|
|
$addresses[] = Address::fromArray($address); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$route->addresses = $addresses; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$route->links = Common::getValue($params, 'links', array()); |
75
|
|
|
$route->directions = Common::getValue($params, 'directions', array()); |
76
|
|
|
$route->path = Common::getValue($params, 'path', array()); |
77
|
|
|
$route->tracking_history = Common::getValue($params, 'tracking_history', array()); |
78
|
|
|
|
79
|
|
|
return $route; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function getRoutes($routeId=null, $params=null) |
83
|
|
|
{ |
84
|
|
|
$query = array( |
85
|
|
|
'api_key' => Route4Me::getApiKey() |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
if ($routeId) { |
89
|
|
|
$query['route_id'] = implode(',', (array) $routeId); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($params) { |
93
|
|
|
if (isset($params['directions'])) { |
94
|
|
|
$query['directions'] = $params['directions']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (isset($params['route_path_output'])) { |
98
|
|
|
$query['route_path_output'] = $params['route_path_output']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (isset($params['device_tracking_history'])) { |
102
|
|
|
$query['device_tracking_history'] = $params['device_tracking_history']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($params['query'])) { |
106
|
|
|
$query['query'] = $params['query']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$query['limit'] = isset($params['limit']) ? $params['limit'] : 30; |
110
|
|
|
$query['offset'] = isset($params['offset']) ? $params['offset'] : 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$json = Route4Me::makeRequst(array( |
114
|
|
|
'url' => self::$apiUrl, |
115
|
|
|
'method' => 'GET', |
116
|
|
|
'query' => $query |
117
|
|
|
)); |
118
|
|
|
|
119
|
|
|
if ($routeId) { |
120
|
|
|
return Route::fromArray($json); die(""); |
|
|
|
|
121
|
|
|
} else { |
122
|
|
|
$routes = array(); |
123
|
|
|
foreach($json as $route) { |
124
|
|
|
$routes[] = Route::fromArray($route); |
125
|
|
|
} |
126
|
|
|
return $routes; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function getRoutePoints($routeId, $params) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$result = Route4Me::makeRequst(array( |
133
|
|
|
'url' => self::$apiUrl, |
134
|
|
|
'method' => 'GET', |
135
|
|
|
'query' => array( |
136
|
|
|
'api_key' => Route4Me::getApiKey(), |
137
|
|
|
'route_id' => $routeId, |
138
|
|
|
'route_path_output' => isset($params['route_path_output']) ? $params['route_path_output'] : null, |
139
|
|
|
'directions' => isset($params['directions']) ? $params['directions'] : null, |
140
|
|
|
) |
141
|
|
|
)); |
142
|
|
|
|
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function duplicateRoute($route_id) |
147
|
|
|
{ |
148
|
|
|
$result = Route4Me::makeRequst(array( |
149
|
|
|
'url' => self::$apiUrlDuplicate, |
150
|
|
|
'method' => 'GET', |
151
|
|
|
'query' => array( |
152
|
|
|
'api_key' => Route4Me::getApiKey(), |
153
|
|
|
'route_id' => $route_id, |
154
|
|
|
'to' => 'none', |
155
|
|
|
) |
156
|
|
|
)); |
157
|
|
|
|
158
|
|
|
return $result; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
public function resequenceRoute($params) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$result = Route4Me::makeRequst(array( |
164
|
|
|
'url' => self::$apiUrl, |
165
|
|
|
'method' => 'PUT', |
166
|
|
|
'query' => array( |
167
|
|
|
'api_key' => Route4Me::getApiKey(), |
168
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
169
|
|
|
'route_destination_id' => isset($params['route_destination_id']) ? $params['route_destination_id'] : null, |
170
|
|
|
), |
171
|
|
|
'body' => array( |
172
|
|
|
'addresses' => isset($params['addresses']) ? $params['addresses'] : null, |
173
|
|
|
) |
174
|
|
|
)); |
175
|
|
|
|
176
|
|
|
return $result; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
public function resequenceAllAddresses($params) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$result = Route4Me::makeRequst(array( |
182
|
|
|
'url' => self::$apiUrlReseq, |
183
|
|
|
'method' => 'GET', |
184
|
|
|
'query' => array( |
185
|
|
|
'api_key' => Route4Me::getApiKey(), |
186
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
187
|
|
|
'disable_optimization' => isset($params['disable_optimization']) ? $params['disable_optimization'] : null, |
188
|
|
|
'optimize' => isset($params['optimize']) ? $params['optimize'] : null, |
189
|
|
|
) |
190
|
|
|
)); |
191
|
|
|
|
192
|
|
|
return $result; |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function mergeRoutes($params) |
197
|
|
|
{ |
198
|
|
|
$result = Route4Me::makeRequst(array( |
199
|
|
|
'url' => self::$apiUrlMerge, |
200
|
|
|
'method' => 'POST', |
201
|
|
|
'query' => array( |
202
|
|
|
'api_key' => Route4Me::getApiKey(), |
203
|
|
|
), |
204
|
|
|
'body' => array( |
205
|
|
|
'route_ids' => isset($params['route_ids']) ? $params['route_ids'] : null, |
206
|
|
|
), |
207
|
|
|
'HTTPHEADER' => isset($this->httpheaders) ? $this->httpheaders : null, |
208
|
|
|
)); |
209
|
|
|
|
210
|
|
|
return $result; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
View Code Duplication |
public function shareRoute($params) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$result = Route4Me::makeRequst(array( |
216
|
|
|
'url' => self::$apiUrlShare, |
217
|
|
|
'method' => 'POST', |
218
|
|
|
'query' => array( |
219
|
|
|
'api_key' => Route4Me::getApiKey(), |
220
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
221
|
|
|
), |
222
|
|
|
'body' => array( |
223
|
|
|
'recipient_email' => isset($params['recipient_email']) ? $params['recipient_email'] : null, |
224
|
|
|
), |
225
|
|
|
'Content-Type' => 'multipart/form-data' |
226
|
|
|
)); |
227
|
|
|
|
228
|
|
|
return $result; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// Getting random route_id from existing routes between $offset and $offset+$limit |
232
|
|
|
public function getRandomRouteId($offset,$limit) |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$query['limit'] = isset($params['limit']) ? $params['limit'] : 30; |
|
|
|
|
235
|
|
|
$query['offset'] = isset($params['offset']) ? $params['offset'] : 0; |
236
|
|
|
|
237
|
|
|
$json = Route4Me::makeRequst(array( |
238
|
|
|
'url' => self::$apiUrl, |
239
|
|
|
'method' => 'GET', |
240
|
|
|
'query' => $query |
241
|
|
|
)); |
242
|
|
|
|
243
|
|
|
if (sizeof($json)>0) { |
244
|
|
|
$routes = array(); |
245
|
|
|
foreach($json as $route) { |
246
|
|
|
$routes[] = Route::fromArray($route); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$num=rand(0,sizeof($routes)-1); |
250
|
|
|
$rRoute=(array)$routes[$num]; |
251
|
|
|
|
252
|
|
|
if (is_array($rRoute)) |
253
|
|
|
{ |
254
|
|
|
return $rRoute["route_id"]; |
255
|
|
|
} |
256
|
|
|
else return null; |
257
|
|
|
} else return null; |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
View Code Duplication |
public function update() |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$route = Route4Me::makeRequst(array( |
265
|
|
|
'url' => self::$apiUrl, |
266
|
|
|
'method' => 'PUT', |
267
|
|
|
'query' => array( |
268
|
|
|
'route_id' => isset($this->route_id) ? $this->route_id : null, |
269
|
|
|
'route_destination_id' => isset($this->route_destination_id) ? $this->route_destination_id : null, |
270
|
|
|
), |
271
|
|
|
'body' => array ( |
272
|
|
|
'parameters' => $this->parameters, |
273
|
|
|
), |
274
|
|
|
'HTTPHEADER' => isset($this->httpheaders) ? $this->httpheaders : null, |
275
|
|
|
)); |
276
|
|
|
|
277
|
|
|
return Route::fromArray($route); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
View Code Duplication |
public function updateAddress() |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
$result = Route4Me::makeRequst(array( |
283
|
|
|
'url' => self::$apiUrlAddress, |
284
|
|
|
'method' => 'PUT', |
285
|
|
|
'query' => array( |
286
|
|
|
'route_id' => isset($this->route_id) ? $this->route_id : null, |
287
|
|
|
'route_destination_id' => isset($this->route_destination_id) ? $this->route_destination_id : null, |
288
|
|
|
), |
289
|
|
|
'body' => get_object_vars($this->parameters), |
290
|
|
|
'HTTPHEADER' => isset($this->httpheaders) ? $this->httpheaders : null, |
291
|
|
|
)); |
292
|
|
|
|
293
|
|
|
return $result; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function addAddresses(array $params) |
297
|
|
|
{ |
298
|
|
|
$route = Route4Me::makeRequst(array( |
299
|
|
|
'url' => self::$apiUrl, |
300
|
|
|
'method' => 'PUT', |
301
|
|
|
'query' => array( |
302
|
|
|
'api_key' => Route4Me::getApiKey(), |
303
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
304
|
|
|
'addresses' => isset($params['addresses']) ? $params['addresses'] : null |
305
|
|
|
) |
306
|
|
|
)); |
307
|
|
|
|
308
|
|
|
return Route::fromArray($route); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
View Code Duplication |
public function insertAddressOptimalPosition(array $params) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$route = Route4Me::makeRequst(array( |
314
|
|
|
'url' => self::$apiUrl, |
315
|
|
|
'method' => 'PUT', |
316
|
|
|
'query' => array( |
317
|
|
|
'api_key' => Route4Me::getApiKey(), |
318
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
319
|
|
|
), |
320
|
|
|
'body' => array( |
321
|
|
|
'addresses' => isset($params['addresses']) ? $params['addresses'] : null, |
322
|
|
|
'optimal_position' => isset($params['optimal_position']) ? $params['optimal_position'] : null, |
323
|
|
|
) |
324
|
|
|
)); |
325
|
|
|
|
326
|
|
|
return Route::fromArray($route); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function addNoteFile($params) |
330
|
|
|
{ |
331
|
|
|
$fname = isset($params['strFilename']) ? $params['strFilename'] : null; |
332
|
|
|
$rpath = realpath($fname); |
333
|
|
|
//echo $rpath;die(""); |
334
|
|
|
$result= Route4Me::makeRequst(array( |
335
|
|
|
'url' => self::$apiUrlNoteFile, |
336
|
|
|
'method' => 'POST', |
337
|
|
|
'query' => array( |
338
|
|
|
'api_key' => Route4Me::getApiKey(), |
339
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
340
|
|
|
'address_id' => isset($params['address_id']) ? $params['address_id'] : null, |
341
|
|
|
'dev_lat' => isset($params['dev_lat']) ? $params['dev_lat'] : null, |
342
|
|
|
'dev_lng' => isset($params['dev_lng']) ? $params['dev_lng'] : null, |
343
|
|
|
'device_type' => isset($params['device_type']) ? $params['device_type'] : null, |
344
|
|
|
'dev_lng' => isset($params['dev_lng']) ? $params['dev_lng'] : null, |
345
|
|
|
), |
346
|
|
|
'body' => array( |
347
|
|
|
'strUpdateType' => isset($params['strUpdateType']) ? $params['strUpdateType'] : null, |
348
|
|
|
'strFilename' => isset($params['strFilename']) ? $params['strFilename'] : null, |
349
|
|
|
'strNoteContents' => isset($params['strNoteContents']) ? $params['strNoteContents'] : null, |
350
|
|
|
), |
351
|
|
|
'FILE' => $rpath, |
352
|
|
|
|
353
|
|
|
'HTTPHEADER' => array( |
354
|
|
|
'Content-Type: application/x-www-form-urlencoded' |
355
|
|
|
) |
356
|
|
|
)); |
357
|
|
|
|
358
|
|
|
return $result; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
View Code Duplication |
public function delete($route_id) |
|
|
|
|
362
|
|
|
{ |
363
|
|
|
$result = Route4Me::makeRequst(array( |
364
|
|
|
'url' => self::$apiUrl, |
365
|
|
|
'method' => 'DELETE', |
366
|
|
|
'query' => array( 'route_id' => $route_id ) |
367
|
|
|
)); |
368
|
|
|
|
369
|
|
|
// The code below doesn't work, altough this method is described as workable in REST API |
370
|
|
|
/* |
371
|
|
|
$result = Route4Me::makeRequst(array( |
372
|
|
|
'url' => self::$apiUrlDelete, |
373
|
|
|
'method' => 'GET', |
374
|
|
|
'query' => array( |
375
|
|
|
'api_key' => Route4Me::getApiKey(), |
376
|
|
|
'route_id' => $route_id, |
377
|
|
|
) |
378
|
|
|
)); |
379
|
|
|
*/ |
380
|
|
|
return $result; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function GetAddressesFromRoute($route_id) |
384
|
|
|
{ |
385
|
|
|
$route1=Route::getRoutes($route_id,null); |
386
|
|
|
if (isset($route1)) { |
387
|
|
|
return $route1->addresses(); |
|
|
|
|
388
|
|
|
} else { return null;} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function GetRandomAddressFromRoute($route_id) |
392
|
|
|
{ |
393
|
|
|
$route1=Route::getRoutes($route_id,null); |
394
|
|
|
|
395
|
|
|
if (isset($route1)) { |
396
|
|
|
$addresses=$route1->addresses; |
397
|
|
|
|
398
|
|
|
$rnd=rand(0,sizeof($addresses)-1); |
399
|
|
|
|
400
|
|
|
return $addresses[$rnd]; |
401
|
|
|
|
402
|
|
|
} else { return null;} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
public function getRouteId() |
406
|
|
|
{ |
407
|
|
|
return $this->route_id; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function getOptimizationId() |
411
|
|
|
{ |
412
|
|
|
return $this->optimization_problem_id; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
public function GetLastLocation(array $params) |
416
|
|
|
{ |
417
|
|
|
$route = Route4Me::makeRequst(array( |
418
|
|
|
'url' => self::$apiUrl, |
419
|
|
|
'method' => 'GET', |
420
|
|
|
'query' => array( |
421
|
|
|
'api_key' => Route4Me::getApiKey(), |
422
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
423
|
|
|
'device_tracking_history' => isset($params['device_tracking_history']) ? $params['device_tracking_history'] : null |
424
|
|
|
) |
425
|
|
|
)); |
426
|
|
|
|
427
|
|
|
return Route::fromArray($route); |
428
|
|
|
|
429
|
|
|
} |
430
|
|
|
|
431
|
|
View Code Duplication |
public function GetTrackingHistoryFromTimeRange(array $params) |
|
|
|
|
432
|
|
|
{ |
433
|
|
|
$route = Route4Me::makeRequst(array( |
434
|
|
|
'url' => self::$apiUrlDeviceLocation, |
435
|
|
|
'method' => 'GET', |
436
|
|
|
'query' => array( |
437
|
|
|
'api_key' => Route4Me::getApiKey(), |
438
|
|
|
'route_id' => isset($params['route_id']) ? $params['route_id'] : null, |
439
|
|
|
'format' => isset($params['format']) ? $params['format'] : null, |
440
|
|
|
'time_period' => isset($params['time_period']) ? $params['time_period'] : null, |
441
|
|
|
'start_date' => isset($params['start_date']) ? $params['start_date'] : null, |
442
|
|
|
'end_date' => isset($params['end_date']) ? $params['end_date'] : null |
443
|
|
|
) |
444
|
|
|
)); |
445
|
|
|
|
446
|
|
|
return $route; |
447
|
|
|
|
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
public function GetAssetTracking(array $params) |
451
|
|
|
{ |
452
|
|
|
$route = Route4Me::makeRequst(array( |
453
|
|
|
'url' => self::$apiUrlAsset, |
454
|
|
|
'method' => 'GET', |
455
|
|
|
'query' => array( |
456
|
|
|
'api_key' => Route4Me::getApiKey(), |
457
|
|
|
'tracking' => isset($params['tracking']) ? $params['tracking'] : null |
458
|
|
|
) |
459
|
|
|
)); |
460
|
|
|
|
461
|
|
|
return $route; |
462
|
|
|
|
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.