Completed
Push — master ( 5ca2a0...946f73 )
by Yannick
11:51
created

TrackerImport::add()   F

Complexity

Conditions 201
Paths 0

Size

Total Lines 370
Code Lines 261

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 201
eloc 261
nc 0
nop 1
dl 0
loc 370
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
require_once(dirname(__FILE__).'/class.Connection.php');
3
require_once(dirname(__FILE__).'/class.Tracker.php');
4
require_once(dirname(__FILE__).'/class.TrackerLive.php');
5
require_once(dirname(__FILE__).'/class.TrackerArchive.php');
6
require_once(dirname(__FILE__).'/class.Stats.php');
7
require_once(dirname(__FILE__).'/class.Source.php');
8
9
class TrackerImport {
10
    private $all_tracked = array();
11
    private $last_delete_hourly = 0;
12
    private $last_delete = 0;
13
    private $stats = array();
14
    private $tmd = 0;
15
    private $source_location = array();
16
    public $db = null;
17
    public $nb = 0;
18
19
    public function __construct($dbc = null) {
20
	global $globalBeta;
21
	$Connection = new Connection($dbc);
22
	$this->db = $Connection->db();
23
	date_default_timezone_set('UTC');
24
25
	// Get previous source stats
26
	/*
27
	$Stats = new Stats($dbc);
28
	$currentdate = date('Y-m-d');
29
	$sourcestat = $Stats->getStatsSource($currentdate);
30
	if (!empty($sourcestat)) {
31
	    foreach($sourcestat as $srcst) {
32
	    	$type = $srcst['stats_type'];
33
		if ($type == 'polar' || $type == 'hist') {
34
		    $source = $srcst['source_name'];
35
		    $data = $srcst['source_data'];
36
		    $this->stats[$currentdate][$source][$type] = json_decode($data,true);
37
	        }
38
	    }
39
	}
40
	*/
41
    }
42
43
    public function checkAll() {
44
	global $globalDebug;
45
	if ($globalDebug) echo "Update last seen tracked data...\n";
46
	foreach ($this->all_tracked as $key => $flight) {
47
	    if (isset($this->all_tracked[$key]['id'])) {
48
		//echo $this->all_tracked[$key]['id'].' - '.$this->all_tracked[$key]['latitude'].'  '.$this->all_tracked[$key]['longitude']."\n";
49
    		$Tracker = new Tracker($this->db);
50
        	$Tracker->updateLatestTrackerData($this->all_tracked[$key]['id'],$this->all_tracked[$key]['ident'],$this->all_tracked[$key]['latitude'],$this->all_tracked[$key]['longitude'],$this->all_tracked[$key]['altitude'],$this->all_tracked[$key]['speed'],$this->all_tracked[$key]['datetime']);
51
            }
52
	}
53
    }
54
55
    public function del() {
56
	global $globalDebug;
57
	// Delete old infos
58
	if ($globalDebug) echo 'Delete old values and update latest data...'."\n";
59
	foreach ($this->all_tracked as $key => $flight) {
60
    	    if (isset($flight['lastupdate'])) {
61
        	if ($flight['lastupdate'] < (time()-3000)) {
62
            	    if (isset($this->all_tracked[$key]['id'])) {
63
            		if ($globalDebug) echo "--- Delete old values with id ".$this->all_tracked[$key]['id']."\n";
64
			/*
65
			$TrackerLive = new TrackerLive();
66
            		$TrackerLive->deleteLiveTrackerDataById($this->all_tracked[$key]['id']);
67
			$TrackerLive->db = null;
68
			*/
69
            		//$real_arrival = $this->arrival($key);
70
            		$Tracker = new Tracker($this->db);
71
            		if ($this->all_tracked[$key]['latitude'] != '' && $this->all_tracked[$key]['longitude'] != '') {
72
				$result = $Tracker->updateLatestTrackerData($this->all_tracked[$key]['id'],$this->all_tracked[$key]['ident'],$this->all_tracked[$key]['latitude'],$this->all_tracked[$key]['longitude'],$this->all_tracked[$key]['altitude'],$this->all_tracked[$key]['speed']);
73
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
74
			}
75
			// Put in archive
76
//			$Tracker->db = null;
77
            	    }
78
            	    unset($this->all_tracked[$key]);
79
    	        }
80
	    }
81
        }
82
    }
83
84
    public function add($line) {
85
	global $globalFork, $globalDistanceIgnore, $globalDaemon, $globalDebug, $globalCoordMinChange, $globalDebugTimeElapsed, $globalCenterLatitude, $globalCenterLongitude, $globalBeta, $globalSourcesupdate, $globalAllTracked;
86
	if (!isset($globalCoordMinChange) || $globalCoordMinChange == '') $globalCoordMinChange = '0.02';
87
	date_default_timezone_set('UTC');
88
	$dataFound = false;
89
	$send = false;
90
	
91
	// SBS format is CSV format
92
	if(is_array($line) && isset($line['ident'])) {
93
	    //print_r($line);
94
  	    if (isset($line['ident'])) {
95
96
		
97
		// Increment message number
98
		if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE) {
99
		    $current_date = date('Y-m-d');
100
		    if (isset($line['source_name'])) $source = $line['source_name'];
101
		    else $source = '';
102
		    if ($source == '' || $line['format_source'] == 'aprs') $source = $line['format_source'];
103
		    if (!isset($this->stats[$current_date][$source]['msg'])) {
104
		    	$this->stats[$current_date][$source]['msg']['date'] = time();
105
		    	$this->stats[$current_date][$source]['msg']['nb'] = 1;
106
		    } else $this->stats[$current_date][$source]['msg']['nb'] += 1;
107
		}
108
		
109
		
110
		$Common = new Common();
111
	        if (!isset($line['id'])) $id = trim($line['ident']);
112
	        else $id = trim($line['id']);
113
		
114
		if (!isset($this->all_tracked[$id])) {
115
		    $this->all_tracked[$id] = array();
116
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('addedTracker' => 0));
117
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('ident' => '','latitude' => '', 'longitude' => '', 'speed' => '', 'altitude' => '', 'heading' => '', 'format_source' => '','source_name' => '','comment'=> '','type' => '','noarchive' => false,'putinarchive' => true,'over_country' => ''));
118
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('lastupdate' => time()));
119
		    if (!isset($line['id'])) {
120
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
121
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $id.'-'.date('YmdHi')));
122
		     } else $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $line['id']));
