diff --git a/README.md b/README.md index 1a07a53..c5dbda6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,46 @@ -# Takcar -small PHP script to take lat long from Traccar and Show them in the FreeTakServer +# TAKCAR + +[](img/f12.png) + +Quick and dirty example of importing **Traccar** `Lon`, `Lat` coordinates into the **FreeTakServer**. + +On page load the script automatically begins a loop getting lat,lon from *Traccar* and posting them to *FTS*. + +The `Test Service` button just tests to see that you can see a result on *FTS*. + +```JSON + +{ + "uid": "999b5874-1ebf-11zz-9e70-4e58de281c19", + "how": "nonCoT", + "name": "POTUS", + "longitude": -77.01385, + "latitude": 38.889, + "role": "Team Member", + "team": "Yellow" +} + +``` + +## STEPS +1. create an account and API Token in Traccar. + +2. Use the `positions` endpoint in Traccar to get the *latitude* and *longitude* into vars. + +```HTTP +http://127.0.0.1:8082/api/positions?token=Fm9OhNJS7SShyw80M8kdqKMcSiOuqqhA +``` + +3. Using the `postPresence` endpoint in the FreeTakServer Import the *latitude* and *longitude* vars into FreeTakServer. + +```HTTP +http://127.0.0.1:19023/ManagePresence/postPresence +``` + +## Notes + +The scripts assume `19023` is the *FTS* API port and that `8082` is the *Traccar* API port. + +It is also assumed that the scripts are running on the same machine as *FTS* and *Traccar*. + +As long as the page is open the loop will continue to run but will stop on close. diff --git a/img/f12.png b/img/f12.png new file mode 100644 index 0000000..880d01a Binary files /dev/null and b/img/f12.png differ diff --git a/index.php b/index.php new file mode 100644 index 0000000..67c3166 --- /dev/null +++ b/index.php @@ -0,0 +1,137 @@ + + + +TAKCAR SERVICE + + + + + + + + + + + + +
+
+ +
+

TAKCAR

+
+ +
+
Quick and dirty example of importing Traccar Lon, Lat coordinates into the FreeTakServer.
+
+ +
+
+
+
+
+ +

+
+ +
+
+
+ + + +
+ + + + \ No newline at end of file diff --git a/serverUpdater.php b/serverUpdater.php new file mode 100644 index 0000000..97f94ec --- /dev/null +++ b/serverUpdater.php @@ -0,0 +1,129 @@ + $item){ +// $markers[] = json_encode( +// array( +// 'id' => $item->id, +// 'latitude' => $item->latitude, +// 'longitude' => $item->longitude, +// 'batteryLevel' => $item->attributes->battery, +// 'Time' => date("Y-m-d h:i:sa") +// ) +// ); + +//forward the positions to the FTS API endpoint +$result = get_FTSAPI($item->id, $item->latitude, $item->longitude); + +$markers[] = json_encode( + array( + 'result' => 0, + 'guid' => $result, + 'Time' => date("Y-m-d h:i:sa") + ) +); +} + +$JSON = json_encode( + array( + 'result' => 0, + 'message' => $markers, + 'Time' => date("Y-m-d h:i:sa"), + ) +); +//return the JSON results +echo "data: {\"result\": 0,\"eud\":[{$JSON}]}\n\n"; +flush(); +die(); + + +function get_TraccarSession($url) { + file_get_contents($url); + + $cookies = array(); + //search for all cookies and return; we only need `JSESSIONID` + foreach ($http_response_header as $hdr) { + if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) { + parse_str($matches[1], $tmp); + $cookies += $tmp; + } + } + return $cookies; +} + +function get_TraccarPosition($url,$JSESSIONID) { +//set the headers and cookie +$opts = array( + 'http'=>array( + 'method'=>"GET", + 'header'=>"Accept-language: en\r\n" . + "Cookie: JSESSIONID=$JSESSIONID\r\n" + ) + ); + + $context = stream_context_create($opts); + + // Open the file using the HTTP headers set above + $file = file_get_contents($url, false, $context); + return $file; +} + +function get_FTSAPI($id, $latitude, $longitude) { + + //http://127.0.0.1:19023/ManagePresence/putPresence + //http://127.0.0.1:19023/ManagePresence/postPresence + $url = "http://127.0.0.1:19023/ManagePresence/postPresence"; + //FTS Bearer token + $token = "Bearer token"; + //generate GUID + $guid = vsprintf('%s%s-%s-4000-8%.3s-%s%s%s0',str_split(dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) ),4)); + + //Json data to post + $postData = array( + "uid" => $guid, + "how" => "nonCoT", + "name" => "id".$id, + "longitude" => $longitude, + "latitude" => $latitude, + "role" => "Team Member", + "team" => "Red" + ); + + // for sending data as json type + $fields = json_encode($postData); + + $ch = curl_init($url); + //set cURL options + curl_setopt( + $ch, + CURLOPT_HTTPHEADER, + array( + 'Content-Type: application/json', + 'Authorization: '.$token + ) + ); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); + curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); + //execute cURL call + $result = curl_exec($ch); + curl_close($ch); + + return $result; +} +?> \ No newline at end of file