-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitchlamp.php
More file actions
57 lines (53 loc) · 1011 Bytes
/
switchlamp.php
File metadata and controls
57 lines (53 loc) · 1011 Bytes
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
<?php
function switchlamp($channel, $on, $level)
{
global $db;
if (func_num_args() < 3)
{
if ($on)
{
$level = 100;
}
else
{
$level = 0;
}
}
$sockf = @fsockopen("unix:///tmp/noolitepcd.sock", 0, $errno, $errstr);
if ($sockf)
{
$filename = pathinfo($_SERVER["PHP_SELF"]);
if ($filename["basename"] == "index.php")
{
$log = "Web: ";
}
else
{
$log = "";
}
$log = $log . "Lamp " . $channel . " state " . $db->querySingle("SELECT state FROM lamps WHERE id=" . $channel . ";", false);
switch ($on)
{
case 1:
$cmd = "on " . $channel;
break;
case 0:
$cmd = "off " . $channel;
break;
}
fwrite($sockf, $cmd);
fclose($sockf);
$db->exec("BEGIN;");
$db->exec("UPDATE lamps SET state=" . $on . ", dimlevel=" . $level . " WHERE id=" . $channel . ";");
$db->exec("COMMIT;");
$log = $log . "->" . $on;
syslog(LOG_INFO, $log);
return 0;
}
else
{
syslog(LOG_ERR, "Socket error: $errstr ($errno)");
return -1;
}
}
?>