123
		    if ($globalAllTracked !== FALSE) $dataFound = true;
124
		}
125
		
126
		if (isset($line['datetime']) && strtotime($line['datetime']) > time()-20*60 && strtotime($line['datetime']) < time()+20*60) {
127
		    if (!isset($this->all_tracked[$id]['datetime']) || strtotime($line['datetime']) >= strtotime($this->all_tracked[$id]['datetime'])) {
128
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => $line['datetime']));
129
		    } else {
130
				if (strtotime($line['datetime']) == strtotime($this->all_tracked[$id]['datetime']) && $globalDebug) echo "!!! Date is the same as previous data for ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."\n";
131
				elseif (strtotime($line['datetime']) > strtotime($this->all_tracked[$id]['datetime']) && $globalDebug) echo "!!! Date previous latest data (".$line['datetime']." > ".$this->all_tracked[$id]['datetime'].") !!! for ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."\n";
132
				return '';
133
		    }
134
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) < time()-20*60) {
135
			if ($globalDebug) echo "!!! Date is too old ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."!!!";
136
			return '';
137
		} elseif (isset($line['datetime']) && strtotime($line['datetime']) > time()+20*60) {
138
			if ($globalDebug) echo "!!! Date is in the future ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."!!!";
139
			return '';
140
		} elseif (!isset($line['datetime'])) {
141
			date_default_timezone_set('UTC');
142
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => date('Y-m-d H:i:s')));
143
		} else {
144
			if ($globalDebug) echo "!!! Unknow date error ".$this->all_tracked[$id]['ident']." - format : ".$line['format_source']."!!!";
145
			return '';
146
		}
147
		
148
		//if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_tracked[$id]['ident'] != trim($line['ident'])) && preg_match('/^[a-zA-Z0-9-]+$/', $line['ident'])) {
