Skip to content

Commit b98e6fd

Browse files
committed
reformatted tk103 server file
1 parent 880398f commit b98e6fd

File tree

1 file changed

+81
-82
lines changed

1 file changed

+81
-82
lines changed

servers/tk103/tk103-server.php

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//print remote client information, ip and port number
3333
echo 'new connection: ' . stream_socket_get_name($new_client, true) . "\n";
3434

35-
$client_socks[] = $new_client;
35+
$client_socks[] = $new_client;
3636
echo "total clients: ". count($client_socks) . "\n";
3737

3838
// $output = "hello new client.\n";
@@ -49,77 +49,76 @@
4949

5050
echo "data: " . $data . "\n";
5151

52-
$tk103_data = explode( ',', $data);
53-
$response = "";
52+
$tk103_data = explode( ',', $data);
53+
$response = "";
5454

5555
switch (count($tk103_data)) {
5656
case 1: // 359710049095095 -> heartbeat requires "ON" response
57-
$response = "ON";
57+
$response = "ON";
5858
echo "sent ON to client\n";
59-
break;
59+
break;
6060
case 3: // ##,imei:359710049095095,A -> this requires a "LOAD" response
61-
if ($tk103_data[0] == "##") {
62-
$response = "LOAD";
61+
if ($tk103_data[0] == "##") {
62+
$response = "LOAD";
6363
echo "sent LOAD to client\n";
64-
}
65-
break;
66-
case 19: // imei:359710049095095,tracker,151006012336,,F,172337.000,A,5105.9792,N,11404.9599,W,0.01,322.56,,0,0,,, -> this is our gps data
67-
$imei = substr($tk103_data[0], 5);
68-
$alarm = $tk103_data[1];
69-
$gps_time = nmea_to_mysql_time($tk103_data[2]);
70-
$latitude = degree_to_decimal($tk103_data[7], $tk103_data[8]);
71-
$longitude = degree_to_decimal($tk103_data[9], $tk103_data[10]);
72-
$speed_in_knots = $tk103_data[11];
73-
$speed_in_mph = 1.15078 * $speed_in_knots;
74-
$bearing = $tk103_data[12];
75-
76-
insert_location_into_db($pdo, $imei, $gps_time, $latitude, $longitude, $speed_in_mph, $bearing);
77-
78-
79-
if ($alarm == "help me") {
80-
$response = "**,imei:" + $imei + ",E;";
81-
}
82-
break;
64+
}
65+
break;
66+
case 19: // imei:359710049095095,tracker,151006012336,,F,172337.000,A,5105.9792,N,11404.9599,W,0.01,322.56,,0,0,,, -> this is our gps data
67+
$imei = substr($tk103_data[0], 5);
68+
$alarm = $tk103_data[1];
69+
$gps_time = nmea_to_mysql_time($tk103_data[2]);
70+
$latitude = degree_to_decimal($tk103_data[7], $tk103_data[8]);
71+
$longitude = degree_to_decimal($tk103_data[9], $tk103_data[10]);
72+
$speed_in_knots = $tk103_data[11];
73+
$speed_in_mph = 1.15078 * $speed_in_knots;
74+
$bearing = $tk103_data[12];
75+
76+
insert_location_into_db($pdo, $imei, $gps_time, $latitude, $longitude, $speed_in_mph, $bearing);
77+
78+
if ($alarm == "help me") {
79+
$response = "**,imei:" + $imei + ",E;";
80+
}
81+
break;
82+
}
83+
84+
if (!$data) {
85+
unset($client_socks[ array_search($sock, $client_socks) ]);
86+
@fclose($sock);
87+
echo "client disconnected. total clients: ". count($client_socks) . "\n";
88+
continue;
89+
}
90+
91+
//send the message back to client
92+
if (sizeof($response) > 0) {
93+
fwrite($sock, $response);
94+
}
8395
}
84-
85-
if (!$data) {
86-
unset($client_socks[ array_search($sock, $client_socks) ]);
87-
@fclose($sock);
88-
echo "client disconnected. total clients: ". count($client_socks) . "\n";
89-
continue;
90-
}
91-
92-
//send the message back to client
93-
if (sizeof($response) > 0) {
94-
fwrite($sock, $response);
95-
}
96-
}
9796
} // end while loop
9897

