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