Permalink
Browse files

GitHub #45 - Schedules can now be manipulated. Adding another conveni…

…ence command to get a schedule by id.
  • Loading branch information...
sqmk committed Apr 2, 2013
1 parent eb2a87e commit 322c4e2c10fe1635d6b5a3011e5ca58d7451347b
@@ -18,25 +18,11 @@
class CreateSchedule implements CommandInterface
{
/**
- * Name
+ * Schedule attributes
*
- * @var string
+ * @var array
*/
- protected $name;
-
- /**
- * Description
- *
- * @var string
- */
- protected $description;
-
- /**
- * Time
- *
- * @var string
- */
- protected $time;
+ protected $attributes = [];
/**
* Command
@@ -63,7 +49,7 @@ public function __construct(
$command !== null && $this->command($command);
// Copy description
- $this->description = $this->name;
+ $this->description = $name;
}
/**
@@ -75,7 +61,7 @@ public function __construct(
*/
public function name($name)
{
- $this->name = (string) $name;
+ $this->attributes['name'] = (string) $name;
return $this;
}
@@ -89,7 +75,7 @@ public function name($name)
*/
public function description($description)
{
- $this->description = (string) $description;
+ $this->attributes['description'] = (string) $description;
return $this;
}
@@ -103,7 +89,7 @@ public function description($description)
*/
public function time($time)
{
- $this->time = $this->convertTimeToUtcDate(
+ $this->attributes['time'] = $this->convertTimeToUtcDate(
(string) $time
);
@@ -133,15 +119,16 @@ public function command(SchedulableInterface $command)
*/
public function send(Client $client)
{
+ // Set command attribute if passed
+ if ($this->command) {
+ $this->attributes['command'] = $this->command->getSchedulableParams($client);
+ }
+
+ // Create schedule
$scheduleId = $client->getTransport()->sendRequest(
"{$client->getUsername()}/schedules",
TransportInterface::METHOD_POST,
- (object) [
- 'name' => $this->name,
- 'description' => $this->description,
- 'time' => $this->time,
- 'command' => $this->command->getSchedulableParams($client)
- ]
+ (object) $this->attributes
);
return $scheduleId;
@@ -154,7 +141,7 @@ public function send(Client $client)
*
* @return string
*/
- protected function convertTimeToUtcDate($time)
+ public function convertTimeToUtcDate($time)
{
try {
$setTime = new \DateTime($time);
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Phue: Philips Hue PHP Client
+ *
+ * @author Michael Squires <sqmk@php.net>
+ * @copyright Copyright (c) 2012 Michael K. Squires
+ * @license http://github.com/sqmk/Phue/wiki/License
+ */
+
+namespace Phue\Command;
+
+use Phue\Client;
+use Phue\Schedule;
+
+/**
+ * Get schedule by id command
+ */
+class GetScheduleById implements CommandInterface
+{
+ /**
+ * Schedule Id
+ *
+ * @var string
+ */
+ protected $scheduleId;
+
+ /**
+ * Constructs a command
+ *
+ * @param int $scheduleId Schedule Id
+ */
+ public function __construct($scheduleId)
+ {
+ $this->scheduleId = (int) $scheduleId;
+ }
+
+ /**
+ * Send command
+ *
+ * @param Client $client Phue Client
+ *
+ * @return Schedule Schedule object
+ */
+ public function send(Client $client)
+ {
+ // Get response
+ $attributes = $client->getTransport()->sendRequest(
+ "{$client->getUsername()}/schedules/{$this->scheduleId}"
+ );
+
+ return new Schedule($this->scheduleId, $attributes, $client);
+ }
+}
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Phue: Philips Hue PHP Client
+ *
+ * @author Michael Squires <sqmk@php.net>
+ * @copyright Copyright (c) 2012 Michael K. Squires
+ * @license http://github.com/sqmk/Phue/wiki/License
+ */
+
+namespace Phue\Command;
+
+use Phue\Client;
+use Phue\Transport\TransportInterface;
+
+/**
+ * Set schedule attributes command
+ */
+class SetScheduleAttributes extends CreateSchedule implements CommandInterface
+{
+ /**
+ * Schedule Id
+ *
+ * @var string
+ */
+ protected $scheduleId;
+
+ /**
+ * Constructs a command
+ *
+ * @param mixed $schedule Schedule Id or Schedule object
+ */
+ public function __construct($schedule)
+ {
+ $this->scheduleId = (string) $schedule;
+ }
+
+ /**
+ * Send command
+ *
+ * @param Client $client Phue Client
+ */
+ public function send(Client $client)
+ {
+ // Set command attribute if passed
+ if ($this->command) {
+ $this->attributes['command'] = $this->command->getSchedulableParams($client);
+ }
+
+ $client->getTransport()->sendRequest(
+ "{$client->getUsername()}/schedules/{$this->scheduleId}",
+ TransportInterface::METHOD_PUT,
+ (object) $this->attributes
+ );
+ }
+}
View
@@ -9,6 +9,9 @@
namespace Phue;
+use Phue\Command\SetScheduleAttributes;
+use Phue\Command\SchedulableInterface;
+
/**
* Schedule object
*/
@@ -69,6 +72,22 @@ public function getName()
return $this->details->name;
}
+ /**
+ * Set name of schedule
+ *
+ * @return Schedule Self object
+ */
+ public function setName($name)
+ {
+ $this->client->sendCommand(
+ (new SetScheduleAttributes($this))->name((string) $name)
+ );
+
+ $this->details->name = (string) $name;
+
+ return $this;
+ }
+
/**
* Get description
*
@@ -79,6 +98,22 @@ public function getDescription()
return $this->details->description;
}
+ /**
+ * Set descriptions
+ *
+ * @return Schedule Self object
+ */
+ public function setDescription($description)
+ {
+ $this->client->sendCommand(
+ (new SetScheduleAttributes($this))->name((string) $description)
+ );
+
+ $this->details->description = (string) $description;
+
+ return $this;
+ }
+
/**
* Get scheduled time
*
@@ -89,6 +124,28 @@ public function getTime()
return $this->details->time;
}
+ /**
+ * Set time
+ *
+ * @param string $time Time
+ *
+ * @return Schedule Self object
+ */
+ public function setTime($time)
+ {
+ // Init command
+ $command = new SetScheduleAttributes($this);
+
+ // Build new time
+ $time = $command->convertTimeToUtcDate($time);
+
+ $this->client->sendCommand(
+ $command->time($time)
+ );
+
+ return $this;
+ }
+
/**
* Get command
*
@@ -103,6 +160,24 @@ public function getCommand()
];
}
+ /**
+ * Set command
+ *
+ * @param SchedulableInterface $command Schedulable command
+ *
+ * @return Schedule Self object
+ */
+ public function setCommand(SchedulableInterface $command)
+ {
+ $this->client->sendCommand(
+ (new SetScheduleAttributes($this))->command($command)
+ );
+
+ $this->details->command = $command->getSchedulableParams($this->client);
+
+ return $this;
+ }
+
/**
* Delete schedule
*/

0 comments on commit 322c4e2

Please sign in to comment.