Completed
Push — master ( cc50d0...0f8c33 )
by Yannick
09:46
created

TrackerImport::add()   F

Complexity

Conditions 162
Paths 0

Size

Total Lines 361
Code Lines 188

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 162
eloc 188
nc 0
nop 1
dl 0
loc 361
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.Scheduler.php');
7
require_once(dirname(__FILE__).'/class.Translation.php');
8
require_once(dirname(__FILE__).'/class.Stats.php');
9
require_once(dirname(__FILE__).'/class.Source.php');
10
11
class TrackerImport {
12
    private $all_tracked = array();
13
    private $last_delete_hourly = 0;
14
    private $last_delete = 0;
15
    private $stats = array();
0 ignored issues
show
Unused Code introduced by
The property $stats is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
    private $tmd = 0;
17
    private $source_location = array();
0 ignored issues
show
Unused Code introduced by
The property $source_location is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
    public $db = null;
19
    public $nb = 0;
20
21
    public function __construct($dbc = null) {
22
	global $globalBeta;
23
	$Connection = new Connection($dbc);
24
	$this->db = $Connection->db();
25
	date_default_timezone_set('UTC');
26
27
	// Get previous source stats
28
	/*
29
	$Stats = new Stats($dbc);
30
	$currentdate = date('Y-m-d');
31
	$sourcestat = $Stats->getStatsSource($currentdate);
32
	if (!empty($sourcestat)) {
33
	    foreach($sourcestat as $srcst) {
34
	    	$type = $srcst['stats_type'];
35
		if ($type == 'polar' || $type == 'hist') {
36
		    $source = $srcst['source_name'];
37
		    $data = $srcst['source_data'];
38
		    $this->stats[$currentdate][$source][$type] = json_decode($data,true);
39
	        }
40
	    }
41
	}
42
	*/
43
    }
44
45
    public function checkAll() {
46
	global $globalDebug;
47
	if ($globalDebug) echo "Update last seen tracked data...\n";
48
	foreach ($this->all_tracked as $key => $flight) {
49
	    if (isset($this->all_tracked[$key]['id'])) {
50
		//echo $this->all_tracked[$key]['id'].' - '.$this->all_tracked[$key]['latitude'].'  '.$this->all_tracked[$key]['longitude']."\n";
51
    		$Tracker = new Tracker($this->db);
52
        	$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']);
53
            }
54
	}
55
    }
56
57
    public function del() {
58
	global $globalDebug;
59
	// Delete old infos
60
	if ($globalDebug) echo 'Delete old values and update latest data...'."\n";
61
	foreach ($this->all_tracked as $key => $flight) {
62
    	    if (isset($flight['lastupdate'])) {
63
        	if ($flight['lastupdate'] < (time()-3000)) {
64
            	    if (isset($this->all_tracked[$key]['id'])) {
65
            		if ($globalDebug) echo "--- Delete old values with id ".$this->all_tracked[$key]['id']."\n";
66
			/*
67
			$TrackerLive = new TrackerLive();
68
            		$TrackerLive->deleteLiveTrackerDataById($this->all_tracked[$key]['id']);
69
			$TrackerLive->db = null;
70
			*/
71
            		//$real_arrival = $this->arrival($key);
72
            		$Tracker = new Tracker($this->db);
73
            		if ($this->all_tracked[$key]['latitude'] != '' && $this->all_tracked[$key]['longitude'] != '') {
74
				$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']);
75
				if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
76
			}
77
			// Put in archive
78
//			$Tracker->db = null;
79
            	    }
80
            	    unset($this->all_tracked[$key]);
81
    	        }
82
	    }
83
        }
84
    }
85
86
    public function add($line) {
87
	global $globalFork, $globalDistanceIgnore, $globalDaemon, $globalDebug, $globalCoordMinChange, $globalDebugTimeElapsed, $globalCenterLatitude, $globalCenterLongitude, $globalBeta, $globalSourcesupdate, $globalAllTracked;
88
	if (!isset($globalCoordMinChange) || $globalCoordMinChange == '') $globalCoordMinChange = '0.02';
89
	date_default_timezone_set('UTC');
90
	$dataFound = false;
91
	$send = false;
92
	
93
	// SBS format is CSV format
94
	if(is_array($line) && isset($line['ident'])) {
95
	    //print_r($line);
96
  	    if (isset($line['ident'])) {
97
98
		/*
99
		// Increment message number
100
		if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE) {
101
		    $current_date = date('Y-m-d');
102
		    $source = $line['source_name'];
103
		    if ($source == '' || $line['format_source'] == 'aprs') $source = $line['format_source'];
104
		    if (!isset($this->stats[$current_date][$source]['msg'])) {
105
		    	$this->stats[$current_date][$source]['msg']['date'] = time();
106
		    	$this->stats[$current_date][$source]['msg']['nb'] = 1;
107
		    } else $this->stats[$current_date][$source]['msg']['nb'] += 1;
108
		}
109
		*/
110
		
111
		$Common = new Common();
112
	        if (!isset($line['id'])) $id = trim($line['ident']);
113
	        else $id = trim($line['id']);
114
		
115
		if (!isset($this->all_tracked[$id])) {
116
		    $this->all_tracked[$id] = array();
117
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('addedTracker' => 0));
118
		    $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' => ''));
119
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('lastupdate' => time()));
120
		    if (!isset($line['id'])) {
121
			if (!isset($globalDaemon)) $globalDaemon = TRUE;
122
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $id.'-'.date('YmdHi')));
123
		     } else $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $line['id']));
