device connection attribute.

I'm trying to create a notification that uses the time of the last device connection. For example, if the device is offline for 5 days, it will send an email notification.

Has anyone managed to do this by chance?

Anton Tananaev 3 years ago

That's called device inactivity notification. You need to configure the timeout attributes on the device.

how can i do this on the platform. I already tried in notification but I didn't find inactivity of the device. should I configure in the calculated attribute?
if you can help me thanks.

I'll leave a code in PHP that I'm using to solve my problem for now. it tells me which devices have been off for more than 5 days.

<?php
$traccarUrl = 'http://your_server_traccar/api/';
$apiUsername = 'your_user_api';
$apiPassword = 'your_pass_api';

// Function to send a notification
function enviarNotificacao($mensagem) {
    // Implement the logic here to send the notification, for example, send an email, a text message, etc.
    // You can use external libraries or services to send the notification.
    // Example:
    // send email
    // mail('seu_email', 'Traccar - Device Offline', $mensagem);
}

// Function to get the current date minus 5 days
function getDataMenos5Dias() {
    $hoje = new DateTime();
    $hoje->sub(new DateInterval('P5D'));
    return $hoje->format('Y-m-d H:i:s');
}

// Makes a request to the Traccar API to get the devices
$apiUrl = $traccarUrl . 'devices';
$context = stream_context_create([
    'http' => [
        'header' => "Authorization: Basic " . base64_encode("$apiUsername:$apiPassword"),
    ],
]);
$response = file_get_contents($apiUrl, false, $context);

// Checks if the request was successful
if ($response !== false) {
    // Convert JSON response to an associative array
    $data = json_decode($response, true);

    // Checks every device
    foreach ($data as $device) {
        $lastUpdate = $device['lastUpdate'];
        $deviceId = $device['id'];

        // Checks if the device has been disconnected for more than 5 days
        if ($lastUpdate < getDataMenos5Dias()) {
            // Sends a notification stating device disconnected
            $mensagem = "Device with ID $deviceId has been disconnected for more than 5 days.";
            enviarNotificacao($mensagem);
        }
    }
} else {
    // Handle the request error
    echo "Error when making a request to the Traccar API";
}
?>
Anton Tananaev 3 years ago

You don't need computed attributes for it.