Skip to content
Permalink
Browse files

Added default views and router which makes it more easier to setup

  • Loading branch information...
Cannonb4ll committed Feb 25, 2019
1 parent cde5bcf commit 801b8df3617a9d72b719012894dffc9433a1ab94
@@ -75,31 +75,16 @@ PHILIPS_HUE_USERNAME= <-- We will get this in below step

Next we will have to create a user to be able to authenticate with your bridge.

Quickly add this to your web.php routes file (this is temporary, you can delete later) so we can get a username:
The package will register 2 routes for you:

```php
Route::get('start-hue', function(){
$hue = new HueClient();
http://{app-url}/hue/auth

return redirect($hue->startOAuth());
});
http://{app-url}/hue/auth/receive

Route::get('hue', function () {
$hue = new HueClient();
if ($code = request('code')) {
$hue->getAccessTokenForTheFirstTime(request('code'));
dd($hue->users()->create('your-username'));
}
dd($hue->lights()->all());
});
```
Visit `/hue/auth` first to start creating a user.

Now visit your application with /start-hue at the end so oAuth2 login can begin.
You will be prompted to allow your own application permissions, accept this, you will be redirected to your own application.
This is the point when you redirect that you will receive the username in the `dd()` method.
This is the point when you redirect that you will receive the username in the view from the package.

Grab this username (this is saved in your bridge), and also enter this in your `.env` file:

@@ -109,6 +94,19 @@ PHILIPS_HUE_USERNAME=

This is it! Now you should be able to execute the methods which we describe below.

You can also disable the default routes from the package by adding `routes => false` to your `services.php` file:

```php
'philips-hue' => [
'client_id' => env('PHILIPS_HUE_CLIENT_ID'),
'client_secret' => env('PHILIPS_HUE_CLIENT_SECRET'),
'app_id' => env('PHILIPS_HUE_APP_ID'),
'device_id' => env('PHILIPS_HUE_DEVICE_ID'),
'user' => env('PHILIPS_HUE_USERNAME'),
'routes' => false
]
```

All the access data is saved in the `storage/app/hue.json` file, this contains the keys to access Philips Hue API.

## Methods
@@ -0,0 +1,83 @@
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Philips Hue - By Dennis Smink</title>

<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
height: 100vh;
margin: 0;
}
.full-height {
height: 50vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
}
.title {
font-size: 84px;
text-align: center;
}
.m-b-md {
margin-bottom: 30px;
}
code {
color: #c7254e;
background-color: #f9f2f4;
border-radius: 4px;
padding: 2px 4px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Philips Hue
</div>

<div class="links">
<p>
Enter this username in your <code>.env</code> with this variable: <code>PHILIPS_HUE_USERNAME</code>
</p>
<strong>Username:</strong> {{ request('username') }}

<p>
If you entered the username in your <code>.env</code> file, click the bottom link to view your lights.<br/>
<a href="{{ action('\Philips\Hue\Http\Controllers\HueController@receive') }}">View Lights</a>
</p>
</div>
</div>
</div>
</body>
</html>
@@ -0,0 +1,119 @@
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Philips Hue Lights - By Dennis Smink</title>

<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
height: 100vh;
margin: 0;
}
.full-height {
height: 50vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
}
.title {
font-size: 84px;
text-align: center;
}
.m-b-md {
margin-bottom: 30px;
}
code {
color: #c7254e;
background-color: #f9f2f4;
border-radius: 4px;
padding: 2px 4px;
}
#lights {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#lights td, #lights th {
border: 1px solid #ddd;
padding: 8px;
}
#lights tr:nth-child(even) {
background-color: #f2f2f2;
}
#lights tr:hover {
background-color: #ddd;
}
#lights th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #0C65D0;
color: white;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Philips Hue Lights
</div>

<table id="lights">
<thead>
<tr>
<th>Name</th>
<th>Product name</th>
<th>State</th>
<th>Model</th>
</tr>
</thead>
<tbody>
@foreach($lights as $light)
<tr>
<td>{{ $light->name }}</td>
<td>{{ $light->productname }}</td>
<td>{{ $light->state->on ? 'On' : 'Off' }}</td>
<td>{{ $light->modelid }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</body>
</html>
@@ -0,0 +1,4 @@
<?php
Route::get('/hue/auth', 'Philips\Hue\Http\Controllers\HueController@auth');
Route::get('/hue/auth/receive', 'Philips\Hue\Http\Controllers\HueController@receive');
@@ -0,0 +1,41 @@
<?php
namespace Philips\Hue\Http\Controllers;
use Philips\Hue\HueClient;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class HueController extends Controller
{
public function auth()
{
$hue = new HueClient;
return redirect($hue->startOAuth());
}
public function receive(Request $request)
{
$hue = new HueClient;
if ($code = $request->input('code')) {
$hue->getAccessTokenForTheFirstTime($code);
$username = $hue->users()->create();
return redirect()->action('\Philips\Hue\Http\Controllers\HueController@receive', ['username' => $username]);
}
// This is being triggered after we created our first user, so the developer can enter the username
// in the .env file.
if ($username = $request->input('username')) {
return view('hue::auth', compact('username', 'hueCredentials'));
}
// If all is well, we display a default view to show its working.
$lights = $hue->lights()->all();
return view('hue::lights', compact('lights'));
}
}
@@ -21,7 +21,7 @@ class HueUser extends HueClient
public function create($name = null, $forceLinkButton = true)
{
if (!$name) {
$name = Str::random();
$name = Str::random(10);
}
if ($forceLinkButton) {
@@ -8,11 +8,10 @@ class PhilipsHueServiceProvider extends ServiceProvider
{
public function boot()
{
//
}
public function register()
{
//
// If we do not disable the routes, load in the roads & views.
if (config('services.philips-hue.routes', true)) {
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'hue');
}
}
}

0 comments on commit 801b8df

Please sign in to comment.
You can’t perform that action at this time.