124
		    if ($globalAllTracked !== FALSE) $dataFound = true;
125
		}
126
		
127
		if (isset($line['datetime']) && strtotime($line['datetime']) > time()-20*60) {
128
		    if (!isset($this->all_tracked[$id]['datetime']) || strtotime($line['datetime']) >= strtotime($this->all_tracked[$id]['datetime'])) {
129
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => $line['datetime']));
130
		    } else {
131
				if (strtotime($line['datetime']) == strtotime($this->all_tracked[$id]['datetime']) && $globalDebug) echo "!!! Date is the same as previous data for ".$this->all_tracked[$id]['hex']." - format : ".$line['format_source']."\n";
132
				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]['hex']." - format : ".$line['format_source']."\n";
133
				return '';
134
		    }
135
		} else {
136
			date_default_timezone_set('UTC');
137
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('datetime' => date('Y-m-d H:i:s')));
138
		}
139
140
		//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'])) {
141
		if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && ($this->all_tracked[$id]['ident'] != trim($line['ident']))) {
142
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('ident' => trim($line['ident'])));
143
		    if ($this->all_tracked[$id]['addedTracker'] == 1) {
144
			$timeelapsed = microtime(true);
145
            		$Tracker = new Tracker($this->db);
146
            		$fromsource = NULL;
147
            		$result = $Tracker->updateIdentTrackerData($this->all_tracked[$id]['id'],$this->all_tracked[$id]['ident'],$fromsource);
148
			if ($globalDebug && $result != 'success') echo '!!! ERROR : '.$result."\n";
149
			$Tracker->db = null;
150
			if ($globalDebugTimeElapsed) echo 'Time elapsed for update identspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
151
		    }
152
		    if (!isset($this->all_tracked[$id]['id'])) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('id' => $this->all_tracked[$id]['ident']));
153
		}
154
155
		if (isset($line['speed']) && $line['speed'] != '') {
156
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($line['speed'])));
157
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed_fromsrc' => true));
158
		} 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'])) {
159
		    $distance = $Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m');
160
		    if ($distance > 1000 && $distance < 10000) {
161
			$speed = $distance/(time() - $this->all_tracked[$id]['time_last_coord']);
162
			$speed = $speed*3.6;
163
			if ($speed < 1000) $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('speed' => round($speed)));
164
  			if ($globalDebug) echo "ø Calculated Speed for ".$this->all_tracked[$id]['hex']." : ".$speed." - distance : ".$distance."\n";
165
		    }
166
		}
167
168
	        if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '' && is_numeric($line['latitude']) && is_numeric($line['longitude'])) {
169
	    	    if (isset($this->all_tracked[$id]['time_last_coord'])) $timediff = round(time()-$this->all_tracked[$id]['time_last_coord']);
170
	    	    else unset($timediff);
171
	    	    if ($this->tmd > 5 || !isset($timediff) || $timediff > 2000 || ($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')))) {
172
			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'])) {
173
			    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'])) {
174
				$this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
175
				$this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
176
				$this->all_tracked[$id]['putinarchive'] = true;
177
				
178
				if ($globalDebug) echo "\n".' ------- Check Country for '.$this->all_tracked[$id]['ident'].' with latitude : '.$line['latitude'].' and longitude : '.$line['longitude'].'.... ';
179
				$timeelapsed = microtime(true);
180
				$Tracker = new Tracker($this->db);
181
				$all_country = $Tracker->getCountryFromLatitudeLongitude($line['latitude'],$line['longitude']);
182
				if (!empty($all_country)) $this->all_tracked[$id]['over_country'] = $all_country['iso2'];
183
				$Tracker->db = null;
184
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update getCountryFromlatitudeLongitude : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
185
				$this->tmd = 0;
186
				if ($globalDebug) echo 'FOUND : '.$this->all_tracked[$id]['over_country'].' ---------------'."\n";
187
			    }
188
			}
189
190
			if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
191
				if (!isset($this->all_tracked[$id]['archive_latitude'])) $this->all_tracked[$id]['archive_latitude'] = $line['latitude'];
192
				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') {
193
				    $this->all_tracked[$id]['livedb_latitude'] = $line['latitude'];
194
				    $dataFound = true;
195
				    $this->all_tracked[$id]['time_last_coord'] = time();
196
				}