149
		if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_tracked[$id]['ident'] != trim($line['ident']))) {
150
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('ident' => trim($line['ident'])));
151
		    if ($this->all_tracked[$id]['addedTracker'] == 1) {
152
			$timeelapsed = microtime(true);
153
            		$Tracker = new Tracker($this->db);
154
            		$fromsource = NULL;
155
            		$result = $Tracker->updateIdentTrackerData($this->all_tracked[$id]['id'],$this->all_tracked[$id]['ident'],$fromsource);
156
			if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
157
			$Tracker->db = null;
158
			if ($globalDebugTimeElapsed) echo 'Time elapsed for update identspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
159
		    }
160
		    if (!isset($this->all_tracked[$id]['id'])) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $this->all_tracked[$id]['ident']));
161
		}
162
163
		if (isset($line['speed']) && $line['speed'] != '') {
164
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($line['speed'])));
165
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed_fromsrc' => true));
166
		} else if (!isset($this->all_tracked[$id]['speed_fromsrc']) && isset($this->all_tracked[$id]['time_last_coord']) && $this->all_tracked[$id]['time_last_coord'] != time() && isset($line['latitude']) && isset($line['longitude'])) {
167
		    $distance = $Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m');
168
		    if ($distance > 100 && $distance < 10000) {
169
			$speed = $distance/(time() - $this->all_tracked[$id]['time_last_coord']);
170
			$speed = $speed*3.6;
171
			if ($speed < 1000) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($speed)));
172
  			if ($globalDebug) echo "ø Calculated Speed for ".$this->all_tracked[$id]['ident']." : ".$speed." - distance : ".$distance."\n";
173
		    }
174
		}
175
176
	        if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '' && is_numeric($line['latitude']) && is_numeric($line['longitude'])) {
177
	    	    if (isset($this->all_tracked[$id]['time_last_coord'])) $timediff = round(time()-$this->all_tracked[$id]['time_last_coord']);
178
	    	    else unset($timediff);
179
	    	    if ($this->tmd > 5 || !isset($timediff) || $timediff > 100 || ($timediff > 30 && isset($this->all_tracked[$id]['latitude']) && isset($this->all_tracked[$id]['longitude']) && $Common->withinThreshold($timediff,$Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')))) {
180
			if (isset($this->all_tracked[$id]['archive_latitude']) && isset($this->all_tracked[$id]['archive_longitude']) && isset($this->all_tracked[$id]['livedb_latitude']) && isset($this->all_tracked[$id]['livedb_longitude'])) {
181
			    if (!$Common->checkLine($this->all_tracked[$id]['archive_latitude'],$this->all_tracked[$id]['archive_longitude'],$this->all_tracked[$id]['livedb_latitude'],$this->all_tracked[$id]['livedb_longitude'],$line['latitude'],$line['longitude'])) {
182
				$this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
183
				$this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
184
				$this->all_tracked[$id]['putinarchive'] = true;
185
				
186
				if ($globalDebug) echo "\n".' ------- Check Country for '.$this->all_tracked[$id]['ident'].' with latitude : '.$line['latitude'].' and longitude : '.$line['longitude'].'.... ';
187
				$timeelapsed = microtime(true);
188
				$Tracker = new Tracker($this->db);
189
				$all_country = $Tracker->getCountryFromLatitudeLongitude($line['latitude'],$line['longitude']);
190
				if (!empty($all_country)) $this->all_tracked[$id]['over_country'] = $all_country['iso2'];
191
				$Tracker->db = null;
192
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update getCountryFromlatitudeLongitude : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
193
				$this->tmd = 0;
194
				if ($globalDebug) echo 'FOUND : '.$this->all_tracked[$id]['over_country'].' ---------------'."\n";
195
			    }
196
			}
197
198
			if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
199
				if (!isset($this->all_tracked[$id]['archive_latitude'])) $this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
200
				if (!isset($this->all_tracked[$id]['livedb_latitude']) || abs($this->all_tracked[$id]['livedb_latitude']-$line['latitude']) > $globalCoordMinChange || $this->all_tracked[$id]['format_source'] == 'aprs') {
201
				    $this->all_tracked[$id]['livedb_latitude'] = $line['latitude'];
202
				    $dataFound = true;
203
				    $this->all_tracked[$id]['time_last_coord'] = time();
204
				}
205
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('latitude' => $line['latitude']));
206
			}
207
			if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
208
			    if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
209
				if (!isset($this->all_tracked[$id]['archive_longitude'])) $this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
210
				if (!isset($this->all_tracked[$id]['livedb_longitude']) || abs($this->all_tracked[$id]['livedb_longitude']-$line['longitude']) > $globalCoordMinChange || $this->all_tracked[$id]['format_source'] == 'aprs') {
211
				    $this->all_tracked[$id]['livedb_longitude'] = $line['longitude'];
212
				    $dataFound = true;
213
				    $this->all_tracked[$id]['time_last_coord'] = time();
214
				}
215
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('longitude' => $line['longitude']));
216
			}
