Browse Source

Parte de Alexander y Kendrick PHP

Kendrick Morales 5 years ago
parent
commit
5bff23e8ae
2 changed files with 58 additions and 0 deletions
  1. 24
    0
      reciver.php
  2. 34
    0
      sender.php

+ 24
- 0
reciver.php View File

1
+<?php
2
+	function read_and_delete_first_line($filename)
3
+	{
4
+		$file = file($filename);
5
+		array_shift($file);
6
+		file_put_contents($filename, $file);
7
+	}
8
+	$decoded_location  = json_decode(file_get_contents('php://input'));
9
+	$inTime = gmdate('H:i:s',time());
10
+	$file = 'db.txt';
11
+	$totalLines = intval(exec("wc -l '$file'"));
12
+	//echo $totalLines. "\n"; //For testing purposes
13
+	if($totalLines > 15)
14
+	{
15
+		read_and_delete_first_line("db.txt");
16
+	}
17
+
18
+	$db = fopen("db.txt","a") or die("FAILED TO ACCESS DATABASE");
19
+	fwrite($db,$decoded_location->latitude. "\t". $decoded_location->longitude. "\t". $inTime. "\n");
20
+	fclose($db);
21
+	//$totalLines = intval(exec("wc -l '$file'")); //For testing purposes
22
+	//echo $totalLines; //For testing purposes
23
+?>
24
+

+ 34
- 0
sender.php View File

1
+<?php
2
+$file = file("db.txt");
3
+$sum_of_lat = 0;
4
+$sum_of_long = 0;
5
+$array_sum_of_time = array(0,0,0);
6
+$array_inTime = 0;
7
+$average = new stdClass();
8
+$average->latitude = 0;
9
+$average->longitude = 0;
10
+$average->inTime = 0;
11
+$encoded_average = '';
12
+$count_loc = count($file);
13
+
14
+for ($i = 0; $i < $count_loc; $i++)
15
+{
16
+        $decoded_location = json_decode($file[$i]);
17
+        $array_inTime = array_map('intval',explode(':',$decoded_location->inTime));
18
+        $array_sum_of_time[0] = $array_sum_of_time[0] + $array_inTime[0];
19
+        $array_sum_of_time[1] = $array_sum_of_time[1] + $array_inTime[1];
20
+        $array_sum_of_time[2] = $array_sum_of_time[2] + $array_inTime[2];
21
+        $sum_of_lat = $sum_of_lat + $decoded_location->latitude;
22
+        $sum_of_long = $sum_of_long + $decoded_location->longitude;
23
+        if ($i+1 == $count_loc)
24
+        {
25
+                $average->latitude = $sum_of_lat/$count_loc;
26
+                $average->longitude = $sum_of_long/$count_loc;
27
+                $average->inTime = strval(floor($array_sum_of_time[0]/$count_loc)) . ":" . strval(floor($array_sum_of_time[1]/$count_loc)) . ":" . strval(floor($array_sum_of_time[2]/$count_loc));
28
+                $encoded_average = json_encode($average);
29
+        }
30
+}
31
+//echo $average->latitude. "<br>". $average->longitude. "<br>". $average->inTime . "<br>". $encoded_average;
32
+header('Content-Type: application/json');
33
+echo $encoded_average;
34
+