Jacek Kowalski
2019-02-20 64994ed50bf27f21ec4a17c241f72bc333ed1afe
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);
33182e 55         
JK 56         $correct = 0;
57         $incorrect = 0;
58         $old = 0;
59         $maxWeight = 0;
1a98b6 60         foreach($mapping as $id => $vehicle) {
JK 61             $dbVehicle = $db->getById($id);
62             if($dbVehicle) {
33182e 63                 $maxWeight = max($maxWeight, $dbVehicle['weight']);
JK 64                 if((int)substr($vehicle['num'], 2) == (int)$dbVehicle['num']) {
65                     $correct += 1;
66                 } else {
67                     $incorrect += 1;
1a98b6 68                 }
JK 69                 continue;
70             }
71             
72             $dbVehicle = $db->getByNum($vehicle['num']);
73             if($dbVehicle && $dbVehicle['id'] != $id) {
33182e 74                 $old += 1;
1a98b6 75             }
JK 76         }
33182e 77         $logger->info('Weight: '.$weight.', correct: '.$correct.', incorrect: '.$incorrect.', old: '.$old);
1a98b6 78         
JK 79         $previousMapping = NULL;
33182e 80         if($incorrect > $correct && $maxWeight > $weight) {
1a98b6 81             throw new Exception('Ignoring result due to better data already present');
5af1a7 82         } elseif($old > $correct) {
33182e 83             $logger->warn('Replacing DB data with the new mapping');
1a98b6 84             $db->clear();
JK 85         } else {
86             $previousMapping = @json_decode(@file_get_contents($source['result']), TRUE);
87         }
88         
89         $db->addMapping($mapping);
90         
91         if(is_array($previousMapping)) {
92             $logger->info('Merging previous data with current mapping');
423dc9 93             $mapping = $mapping + $previousMapping;
1a98b6 94             ksort($mapping);
JK 95         }
96         
97         $json = json_encode($mapping);
98         if(!file_put_contents($source['result_temp'], $json)) {
99             throw new Exception('Result save failed');
100         }
101         rename($source['result_temp'], $source['result']);
4673cc 102         $logger->info('Finished');
JK 103     } catch(Throwable $e) {
104         $logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]);
105     }
138f39 106 }