-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
136 lines (123 loc) · 4.09 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
126
127
128
129
130
131
132
133
134
135
136
<html>
<head>
<title>Lighting</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="admin/css/main.css" type="text/css" rel="stylesheet"/>
<link rel="shortcut icon" href="favicon.png" type="image/png">
<script type="text/javascript" src="admin/js/ext/jquery-2.1.1.min.js"></script>
</head>
<body>
<?php
if (!file_exists("lamps.db"))
{
?>
<div id="loginPage">
<div id="header">Первый запуск</div>
<div id="loginData">
Отсутствует база данных текущего состояния ламп. Вероятно, это первый запуск системы — либо же файл был поврежден. Нажмите кнопку «Создать», чтобы создать новую базу данных.
</div>
<div id="footer"><button id="createDb">Создать</button></div>
</div>
</body>
<script>
$("#createDb").click(function() {
$.ajax({
url: "admin/createdb.php",
success: function() {
location.reload();
}
});
});
</script>
</html>
<?php
exit;
}
$db = new SQLite3("lamps.db", SQLITE3_OPEN_READONLY);
$db->busyTimeout(1000);
//$db->exec("PRAGMA synchronous=OFF;");
//$db->exec("PRAGMA journal_mode=MEMORY;");
libxml_use_internal_errors(true);
$lamps = simplexml_load_file("lamps.xml");
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if ($detect->isMobile())
{
?><table><?php
foreach ($lamps->channel as $lamp)
{
$state = $db->querySingle("SELECT state FROM lamps WHERE id=" . $lamp["id"] . ";", false);
if ($state == 0)
{
$state = 1;
$image = "switch-off.png";
}
else
{
$state = 0;
$image = "switch-on.png";
}
list($width, $height, $type, $attr) = getimagesize($image);
printf("<tr><td>%s: </td><td><img src='%s' id='%s' class='lamp' %s /></td></tr>", $lamp["title"], $image, $lamp["id"], $attr);
}
list($width, $height, $type, $attr) = getimagesize("switch-on.png");
printf("<tr><td>Выключить всё: </td><td><img src='switch-on.png' class='lamp' id='0' %s /></td></tr></table>", $attr);
}
else
{
list($width, $height, $type, $attr) = getimagesize("apartments.png");
?>
<div style="width:<?php echo $width+60;?>px; margin: 20px auto;" >
<div id="header">
<font size="+1">Освещение в доме</font>
<img src='switch-on.png' title="Выключить всё" style="float: right;" class='lamp' id='0' />
</div>
<div id="xml">
<div style="position: relative; margin: 10px;">
<img src='apartments.png' style='vertical-align: bottom' <?php echo $attr;?> />
<?php
foreach ($lamps->channel as $lamp)
{
if (($lamp->image["x"] >= 0) && ($lamp->image["y"] >= 0))
{
$shift=$lamp->image["x"];
$imageURL = ($lamp["type"] == "other")? "wallsocket" : "lightbulb";
$imageURL .= ($db->querySingle("SELECT state FROM lamps WHERE id=" . $lamp["id"] . ";", false) == 0)? "-off.png" : "-on.png";
list($width, $height, $type, $attr) = getimagesize($imageURL);
printf("<img src='%s' title='%s' id='%s' class='lamp' style='z-index:100; position: absolute; left: %dpx; top: %dpx;' %s />", $imageURL, $lamp["title"], $lamp["id"], $lamp->image["x"], $lamp->image["y"], $attr);
}
}
?>
</div>
</div>
<div id="footer">
<button id="saveFile" onclick="window.open('admin/index.php','_self',false)">Настройка ламп и выключателей</button>
</div>
</div>
<?php } ?>
<script>
$(".lamp").click(function() {
var state = 1;
if ($(this).attr("src").indexOf("on.png") >= 0) {
state = 0;
}
var image = $(this);
lampID = image.attr("id");
cmd = "onofflamp.php?lamp=" + lampID + "&on=" + state;
$.ajax({
url: cmd,
success: function(){
imageUrl = (state == 0)? image.attr("src").replace("on.png", "off.png") : image.attr("src").replace("off.png", "on.png");
image.attr("src", imageUrl);
if (lampID == 0) {
setTimeout(function(){
image.attr("src", imageUrl.replace("off.png", "on.png"));
}, 200);
location.reload();
}
}
});
});
</script>
</body>
</html>