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