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