9998
function insert_location_into_db($pdo, $imei, $gps_time, $latitude, $longitude,$speed_in_mph, $bearing) {
10099

101-
$params = array(':latitude' => $latitude,
102-
':longitude' => $longitude,
103-
':user_name' => "tk103-user",
104-
':phone_number' => $imei,
105-
':session_id' => "1",
106-
':speed' => $speed_in_mph,
107-
':direction' => $bearing,
108-
':distance' => "0",
109-
':gps_time' => $gps_time,
110-
':location_method' => "",
111-
':accuracy' => "0",
112-
':extra_info' => "",
113-
':event_type' => "tk103");
114-
115-
// PLEASE NOTE, I am hardcoding the wordpress table prefix (wp_) until I can find a better way
116-
117-
$stmt = $pdo->prepare('CALL wp_save_gps_location(
118-
:latitude,
100+
$params = array(':latitude' => $latitude,
101+
':longitude' => $longitude,
102+
':user_name' => "tk103-user",
103+
':phone_number' => $imei,
104+
':session_id' => "1",
105+
':speed' => $speed_in_mph,
106+
':direction' => $bearing,
107+
':distance' => "0",
108+
':gps_time' => $gps_time,
109+
':location_method' => "",
110+
':accuracy' => "0",
111+
':extra_info' => "",
112+
':event_type' => "tk103");
113+
114+
// PLEASE NOTE, I am hardcoding the wordpress table prefix (wp_) until I can find a better way
115+
116+
$stmt = $pdo->prepare('CALL wp_save_gps_location(
117+
:latitude,
119118
:longitude,
120119
:user_name,
121-
:phone_number,
122-
:session_id,
120+
:phone_number,
121+
:session_id,
123122
:speed,
124123
:direction,
125124
:distance,
@@ -128,32 +127,32 @@ function insert_location_into_db($pdo, $imei, $gps_time, $latitude, $longitude,$
128127
:accuracy,
129128
:extra_info,
130129
:event_type);');
131-
132-
$stmt->execute($params);
133-
$timestamp = $stmt->fetchColumn();
134-
// echo "inserted into db: " . $timestamp . "\n";
130+
131+
$stmt->execute($params);
132+
$timestamp = $stmt->fetchColumn();
133+
// echo "inserted into db: " . $timestamp . "\n";
135134
}
136135

137136
function nmea_to_mysql_time($date_time){
138-
$year = substr($date_time,0,2);
139-
$month = substr($date_time,2,2);
140-
$day = substr($date_time,4,2);
141-
$hour = substr($date_time,6,2);
142-
$minute = substr($date_time,8,2);
143-
$second = substr($date_time,10,2);
144-
145-
return date("Y-m-d H:i:s", mktime($hour,$minute,$second,$month,$day,$year));
137+
$year = substr($date_time,0,2);
138+
$month = substr($date_time,2,2);
139+
$day = substr($date_time,4,2);
140+
$hour = substr($date_time,6,2);
141+
$minute = substr($date_time,8,2);
142+
$second = substr($date_time,10,2);
143+
144+
return date("Y-m-d H:i:s", mktime($hour,$minute,$second,$month,$day,$year));
146145
}
147-
146+
148147
function degree_to_decimal($coordinates_in_degrees, $direction){
149-
$degrees = (int)($coordinates_in_degrees / 100);
150-
$minutes = $coordinates_in_degrees - ($degrees * 100);
151-
$seconds = $minutes / 60;
152-
$coordinates_in_decimal = $degrees + $seconds;
153-
154-
if (($direction == "S") || ($direction == "W")) {
155-
$coordinates_in_decimal = $coordinates_in_decimal * (-1);
156-
}
157-
158-
return number_format($coordinates_in_decimal, 6,'.','');
148+
$degrees = (int)($coordinates_in_degrees / 100);
149+
$minutes = $coordinates_in_degrees - ($degrees * 100);
150+
$seconds = $minutes / 60;
151+
$coordinates_in_decimal = $degrees + $seconds;
152+
153+
if (($direction == "S") || ($direction == "W")) {
154+
$coordinates_in_decimal = $coordinates_in_decimal * (-1);
155+
}
156+
157+
return number_format($coordinates_in_decimal, 6,'.','');
159158
}

0 commit comments

Comments
 (0)