217
218
		    } else if ($globalDebug && $timediff > 20) {
219
			$this->tmd = $this->tmd + 1;
220
			echo '!!! Too much distance in short time... for '.$this->all_tracked[$id]['ident']."\n";
221
			echo 'Time : '.$timediff.'s - Distance : '.$Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')."m -";
222
			echo 'Speed : '.(($Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')/$timediff)*3.6)." km/h - ";
223
			echo 'Lat : '.$line['latitude'].' - long : '.$line['longitude'].' - prev lat : '.$this->all_tracked[$id]['latitude'].' - prev long : '.$this->all_tracked[$id]['longitude']." \n";
224
		    }
225
		}
226
		if (isset($line['last_update']) && $line['last_update'] != '') {
227
		    if (isset($this->all_tracked[$id]['last_update']) && $this->all_tracked[$id]['last_update'] != $line['last_update']) $dataFound = true;
228
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('last_update' => $line['last_update']));
229
		}
230
		if (isset($line['format_source']) && $line['format_source'] != '') {
231
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('format_source' => $line['format_source']));
232
		}
233
		if (isset($line['source_name']) && $line['source_name'] != '') {
234
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('source_name' => $line['source_name']));
235
		}
236
		if (isset($line['comment']) && $line['comment'] != '') {
237
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('comment' => $line['comment']));
238
		    //$dataFound = true;
239
		}
240
		if (isset($line['type']) && $line['type'] != '') {
241
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('type' => $line['type']));
242
		    //$dataFound = true;
243
		}
244
245
		if (isset($line['altitude']) && $line['altitude'] != '') {
246
		    //if (!isset($this->all_tracked[$id]['altitude']) || $this->all_tracked[$id]['altitude'] == '' || ($this->all_tracked[$id]['altitude'] > 0 && $line['altitude'] != 0)) {
247
			if (is_int($this->all_tracked[$id]['altitude']) && abs(round($line['altitude']/100)-$this->all_tracked[$id]['altitude']) > 3) $this->all_tracked[$id]['putinarchive'] = true;
248
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('altitude' => round($line['altitude']/100)));
249
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('altitude_real' => $line['altitude']));
250
			//$dataFound = true;
251
		    //} elseif ($globalDebug) echo "!!! Strange altitude data... not added.\n";
252
  		}
253
254
		if (isset($line['noarchive']) && $line['noarchive'] === true) {
255
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('noarchive' => true));
256
		}
257
		
258
		if (isset($line['heading']) && $line['heading'] != '') {
259
		    if (is_int($this->all_tracked[$id]['heading']) && abs($this->all_tracked[$id]['heading']-round($line['heading'])) > 10) $this->all_tracked[$id]['putinarchive'] = true;
260
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($line['heading'])));
261
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading_fromsrc' => true));
262
		    //$dataFound = true;
