forked from sergejey/majordomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btraced.php
142 lines (118 loc) · 4.19 KB
/
btraced.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Main project script
*
* @package MajorDoMo
* @author Serge Dzheigalo <[email protected]> http://smartliving.ru/
* @version 1.1
*/
Define('BTRACED', 1);
// Get the received data from the iPhone (XML data)
$body = @file_get_contents('php://input');
// Try to load the XML
$xml = simplexml_load_string($body);
// If there was an error report it...
if ($xml == false)
{
// Error loading XML..., send it back to the iPhone
echo '{ "id":902, "error":true, "message":"Cant load XML", "valid":true }';
exit;
}
else
{
// Get username and password
$username = $xml->username;
$password = $xml->password;
// Optional: You can check the username and password against your database
// Uncomment for hardcoded testing
// if (($username != 'user') && ($password != 'test'))
// {
// echo '{ "id":1, "error":true, "valid":true }';
// exit();
// }
// Get device identification
$deviceId = $xml->devId;
$_REQUEST['deviceid']=$deviceId;
// Prepare list of points
$goodPointsList = "";
// Start processing each travel
foreach ($xml->travel as $travel)
{
// Get travel common information
$travelId = $travel->id;
$travelName = $travel->description;
$travelLength = $travel->length;
$travelTime = $travel->time;
$travelTPoints = $travel->tpoints;
// Prepare the succesful points
$goodPointsList = '';
// Process each point
foreach ($travel->point as $point)
{
// Get all the information for this point
$pointId = $point->id;
$pointDate = date("Y-m-d H:i:s", trim($point->date));
$pointLat = $point->lat;
$pointLon = $point->lon;
$pointSpeed = $point->speed;
$pointCourse = $point->course;
$pointHAccu = $point->haccu;
$pointBatt = $point->bat;
$pointVAccu = $point->vaccu;
$pointAltitude = $point->altitude;
$pointContinous = $point->continous;
$pointTDist = $point->tdist;
$pointRDist = $point->rdist;
$pointTTime = $point->ttime;
$p=array();
$p['TM'] = (int)trim($point->date);
$p['LATITUDE'] = $point->lat;
$p['LONGITUDE'] = $point->lon;
$p['SPEED'] = $point->speed;
$p['BATTERY'] = $point->bat;
$p['ALTITUDE'] = $point->altitude;
$all_points[]=$p;
// Create SQL sentence
//$sql = "INSERT INTO tblBtracedTripsData (DevID, TripID, TripName, TripLength, TripTime, TripTotalPoints, PointID, PointDate, PointLat, PointLon, PointSpeed, PointCourse, PointHAccu, PointBatt, PointVAccu, PointAltitude, PointContinuos, PointTotalDistance, PointRelativeDistance, PointTotalTime) VALUES ('$deviceId','$travelId','$travelName','$travelLength','$travelTime','$travelTPoints','$pointId','$pointDate','$pointLat','$pointLon','$pointSpeed','$pointCourse','$pointHAccu','$pointBatt','$pointVAccu','$pointAltitude','$pointContinous','$pointTDist','$pointRDist','$pointTTime')";
//$insertResult = mysql_query($sql, $conexion);
$goodPointsList .= $pointId.",";
}
}
// Check if there was points
if ($goodPointsList != "")
{
// Remove last comma
$goodPointsList = substr($goodPointsList, 0, -1);
// Send back the answer for the saved points
echo '{"id":0, "tripid":'.$travelId.',"points":['.$goodPointsList.'],"valid":true}';
}
else
{
// Just OK, the code should never reach here as we always have points
echo '{"id":0, "tripid":'.$travelId.',"valid":true}';
exit;
}
}
function cmp ($a, $b)
{
if ($a['TM'] == $b['TM']) return 0;
return ($a['TM'] > $b['TM']) ? -1 : 1;
}
usort ($all_points, "cmp");
$cp = $all_points[0];
$_REQUEST['latitude'] = $cp['LATITUDE'];
$_REQUEST['longitude'] = $cp['LONGITUDE'];
$_REQUEST['altitude'] = $cp['ALTITUDE'];
$_REQUEST['speed'] = $cp['SPEED'];
$_REQUEST['battlevel'] = $cp['BATTERY'];
/*
$_POST['latitude']
$_POST['longitude']
$_POST['altitude']
$_POST['speed']
$_POST['provider']
$_POST['battlevel']
$_POST['charging']
*/
include_once('./gps.php');
?>