197
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('latitude' => $line['latitude']));
198
			}
199
			if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
200
			    if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
201
				if (!isset($this->all_tracked[$id]['archive_longitude'])) $this->all_tracked[$id]['archive_longitude'] = $line['longitude'];
202
				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') {
203
				    $this->all_tracked[$id]['livedb_longitude'] = $line['longitude'];
204
				    $dataFound = true;
205
				    $this->all_tracked[$id]['time_last_coord'] = time();
206
				}
207
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('longitude' => $line['longitude']));
208
			}
209
210
		    } else if ($globalDebug && $timediff > 20) {
211
			$this->tmd = $this->tmd + 1;
212
			echo '!!! Too much distance in short time... for '.$this->all_tracked[$id]['ident']."\n";
213
			echo 'Time : '.$timediff.'s - Distance : '.$Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')."m -";
214
			echo 'Speed : '.(($Common->distance($line['latitude'],$line['longitude'],$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude'],'m')/$timediff)*3.6)." km/h - ";
215
			echo 'Lat : '.$line['latitude'].' - long : '.$line['longitude'].' - prev lat : '.$this->all_tracked[$id]['latitude'].' - prev long : '.$this->all_tracked[$id]['longitude']." \n";
216
		    }
217
		}
218
		if (isset($line['last_update']) && $line['last_update'] != '') {
219
		    if (isset($this->all_tracked[$id]['last_update']) && $this->all_tracked[$id]['last_update'] != $line['last_update']) $dataFound = true;
220
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('last_update' => $line['last_update']));
221
		}
222
		if (isset($line['format_source']) && $line['format_source'] != '') {
223
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('format_source' => $line['format_source']));
224
		}
225
		if (isset($line['source_name']) && $line['source_name'] != '') {
226
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('source_name' => $line['source_name']));
227
		}
228
		if (isset($line['comment']) && $line['comment'] != '') {
229
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('comment' => $line['comment']));
230
		    //$dataFound = true;
231
		}
232
		if (isset($line['type']) && $line['type'] != '') {
233
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('type' => $line['type']));
234
		    //$dataFound = true;
235
		}
236
237
		if (isset($line['altitude']) && $line['altitude'] != '') {
238
		    //if (!isset($this->all_tracked[$id]['altitude']) || $this->all_tracked[$id]['altitude'] == '' || ($this->all_tracked[$id]['altitude'] > 0 && $line['altitude'] != 0)) {
239
			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;
240
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('altitude' => round($line['altitude']/100)));
241
			$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('altitude_real' => $line['altitude']));
242
			//$dataFound = true;
243
		    //} elseif ($globalDebug) echo "!!! Strange altitude data... not added.\n";
244
  		}
245
246
		if (isset($line['noarchive']) && $line['noarchive'] === true) {
247
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('noarchive' => true));
248
		}
249
		
250
		if (isset($line['heading']) && $line['heading'] != '') {
251
		    if (is_int($this->all_tracked[$id]['heading']) && abs($this->all_tracked[$id]['heading']-round($line['heading'])) > 10) $this->all_tracked[$id]['putinarchive'] = true;
252
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($line['heading'])));
253
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading_fromsrc' => true));
254
		    //$dataFound = true;
255
  		} 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']) {
256
  		    $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']);
257
		    $this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('heading' => round($heading)));
258
		    if (abs($this->all_tracked[$id]['heading']-round($heading)) > 10) $this->all_tracked[$id]['putinarchive'] = true;
259
  		    if ($globalDebug) echo "ø Calculated Heading for ".$this->all_tracked[$id]['ident']." : ".$heading."\n";
260
  		}
261
		//if (isset($globalSourcesupdate) && $globalSourcesupdate != '' && isset($this->all_tracked[$id]['lastupdate']) && time()-$this->all_tracked[$id]['lastupdate'] < $globalSourcesupdate) $dataFound = false;
