Jacek Kowalski
2019-02-07 4673cc38e7b5b1b87d8e009137fc2d5eae688fd7
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',
13     ],
14 ];
138f39 15
4673cc 16 foreach($sources as $name => $source) {
JK 17     $logger = new Monolog\Logger('fetch_'.$name);
18     try {
19         $logger->info('Fetching '.$name.' position data from FTP...');
20         $updated = ftp_fetch_if_newer($source['gtfs'], __DIR__.'/data/'.$source['gtfs_file']);
21         if(!$updated) {
22             $logger->info('Nothing to do, remote file not newer than local one');
23             continue;
24         }
25         
26         $logger->info('Fetching '.$name.' positions from TTSS...');
27         fetch($source['ttss'], __DIR__.'/data/'.$source['ttss_file']);
28         
29         $logger->info('Loading data...');
30         $mapper = new Mapper();
31         $mapper->loadTTSS(__DIR__.'/data/'.$source['ttss_file']);
32         $mapper->loadGTFS(__DIR__.'/data/'.$source['gtfs_file']);
33         
34         $logger->info('Finding correct offset...');
35         $offset = $mapper->findOffset();
36         if($offset) {
37             $logger->info('Got offset '.$offset.', creating mapping...');
38             $mapping = $mapper->getMapping($offset);
39             echo json_encode($mapping);
40         }
41         $logger->info('Finished');
42     } catch(Throwable $e) {
43         $logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]);
44     }
138f39 45 }