forked from amiravni/ESP8266-Water-Heater-Control
/
functions.php
77 lines (61 loc) · 1.77 KB
/
functions.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
<?php
include_once "init.php";
date_default_timezone_set('Asia/Jerusalem');
function startConnection() {
$servername = "localhost";
$username = USERNAME;
$password = PWD_DB;
$dbname = NAME_DB;
// Create connection
$connTMP = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connTMP->connect_error) {
die("Connection failed: " . $connTMP->connect_error);
}
return $connTMP;
}
function deleteTable($conn) {
$sql = "DROP TABLE ".NAME_DB_TOUSE."";
return $conn->query($sql);
}
function createTable($conn) {
$sql = "CREATE TABLE IF NOT EXISTS ".NAME_DB_TOUSE." (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
toRepeat VARCHAR(30) NOT NULL,
startTime TIMESTAMP,
endTime TIMESTAMP
)";
return $conn->query($sql);
}
function getTable($conn) {
$sql = "SELECT id, name, toRepeat, startTime,endTime FROM ".NAME_DB_TOUSE." ORDER BY startTime";
return $conn->query($sql);
}
function EraseQuery($conn,$id){
$sql = "DELETE FROM ".NAME_DB_TOUSE." WHERE id=". $id;
return ($conn->query($sql));
}
function handleDailyQuery($conn,$row) {
$timeGap = (strtotime($row["endTime"]) - strtotime(date("Y-m-d H:i:s")) < 0);
if ($timeGap) {
echo $row["toRepeat"]." <br>";
if ($row["toRepeat"] == "Daily") {
$newTimeStart = date('Y-m-d H:i', strtotime('+'. 1 .' days',strtotime($row["startTime"])));
$newTimeEnd = date('Y-m-d H:i', strtotime('+'. 1 .' days',strtotime($row["endTime"])));
$sql = "UPDATE ".NAME_DB_TOUSE." SET startTime='".$newTimeStart."',endTime='".$newTimeEnd."' WHERE id=".$row["id"];
if ($conn->query($sql) === TRUE) {
return TRUE;
} else {
EraseQuery($conn,$row["id"]);
return FALSE;
}
}
else {
EraseQuery($conn,$row["id"]);
return FALSE;
}
}
return TRUE;
}
?>