Jacek Kowalski
2019-02-19 e654e6660d8413231787d2b98961cbf4fc4442c7
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 = [
JK 9     'buses' => [
10         'gtfs' => 'ftp://ztp.krakow.pl/VehiclePositions_A.pb',
11         'gtfs_file' => 'VehiclePositions_A.pb',
12         'ttss' => 'http://91.223.13.70/internetservice/geoserviceDispatcher/services/vehicleinfo/vehicles',
13         'ttss_file' => 'vehicles_A.json',
1a98b6 14         'database' => 'mapping_A.sqlite3',
914dc8 15         'result' => 'mapping_A.json',
4673cc 16     ],
JK 17 ];
138f39 18
4673cc 19 foreach($sources as $name => $source) {
JK 20     $logger = new Monolog\Logger('fetch_'.$name);
21     try {
1a98b6 22         foreach(['gtfs_file', 'ttss_file', 'database', 'result'] as $field) {
JK 23             $source[$field] = __DIR__.'/data/'.$source[$field];
24         }
25         $source['result_temp'] = $source['result'].'.tmp';
26         
4673cc 27         $logger->info('Fetching '.$name.' position data from FTP...');
1a98b6 28         $updated = ftp_fetch_if_newer($source['gtfs'], $source['gtfs_file']);
4673cc 29         if(!$updated) {
JK 30             $logger->info('Nothing to do, remote file not newer than local one');
31             continue;
32         }
33         
34         $logger->info('Fetching '.$name.' positions from TTSS...');
1a98b6 35         fetch($source['ttss'],$source['ttss_file']);
4673cc 36         
JK 37         $logger->info('Loading data...');
38         $mapper = new Mapper();
1a98b6 39         $mapper->loadTTSS($source['ttss_file']);
JK 40         $mapper->loadGTFS($source['gtfs_file']);
41         
42         $db = new Database($source['database']);
4673cc 43         
JK 44         $logger->info('Finding correct offset...');
45         $offset = $mapper->findOffset();
1a98b6 46         if(!$offset) {
JK 47             throw new Exception('Offset not found');
4673cc 48         }
1a98b6 49         
JK 50         $logger->info('Got offset '.$offset.', creating mapping...');
51         $mapping = $mapper->getMapping($offset);
52         
53         $logger->info('Checking the data for correctness...');
54         $weight = count($mapping);
55         $replace = 0;
56         $ignore = 0;
57         foreach($mapping as $id => $vehicle) {
58             $dbVehicle = $db->getById($id);
59             if($dbVehicle) {
60                 if((int)substr($vehicle['num'], 2) != (int)$dbVehicle['num']) {
61                     if($weight > $dbVehicle['weight']) {
62                         $replace += 1;
e654e6 63                         $logger->warn($vehicle['num'].' voting to replace '.$dbVehicle['num'].' (same ID: '.$id.')');
1a98b6 64                     } else {
JK 65                         $ignore += 1;
e654e6 66                         $logger->warn($vehicle['num'].' voting to ignore '.$dbVehicle['num'].' (same ID: '.$id.')');
1a98b6 67                     }
JK 68                 }
69                 continue;
70             }
71             
72             $dbVehicle = $db->getByNum($vehicle['num']);
73             if($dbVehicle && $dbVehicle['id'] != $id) {
74                 $replace += 1;
e654e6 75                 $logger->warn($vehicle['id'].' voting to replace '.$dbVehicle['id'].' (same num: '.$vehicle['num'].')');
1a98b6 76             }
JK 77         }
78         $logger->info('Weight: '.$weight.', ignore: '.$ignore.', replace: '.$replace);
79         
80         $previousMapping = NULL;
81         if($ignore > 0 && $ignore >= $replace) {
82             throw new Exception('Ignoring result due to better data already present');
83         } elseif($replace > 0) {
84             $logger->warn('Replacing DB data with the mapping');
85             $db->clear();
86         } else {
87             $previousMapping = @json_decode(@file_get_contents($source['result']), TRUE);
88         }
89         
90         $db->addMapping($mapping);
91         
92         if(is_array($previousMapping)) {
93             $logger->info('Merging previous data with current mapping');
94             $mapping = $previousMapping + $mapping;
95             ksort($mapping);
96         }
97         
98         $json = json_encode($mapping);
99         if(!file_put_contents($source['result_temp'], $json)) {
100             throw new Exception('Result save failed');
101         }
102         rename($source['result_temp'], $source['result']);
4673cc 103         $logger->info('Finished');
JK 104     } catch(Throwable $e) {
105         $logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]);
106     }
138f39 107 }