commit | author | age
|
138f39
|
1 |
<?php |
b62720
|
2 |
require_once(__DIR__.'/vendor/autoload.php'); |
4673cc
|
3 |
require_once(__DIR__.'/lib/fetch.php'); |
c077c7
|
4 |
require_once(__DIR__.'/lib/output.php'); |
7b7026
|
5 |
require_once(__DIR__.'/lib/vehicle_types.php'); |
b62720
|
6 |
require_once(__DIR__.'/config.php'); |
138f39
|
7 |
|
4673cc
|
8 |
foreach($sources as $name => $source) { |
JK |
9 |
$logger = new Monolog\Logger('fetch_'.$name); |
|
10 |
try { |
|
11 |
$logger->info('Fetching '.$name.' position data from FTP...'); |
0c85a7
|
12 |
$updated = ftp_fetch_if_newer($source['gtfsrt'], $source['gtfsrt_file']); |
4673cc
|
13 |
if(!$updated) { |
JK |
14 |
$logger->info('Nothing to do, remote file not newer than local one'); |
|
15 |
continue; |
|
16 |
} |
|
17 |
|
0c85a7
|
18 |
$logger->info('Fetching '.$name.' position data from TTSS...'); |
JK |
19 |
fetch($source['ttss'], $source['ttss_file']); |
4673cc
|
20 |
|
JK |
21 |
$logger->info('Loading data...'); |
|
22 |
$mapper = new Mapper(); |
9afb6b
|
23 |
|
1a98b6
|
24 |
$mapper->loadTTSS($source['ttss_file']); |
9afb6b
|
25 |
$timeDifference = time() - $mapper->getTTSSDate(); |
5b197e
|
26 |
if(abs($timeDifference) > 120) { |
9afb6b
|
27 |
throw new Exception('TTSS timestamp difference ('.$timeDifference.'s) is too high, aborting!'); |
JK |
28 |
} |
|
29 |
|
0c85a7
|
30 |
$mapper->loadGTFSRT($source['gtfsrt_file']); |
9afb6b
|
31 |
$timeDifference = time() - $mapper->getGTFSRTDate(); |
5b197e
|
32 |
if(abs($timeDifference) > 120) { |
9afb6b
|
33 |
throw new Exception('GTFSRT timestamp difference ('.$timeDifference.'s) is too high, aborting!'); |
JK |
34 |
} |
1a98b6
|
35 |
|
JK |
36 |
$db = new Database($source['database']); |
4673cc
|
37 |
|
JK |
38 |
$logger->info('Finding correct offset...'); |
|
39 |
$offset = $mapper->findOffset(); |
1a98b6
|
40 |
if(!$offset) { |
JK |
41 |
throw new Exception('Offset not found'); |
4673cc
|
42 |
} |
1a98b6
|
43 |
|
JK |
44 |
$logger->info('Got offset '.$offset.', creating mapping...'); |
7b7026
|
45 |
$mapping = $mapper->mapUsingOffset($offset); |
1a98b6
|
46 |
|
JK |
47 |
$logger->info('Checking the data for correctness...'); |
|
48 |
$weight = count($mapping); |
33182e
|
49 |
|
JK |
50 |
$correct = 0; |
|
51 |
$incorrect = 0; |
|
52 |
$old = 0; |
|
53 |
$maxWeight = 0; |
7b7026
|
54 |
foreach($mapping as $id => $num) { |
1a98b6
|
55 |
$dbVehicle = $db->getById($id); |
JK |
56 |
if($dbVehicle) { |
7b7026
|
57 |
$maxWeight = max($maxWeight, (int)$dbVehicle['weight']); |
JK |
58 |
if($num === $dbVehicle['num']) { |
33182e
|
59 |
$correct += 1; |
JK |
60 |
} else { |
|
61 |
$incorrect += 1; |
1a98b6
|
62 |
} |
JK |
63 |
continue; |
|
64 |
} |
|
65 |
|
7b7026
|
66 |
$dbVehicle = $db->getByNum($num); |
JK |
67 |
if($dbVehicle && $dbVehicle['id'] !== $id) { |
33182e
|
68 |
$old += 1; |
1a98b6
|
69 |
} |
JK |
70 |
} |
7b7026
|
71 |
|
33182e
|
72 |
$logger->info('Weight: '.$weight.', correct: '.$correct.', incorrect: '.$incorrect.', old: '.$old); |
1a98b6
|
73 |
|
33182e
|
74 |
if($incorrect > $correct && $maxWeight > $weight) { |
1a98b6
|
75 |
throw new Exception('Ignoring result due to better data already present'); |
JK |
76 |
} |
|
77 |
|
c077c7
|
78 |
|
JK |
79 |
$logger->info('Creating mapping...'); |
|
80 |
|
1a98b6
|
81 |
$db->addMapping($mapping); |
JK |
82 |
|
459fb4
|
83 |
$finalMapping = createMapping($db, $source['mapper'], $source); |
c077c7
|
84 |
|
JK |
85 |
|
|
86 |
$logger->info('Creating vehicle list...'); |
|
87 |
|
459fb4
|
88 |
createVehiclesList($mapper->getTTSSTrips(), $finalMapping, $source); |
c077c7
|
89 |
|
4673cc
|
90 |
$logger->info('Finished'); |
JK |
91 |
} catch(Throwable $e) { |
|
92 |
$logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]); |
|
93 |
} |
138f39
|
94 |
} |