Commands API Help Needed

jaimzj8 years ago

Dear Anton / All,

Has anybody implemented commands API Via PHP.

I am interested in knowing a little bit on how to issue commands to a particular device via PHP.

What are the parameters to be posted example, such as device id?..

I am using 3.5 version Traccar, However in swagger file I am not able to figure out how or what to do with commands.

Anton Tananaev8 years ago

Have you tried to search forum and GitHub. There are some PHP threads out there, so you can try to ask in one of those.

jaimzj8 years ago

Dear Anton, One doubt.

I haven't found any good example posts here in forum and GitHub, However my doubt is.

Say I manage to send a command via web-services., How do I handle or read the response from the command I sent.

-James

Anton Tananaev8 years ago

Usually you just receive a message from device later as a confirmation.

bbeepp8 years ago

Try this.

class traccar {

    public static $host='http://localhost:8082';

    private static $adminEmail='admin';
    
    private static $adminPassword='admin';

    public static $cookie;

    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('Content-Type: application/json'));
    }

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

        $data='email='.$email.'&password='.$password;
        
        return self::curl('/api/session', 'POST','' ,$data,array('Content-Type: application/x-www-form-urlencoded'));
    }

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

        $data='{"id":'.$id.',"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('Content-Type: application/json'));
    }

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

        $data='{"id":'.$id.',"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('Content-Type: application/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('Content-Type: application/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('Content-Type: application/json'));
    }

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

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

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

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

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

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

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

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

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

    public static function logout($cookie) {
        
        return self::curl('/api/session', 'DELETE',$cookie ,'',array('Content-Type: application/x-www-form-urlencoded'));
    }

    public static function periodicReporting($deviceId,$frequency,$unit,$cookie) {

        $data='{"deviceId": '.$deviceId.', "type": "positionPeriodic", "id": -1, "attributes": {"frequency": '.$frequency*$unit.'}}';

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

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

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

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

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

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

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

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

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

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

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

        $res=new stdClass();

        $res->responseCode='';

        $res->error='';

        $header[]="Cookie: ".$cookie;

        $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);
        }

        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;
    }
}


$t=traccar::login($email,$password);

if($t->responseCode=='200') {

    $traccarCookie = traccar::$cookie;

    $t = traccar::engineStop($deviceId, $traccarCookie);

    if($t->responseCode=='200') {

        echo 'ok';
    }
    else {

        $response=json_decode($t->response);

        echo $response->details;
    }
}
else echo 'Incorrect email address or password';
jaimzj8 years ago

Dear @bbeepp

I am gonna try this, this is absolutely amazing by the looks :)

-Thanks a lot.