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