forked from peec/x10php
/
X10Device.php
131 lines (100 loc) · 2.85 KB
/
X10Device.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* All X10 modules override this abstract.
* @author Petter Kjelkenes <kjelkenes@gmail.com>
*
*/
abstract class X10Device{
protected $x10ApiExcecutable;
protected $code;
protected $callbackLogger;
protected $featureFlags = null;
protected $featureList = array();
const TYPE_POWER = 1;
const TYPE_RADIO = 2;
/* Single flags */
const F_ON = 1;
const F_OFF = 2;
const F_DIM = 4;
const F_BRIGHT = 8;
/* Groups */
// F_ON | F_OFF
const FG_ON_OFF = 3;
// F_DIM | F_BRIGHT
const FG_DIM_BRIGHT = 12;
/**
* Sets what features is allowed to do.
* See the F_ constants in this class.
* Use it bitwise like so:
* F_ON | F_OFF | F_DIM etc.
* @param int $flags Bitwise flags of F_ constants in this class.
*/
public function setAllowedFeatures($flags){
$this->featureFlags = $flags;
}
public function isFeatureAllowed($feature){
return $this->featureFlags === null || $feature & $this->featureFlags;
}
public function getAllowedFlags(){
return $this->featureFlags;
}
/**
* Convenicene method to get readable names of each feature.
*/
static public function getFeatures(){
$ref = new \ReflectionClass('X10Device');
$constants = $ref->getConstants();
$returns = array();
foreach($constants as $key => $val){
if (substr($key, 0, 2) == 'F_'){
$r = $key;
switch($key){
case 'F_ON': $r = 'On'; break;
case 'F_OFF': $r = 'Off'; break;
case 'F_DIM': $r = 'Dim'; break;
case 'F_BRIGHT': $r = 'Bright'; break;
}
$returns[$val] = $r;
}
}
return $returns;
}
/**
* Constructs X10 Device module.
* @param string $executable Path to SDK excecutable.
* @param string $deviceCode Device Code ( example a1 )
* @param closure annonymous function that
*/
public function __construct($executable, $deviceCode, $callbackLogger = null){
$this->x10ApiExcecutable = $executable;
$this->code = $deviceCode;
$this->callbackLogger = $callbackLogger;
$this->featureList = self::getFeatures();
}
protected function chkFlag($flagsRequired){
if (!$this->isFeatureAllowed($flagsRequired)){
$msg = "Feature error on device (#{$this->code}): Feature(s): ";
foreach($this->featureList as $k => $v){
if ($k & $flagsRequired)$msg .= "$v,";
}
$msg .= " is required for this device.";
throw new \Exception($msg);
}
}
/**
* Types of devices has other commands, this is abstract.
* @param string $query The custom query. Eg. "on" or "On".
*/
abstract public function rawCommand($method, $arguments, $query);
public function getCode(){
return $this->code;
}
public function log($name, $arguments, $command){
$closure = $this->callbackLogger;
if ($closure !==null && is_callable($closure)){
$action = new \X10Action($this, $name, $arguments, $command);
// Call it and send action to it.
$closure($action);
}
}
}