-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from devbanana/feature/add-geolocation-support
Add geolocation support
- Loading branch information
Showing
11 changed files
with
454 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Request; | ||
|
||
/** | ||
* @author Brandon Olivares <[email protected]> | ||
*/ | ||
class Altitude | ||
{ | ||
/** | ||
* @var float | ||
*/ | ||
public $altitudeInMeters; | ||
|
||
/** | ||
* @var float|null | ||
*/ | ||
public $accuracyInMeters; | ||
|
||
/** | ||
* @param array $amazonRequest | ||
* | ||
* @return Altitude | ||
*/ | ||
public static function fromAmazonRequest(array $amazonRequest): self | ||
{ | ||
$altitude = new self(); | ||
|
||
$altitude->altitudeInMeters = floatval($amazonRequest['altitudeInMeters']); | ||
$altitude->accuracyInMeters = isset($amazonRequest['accuracyInMeters']) ? floatval($amazonRequest['accuracyInMeters']) : null; | ||
|
||
return $altitude; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Request; | ||
|
||
/** | ||
* @author Brandon Olivares <[email protected]> | ||
*/ | ||
class Coordinate | ||
{ | ||
/** | ||
* @var float | ||
*/ | ||
public $latitudeInDegrees; | ||
|
||
/** | ||
* @var float | ||
*/ | ||
public $longitudeInDegrees; | ||
|
||
/** | ||
* @var float | ||
*/ | ||
public $accuracyInMeters; | ||
|
||
/** | ||
* @param array $amazonRequest | ||
* | ||
* @return Coordinate | ||
*/ | ||
public static function fromAmazonRequest(array $amazonRequest): self | ||
{ | ||
$coordinate = new self(); | ||
|
||
$coordinate->latitudeInDegrees = floatval($amazonRequest['latitudeInDegrees']); | ||
$coordinate->longitudeInDegrees = floatval($amazonRequest['longitudeInDegrees']); | ||
$coordinate->accuracyInMeters = floatval($amazonRequest['accuracyInMeters']); | ||
|
||
return $coordinate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Request; | ||
|
||
/** | ||
* @author Brandon Olivares <[email protected]> | ||
*/ | ||
class Geolocation | ||
{ | ||
/** | ||
* @var LocationServices|null | ||
*/ | ||
public $locationServices; | ||
|
||
/** | ||
* @var \DateTime | ||
*/ | ||
public $timestamp; | ||
|
||
/** | ||
* @var Coordinate|null | ||
*/ | ||
public $coordinate; | ||
|
||
/** | ||
* @var Altitude|null | ||
*/ | ||
public $altitude; | ||
|
||
/** | ||
* @var Heading|null | ||
*/ | ||
public $heading; | ||
|
||
/** | ||
* @var Speed|null | ||
*/ | ||
public $speed; | ||
|
||
/** | ||
* @param array $amazonRequest | ||
* | ||
* @return Geolocation | ||
*/ | ||
public static function fromAmazonRequest(array $amazonRequest): self | ||
{ | ||
$geolocation = new self(); | ||
|
||
$geolocation->locationServices = isset($amazonRequest['locationServices']) ? LocationServices::fromAmazonRequest($amazonRequest['locationServices']) : null; | ||
$geolocation->timestamp = new \DateTime($amazonRequest['timestamp']); | ||
$geolocation->coordinate = isset($amazonRequest['coordinate']) ? Coordinate::fromAmazonRequest($amazonRequest['coordinate']) : null; | ||
$geolocation->altitude = isset($amazonRequest['altitude']) ? Altitude::fromAmazonRequest($amazonRequest['altitude']) : null; | ||
$geolocation->heading = isset($amazonRequest['heading']) ? Heading::fromAmazonRequest($amazonRequest['heading']) : null; | ||
$geolocation->speed = isset($amazonRequest['speed']) ? Speed::fromAmazonRequest($amazonRequest['speed']) : null; | ||
|
||
return $geolocation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Request; | ||
|
||
/** | ||
* @author Brandon Olivares <[email protected]> | ||
*/ | ||
class Heading | ||
{ | ||
/** | ||
* @var float | ||
*/ | ||
public $directionInDegrees; | ||
|
||
/** | ||
* @var float|null | ||
*/ | ||
public $accuracyInDegrees; | ||
|
||
/** | ||
* @param array $amazonRequest | ||
* | ||
* @return Heading | ||
*/ | ||
public static function fromAmazonRequest(array $amazonRequest): self | ||
{ | ||
$heading = new self(); | ||
|
||
$heading->directionInDegrees = floatval($amazonRequest['directionInDegrees']); | ||
$heading->accuracyInDegrees = isset($amazonRequest['accuracyInDegrees']) ? floatval($amazonRequest['accuracyInDegrees']) : null; | ||
|
||
return $heading; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Request; | ||
|
||
/** | ||
* @author Brandon Olivares <[email protected]> | ||
*/ | ||
class LocationServices | ||
{ | ||
const ACCESS_ENABLED = 'ENABLED'; | ||
const ACCESS_DISABLED = 'DISABLED'; | ||
|
||
const STATUS_RUNNING = 'RUNNING'; | ||
const STATUS_STOPPED = 'STOPPED'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $access; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $status; | ||
|
||
/** | ||
* @param array $amazonRequest | ||
* | ||
* @return LocationServices | ||
*/ | ||
public static function fromAmazonRequest(array $amazonRequest): self | ||
{ | ||
$locationServices = new self(); | ||
|
||
$locationServices->access = $amazonRequest['access']; | ||
$locationServices->status = $amazonRequest['status']; | ||
|
||
return $locationServices; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Request; | ||
|
||
/** | ||
* @author Brandon Olivares <[email protected]> | ||
*/ | ||
class Speed | ||
{ | ||
/** | ||
* @var float | ||
*/ | ||
public $speedInMetersPerSecond; | ||
|
||
/** | ||
* @var float|null | ||
*/ | ||
public $accuracyInMetersPerSecond; | ||
|
||
/** | ||
* @param array $amazonRequest | ||
* | ||
* @return Speed | ||
*/ | ||
public static function fromAmazonRequest(array $amazonRequest): self | ||
{ | ||
$speed = new self(); | ||
|
||
$speed->speedInMetersPerSecond = floatval($amazonRequest['speedInMetersPerSecond']); | ||
$speed->accuracyInMetersPerSecond = isset($amazonRequest['accuracyInMetersPerSecond']) ? floatval($amazonRequest['accuracyInMetersPerSecond']) : null; | ||
|
||
return $speed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace MaxBeckers\AmazonAlexa\Test\Request; | ||
|
||
use MaxBeckers\AmazonAlexa\Request\Request; | ||
use MaxBeckers\AmazonAlexa\Request\Request\Standard\IntentRequest; | ||
use MaxBeckers\AmazonAlexa\Response\Card; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @author Brandon Olivares <[email protected]> | ||
*/ | ||
class GeolocationRequestTest extends TestCase | ||
{ | ||
public function testGeolocationNeedsPermission() | ||
{ | ||
$requestBody = file_get_contents(__DIR__.'/RequestData/geolocationPermission.json'); | ||
$request = Request::fromAmazonRequest($requestBody, 'https://s3.amazonaws.com/echo.api/echo-api-cert.pem', 'signature'); | ||
$this->assertInstanceOf(IntentRequest::class, $request->request); | ||
$this->assertArrayHasKey('Geolocation', $request->context->system->device->supportedInterfaces); | ||
$this->assertNull($request->context->geolocation); | ||
|
||
// Create permissions card. | ||
$card = Card::createAskForPermissionsConsent([Card::PERMISSION_GEOLOCATION]); | ||
$this->assertInstanceOf(Card::class, $card); | ||
} | ||
|
||
public function testGetGeolocationData() | ||
{ | ||
$requestBody = file_get_contents(__DIR__.'/RequestData/geolocation.json'); | ||
$request = Request::fromAmazonRequest($requestBody, 'https://s3.amazonaws.com/echo.api/echo-api-cert.pem', 'signature'); | ||
$this->assertInstanceOf(IntentRequest::class, $request->request); | ||
$this->assertArrayHasKey('Geolocation', $request->context->system->device->supportedInterfaces); | ||
|
||
$geolocation = $request->context->geolocation; | ||
$this->assertNotNull($geolocation); | ||
|
||
$this->assertNotNull($geolocation->locationServices); | ||
$this->assertEquals('ENABLED', $geolocation->locationServices->access); | ||
$this->assertEquals('RUNNING', $geolocation->locationServices->status); | ||
$this->assertEquals(new \DateTime('2019-06-12T19:13:01+00:00'), $geolocation->timestamp); | ||
$this->assertNotNull($geolocation->coordinate); | ||
$this->assertEquals(40.3, $geolocation->coordinate->latitudeInDegrees); | ||
$this->assertEquals(-78.9, $geolocation->coordinate->longitudeInDegrees); | ||
$this->assertEquals(100, $geolocation->coordinate->accuracyInMeters); | ||
$this->assertNotNull($geolocation->altitude); | ||
$this->assertEquals(600, $geolocation->altitude->altitudeInMeters); | ||
$this->assertEquals(100, $geolocation->altitude->accuracyInMeters); | ||
$this->assertNotNull($geolocation->heading); | ||
$this->assertEquals(48.12, $geolocation->heading->directionInDegrees); | ||
$this->assertEquals(5, $geolocation->heading->accuracyInDegrees); | ||
$this->assertNotNull($geolocation->speed); | ||
$this->assertEquals(1, $geolocation->speed->speedInMetersPerSecond); | ||
$this->assertEquals(1.0, $geolocation->speed->accuracyInMetersPerSecond); | ||
} | ||
} |
Oops, something went wrong.