Jacek Kowalski
2019-02-08 914dc87c49752c602d05e171efe6fd438164ee94
commit | author | age
138f39 1 <?php
4673cc 2 require_once(__DIR__.'/lib/fetch.php');
JK 3 require_once(__DIR__.'/lib/mapper.php');
138f39 4
4673cc 5 $logger = new Monolog\Logger('Parse changes');
138f39 6
4673cc 7 $sources = [
JK 8     'buses' => [
9         'gtfs' => 'ftp://ztp.krakow.pl/VehiclePositions_A.pb',
10         'gtfs_file' => 'VehiclePositions_A.pb',
11         'ttss' => 'http://91.223.13.70/internetservice/geoserviceDispatcher/services/vehicleinfo/vehicles',
12         'ttss_file' => 'vehicles_A.json',
914dc8 13         'result' => 'mapping_A.json',
4673cc 14     ],
JK 15 ];
138f39 16
4673cc 17 foreach($sources as $name => $source) {
JK 18     $logger = new Monolog\Logger('fetch_'.$name);
19     try {
20         $logger->info('Fetching '.$name.' position data from FTP...');
21         $updated = ftp_fetch_if_newer($source['gtfs'], __DIR__.'/data/'.$source['gtfs_file']);
22         if(!$updated) {
23             $logger->info('Nothing to do, remote file not newer than local one');
24             continue;
25         }
26         
27         $logger->info('Fetching '.$name.' positions from TTSS...');
28         fetch($source['ttss'], __DIR__.'/data/'.$source['ttss_file']);
29         
30         $logger->info('Loading data...');
31         $mapper = new Mapper();
32         $mapper->loadTTSS(__DIR__.'/data/'.$source['ttss_file']);
33         $mapper->loadGTFS(__DIR__.'/data/'.$source['gtfs_file']);
34         
35         $logger->info('Finding correct offset...');
36         $offset = $mapper->findOffset();
37         if($offset) {
38             $logger->info('Got offset '.$offset.', creating mapping...');
39             $mapping = $mapper->getMapping($offset);
914dc8 40             $json = json_encode($mapping);
JK 41             if(!file_put_contents(__DIR__.'/data/'.$source['result'].'.tmp', $json)) {
42                 throw new Exception('Result save failed');
43             }
44             rename(__DIR__.'/data/'.$source['result'].'.tmp', __DIR__.'/data/'.$source['result']);
4673cc 45         }
JK 46         $logger->info('Finished');
47     } catch(Throwable $e) {
48         $logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]);
49     }
138f39 50 }