-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
125 lines (110 loc) · 2.88 KB
/
index.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
<?php
$settings = parse_ini_file('config.ini', true);
try {
$db = new \PDO(
"mysql:dbname={$settings['db']['name']};host={$settings['db']['server']}",
$settings['db']['user'],
$settings['db']['passwort'],
[
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
\PDO::ATTR_PERSISTENT => true
]
);
$switches = [];
foreach ($db->query("select SwitchName,IPAddr,RoomName from Switch inner join Room using(RoomID)") as $switch) {
$switches[$switch['IPAddr']] = $switch['SwitchName'];
}
} catch (Exception $x) {
error_log($x->errorInfo[2]);
exit(0);
}
function getSwitchState($switchIP)
{
$ch = curl_init();
$url = "http://{$switchIP}/report";
// please use curl_setopt_array here
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
?>
<!doctype html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.flex-container>button {
background: #5AB400;
width: 200px;
height: 200px;
margin: 10px;
text-align: center;
}
.flex-container>button.on {
background: #7AD400;
}
</style>
<div class="flex-container" id="switch-panel">
<?php
foreach ($switches as $url => $name) {
$state = getSwitchState($url);
$state = $state?->relay ? "on" : "";
echo "<button class='switch-button $state' data-switch-ip='$url'>
<h2>$name</h2>
<p></p>
<p style='font-size:small;'>0.0</p>
<p>0.0</p>
</button>";
}
?>
</div>
<script>
function toggle(sw) {
let xhr = new XMLHttpRequest();
let url = "./ajax/toggle.php?switch-ip=" + sw.target.dataset["switchIp"];
xhr.open("GET", url);
xhr.responseType = "json";
xhr.timeout = 1000;
xhr.onload = function() {
if (xhr.response["relay"])
sw.target.classList.add("on");
else
sw.target.classList.remove("on");
};
xhr.send();
}
window.onload = function() {
let switchPanel = document.getElementById("switch-panel");
switchPanel.addEventListener("click", toggle);
};
function checkSwitches() {
let switches = document.querySelectorAll(".switch-button");
switches.forEach((sw) => {
if (sw) {
let xhr = new XMLHttpRequest();
let url = "./ajax/state.php?switch-ip=" + sw.dataset.switchIp;
xhr.open("GET", url);
xhr.responseType = "json";
xhr.timeout = 2000;
xhr.onload = function() {
let temp = xhr.response["temperature"];
let power = xhr.response["power"];
sw.getElementsByTagName("p")[1].innerText = temp.toFixed(1) + '°C';
sw.getElementsByTagName("p")[2].innerText = power.toFixed(1) + ' watt';
if (xhr.response["relay"])
sw.classList.add("on");
else
sw.classList.remove("on");
};
xhr.send();
}
});
}
window.setInterval(checkSwitches, 2500);
</script>