API-Command : type = custom

binooetomo8 years ago

Dear All

For 'custom' type Command (ref: https://www.traccar.org/forums/topic/feature-request-opening-to-the-world/#post-5369) ,

If :
protocol : meitrack
deviceId=4
Command to send (copy paste from device's docs) = '@@Q25,353358017784062,A10*6A\r\n'

What Params I need to send ?

  1. is it : {'raw': '@@Q25,353358017784062,A10*6A\r\n', 'type': 'custom', 'deviceId': 4}, or
  2. is it : {'raw': '40405132352c3335333335383031373738343036322c4131302a36415c725c6ea', 'type': 'custom', 'deviceId': 4}

Note : I'm using python's 'requests', and still got 'Bad Request'

Sincerely
-bino-

Anton Tananaev8 years ago

Custom command is not supported for Meitrack yet.

binooetomo8 years ago

Dear Anton.

Thankyou for your verry fast response.

Could you please tell me which protocol the most supported by traccar ?

Sincerely
-bino-

Anton Tananaev8 years ago

It's hard to tell. GPS103 is a very popular one.

binooetomo8 years ago

Dear Anton.

I See.

Thankyou for your response.

-bino-0

smhhadima8 years ago

Dear Anton,

it Meitrack custom commands supported yet?

thank you

Anton Tananaev8 years ago

Custom command is supported for pretty much all protocols.

guerrab6 years ago

i want send custom command by traccar but has not working

$login = traccar::loginAdmin();
$traccarCookie = traccar::$cookie;

$deviceinfo    = traccar::sendCommand(60550,'custom','Nouveau','find',$traccarCookie);

echo '
';
print_r($deviceinfo);
echo '
';

Note : I'm using php 'requests'

Sincerely

guerrab6 years ago

my class php:
class traccar {

public static $host='http://demo.traccar.org';

private static $adminEmail    = 'admin';

private static $adminPassword = 'admin';

public static  $cookie;

private static $json='Content-Type: application/json';

private static $urlencoded='Content-Type: application/x-www-form-urlencoded';

public static function loginAdmin() {

    return self::login(self::$adminEmail,self::$adminPassword);
}

public static function register($name,$email,$password,$cookie='') {

    $data='{"name":"'.$name.'","email":"'.$email.'","password":"'.$password.'"}';

    return self::curl('/api/users','POST',$cookie,$data,array(self::$json));
}

public static function login($email,$password) {

    $data='email='.$email.'&password='.$password;

    return self::curl('/api/session','POST','',$data,array(self::$urlencoded));
}

public static function addUser($name,$email,$password,$cookie) {

    $data='{"id":-1,"name":"'.$name.'","email":"'.$email.'","password":"'.$password.'","admin":false,"map":"","distanceUnit":"","speedUnit":"","latitude":0,"longitude":0,"zoom":0,"twelveHourFormat":false}';

    return self::curl('/api/users/','POST',$cookie ,$data,array(self::$json));
}

public static function updateUser($id,$name,$email,$password,$cookie) {

    $password=$password!='' ? $password:'';

    $data='{"id":'.$id.',"attributes":{},"name":"'.$name.'","email":"'.$email.'","readonly":false,"admin":false,"map":"","distanceUnit":"","speedUnit":"","latitude":0,"longitude":0,"zoom":0,"twelveHourFormat":false,"password":"'.$password.'"}';

    return self::curl('/api/users/'.$id,'PUT',$cookie ,$data,array(self::$json));
}

public static function deleteUser($id,$name,$email,$cookie) {

    $data='{"id":'.$id.',"attributes":{},"name":"'.$name.'","email":"'.$email.'","readonly":false,"admin":false,"map":"","distanceUnit":"","speedUnit":"","latitude":0,"longitude":0,"zoom":0,"twelveHourFormat":false,"password":""}';

    return self::curl('/api/users/'.$id,'DELETE',$cookie ,$data,array(self::$json));
}

public static function addDevice($name,$uniqueId,$cookie) {

    $data='{"id":-1,"name":"'.$name.'","uniqueId":"'.$uniqueId.'","status":"","lastUpdate":null,"groupId":0}';

    return self::curl('/api/devices','POST',$cookie ,$data,array(self::$json));
}



public static function editDevice($id,$name,$uniqueId,$cookie) {

    $data='{"id":'.$id.',"name":"'.$name.'","uniqueId":"'.$uniqueId.'","status":"","lastUpdate":null,"groupId":0}';

    return self::curl('/api/devices/'.$id,'PUT',$cookie ,$data,array(self::$json));
}

public static function deleteDevice($id,$cookie) {

    $data='{"id":'.$id.',"status":"","lastUpdate":null,"groupId":0,"positionId":0}';

    return self::curl('/api/devices/'.$id,'DELETE',$cookie ,$data,array(self::$json));
}

public static function getDevicePositionID($id,$cookie) {

    $data='id='.$id.'';

    return self::curl('/api/devices?'.$data,'GET',$cookie ,'',array(self::$json));
}

public static function addDevicePermissions($userId,$deviceId,$cookie) {

    $data='{"userId":'.$userId.',"deviceId":'.$deviceId.'}';

    return self::curl('/api/permissions/devices','POST',$cookie ,$data,array(self::$json));
}

public static function deleteDevicePermissions($userId,$deviceId,$cookie) {

    $data='{"userId":'.$userId.',"deviceId":'.$deviceId.'}';

    return self::curl('/api/permissions/devices','DELETE',$cookie ,$data,array(self::$json));
}

public static function reportsRoute($deviceId,$from,$to,$cookie) {
    $data='deviceId='.$deviceId.'&from='.$from.'&to='.$to;
    return self::curl('/api/reports/route?'.$data,'GET',$cookie ,'',array());
}

public static function reportsTrips($deviceId,$from,$to,$cookie) {
    $data='deviceId='.$deviceId.'&from='.$from.'&to='.$to;
    return self::curl('/api/reports/trips?'.$data,'GET',$cookie ,'',array());
}

public static function logout($cookie) {

    return self::curl('/api/session','DELETE',$cookie ,'',array(self::$urlencoded));
}

public static function addGeofance($name,$area,$cookie) {

    $data='{"id":-1,"name":"'.$name.'","description":"","area":"'.$area.'"}';

    return self::curl('/api/geofences','POST',$cookie ,$data,array(self::$json));
}

public static function editGeofance($id,$name,$area,$cookie) {

    $data='{"id":'.$id.',"attributes":{},"name":"'.$name.'","description":"","area":"'.$area.'"}';

    return self::curl('/api/geofences/'.$id,'PUT',$cookie,$data,array(self::$json));
}

public static function deleteGeofance($id,$name,$area,$cookie) {

    $data='{"id":'.$id.',"attributes":{},"name":"'.$name.'","description":"","area":"'.$area.'"}';

    return self::curl('/api/geofences/'.$id,'DELETE',$cookie ,$data,array(self::$json));
}

public static function addGeofancePermisions($userId,$geofenceId,$cookie) {

    $data='{"userId":'.$userId.',"geofenceId":'.$geofenceId.'}';

    return self::curl('/api/permissions/geofences','POST',$cookie ,$data,array(self::$json));
}

public static function deleteGeofancePermisions($userId,$geofenceId,$cookie) {

    $data='{"userId":'.$userId.',"geofenceId":'.$geofenceId.'}';

    return self::curl('/api/permissions/geofences','DELETE',$cookie ,$data,array(self::$json));
}

public static function addDeviceGeofance($deviceId,$geofenceId,$cookie) {

    $data='{"deviceId":'.$deviceId.',"geofenceId":'.$geofenceId.'}';

    return self::curl('/api/devices/geofences','POST',$cookie ,$data,array(self::$json));
}

public static function deleteDeviceGeofance($deviceId,$geofenceId,$cookie) {

    $data='{"deviceId":'.$deviceId.',"geofenceId":'.$geofenceId.'}';

    return self::curl('/api/devices/geofences', 'DELETE',$cookie ,$data,array(self::$json));
}

public static function addUsersNotifications($userId,$type,$attributes,$cookie) {

    $data='{"userId":'.$userId.',"type":"'.$type.'","attributes":'.$attributes.'}';

    return self::curl('/api/users/notifications','POST',$cookie ,$data,array(self::$json));
}

public static function positions($Id,$cookie) {

    $data='id='.$Id;

    return self::curl('/api/positions?'.$data,'GET',$cookie,'' ,array(self::$json));

}

public static function commandtypes($deviceId,$cookie) {

    $data='deviceId='.$deviceId;

    return self::curl('/api/commandtypes?'.$data,'GET',$cookie ,'',array());
}

public static function sendCommand($deviceId,$type,$description,$attributes,$cookie){

    if($type=='custom') {
        $attributes=',"attributes":{"data":"'.$attributes.'"}';
    }

    else if($type=='positionPeriodic'){
        $attributes=',"attributes":{"frequency":'.$attributes.'}';
    }



    $data='{"deviceId":'.$deviceId.',"type":"'.$type.'","description":"'.$description.'","id":-1'.$attributes.'}';

    return self::curl('/api/commands','POST',$cookie ,$data,array(self::$json));
}

public static function curl($task,$method,$cookie,$data,$header)
{

    $res=new stdClass();

    $res->responseCode='';

    $res->error='';










    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, self::$host.$task);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_setopt($ch, CURLOPT_HEADER, 1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

    if($method=='POST' || $method=='PUT' || $method=='DELETE') {

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


        $header[]="Cookie: ".$cookie;
    }
    else
    {
        $header = array('Accept: application/json',"Cookie: ".$cookie);
    }

    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

    $data=curl_exec($ch);

    $size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

    if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', substr($data, 0, $size), $c) == 1) self::$cookie = $c[1];

    $res->response = substr($data, $size);

    if(!curl_errno($ch)) {

        $res->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    }
    else {

        $res->responseCode=400;

        $res->error= curl_error($ch);
    }


    curl_close($ch);

    return $res;
}

}