262
263
		if ($dataFound === true && isset($this->all_tracked[$id]['ident'])) {
264
		    $this->all_tracked[$id]['lastupdate'] = time();
265
		    if ($this->all_tracked[$id]['addedTracker'] == 0) {
266
		        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'])) {
267
			    if (!isset($this->all_tracked[$id]['forcenew']) || $this->all_tracked[$id]['forcenew'] == 0) {
268
				if ($globalDebug) echo "Check if aircraft is already in DB...";
269
				$timeelapsed = microtime(true);
270
				$TrackerLive = new TrackerLive($this->db);
271
				if (isset($line['id'])) {
272
				    $recent_ident = $TrackerLive->checkIdRecent($line['id']);
273
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
274
				} elseif (isset($this->all_tracked[$id]['ident']) && $this->all_tracked[$id]['ident'] != '') {
275
				    $recent_ident = $TrackerLive->checkIdentRecent($this->all_tracked[$id]['ident']);
276
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update checkIdentRecent : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
277
				} else $recent_ident = '';
278
				$TrackerLive->db=null;
279
280
				if ($globalDebug && $recent_ident == '') echo " Not in DB.\n";
281
				elseif ($globalDebug && $recent_ident != '') echo " Already in DB.\n";
282
			    } else {
283
				$recent_ident = '';
284
				$this->all_tracked[$id] = array_merge($this->all_tracked[$id],array('forcenew' => 0));
285
			    }
286
			    //if there was no aircraft with the same callsign within the last hour and go post it into the archive
287
			    if($recent_ident == "")
288
			    {
289
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['ident']." in archive DB : ";
290
				//adds the spotter data for the archive
291
				    $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...
292
				    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')));
293
				    $timeelapsed = microtime(true);
294
				    $Tracker = new Tracker($this->db);
295
				    $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']);
296
				    $Tracker->db = null;
297
				    if ($globalDebug && isset($result)) echo $result."\n";
298
				    if ($globalDebugTimeElapsed) echo 'Time elapsed for update addspotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
299
				    
300
				    /*
301
				    // Add source stat in DB
302
				    $Stats = new Stats($this->db);
303
				    if (!empty($this->stats)) {
304
					if ($globalDebug) echo 'Add source stats : ';
305
				        foreach($this->stats as $date => $data) {
306
					    foreach($data as $source => $sourced) {
307
					        //print_r($sourced);
308
				    	        if (isset($sourced['polar'])) echo $Stats->addStatSource(json_encode($sourced['polar']),$source,'polar',$date);
309
				    	        if (isset($sourced['hist'])) echo $Stats->addStatSource(json_encode($sourced['hist']),$source,'hist',$date);
310
				    		if (isset($sourced['msg'])) {
311
				    		    if (time() - $sourced['msg']['date'] > 10) {
312
				    		        $nbmsg = round($sourced['msg']['nb']/(time() - $sourced['msg']['date']));
313
				    		        echo $Stats->addStatSource($nbmsg,$source,'msg',$date);
314
			    			        unset($this->stats[$date][$source]['msg']);
315
			    			    }
316
			    			}
317
			    		    }
318
			    		    if ($date != date('Y-m-d')) {
319
			    			unset($this->stats[$date]);
320
			    		    }
321
				    	}
322
				    	if ($globalDebug) echo 'Done'."\n";
323
324
				    }
325
				    $Stats->db = null;
326
				    */
327
				    $this->del();
328
				//$ignoreImport = false;
329
				$this->all_tracked[$id]['addedTracker'] = 1;
330
				//print_r($this->all_tracked[$id]);
331
				if ($this->last_delete == 0 || time() - $this->last_delete > 1800) {
332
				    if ($globalDebug) echo "---- Deleting Live Tracker data older than 9 hours...";
333
				    //TrackerLive->deleteLiveTrackerDataNotUpdated();
334
				    $TrackerLive = new TrackerLive($this->db);
335
				    $TrackerLive->deleteLiveTrackerData();
336
				    $TrackerLive->db=null;
337
				    if ($globalDebug) echo " Done\n";
338
				    $this->last_delete = time();
339
				}
340
			    } else {
341
				$this->all_tracked[$id]['id'] = $recent_ident;
342
				$this->all_tracked[$id]['addedTracker'] = 1;
343
				if (isset($globalDaemon) && !$globalDaemon) {
344
					$Tracker = new Tracker($this->db);
345
					$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']);
346
					$Tracker->db = null;
347
				}
348
				
349
			    }
350
			}
351
		    }
352
		    //adds the spotter LIVE data
353
		    if ($globalDebug) {
354
			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";
355
		    }
356
		    $ignoreImport = false;
