Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue #2 - Directory structure and composer file.

  • Loading branch information...
commit d487eb5a8b109b4fbfa485e337d133360cc123c7 1 parent 09fd72a
@sqmk authored
View
1  .gitignore
@@ -0,0 +1 @@
+composer.lock
View
29 LICENSE
@@ -0,0 +1,29 @@
+Software License Agreement (The BSD 3-Clause License)
+
+Copyright (c) 2012, Michael K. Squires
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of Michael K. Squires nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
6 bin/phue-authenticate
@@ -0,0 +1,6 @@
+#!/usr/bin/env php
+<?php
+
+require_once '../vendor/autoload.php';
+
+$client = new \Phue\Client('127.0.0.1');
View
28 composer.json
@@ -0,0 +1,28 @@
+{
+ "name": "sqmk/phue",
+ "description": "Phue",
+ "type": "library",
+ "keywords": [
+ "phue",
+ "philips",
+ "hue",
+ "client"
+ ],
+ "homepage": "http://github.com/sqmk/phue",
+ "license": "BSD-3-Clause",
+ "require": {
+ "php": "5.4.*"
+ },
+ "require-dev": {
+ "phpunit/PHPUnit": "3.7.*"
+ },
+ "autoload": {
+ "psr-0": {
+ "Phue": "library/",
+ "PhueTest": "tests/"
+ }
+ },
+ "bin": [
+ "bin/phue-authenticate"
+ ]
+}
View
121 library/Phue/Client.php
@@ -0,0 +1,121 @@
+<?php
+
+namespace Phue;
+
+use Phue\Transport\Http,
+ Phue\Command\CommandInterface;
+
+/**
+ * Client for connecting to Philips Hue bridge
+ */
+class Client
+{
+ /**
+ * Client name
+ */
+ const CLIENT_NAME = 'Phue';
+
+ /**
+ * Host address
+ *
+ * @var string
+ */
+ protected $host = null;
+
+ /**
+ * Username
+ *
+ * @var string
+ */
+ protected $username = null;
+
+ /**
+ * Transport
+ *
+ * @var TransportInterface
+ */
+ protected $transport = null;
+
+ /**
+ * Construct a Phue Client
+ *
+ * @param string $host Host address
+ * @param string $username Username
+ */
+ public function __construct($host, $username = null)
+ {
+ $this->host = $this->setHost($host);
+ $this->username = $this->setUsername($username);
+ }
+
+ /**
+ * Get host
+ *
+ * @return string Host address
+ */
+ public function getHost()
+ {
+ return $this->host;
+ }
+
+ /**
+ * Set host
+ *
+ * @return void
+ */
+ public function setHost($host)
+ {
+ $this->host = (string) $host;
+ }
+
+ /**
+ * Get username
+ *
+ * @return string Username
+ */
+ public function getUsername()
+ {
+ return $this->username;
+ }
+
+ /**
+ * Set username
+ *
+ * @param string $username Username
+ *
+ * @return void
+ */
+ public function setUsername($username)
+ {
+ $this->username = (string) $username;
+ }
+
+ /**
+ * Get transport
+ *
+ * @return TransportInterface
+ */
+ protected function getTransport()
+ {
+ // Set transport if haven't
+ if (!$this->transport) {
+ $this->transport = new Http($this);
+ }
+
+ return $this->transport;
+ }
+
+ /**
+ * Send command to server
+ *
+ * @param Command $command Phue command
+ *
+ * @return void
+ */
+ public function sendCommand(Command $command)
+ {
+ // Get transport and set connection details by command
+ $transport = $this->getTransport();
+ $transport->sendRequestByCommand($command);
+ }
+}
View
22 library/Phue/Command/Authenticate.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace Phue\Command;
+
+use Phue\Client,
+ Phue\Command\CommandInterface;
+
+/**
+ * Authenticate command
+ */
+class Authenticate implements CommandInterface
+{
+ public function getMethod()
+ {
+ return 'POST';
+ }
+
+ public function getPath()
+ {
+ return 'api/'
+ }
+}
View
30 library/Phue/Command/CommandInterface.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Phue\Command;
+
+/**
+ * Command Interface
+ */
+interface CommandInterface
+{
+ /**
+ * Get method for command
+ *
+ * @return string Method name
+ */
+ public function getMethod();
+
+ /**
+ * Get path for command
+ *
+ * @return string Path name
+ */
+ public function getPath();
+
+ /**
+ * Get data for command
+ *
+ * @return stdClass Object
+ */
+ public function getData();
+}
View
111 library/Phue/Transport/Http.php
@@ -0,0 +1,111 @@
+<?php
+
+namespace Phue\Transport;
+
+use Phue\Client,
+ Phue\Command\CommandInterface,
+ Phue\Transport\TransportInterface;
+
+/**
+ * Http transport
+ */
+class Http implements TransportInterface
+{
+ /**
+ * Phue Client
+ *
+ * @var Client
+ */
+ protected $client = null;
+
+ /**
+ * Curl connection
+ *
+ * @var resource Curl resource
+ */
+ protected $connection = null;
+
+ /**
+ * Construct Http transport
+ *
+ * @param Client $client
+ */
+ public function __construct(Client $client)
+ {
+ $this->client = null;
+ }
+
+ /**
+ * Open connection
+ *
+ * @return void
+ */
+ protected function open()
+ {
+ // Don't continue if client already set
+ if ($this->client !== null) {
+ return;
+ }
+
+ $this->client = curl_init
+ }
+
+ /**
+ * Send request by command
+ *
+ * @param CommandInterface $command
+ *
+ * @return void
+ */
+ public function sendRequestByCommand(CommandInterface $command)
+ {
+ // Build base URL
+ $url = 'http://' . $this->client->getHost() . '/api/';
+
+ // Add username if one
+ if ($this->client->getUsername()) {
+ $url .= $this->client->getUsername() . '/';
+ }
+
+ // Initialize connection
+ $this->open();
+
+ // Set connection options
+ curl_setopt($this->connection, CURLOPT_URL, $url);
+ curl_setopt($this->connection, CURLOPT_HEADER, false);
+ curl_setopt($this->connection, CURLOPT_RETURNTRANSFER, true);
+
+ // Get response results
+ $results = curl_exec($this->connection);
+
+ // Close connection
+ $this->close();
+
+ var_dump($results);
+ }
+
+ /**
+ * Close connection
+ *
+ * @return void
+ */
+ protected function close()
+ {
+ // Don't continue if no connection
+ if ($this->connection === null) {
+ return;
+ }
+
+ curl_close($this->connection);
+ }
+
+ /**
+ * Destruct Http transport
+ *
+ * @return void
+ */
+ public function __destruct()
+ {
+ $this->close();
+ }
+}
View
20 library/Phue/Transport/TransportInterface.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Phue\Transport;
+
+use Phue\Command\CommandInterface;
+
+/**
+ * Transport Interface
+ */
+interface TransportInterface
+{
+ /**
+ * Send request by command
+ *
+ * @param CommandInterface $command Phue command
+ *
+ * @return void
+ */
+ public function sendRequestByCommand(CommandInterface $command);
+}
View
2  vendor/.gitignore
@@ -0,0 +1,2 @@
+*
+!.gitignore
Please sign in to comment.
Something went wrong with that request. Please try again.