Skip to content

Commit b076d53

Browse files
committed
added files
1 parent 5024473 commit b076d53

File tree

4 files changed

+312
-2
lines changed

4 files changed

+312
-2
lines changed

README.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1-
# Takcar
2-
small PHP script to take lat long from Traccar and Show them in the FreeTakServer
1+
# TAKCAR
2+
3+
[<img src="img/f12.png" width="800"/>](img/f12.png)
4+
5+
Quick and dirty example of importing **Traccar** `Lon`, `Lat` coordinates into the **FreeTakServer**.
6+
7+
On page load the script automatically begins a loop getting lat,lon from *Traccar* and posting them to *FTS*.
8+
9+
The `Test Service` button just tests to see that you can see a result on *FTS*.
10+
11+
```JSON
12+
13+
{
14+
"uid": "999b5874-1ebf-11zz-9e70-4e58de281c19",
15+
"how": "nonCoT",
16+
"name": "POTUS",
17+
"longitude": -77.01385,
18+
"latitude": 38.889,
19+
"role": "Team Member",
20+
"team": "Yellow"
21+
}
22+
23+
```
24+
25+
## STEPS
26+
1. create an account and API Token in Traccar.
27+
28+
2. Use the `positions` endpoint in Traccar to get the *latitude* and *longitude* into vars.
29+
30+
```HTTP
31+
http://127.0.0.1:8082/api/positions?token=Fm9OhNJS7SShyw80M8kdqKMcSiOuqqhA
32+
```
33+
34+
3. Using the `postPresence` endpoint in the FreeTakServer Import the *latitude* and *longitude* vars into FreeTakServer.
35+
36+
```HTTP
37+
http://127.0.0.1:19023/ManagePresence/postPresence
38+
```
39+
40+
## Notes
41+
42+
The scripts assume `19023` is the *FTS* API port and that `8082` is the *Traccar* API port.
43+
44+
It is also assumed that the scripts are running on the same machine as *FTS* and *Traccar*.
45+
46+
As long as the page is open the loop will continue to run but will stop on close.

img/f12.png

29.6 KB
Loading

index.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>TAKCAR SERVICE</title>
5+
6+
<!-- Optional theme -->
7+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/darkly/bootstrap.min.css">
8+
9+
<!-- Jquery -->
10+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
11+
12+
<!-- Latest compiled and minified JavaScript -->
13+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
14+
15+
</head>
16+
<body>
17+
<br>
18+
<div class="container-fluid">
19+
20+
<div class="row">
21+
<h1 class="col-md-12">TAKCAR</h1>
22+
</div>
23+
24+
<div class="row">
25+
<h6 class="col-md-12">Quick and dirty example of importing Traccar Lon, Lat coordinates into the FreeTakServer.</h6>
26+
</div>
27+
28+
<div class="row">
29+
<div class="col-lg-6">
30+
<div class="bs-component">
31+
<div class="form-group">
32+
<br>
33+
<button id="testFunction" type="button" onclick="testFunction()" class="btn btn-primary">Test Service</button>
34+
<p id="data" style="margin:10px;"></p>
35+
<br>
36+
<!-- <table id="table" class="table table">
37+
<thead>
38+
<tr>
39+
<th scope="col">#</th>
40+
<th scope="col">Result</th>
41+
<th scope="col">Time</th>
42+
<th scope="col">Data</th>
43+
</tr>
44+
</thead>
45+
<tbody>
46+
<tr>
47+
<th scope="row">1</th>
48+
<td style="color:;"></td>
49+
<td></td>
50+
<td></td>
51+
</tr>
52+
</tbody>
53+
</table> -->
54+
</div>
55+
</div>
56+
</div>
57+
58+
59+
60+
</div>
61+
62+
<script>
63+
if(typeof(EventSource) !== "undefined") {
64+
65+
//set endpoint
66+
var source = new EventSource("serverUpdater.php");
67+
68+
//prints onmessage result to console
69+
source.addEventListener('message', function(e) {
70+
//console.log("message", e.data);
71+
var inputChecked;
72+
//Parse the JSON data
73+
const obj = JSON.parse(e.data);
74+
//console.log(obj);
75+
if(obj.result == 0)
76+
{
77+
//loop through each device
78+
$.each(obj.eud[0].message, function(key , devices){
79+
var table = new Device(devices);
80+
//display in table in console
81+
console.table(table);
82+
});
83+
}
84+
}, false);
85+
86+
function Device(device) {
87+
this.device = device;
88+
}
89+
90+
} else {
91+
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
92+
}
93+
94+
function uuidv4() {
95+
//Generate GUID
96+
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
97+
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
98+
);
99+
}
100+
101+
// function to handle success
102+
function success() {
103+
//var data = JSON.parse(this.responseText); //parse the string to JSON
104+
//console.log('Success:', data);
105+
alert('Success: ' + JSON.stringify(this.responseText));
106+
}
107+
108+
// function to handle error
109+
function error(err) {
110+
console.log('Request Failed', err); //error details will be in the "err" object
111+
alert('Failed: ' + JSON.stringify(err));
112+
}
113+
114+
var Authorization = "Bearer token";
115+
116+
function testFunction(){
117+
var xhr = new XMLHttpRequest(); //invoke a new instance of the XMLHttpRequest
118+
xhr.onload = success; // call success function if request is successful
119+
xhr.onerror = error; // call error function if request failed
120+
121+
var json = {uid: uuidv4(),
122+
how: "nonCoT",
123+
name: "POTUS",
124+
longitude: -77.01385,
125+
latitude: 38.889,
126+
role: "Team Member",
127+
team: "Yellow"};//JSON object
128+
129+
xhr.open('POST', 'http://127.0.0.1:19023/ManagePresence/postPresence', true); // open a GET request
130+
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');//application/json;charset=UTF-8
131+
xhr.setRequestHeader('Authorization', Authorization);//application/json;charset=UTF-8
132+
xhr.send(JSON.stringify(json)); // send the request to the server.
133+
}
134+
135+
</script>
136+
</body>
137+
</html>

