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 = [ |
0c85a7
|
9 |
'bus' => [ |
JK |
10 |
'gtfsrt' => 'ftp://ztp.krakow.pl/VehiclePositions_A.pb', |
|
11 |
'gtfsrt_file' => 'VehiclePositions_A.pb', |
4673cc
|
12 |
'ttss' => 'http://91.223.13.70/internetservice/geoserviceDispatcher/services/vehicleinfo/vehicles', |
JK |
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 { |
0c85a7
|
22 |
foreach(['gtfsrt_file', 'ttss_file', 'database', 'result'] as $field) { |
1a98b6
|
23 |
$source[$field] = __DIR__.'/data/'.$source[$field]; |
JK |
24 |
} |
|
25 |
$source['result_temp'] = $source['result'].'.tmp'; |
|
26 |
|
4673cc
|
27 |
$logger->info('Fetching '.$name.' position data from FTP...'); |
0c85a7
|
28 |
$updated = ftp_fetch_if_newer($source['gtfsrt'], $source['gtfsrt_file']); |
4673cc
|
29 |
if(!$updated) { |
JK |
30 |
$logger->info('Nothing to do, remote file not newer than local one'); |
|
31 |
continue; |
|
32 |
} |
|
33 |
|
0c85a7
|
34 |
$logger->info('Fetching '.$name.' position data from TTSS...'); |
JK |
35 |
fetch($source['ttss'], $source['ttss_file']); |
4673cc
|
36 |
|
JK |
37 |
$logger->info('Loading data...'); |
|
38 |
$mapper = new Mapper(); |
9afb6b
|
39 |
|
1a98b6
|
40 |
$mapper->loadTTSS($source['ttss_file']); |
9afb6b
|
41 |
$timeDifference = time() - $mapper->getTTSSDate(); |
JK |
42 |
if(abs($timeDifference) > 60) { |
|
43 |
throw new Exception('TTSS timestamp difference ('.$timeDifference.'s) is too high, aborting!'); |
|
44 |
} |
|
45 |
|
0c85a7
|
46 |
$mapper->loadGTFSRT($source['gtfsrt_file']); |
9afb6b
|
47 |
$timeDifference = time() - $mapper->getGTFSRTDate(); |
JK |
48 |
if(abs($timeDifference) > 60) { |
|
49 |
throw new Exception('GTFSRT timestamp difference ('.$timeDifference.'s) is too high, aborting!'); |
|
50 |
} |
1a98b6
|
51 |
|
JK |
52 |
$db = new Database($source['database']); |
4673cc
|
53 |
|
JK |
54 |
$logger->info('Finding correct offset...'); |
|
55 |
$offset = $mapper->findOffset(); |
1a98b6
|
56 |
if(!$offset) { |
JK |
57 |
throw new Exception('Offset not found'); |
4673cc
|
58 |
} |
1a98b6
|
59 |
|
JK |
60 |
$logger->info('Got offset '.$offset.', creating mapping...'); |
0c85a7
|
61 |
$mapping = $mapper->mapUsingOffset($offset); |
1a98b6
|
62 |
|
JK |
63 |
$logger->info('Checking the data for correctness...'); |
|
64 |
$weight = count($mapping); |
33182e
|
65 |
|
JK |
66 |
$correct = 0; |
|
67 |
$incorrect = 0; |
|
68 |
$old = 0; |
|
69 |
$maxWeight = 0; |
1a98b6
|
70 |
foreach($mapping as $id => $vehicle) { |
JK |
71 |
$dbVehicle = $db->getById($id); |
|
72 |
if($dbVehicle) { |
33182e
|
73 |
$maxWeight = max($maxWeight, $dbVehicle['weight']); |
JK |
74 |
if((int)substr($vehicle['num'], 2) == (int)$dbVehicle['num']) { |
|
75 |
$correct += 1; |
|
76 |
} else { |
|
77 |
$incorrect += 1; |
1a98b6
|
78 |
} |
JK |
79 |
continue; |
|
80 |
} |
|
81 |
|
|
82 |
$dbVehicle = $db->getByNum($vehicle['num']); |
|
83 |
if($dbVehicle && $dbVehicle['id'] != $id) { |
33182e
|
84 |
$old += 1; |
1a98b6
|
85 |
} |
JK |
86 |
} |
33182e
|
87 |
$logger->info('Weight: '.$weight.', correct: '.$correct.', incorrect: '.$incorrect.', old: '.$old); |
1a98b6
|
88 |
|
JK |
89 |
$previousMapping = NULL; |
33182e
|
90 |
if($incorrect > $correct && $maxWeight > $weight) { |
1a98b6
|
91 |
throw new Exception('Ignoring result due to better data already present'); |
5af1a7
|
92 |
} elseif($old > $correct) { |
33182e
|
93 |
$logger->warn('Replacing DB data with the new mapping'); |
1a98b6
|
94 |
$db->clear(); |
JK |
95 |
} else { |
|
96 |
$previousMapping = @json_decode(@file_get_contents($source['result']), TRUE); |
|
97 |
} |
|
98 |
|
|
99 |
$db->addMapping($mapping); |
|
100 |
|
|
101 |
if(is_array($previousMapping)) { |
|
102 |
$logger->info('Merging previous data with current mapping'); |
423dc9
|
103 |
$mapping = $mapping + $previousMapping; |
1a98b6
|
104 |
ksort($mapping); |
JK |
105 |
} |
|
106 |
|
|
107 |
$json = json_encode($mapping); |
|
108 |
if(!file_put_contents($source['result_temp'], $json)) { |
|
109 |
throw new Exception('Result save failed'); |
|
110 |
} |
|
111 |
rename($source['result_temp'], $source['result']); |
4673cc
|
112 |
$logger->info('Finished'); |
JK |
113 |
} catch(Throwable $e) { |
|
114 |
$logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]); |
|
115 |
} |
138f39
|
116 |
} |