Jacek Kowalski
2021-07-30 812d3f5e104b33f68ce7bda06158040ba54975e4
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 {
9419f7 8         $logger->info('Fetching '.$name.' GTFS position data ...');
JK 9         $updated = Fetch::auto($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         
9419f7 15         $logger->info('Fetching '.$name.' TTSS position data...');
JK 16         Fetch::auto($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...');
413654 42         $mapping = $mapper->mapVehicleIdsUsingOffset($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         
413654 75         $output = new Output($db, $mapper, $source['vehicle_types']);
c077c7 76         
413654 77         $logger->info('Saving mapping...');
c077c7 78         
413654 79         $db->addMapping($mapping, $mapper);
1a98b6 80         
413654 81         $fullMapping = $output->createMapping($source);
c077c7 82         
JK 83         $logger->info('Creating vehicle list...');
84         
413654 85         $output->createVehiclesList($fullMapping, $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 }