serverUpdater.php

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
// set headers
3+
header('Content-Type: text/event-stream');
4+
header('Cache-Control: no-cache');
5+
header('Connection: keep-alive');
6+
7+
//get the session cookie
8+
$session = json_encode(get_TraccarSession("http://127.0.0.1:8082/api/session?token=hdbtrsy6576hyw84567cSiOuqqhA"));
9+
10+
$api_result = json_decode($session, true);
11+
$JSESSIONID = $api_result['JSESSIONID'];
12+
13+
//get the Traccar positions
14+
$positions = get_TraccarPosition("http://127.0.0.1:8082/api/positions?token=hdbtrsy6576hyw84567cSiOuqqhA",$JSESSIONID);
15+
16+
$json = json_decode($positions);
17+
18+
//loop through the positions
19+
foreach($json as $key => $item){
20+
// $markers[] = json_encode(
21+
// array(
22+
// 'id' => $item->id,
23+
// 'latitude' => $item->latitude,
24+
// 'longitude' => $item->longitude,
25+
// 'batteryLevel' => $item->attributes->battery,
26+
// 'Time' => date("Y-m-d h:i:sa")
27+
// )
28+
// );
29+
30+
//forward the positions to the FTS API endpoint
31+
$result = get_FTSAPI($item->id, $item->latitude, $item->longitude);
32+
33+
$markers[] = json_encode(
34+
array(
35+
'result' => 0,
36+
'guid' => $result,
37+
'Time' => date("Y-m-d h:i:sa")
38+
)
39+
);
40+
}
41+
42+
$JSON = json_encode(
43+
array(
44+
'result' => 0,
45+
'message' => $markers,
46+
'Time' => date("Y-m-d h:i:sa"),
47+
)
48+
);
49+
//return the JSON results
50+
echo "data: {\"result\": 0,\"eud\":[{$JSON}]}\n\n";
51+
flush();
52+
die();
53+
54+
55+
function get_TraccarSession($url) {
56+
file_get_contents($url);
57+
58+
$cookies = array();
59+
//search for all cookies and return; we only need `JSESSIONID`
60+
foreach ($http_response_header as $hdr) {
61+
if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
62+
parse_str($matches[1], $tmp);
63+
$cookies += $tmp;
64+
}
65+
}
66+
return $cookies;
67+
}
68+
69+
function get_TraccarPosition($url,$JSESSIONID) {
70+
//set the headers and cookie
71+
$opts = array(
72+
'http'=>array(
73+
'method'=>"GET",
74+
'header'=>"Accept-language: en\r\n" .
75+
"Cookie: JSESSIONID=$JSESSIONID\r\n"
76+
)
77+
);
78+
79+
$context = stream_context_create($opts);
80+
81+
// Open the file using the HTTP headers set above
82+
$file = file_get_contents($url, false, $context);
83+
return $file;
84+
}
85+
86+
function get_FTSAPI($id, $latitude, $longitude) {
87+
88+
//http://127.0.0.1:19023/ManagePresence/putPresence
89+
//http://127.0.0.1:19023/ManagePresence/postPresence
90+
$url = "http://127.0.0.1:19023/ManagePresence/postPresence";
91+
//FTS Bearer token
92+
$token = "Bearer token";
93+
//generate GUID
94+
$guid = vsprintf('%s%s-%s-4000-8%.3s-%s%s%s0',str_split(dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) ),4));
95+
96+
//Json data to post
97+
$postData = array(
98+
"uid" => $guid,
99+
"how" => "nonCoT",
100+
"name" => "id".$id,
101+
"longitude" => $longitude,
102+
"latitude" => $latitude,
103+
"role" => "Team Member",
104+
"team" => "Red"
105+
);
106+
107+
// for sending data as json type
108+
$fields = json_encode($postData);
109+
110+
$ch = curl_init($url);
111+
//set cURL options
112+
curl_setopt(
113+
$ch,
114+
CURLOPT_HTTPHEADER,
115+
array(
116+
'Content-Type: application/json',
117+
'Authorization: '.$token
118+
)
119+
);
120+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
121+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
122+
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
123+
//execute cURL call
124+
$result = curl_exec($ch);
125+
curl_close($ch);
126+
127+
return $result;
128+
}
129+
?>

0 commit comments

Comments
 (0)