gboudreau / nissan-connect-php

Use the Nissan Connect (was Carwings) API using a simple PHP class.
GNU General Public License v3.0
38 stars 17 forks source link

Find my car function #14

Closed carestad closed 7 years ago

carestad commented 7 years ago

Is it possible to implement the find my car function as well to get the latitude and longitude of the car?

carestad commented 7 years ago

Something like this might work:

      public function getLocation() {
        $initResult = $this->sendRequest('MyCarFinderRequest.php');

        $result = new stdClass;
        $code = $initResult->status;

        if (200 == $code && !empty($this->resultKey)) {
          $start = time();

          while (true) {
            $result = $this->sendRequest('MyCarFinderResultRequest.php', ['resultKey' => $this->resultKey]);

            // Don't get stuck forever
            if (time() - $start < 100) {
              if (1 != $result->responseFlag) {
                echo "Still not ready...\n";
              }

              sleep(5);
              continue;
            }
            else {
              echo "Taking too long. Have to give up.\n";
            }

            break;
          }
        }

        return $result;
      }   
gboudreau commented 7 years ago

Is "find my car" a new feature in the Nissan LEAF official app, for newer LEAF models ? I have never seen that in my version of the app.

carestad commented 7 years ago

Hm, it might be? My app version is 3.0.6, last updated in November 2016. It's from Nissan Europe though, so maybe it's for European users? My Leaf is a 2016 model so it might be newer models only perhaps.

I also noticed the endpoint my app uses is a bit different, and all the requests goes to https://gdcportalgw.its-mo.com/gworchest_160803EC/gdc/

gboudreau commented 7 years ago

I'd need a PR to integrate this functionality, as I don't have access to it. If you tested the above function, and it works, I can integrate that.

carestad commented 7 years ago

Well kinda, but it has some debugging info that is unnecessary if it is to be pushed to master. I also realized that there was a waitUntilSuccess() method that did most of what I do there, so the simplest implementation of this is really just:

public function getLocation() {
  $result = $this->sendRequest('MyCarFinderRequest.php');
  return $this->waitUntilSuccess('MyCarFinderResultRequest.php');
}   

A var_dump of the result will give you something like this:

object(stdClass)#3 (9) {
  ["status"]=>
  int(200)
  ["responseFlag"]=>
  string(1) "1"
  ["resultCode"]=>
  string(1) "1"
  ["timeStamp"]=>
  string(19) "2017-10-12 18:38:19"
  ["Location"]=>
  object(stdClass)#12 (12) {
    ["Position"]=>
    string(11) "UNAVAILABLE"
    ["LocationType"]=>
    string(5) "WGS84"
    ["LatitudeMode"]=>
    string(5) "NORTH"
    ["LongitudeMode"]=>
    string(4) "EAST"
    ["Home"]=>
    string(7) "OUTSIDE"
    ["LatitudeDeg"]=>
    string(2) "99"
    ["LatitudeMin"]=>
    string(2) "99"
    ["LatitudeSec"]=>
    string(4) "9999"
    ["LongitudeDeg"]=>
    string(1) "9"
    ["LongitudeMin"]=>
    string(2) "99"
    ["LongitudeSec"]=>
    string(4) "9999"
    ["Country"]=>
    string(0) ""
  }
  ["receivedDate"]=>
  string(16) "2017/10/12 18:38"
  ["TargetDate"]=>
  string(16) "2017/10/12 18:38"
  ["lat"]=>
  string(15) "1.234567890"
  ["lng"]=>
  string(15) "1.234568790"
}

I can create a pull request for you.

gboudreau commented 7 years ago

Merged in #15

Raiden38 commented 7 years ago

Where is the MyCarFinderRequest.php file? Do we need it to make it work?

Can this be called at the same time then a normal status request? How? Thanks!

carestad commented 7 years ago

You just need to call the getLocation() method after getStatus(). For example:

$nc = new NissanConnect('YourUsername', 'YourPassword');
$status = $nc->getStatus();
$loc = $nc->getLocation();

var_dump($status, $loc);
gboudreau commented 7 years ago

@Raiden38 This is not available to everyone. If you have this feature in the Nissan official app, then the code posted by carestad should work.