263
  		} elseif (!isset($this->all_tracked[$id]['heading_fromsrc']) && isset($this->all_tracked[$id]['archive_latitude']) && $this->all_tracked[$id]['archive_latitude'] != $this->all_tracked[$id]['latitude'] && isset($this->all_tracked[$id]['archive_longitude']) && $this->all_tracked[$id]['archive_longitude'] != $this->all_tracked[$id]['longitude']) {
264
  		    $heading = $Common->getHeading($this->all_tracked[$id]['archive_latitude'],$this->all_tracked[$id]['archive_longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
265
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($heading)));
266
		    if (abs($this->all_tracked[$id]['heading']-round($heading)) > 10) $this->all_tracked[$id]['putinarchive'] = true;
267
  		    if ($globalDebug) echo "ø Calculated Heading for ".$this->all_tracked[$id]['ident']." : ".$heading."\n";
268
  		}
269
		//if (isset($globalSourcesupdate) && $globalSourcesupdate != '' && isset($this->all_tracked[$id]['lastupdate']) && time()-$this->all_tracked[$id]['lastupdate'] < $globalSourcesupdate) $dataFound = false;
270
271
		if ($dataFound === true && isset($this->all_tracked[$id]['ident'])) {
272
		    $this->all_tracked[$id]['lastupdate'] = time();
273
		    if ($this->all_tracked[$id]['addedTracker'] == 0) {
274
		        if (!isset($globalDistanceIgnore['latitude']) || $this->all_tracked[$id]['longitude'] == ''  || $this->all_tracked[$id]['latitude'] == '' || (isset($globalDistanceIgnore['latitude']) && $Common->distance($this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude']) < $globalDistanceIgnore['distance'])) {
275
			    if (!isset($this->all_tracked[$id]['forcenew']) || $this->all_tracked[$id]['forcenew'] == 0) {
276
				if ($globalDebug) echo "Check if aircraft is already in DB...";
277
				$timeelapsed = microtime(true);
278
				$TrackerLive = new TrackerLive($this->db);
279
				if (isset($line['id'])) {
280
				    $recent_ident = $TrackerLive->checkIdRecent($line['id']);
281
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
282
				} elseif (isset($this->all_tracked[$id]['ident']) && $this->all_tracked[$id]['ident'] != '') {
283
				    $recent_ident = $TrackerLive->checkIdentRecent($this->all_tracked[$id]['ident']);
284
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
285
				} else $recent_ident = '';
286
				$TrackerLive->db=null;
287
288
				if ($globalDebug && $recent_ident == '') echo " Not in DB.\n";
289
				elseif ($globalDebug && $recent_ident != '') echo " Already in DB.\n";
290
			    } else {
291
				$recent_ident = '';
292
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('forcenew' => 0));
293
			    }
294
			    //if there was no aircraft with the same callsign within the last hour and go post it into the archive
295
			    if($recent_ident == "")
296
			    {
297
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['ident']." in archive DB : ";
298
				//adds the spotter data for the archive
299
				    $highlight = '';
0 ignored issues
show
Unused Code introduced by
$highlight is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
300
				    if (!isset($this->all_tracked[$id]['id'])) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $this->all_tracked[$id]['ident'].'-'.date('YmdHi')));
301
				    $timeelapsed = microtime(true);
302
				    $Tracker = new Tracker($this->db);
303
				    $result = $Tracker->addTrackerData($this->all_tracked[$id]['id'], $this->all_tracked[$id]['ident'], $this->all_tracked[$id]['latitude'], $this->all_tracked[$id]['longitude'], $this->all_tracked[$id]['altitude'], $this->all_tracked[$id]['heading'], $this->all_tracked[$id]['speed'], $this->all_tracked[$id]['datetime'], $this->all_tracked[$id]['comment'],$this->all_tracked[$id]['type'],$this->all_tracked[$id]['format_source'],$this->all_tracked[$id]['source_name']);
304
				    $Tracker->db = null;
305
				    if ($globalDebug && isset($result)) echo $result."\n";
306
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update addspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
307
				    
308
				    
309
				    // Add source stat in DB
310
				    $Stats = new Stats($this->db);
311
				    if (!empty($this->stats)) {
312
					if ($globalDebug) echo 'Add source stats : ';
313
				        foreach($this->stats as $date => $data) {
314
					    foreach($data as $source => $sourced) {
315
					        //print_r($sourced);
316
				    	        if (isset($sourced['polar'])) echo $Stats->addStatSource(json_encode($sourced['polar']),$source,'polar_tracker',$date);
317
				    	        if (isset($sourced['hist'])) echo $Stats->addStatSource(json_encode($sourced['hist']),$source,'hist_tracker',$date);
318
				    		if (isset($sourced['msg'])) {
319
				    		    if (time() - $sourced['msg']['date'] > 10) {
320
				    		        $nbmsg = round($sourced['msg']['nb']/(time() - $sourced['msg']['date']));
321
				    		        echo $Stats->addStatSource($nbmsg,$source,'msg_tracker',$date);
322
			    			        unset($this->stats[$date][$source]['msg']);
323
			    			    }
324
			    			}
325
			    		    }
326
			    		    if ($date != date('Y-m-d')) {
327
			    			unset($this->stats[$date]);
328
			    		    }
329
				    	}
330
				    	if ($globalDebug) echo 'Done'."\n";
331
332
				    }
333
				    $Stats->db = null;
334
				    
335
				    $this->del();
336
				//$ignoreImport = false;
337
				$this->all_tracked[$id]['addedTracker'] = 1;
338
				//print_r($this->all_tracked[$id]);
339
				if ($this->last_delete == 0 || time() - $this->last_delete > 1800) {
340
				    if ($globalDebug) echo "---- Deleting Live Tracker data older than 9 hours...";
341
				    //TrackerLive->deleteLiveTrackerDataNotUpdated();
342
				    $TrackerLive = new TrackerLive($this->db);
343
				    $TrackerLive->deleteLiveTrackerData();
344
				    $TrackerLive->db=null;
345
				    if ($globalDebug) echo " Done\n";
346
				    $this->last_delete = time();
347
				}
348
			    } else {
349
				$this->all_tracked[$id]['id'] = $recent_ident;
350
				$this->all_tracked[$id]['addedTracker'] = 1;
351
				if (isset($globalDaemon) && !$globalDaemon) {
352
					$Tracker = new Tracker($this->db);
353
					$Tracker->updateLatestTrackerData($this->all_tracked[$id]['id'],$this->all_tracked[$id]['ident'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$this->all_tracked[$id]['altitude'],$this->all_tracked[$id]['speed'],$this->all_tracked[$id]['datetime']);
354
					$Tracker->db = null;
355
				}
356
				
357
			    }
358
			}
359
		    }
360
		    //adds the spotter LIVE data
361
		    if ($globalDebug) {
362
			echo 'DATA : ident : '.$this->all_tracked[$id]['ident'].' - type : '.$this->all_tracked[$id]['type'].' - Latitude : '.$this->all_tracked[$id]['latitude'].' - Longitude : '.$this->all_tracked[$id]['longitude'].' - Altitude : '.$this->all_tracked[$id]['altitude'].' - Heading : '.$this->all_tracked[$id]['heading'].' - Speed : '.$this->all_tracked[$id]['speed']."\n";
363
		    }
364
		    $ignoreImport = false;
365
366
		    if (!$ignoreImport) {
367
			if (!isset($globalDistanceIgnore['latitude']) || (isset($globalDistanceIgnore['latitude']) && $Common->distance($this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude']) < $globalDistanceIgnore['distance'])) {
368
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['ident']." from ".$this->all_tracked[$id]['format_source']." in Live DB : ";
369
				$timeelapsed = microtime(true);
370
				$TrackerLive = new TrackerLive($this->db);
371
				$result = $TrackerLive->addLiveTrackerData($this->all_tracked[$id]['id'], $this->all_tracked[$id]['ident'], $this->all_tracked[$id]['latitude'], $this->all_tracked[$id]['longitude'], $this->all_tracked[$id]['altitude'], $this->all_tracked[$id]['heading'], $this->all_tracked[$id]['speed'],$this->all_tracked[$id]['datetime'], $this->all_tracked[$id]['putinarchive'],$this->all_tracked[$id]['comment'],$this->all_tracked[$id]['type'],$this->all_tracked[$id]['noarchive'],$this->all_tracked[$id]['format_source'],$this->all_tracked[$id]['source_name'],$this->all_tracked[$id]['over_country']);
372
				$TrackerLive->db = null;
373
				$this->all_tracked[$id]['putinarchive'] = false;
374
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update addlivespotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
375
376
				// Put statistics in $this->stats variable
377
				
378
				if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE && $this->all_tracked[$id]['latitude'] != '' && $this->all_tracked[$id]['longitude'] != '') {
379
					$source = $this->all_tracked[$id]['source_name'];
380
					if ($source == '') $source = $this->all_tracked[$id]['format_source'];
381
					if (!isset($this->source_location[$source])) {
382
						$Location = new Source();
383
						$coord = $Location->getLocationInfobySourceName($source);
384
						if (count($coord) > 0) {
385
							$latitude = $coord[0]['latitude'];
386
							$longitude = $coord[0]['longitude'];
387
						} else {
388
							$latitude = $globalCenterLatitude;
389
							$longitude = $globalCenterLongitude;
390
						}
391
						$this->source_location[$source] = array('latitude' => $latitude,'longitude' => $longitude);
392
					} else {
393
						$latitude = $this->source_location[$source]['latitude'];
394
						$longitude = $this->source_location[$source]['longitude'];
395
					}
396
					$stats_heading = $Common->getHeading($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
397
					//$stats_heading = $stats_heading%22.5;
398
					$stats_heading = round($stats_heading/22.5);
399
					$stats_distance = $Common->distance($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
400
					$current_date = date('Y-m-d');
401
					if ($stats_heading == 16) $stats_heading = 0;
402
					if (!isset($this->stats[$current_date][$source]['polar'][1])) {
403
						for ($i=0;$i<=15;$i++) {
404
						    $this->stats[$current_date][$source]['polar'][$i] = 0;
405
						}
406
						$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
407
					} else {
408
						if ($this->stats[$current_date][$source]['polar'][$stats_heading] < $stats_distance) {
409
							$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
410
						}
411
					}
412
					$distance = (round($stats_distance/10)*10);
413
					//echo '$$$$$$$$$$ DISTANCE : '.$distance.' - '.$source."\n";
414
					//var_dump($this->stats);
415
					if (!isset($this->stats[$current_date][$source]['hist'][$distance])) {
416
						if (isset($this->stats[$current_date][$source]['hist'][0])) {
417
						    end($this->stats[$current_date][$source]['hist']);
418
						    $mini = key($this->stats[$current_date][$source]['hist'])+10;
419
						} else $mini = 0;
420
						for ($i=$mini;$i<=$distance;$i+=10) {
421
						    $this->stats[$current_date][$source]['hist'][$i] = 0;
422
						}
423
						$this->stats[$current_date][$source]['hist'][$distance] = 1;
424
					} else {
425
						$this->stats[$current_date][$source]['hist'][$distance] += 1;
426
					}
427
				}
428
429
				$this->all_tracked[$id]['lastupdate'] = time();
430
				if ($this->all_tracked[$id]['putinarchive']) $send = true;
431
				if ($globalDebug) echo $result."\n";
432
			} elseif (isset($this->all_tracked[$id]['latitude']) && isset($globalDistanceIgnore['latitude']) && $globalDebug) echo "!! Too far -> Distance : ".$Common->distance($this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],$globalDistanceIgnore['latitude'],$globalDistanceIgnore['longitude'])."\n";
433
			//$this->del();
434
			
435
			
436
			if ($this->last_delete_hourly == 0 || time() - $this->last_delete_hourly > 900) {
437
			    if ($globalDebug) echo "---- Deleting Live Tracker data Not updated since 2 hour...";
438
			    $TrackerLive = new TrackerLive($this->db);
439
			    $TrackerLive->deleteLiveTrackerDataNotUpdated();
440
			    $TrackerLive->db = null;
441
			    //TrackerLive->deleteLiveTrackerData();
442
			    if ($globalDebug) echo " Done\n";
443
			    $this->last_delete_hourly = time();
444
			}
445
			
446
		    }
447
		    //$ignoreImport = false;
448
		}
449
		//if (function_exists('pcntl_fork') && $globalFork) pcntl_signal(SIGCHLD, SIG_IGN);
450
		if ($send) return $this->all_tracked[$id];
451
	    }
452
	}
453
    }
454
}
455
?>
456