Jacek Kowalski
2019-06-17 9970fbd17d1a67efad67f3dafaa07730f83210b5
commit | author | age
138f39 1 <?php
1a98b6 2 require_once(__DIR__.'/lib/database.php');
4673cc 3 require_once(__DIR__.'/lib/fetch.php');
JK 4 require_once(__DIR__.'/lib/mapper.php');
138f39 5
4673cc 6 $logger = new Monolog\Logger('Parse changes');
138f39 7
4673cc 8 $sources = [
0c85a7 9     'bus' => [
JK 10         'gtfsrt' => 'ftp://ztp.krakow.pl/VehiclePositions_A.pb',
11         'gtfsrt_file' => 'VehiclePositions_A.pb',
a2010c 12         'ttss' => 'http://ttss.mpk.krakow.pl/internetservice/geoserviceDispatcher/services/vehicleinfo/vehicles',
4673cc 13         'ttss_file' => 'vehicles_A.json',
1a98b6 14         'database' => 'mapping_A.sqlite3',
914dc8 15         'result' => 'mapping_A.json',
9970fb 16         'mapper' => 'numToTypeB',
4673cc 17     ],
JK 18 ];
138f39 19
4673cc 20 foreach($sources as $name => $source) {
JK 21     $logger = new Monolog\Logger('fetch_'.$name);
22     try {
0c85a7 23         foreach(['gtfsrt_file', 'ttss_file', 'database', 'result'] as $field) {
1a98b6 24             $source[$field] = __DIR__.'/data/'.$source[$field];
JK 25         }
26         $source['result_temp'] = $source['result'].'.tmp';
27         
4673cc 28         $logger->info('Fetching '.$name.' position data from FTP...');
0c85a7 29         $updated = ftp_fetch_if_newer($source['gtfsrt'], $source['gtfsrt_file']);
4673cc 30         if(!$updated) {
JK 31             $logger->info('Nothing to do, remote file not newer than local one');
32             continue;
33         }
34         
0c85a7 35         $logger->info('Fetching '.$name.' position data from TTSS...');
JK 36         fetch($source['ttss'], $source['ttss_file']);
4673cc 37         
JK 38         $logger->info('Loading data...');
39         $mapper = new Mapper();
9afb6b 40         
1a98b6 41         $mapper->loadTTSS($source['ttss_file']);
9afb6b 42         $timeDifference = time() - $mapper->getTTSSDate();
JK 43         if(abs($timeDifference) > 60) {
44             throw new Exception('TTSS timestamp difference ('.$timeDifference.'s) is too high, aborting!');
45         }
46         
0c85a7 47         $mapper->loadGTFSRT($source['gtfsrt_file']);
9afb6b 48         $timeDifference = time() - $mapper->getGTFSRTDate();
JK 49         if(abs($timeDifference) > 60) {
50             throw new Exception('GTFSRT timestamp difference ('.$timeDifference.'s) is too high, aborting!');
51         }
1a98b6 52         
JK 53         $db = new Database($source['database']);
4673cc 54         
JK 55         $logger->info('Finding correct offset...');
56         $offset = $mapper->findOffset();
1a98b6 57         if(!$offset) {
JK 58             throw new Exception('Offset not found');
4673cc 59         }
1a98b6 60         
JK 61         $logger->info('Got offset '.$offset.', creating mapping...');
9970fb 62         $mapping = $mapper->mapUsingOffset($offset, $source['mapper']);
1a98b6 63         
JK 64         $logger->info('Checking the data for correctness...');
65         $weight = count($mapping);
33182e 66         
JK 67         $correct = 0;
68         $incorrect = 0;
69         $old = 0;
70         $maxWeight = 0;
1a98b6 71         foreach($mapping as $id => $vehicle) {
JK 72             $dbVehicle = $db->getById($id);
73             if($dbVehicle) {
33182e 74                 $maxWeight = max($maxWeight, $dbVehicle['weight']);
JK 75                 if((int)substr($vehicle['num'], 2) == (int)$dbVehicle['num']) {
76                     $correct += 1;
77                 } else {
78                     $incorrect += 1;
1a98b6 79                 }
JK 80                 continue;
81             }
82             
83             $dbVehicle = $db->getByNum($vehicle['num']);
84             if($dbVehicle && $dbVehicle['id'] != $id) {
33182e 85                 $old += 1;
1a98b6 86             }
JK 87         }
33182e 88         $logger->info('Weight: '.$weight.', correct: '.$correct.', incorrect: '.$incorrect.', old: '.$old);
1a98b6 89         
JK 90         $previousMapping = NULL;
33182e 91         if($incorrect > $correct && $maxWeight > $weight) {
1a98b6 92             throw new Exception('Ignoring result due to better data already present');
5af1a7 93         } elseif($old > $correct) {
33182e 94             $logger->warn('Replacing DB data with the new mapping');
1a98b6 95             $db->clear();
JK 96         } else {
97             $previousMapping = @json_decode(@file_get_contents($source['result']), TRUE);
98         }
99         
100         $db->addMapping($mapping);
101         
102         if(is_array($previousMapping)) {
103             $logger->info('Merging previous data with current mapping');
423dc9 104             $mapping = $mapping + $previousMapping;
1a98b6 105             ksort($mapping);
JK 106         }
107         
108         $json = json_encode($mapping);
109         if(!file_put_contents($source['result_temp'], $json)) {
110             throw new Exception('Result save failed');
111         }
112         rename($source['result_temp'], $source['result']);
4673cc 113         $logger->info('Finished');
JK 114     } catch(Throwable $e) {
115         $logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]);
116     }
138f39 117 }