Websocket access to all events/devices

eiten8 years ago

Hello,

is there a possibility to access all events over websockets? I mean not only the events for a user, but the events for all users?

Thanks, Edi

Anton Tananaev8 years ago

Well, if you link all devices to your user you will get all the events.

eiten8 years ago

Ok, thanks. The Idea was to get all events and send Telegram notifications to the "owner" of the event. So I'll solve this with multiple instances of the websocket listener for the Moment and if I get more clients, maybe I'll do a direct integration into the server.

Anton Tananaev8 years ago

You might want to use event forwarding:

https://www.traccar.org/configuration-file/

eiten8 years ago

Ah, that sounds good... but somehow, I don't get much data in the POST... I was following
this post.
traccar.xml:

<entry key='event.forward.enable'>true</entry>
<entry key='event.forward.url'>http://localhost/traccar/event.php</entry>
<entry key='event.forward.header'>
    content-type: application/json
    cache-control: no-cache
</entry>
<entry key='forward.enable'>true</entry>
<entry key='forward.url'>http://localhost/traccar/position.php</entry>

event.php:

<?php
  $log_dir = '/tmp/';
  $log_name = "posts-" . $_SERVER['REMOTE_ADDR'] . "-" . date("Y-m-d-H") . ".log";
  $log_entry = gmdate('r') . "\t" . $_SERVER['REQUEST_URI'] . "\n\tpost: " . serialize($_POST) . "\n\ttype: " . $_POST['event']['type'] . PHP_EOL;
  $fp=fopen( $log_dir . $log_name, 'a' );
  fputs($fp, $log_entry);
  fclose($fp);
?>

Result (pressing SOS on Android client):

Mon, 15 May 2017 13:21:22 +0000 /traccar/event.php
        post: a:0:{}
        type:
jaimzj8 years ago

Hello eiten,

Here is what I have done in my php file, if this helps you.

<?php
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

$type = $data->{'event'}->{'type'};
$serverTime = convertUTCToIST($data->{'event'}->{'serverTime'});
$deviceName = $data->{'device'}->{'name'};
$alarmType = $data->{'event'}->{'attributes'};
$alarmTypeReturn = $alarmType->{'alarm'};
$deviceId = $data->{'event'}->{'deviceId'};
$geofenceId = $data->{'event'}->{'geofenceId'};

?>

You can further build, and customize this to your requirement. ensure the event forwarder is pointing to the php with the above code.

eiten8 years ago

Hello Nefretiti,

cool, that works perfectly! Thanks! I understood that the data is forwarded as POST data.

HTH, Edi

Haim Rodrik5 years ago

Hello jaimzj , I will implement the line as below

traccar.xml

 <entry key='event.forward.enable'>true</entry>
<entry key='event.forward.url'>http://localhost/traccar/event.php</entry>
<entry key='event.forward.header'>
    content-type: application/json
    cache-control: no-cache
</entry>

I don't have any devices on the Traccar server, developing devices. I have only Traccar Android for server test communication. Do you have an example data log to send and see if it works? Is forwarded event data includes GPS location info?

event.php

<?php
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

$type = $data->{'event'}->{'type'};
$serverTime = convertUTCToIST($data->{'event'}->{'serverTime'});
$deviceName = $data->{'device'}->{'name'};
$alarmType = $data->{'event'}->{'attributes'};
$alarmTypeReturn = $alarmType->{'alarm'};
$deviceId = $data->{'event'}->{'deviceId'};
$geofenceId = $data->{'event'}->{'geofenceId'};
?>