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