forked from amiravni/ESP8266-Water-Heater-Control
/
dude.php
267 lines (236 loc) · 6.95 KB
/
dude.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
include_once "functions.php";
function addHeader() {
echo '<html><head>';
echo '<title>Amir and Batia Water Heater Schedule</title>';
echo '<meta charset=utf-8 /> <meta name="viewport" content="width=device-width, initial-scale=1">';
echo ' <style id="jsbin-css">
.tableRow {
margin-left: auto;
margin-right: auto;
}
.tableCell {
margin-left: auto;
margin-right: auto;
text-align: center;
border: 2px solid black;
}
</style>';
echo '</head>';
echo '<body><div align="center"><h2>Amir and Batia Water Heater Schedule</h2></div>';
}
function addFooter() {
echo '</body></html>';
}
function drawTableCell($val) {
echo '<td class="tableCell">' . $val . '</td>';
}
function drawTableRow($row) {
echo '<tr class="tableRow">';
$formID = '"toErase'.$row["id"].'"';
drawTableCell('<form id='.$formID.' method="POST">
<input type="hidden" name="Command" value="Erase">
<input type="hidden" name="id" value="'. $row["id"] .'">'.
"<button onclick='submit_Form(".$formID.")'>Erase</button>".
'</form>');
drawTableCell($row["id"]);
drawTableCell($row["name"]);
drawTableCell($row["toRepeat"]);
drawTableCell($row["startTime"]);
drawTableCell($row["endTime"]);
echo '</tr>';
}
function printTable($conn,$result) {
echo '<div align="center"> Current Schedule: <br> <table id="mainTable"><tr>';
drawTableCell("");
drawTableCell("id");
drawTableCell("name");
drawTableCell("toRepeat");
drawTableCell("startTime");
drawTableCell("endTime");
echo '</tr>';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (handleDailyQuery($conn,$row)) {
drawTableRow($row);
}
}
} else {
echo "0 results";
}
echo '</table></div>';
}
function addQuerySelect($conn,$sVar) {
$stmt = $conn->prepare("INSERT INTO ".NAME_DB_TOUSE." (name, toRepeat, startTime, endTime) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $sVar->name, $sVar->toRepeat, $sVar->startTime,$sVar->endTime);
$result = $stmt->execute();
$stmt->close();
return $result;
}
function getVariablesAdd($sVar){
$sVar->name = ($_POST['myName']);
$sVar->toRepeat = ($_POST['myRepeat']);
$sVar->startTime = date(($_POST['startDate'] .' '. $_POST['startTime'] ));
// $sVar->endTime = $sVar->startTime;
$sVar->endTime =date('Y-m-d H:i', strtotime('+'. $_POST['Duration'] .' minutes',strtotime($sVar->startTime)));
return $sVar;
}
function getVariablesErase($sVar){
$sVar->id = ($_POST['id']);
return $sVar;
}
function getVariablesUpdate($sVar){
$sVar->action = ($_POST['myAction']);
return $sVar;
}
function checkVariables(){
if ( strlen($_POST['myName']) < 6 &&
strlen($_POST['myRepeat']) < 6 &&
(strlen($_POST['startDate']) == 10 || strlen($_POST['startDate']) == 0) &&
(strlen($_POST['startTime']) == 5 || strlen($_POST['startTime']) == 0) &&
strlen($_POST['Duration']) < 4 &&
strlen($_POST['myAction']) < 30 &&
strlen($_POST['Command']) < 7 &&
strlen($_POST['id']) < 6 )
{
return 34566543;
}
return 1;
}
$sVar = array(
'Command' => NULL,
'id' => NULL,
'name' => NULL,
'toRepeat' => NULL,
'startTime' => NULL,
'endTime' => NULL,
'action' => NULL
);
addHeader();
$conn = startConnection();
if (checkVariables()!=34566543) {
die('Variables Problem');
}
$sVar = new StdClass();
$sVar->Command = ($_POST['Command']);
if ($sVar->Command != NULL) {
if ( sha1($_POST['psswd']) == PWD_SHA) {
if ($sVar->Command=='Add'){
$sVar = getVariablesAdd($sVar);
if (!addQuerySelect($conn,$sVar)) {
die("add Problem");
}
}
elseif ($sVar->Command=='Erase'){
$sVar = getVariablesErase($sVar);
if (!EraseQuery($conn,$sVar->id)) {
die("Erase Problem");
}
}
elseif ($sVar->Command=='Update'){
$sVar = getVariablesUpdate($sVar);
$ipAddFile = fopen(IPtextName, "r") or die("Unable to open file!");
$ipAdd = fread($ipAddFile,filesize(IPtextName));
fclose($ipAddFile);
$baseURL = 'http://'. $ipAdd;
$fullURL = $baseURL;
switch ($sVar->action) {
case "Update From List":
$fullURL = $baseURL. '/gpio/u';;
break;
case "Get Current Status":
$fullURL = $baseURL. '/gpio/s';
break;
case "Set On":
$fullURL = $baseURL. '/gpio/1';
break;
case "Set Off":
$fullURL = $baseURL. '/gpio/0';
break;
}
}
else {
die('Something not right!');
}
}
else {
die('Password is wrong!');
}
}
printTable($conn,getTable($conn));
?>
<div> Add Query to Schedule:
<form id="addQuery" action="dude.php" method="POST">
<input type="hidden" name="Command" value="Add">
Name:<input list="names" name="myName" value="Amir">
<datalist id="names">
<option value="Amir">
<option value="Batia">
</datalist> <br>
Repeat:<input list="toRepeat" name="myRepeat" value="Once">
<datalist id="toRepeat">
<option value="Once">
<option value="Daily">
</datalist> <br>
Starting Date: :<input type="date" name="startDate" value="<?php echo date('Y-m-d'); ?>"><br>
Starting Hour: :<input type="Time" name="startTime" value="<?php echo date('H:i'); ?>"> <br>
Duration (Minutes): <input type="Number" name="Duration" value="45"> <br>
<button onclick='submit_Form("addQuery")'>Add</button>
</form>
</div>
<div> Water Heater update automatically every 5 minutes, For Manual update:
<form id="updateManual" action="dude.php" method="POST">
<input type="hidden" name="Command" value="Update">
Action:<input list="action" name="myAction">
<datalist id="action">
<option value="Update From List">
<option value="Get Current Status">
<option value="Set On">
<option value="Set Off">
</datalist> <br>
<button onclick='submit_Form("updateManual")'>Update</button>
<div>
<?php
if ($sVar->Command=='Update') {
$currState = file_get_contents($fullURL);
if ( $currState==NULL) echo 'Could Not get respond from Water Heater';
else echo $currState;
echo '<br>';
}
$stateFile = fopen(myStateName, "r") or die("Unable to open file!");
$stringFromFile = fread($stateFile,filesize(myStateName));
fclose($stateFile);
$pos = strpos($stringFromFile, " ");
$lastTime = date("Y-m-d H:i:s",substr($stringFromFile, 0, $pos));
$lastReset = substr($stringFromFile, $pos+3);
if (substr($stringFromFile, $pos+1,1)=="0")
$lastState = "Off";
else
$lastState = "On";
echo "Last auto-contact from the water heater was on ". $lastTime. " and the status was ". $lastState. " (Reset was ".$lastReset." Minutes ago)";
?>
</div>
<script type='text/javascript'>
var table = document.getElementById("mainTable");
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
if(i % 2 == 0){
rows[i].style.backgroundColor = "#FFFFFF";
}else{
rows[i].style.backgroundColor = "#AAAAAA";
}
}
function submit_Form(formID) {
var form = document.getElementById(formID);
var psswd = prompt("Please enter password", "pwd");
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'psswd';
input.value = psswd;
form.appendChild(input);
//console.log(form);
form.submit();
}
</script>
</body></html>