357
358
		    if (!$ignoreImport) {
359
			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'])) {
360
				if ($globalDebug) echo "\o/ Add ".$this->all_tracked[$id]['ident']." from ".$this->all_tracked[$id]['format_source']." in Live DB : ";
361
				$timeelapsed = microtime(true);
362
				$TrackerLive = new TrackerLive($this->db);
363
				$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']);
364
				$TrackerLive->db = null;
365
				$this->all_tracked[$id]['putinarchive'] = false;
366
				if ($globalDebugTimeElapsed) echo 'Time elapsed for update addlivespotterdata : '.round(microtime(true)-$timeelapsed,2).'s'."\n";
367
368
				// Put statistics in $this->stats variable
369
				/*
370
				if (isset($line['sourcestats']) && $line['sourcestats'] == TRUE && $line['format_source'] != 'aprs' && $this->all_tracked[$id]['latitude'] != '' && $this->all_tracked[$id]['longitude'] != '') {
371
					$source = $this->all_tracked[$id]['source_name'];
372
					if ($source == '') $source = $this->all_tracked[$id]['format_source'];
373
					if (!isset($this->source_location[$source])) {
374
						$Location = new Source();
375
						$coord = $Location->getLocationInfobySourceName($source);
376
						if (count($coord) > 0) {
377
							$latitude = $coord[0]['latitude'];
378
							$longitude = $coord[0]['longitude'];
379
						} else {
380
							$latitude = $globalCenterLatitude;
381
							$longitude = $globalCenterLongitude;
382
						}
383
						$this->source_location[$source] = array('latitude' => $latitude,'longitude' => $longitude);
384
					} else {
385
						$latitude = $this->source_location[$source]['latitude'];
386
						$longitude = $this->source_location[$source]['longitude'];
387
					}
388
					$stats_heading = $Common->getHeading($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
389
					//$stats_heading = $stats_heading%22.5;
390
					$stats_heading = round($stats_heading/22.5);
391
					$stats_distance = $Common->distance($latitude,$longitude,$this->all_tracked[$id]['latitude'],$this->all_tracked[$id]['longitude']);
392
					$current_date = date('Y-m-d');
393
					if ($stats_heading == 16) $stats_heading = 0;
394
					if (!isset($this->stats[$current_date][$source]['polar'][1])) {
395
						for ($i=0;$i<=15;$i++) {
396
						    $this->stats[$current_date][$source]['polar'][$i] = 0;
397
						}
398
						$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
399
					} else {
400
						if ($this->stats[$current_date][$source]['polar'][$stats_heading] < $stats_distance) {
401
							$this->stats[$current_date][$source]['polar'][$stats_heading] = $stats_distance;
402
						}
403
					}
404
					$distance = (round($stats_distance/10)*10);
405
					//echo '$$$$$$$$$$ DISTANCE : '.$distance.' - '.$source."\n";
406
					//var_dump($this->stats);
407
					if (!isset($this->stats[$current_date][$source]['hist'][$distance])) {
408
						if (isset($this->stats[$current_date][$source]['hist'][0])) {
409
						    end($this->stats[$current_date][$source]['hist']);
410
						    $mini = key($this->stats[$current_date][$source]['hist'])+10;
411
						} else $mini = 0;
412
						for ($i=$mini;$i<=$distance;$i+=10) {
413
						    $this->stats[$current_date][$source]['hist'][$i] = 0;
414
						}
415
						$this->stats[$current_date][$source]['hist'][$distance] = 1;
416
					} else {
417
						$this->stats[$current_date][$source]['hist'][$distance] += 1;
418
					}
419
				}
420
				*/
421
422
				$this->all_tracked[$id]['lastupdate'] = time();
423
				if ($this->all_tracked[$id]['putinarchive']) $send = true;
424
				if ($globalDebug) echo $result."\n";
425
			} 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";
426
			//$this->del();
427
			
428
			
429
			if ($this->last_delete_hourly == 0 || time() - $this->last_delete_hourly > 900) {
430
			    if ($globalDebug) echo "---- Deleting Live Tracker data Not updated since 2 hour...";
431
			    $TrackerLive = new TrackerLive($this->db);
432
			    $TrackerLive->deleteLiveTrackerDataNotUpdated();
433
			    $TrackerLive->db = null;
434
			    //TrackerLive->deleteLiveTrackerData();
435
			    if ($globalDebug) echo " Done\n";
436
			    $this->last_delete_hourly = time();
437
			}
438
			
439
		    }
440
		    //$ignoreImport = false;
441
		}
442
		//if (function_exists('pcntl_fork') && $globalFork) pcntl_signal(SIGCHLD, SIG_IGN);
443
		if ($send) return $this->all_tracked[$id];
444
	    }
445
	}
446
    }
447
}
448
?>
449