forked from peec/x10php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
X10DevicePower.php
99 lines (80 loc) · 2.51 KB
/
X10DevicePower.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
<?php
/**
* A generic Power device module.
* Such devices that communicate over the powerline.
* @author Petter Kjelkenes <[email protected]>
*
*/
class X10DevicePower extends X10Device{
/**
* Runs a raw command
* @param string $query example on, off, etc..
*/
public function rawCommand($method, $args, $query){
$com = $this->x10ApiExcecutable . ' sendplc ' . $this->code . ' ' . $query;
$this->log($method, $args, $com);
return shell_exec($com);
}
/**
* Turn device ON.
*/
public function on(){
$this->chkFlag(X10Device::F_ON);
return $this->rawCommand(__METHOD__, func_get_args(), 'on');
}
/**
* Turn device OFF.
*/
public function off(){
$this->chkFlag(X10Device::F_OFF);
return $this->rawCommand(__METHOD__, func_get_args(), 'off');
}
/**
* Dim lights to a percent value from 0 - 100.
* @param int $percent Percent ( 0 - 100 )
*/
public function dim($percent){
$this->chkFlag(X10Device::F_DIM);
$percent = (int)$percent;
if ($percent > 100 || $percent < 0)throw new \Exception("Dim percent from 0 - 100 is allowed.");
return $this->rawCommand(__METHOD__, func_get_args(), "dim $percent");
}
/**
* Bright lights to a percent value from 0 - 100.
* @param int $percent Percent ( 0 - 100 )
*/
public function bright($percent){
$this->chkFlag(X10Device::F_BRIGHT);
$percent = (int)$percent;
if ($percent > 100 || $percent < 0)throw new \Exception("Bright percent from 0 - 100 is allowed.");
return $this->rawCommand(__METHOD__, func_get_args(), "bright $percent");
}
public function extendedCode($command, $value){
if (!ctype_xdigit($command) || !ctype_xdigit($value))throw new \Exception("Command and value in custom extended code must both be hex codes.");
return $this->rawCommand(__METHOD__, func_get_args(), "$command $value");
}
/**
* Runs a raw query.
* Returns integer as a result.
*/
public function rawQuery($query){
$com = $this->x10ApiExcecutable . ' queryplc ' . $this->code . ' ' . $query;
$result = trim(shell_exec($com));
if ($result === '')throw new \Exception("Unable to run raw query against device ($this->code). Are you sure executable path is correct? Command executed: $com");
$result = (int)$result;
return $result;
}
/**
* Returns true if a device is on, false otherwise ( even if not known )
*/
public function isOn(){
return $this->rawQuery('on') === 1;
}
/**
* Returns the current dim level.
* -1 if not known.
*/
public function dimLevel(){
return $this->rawQuery('dim');
}
}