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