Skip to content

Commit

Permalink
Added functions for adapters and room thermostats
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubelbub committed Mar 29, 2014
1 parent f0d9c1d commit 36aaa33
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 26 deletions.
86 changes: 60 additions & 26 deletions Request/SetActuatorStatesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

namespace Bubelbub\SmartHomePHP\Request;
use Bubelbub\SmartHomePHP\SmartHome;

/**
* Class SetActuatorStatesRequest
Expand All @@ -17,57 +18,90 @@
class SetActuatorStatesRequest extends BaseRequest
{
/**
* @var array with actuator states
* @var \SimpleXMLElement with actuator states
*/
private $actuatorStates = array();
private $actuatorStates;

/**
* {@inheritdoc}
*/
public function __construct(SmartHome $smartHome)
{
parent::__construct($smartHome);

/** Prepare the SetActuatorStates request */
$request = $this->getRequest();
$request->addAttribute('BasedOnConfigVersion', $this->smartHome->getConfigVersion());
$this->actuatorStates = $request->addChild('ActuatorStates');
}

/**
* {@inheritdoc}
*/
public function send($expectedResponse = 'ControlResultResponse', $useSession = true, $try = 1)
{
if($this->smartHome->getConfigVersion() === null)
{
$this->smartHome->login();
}
$request = $this->getRequest();
$request->addAttribute('BasedOnConfigVersion', $this->smartHome->getConfigVersion());

$actuatorStates = $request->addChild('ActuatorStates');
foreach($this->actuatorStates as $actuatorState)
{
$logicalDeviceState = $actuatorStates->addChild('LogicalDeviceState');
foreach($actuatorState as $key => $value)
{
$logicalDeviceState->addAttribute($key, $value);
}
}
return parent::send($expectedResponse, $useSession, $try);
}

/**
* Sets the temperature and mode for heaters
*
* @param string $logicalDeviceId the logical device id
* @param string|float $pointTemperature the temperature to set
* @param string $mode the mode of temperature actuator (Auto|Manu)
*/
public function addRoomTemperatureActuatorState($logicalDeviceId, $pointTemperature, $mode)
{
$this->actuatorStates[] = array(
'xmlns:xsi:type' => 'RoomTemperatureActuatorState',
'LID' => $logicalDeviceId,
'PtTmp' => $pointTemperature,
'OpnMd' => $mode,
'WRAc' => false
);
if((int) $pointTemperature < 6) { $pointTemperature = 6; }
if((int) $pointTemperature > 30) { $pointTemperature = 30; }
if(!preg_match('#^[0-9]+(\.[05]+)?$#i', $pointTemperature))
{
throw new \Exception('The parameter "PointTemperature" should be a value like "6.0" "6.5" "12" "12.5" ..."');
}

$logicalDeviceState = $this->actuatorStates->addChild('LogicalDeviceState');
$logicalDeviceState->addAttribute('xmlns:xsi:type', 'RoomTemperatureActuatorState');
$logicalDeviceState->addAttribute('LID', $logicalDeviceId);
$logicalDeviceState->addAttribute('PtTmp', $pointTemperature);
$logicalDeviceState->addAttribute('OpnMd', ucfirst(strtolower($mode)));
$logicalDeviceState->addAttribute('WRAc', 'false');
}

/**
* Sets the on/off state for adapters
*
* @param string $logicalDeviceId the logical device id
* @param bool $value the state to set
* @param bool $value the state to set (on=true/off=false)
*/
public function addSwitchActuatorState($logicalDeviceId, $value)
{
$this->actuatorStates[] = array(
'xmlns:xsi:type' => 'SwitchActuatorState',
'LID' => $logicalDeviceId,
'IsOn' => $value
);
$logicalDeviceState = $this->actuatorStates->addChild('LogicalDeviceState');
$logicalDeviceState->addAttribute('xmlns:xsi:type', 'SwitchActuatorState');
$logicalDeviceState->addAttribute('LID', $logicalDeviceId);
$logicalDeviceState->addAttribute('IsOn', preg_match('#^(on|1|true)$#i', (string) $value) ? 'true' : 'false');
}

/**
* Currently unknown!?
*
* @param string $logicalDeviceId the logical device id
* @param bool $value the new state of the device (true = on, false = off)
*/
public function addLogicalDeviceState($logicalDeviceId, $value)
{
$logicalDeviceState = $this->actuatorStates->addChild('LogicalDeviceState');
$logicalDeviceState->addAttribute('xmlns:xsi:type', 'GenericDeviceState');
$logicalDeviceState->addAttribute('LID', $logicalDeviceId);

$ppts = $logicalDeviceState->addChild('Ppts');
$ppt = $ppts->addChild('Ppt');
$ppt->addAttribute('xmlns:xsi:type', 'BooleanProperty');
$ppt->addAttribute('Name', 'Value');
$ppt->addAttribute('Value', $value ? 'true' : 'false'); // text!
}
}
29 changes: 29 additions & 0 deletions SmartHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Bubelbub\SmartHomePHP\Request\GetShcTypeRequest;
use Bubelbub\SmartHomePHP\Request\LoginRequest;
use Bubelbub\SmartHomePHP\Request\ReleaseConfigurationLockRequest;
use Bubelbub\SmartHomePHP\Request\SetActuatorStatesRequest;

/**
* Class SmartHome
Expand Down Expand Up @@ -189,6 +190,34 @@ public function getMessageList()
return $getMessageListRequest->send();
}

/**
* Set the state of an logical device
*
* @param string $logicalDeviceId the logical device id
* @param boolean $on the new state of the device (true = on, false = off)
* @return \SimpleXMLElement
*/
function setLogicalDeviceState($logicalDeviceId, $on)
{
$setActuatorStatesRequest = new SetActuatorStatesRequest($this);
$setActuatorStatesRequest->addLogicalDeviceState($logicalDeviceId, $on);
return $setActuatorStatesRequest->send();
}

/**
* Set the state of an adapter for example
*
* @param string $logicalDeviceId the logical device id
* @param boolean $on the new state of the device (true = on, false = off)
* @return \SimpleXMLElement
*/
function setSwitchActuatorState($logicalDeviceId, $on)
{
$setActuatorStatesRequest = new SetActuatorStatesRequest($this);
$setActuatorStatesRequest->addSwitchActuatorState($logicalDeviceId, $on);
return $setActuatorStatesRequest->send();
}

/**
* @param bool $overrideLock
* @return \SimpleXMLElement
Expand Down
12 changes: 12 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@

// Get all messages
print_r($sh->getMessageList());

// Set room temperature of heater to 18°C
/*
$setActuatorStatesRequest = new SetActuatorStatesRequest($sh);
$setActuatorStatesRequest->addRoomTemperatureActuatorState('id of heater (LID)', 12, 'auto');
$setActuatorStatesRequest->send();
*/

// Switch on your adapter for computer
//$sh->setSwitchActuatorState('id of computer adapter (LID)', 'on'); // 'on' could be true, too

// Now you could wake your computer up! (@since 30.03.2014)

1 comment on commit 36aaa33

@Bubelbub
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3

Please sign in to comment.