-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5024473
commit b076d53
Showing
4 changed files
with
312 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,46 @@ | ||
# Takcar | ||
small PHP script to take lat long from Traccar and Show them in the FreeTakServer | ||
# TAKCAR | ||
|
||
[<img src="img/f12.png" width="800"/>](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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>TAKCAR SERVICE</title> | ||
|
||
<!-- Optional theme --> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/darkly/bootstrap.min.css"> | ||
|
||
<!-- Jquery --> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> | ||
|
||
<!-- Latest compiled and minified JavaScript --> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> | ||
|
||
</head> | ||
<body> | ||
<br> | ||
<div class="container-fluid"> | ||
|
||
<div class="row"> | ||
<h1 class="col-md-12">TAKCAR</h1> | ||
</div> | ||
|
||
<div class="row"> | ||
<h6 class="col-md-12">Quick and dirty example of importing Traccar Lon, Lat coordinates into the FreeTakServer.</h6> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col-lg-6"> | ||
<div class="bs-component"> | ||
<div class="form-group"> | ||
<br> | ||
<button id="testFunction" type="button" onclick="testFunction()" class="btn btn-primary">Test Service</button> | ||
<p id="data" style="margin:10px;"></p> | ||
<br> | ||
<!-- <table id="table" class="table table"> | ||
<thead> | ||
<tr> | ||
<th scope="col">#</th> | ||
<th scope="col">Result</th> | ||
<th scope="col">Time</th> | ||
<th scope="col">Data</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<th scope="row">1</th> | ||
<td style="color:;"></td> | ||
<td></td> | ||
<td></td> | ||
</tr> | ||
</tbody> | ||
</table> --> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
|
||
</div> | ||
|
||
<script> | ||
if(typeof(EventSource) !== "undefined") { | ||
//set endpoint | ||
var source = new EventSource("serverUpdater.php"); | ||
//prints onmessage result to console | ||
source.addEventListener('message', function(e) { | ||
//console.log("message", e.data); | ||
var inputChecked; | ||
//Parse the JSON data | ||
const obj = JSON.parse(e.data); | ||
//console.log(obj); | ||
if(obj.result == 0) | ||
{ | ||
//loop through each device | ||
$.each(obj.eud[0].message, function(key , devices){ | ||
var table = new Device(devices); | ||
//display in table in console | ||
console.table(table); | ||
}); | ||
} | ||
}, false); | ||
function Device(device) { | ||
this.device = device; | ||
} | ||
} else { | ||
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events..."; | ||
} | ||
function uuidv4() { | ||
//Generate GUID | ||
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => | ||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) | ||
); | ||
} | ||
// function to handle success | ||
function success() { | ||
//var data = JSON.parse(this.responseText); //parse the string to JSON | ||
//console.log('Success:', data); | ||
alert('Success: ' + JSON.stringify(this.responseText)); | ||
} | ||
// function to handle error | ||
function error(err) { | ||
console.log('Request Failed', err); //error details will be in the "err" object | ||
alert('Failed: ' + JSON.stringify(err)); | ||
} | ||
var Authorization = "Bearer token"; | ||
function testFunction(){ | ||
var xhr = new XMLHttpRequest(); //invoke a new instance of the XMLHttpRequest | ||
xhr.onload = success; // call success function if request is successful | ||
xhr.onerror = error; // call error function if request failed | ||
var json = {uid: uuidv4(), | ||
how: "nonCoT", | ||
name: "POTUS", | ||
longitude: -77.01385, | ||
latitude: 38.889, | ||
role: "Team Member", | ||
team: "Yellow"};//JSON object | ||
xhr.open('POST', 'http://127.0.0.1:19023/ManagePresence/postPresence', true); // open a GET request | ||
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');//application/json;charset=UTF-8 | ||
xhr.setRequestHeader('Authorization', Authorization);//application/json;charset=UTF-8 | ||
xhr.send(JSON.stringify(json)); // send the request to the server. | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
// set headers | ||
header('Content-Type: text/event-stream'); | ||
header('Cache-Control: no-cache'); | ||
header('Connection: keep-alive'); | ||
|
||
//get the session cookie | ||
$session = json_encode(get_TraccarSession("http://127.0.0.1:8082/api/session?token=hdbtrsy6576hyw84567cSiOuqqhA")); | ||
|
||
$api_result = json_decode($session, true); | ||
$JSESSIONID = $api_result['JSESSIONID']; | ||
|
||
//get the Traccar positions | ||
$positions = get_TraccarPosition("http://127.0.0.1:8082/api/positions?token=hdbtrsy6576hyw84567cSiOuqqhA",$JSESSIONID); | ||
|
||
$json = json_decode($positions); | ||
|
||
//loop through the positions | ||
foreach($json as $key => $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; | ||
} | ||
?> |