-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitchPlug.php
183 lines (171 loc) · 4.37 KB
/
SwitchPlug.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
<?php
header("Access-Control-Allow-Origin: *");
$hi = $_GET['function-name'];
//echo $hi;
if($hi == 'GetDescription'){
//echo "here";
echo Get_Plug_Description($_GET['plug']);
}
if($hi == 'GetState'){
echo Get_Plug_State($_GET['plug']);
}
if($hi == 'GetAll'){
//echo "getting All..";
echo Get_All();
}
if($hi == 'switchPlug')
{
$State = filter_var($_GET['state'],FILTER_VALIDATE_BOOLEAN);
Switch_Plug($_GET['plug'],$State);
}
if($hi == 'addPlug')
{
Add_Plug($_GET['plug'],$_GET['description'],$_GET['oncode'],$_GET['offcode']);
}
if($hi == 'changeDescription')
{
Change_Description($_GET['plug'],$_GET['description']);
}
if($hi == 'changeVisibility')
{
Change_Visibility($_GET['plug'],$_GET['state']);
}
/*
Switch_Plug(2,true);
echo "Plug 2 On ";
echo Get_Plug_State(2);
sleep(2);
echo " Turing Plug Off ";
Switch_Plug(2,false);
echo " Plug 2 Off ";
echo Get_Plug_State(2);
*/
function Get_Conn()
{
$servername = "localhost";
$username = "server";
$password = "password";
$dbname = "home";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn)
{
die("Connection Failed: " .mysqli_connect_error());
}
return $conn;
}
function Switch_Plug($plug, $State)
{
//$conn = Get_Conn():
if(!$conn)
{
$conn = Get_Conn();
}
if (!$conn)
{
die("Connection Failed: " .mysqli_connect_error());
}
//$time = 1;//time();
if($State == true)
{
$sqlGet = "select oncode from Plugs where plug = '$plug' limit 1";
$sqlSet = "update Plugs set state = 1 Where plug = '$plug' ";
$result = mysqli_fetch_assoc(mysqli_query($conn, $sqlGet));
$code = $result['oncode'];
$result = mysqli_fetch_assoc(mysqli_query($conn, $sqlSet));
exec("sudo /var/www/html/Scripts/SendPlugCode '$code'", $eOutput, $eResult);
echo "Plug: '$plug' On - Code: '$code' - Return: '$eResult' - eOutput: '$eOutput'";
}
else
{
$sqlGet = "select offcode from Plugs where plug = '$plug' limit 1";
$result = mysqli_fetch_assoc(mysqli_query($conn, $sqlGet));
$sqlSet = "update Plugs set state = 0 Where plug = '$plug' " ;
$code = $result['offcode'];
$result = mysqli_fetch_assoc(mysqli_query($conn, $sqlSet));
exec("sudo /var/www/html/Scripts/SendPlugCode '$code'", $eOutput, $eResult);
echo "Plug: '$plug' Off - Code: '$code' - Return: '$eResult' - eOutput: '$eOutput'";
}
//echo $code;
mysql_close($conn);
}
function Get_Plug_State($plug)
{
$conn = Get_Conn();
$sql= "select state from Plugs where plug = '$plug' limit 1";
$result = mysqli_fetch_assoc(mysqli_query($conn, $sql));
return $result['state'];
mysql_close($conn);
}
function Get_Plug_Description($plug)
{
$conn = Get_Conn();
$sql= "select description from Plugs where plug = '$plug' limit 1";
$result = mysqli_fetch_assoc(mysqli_query($conn, $sql));
echo $result['description'];
mysql_close($conn);
}
function Get_All()
{
$conn = Get_Conn();
$sql = "select plug, description, state, display from Plugs";
$result = mysqli_query($conn,$sql);
$output = "[";
while($rs = mysqli_fetch_assoc($result)){
if($output != "["){$output .= ",";}
$output .= '{"plug":"' . $rs["plug"] . '",';
$output .= '"description":"' . $rs["description"] . '",';
$output .= '"display":"' . $rs["display"] . '",';
$output .= '"state":"' . $rs["state"] . '"}';
}
$output .= "]";
mysql_close($conn);
echo($output);
//echo($result);
}
function Add_Plug($plug, $description, $oncode, $offcode)
{
$conn = Get_Conn();
$sql = "select 1 from Plugs where plug = '$plug' limit 1";
if(mysqli_fetch_row(mysqli_query($conn,$sql)))
{
die("Plug: '$plug' already assigned".mysqli_connect_error());
}
else
{
$sql = "insert into Plugs values('$plug', '$description', '$oncode', '$offcode', false, 'Time()', true )";
if(mysqli_query($conn,$sql))
echo "Plug Added";
else
echo "Error" . $sql . mysqli_error($conn);
}
mysqli_close();
}
Function Change_Description($plug,$description)
{
$conn = Get_Conn();
$sql = "update Plugs set description '$description' where plug = '$plug'";
if(mysqli_query($conn,$sql))
{
echo "Updated: '$plug' to '$description'";
}
else
{
"Error updating: " . mysqli_error($conn);
}
mysqli_close($conn);
}
Function Change_Visibility($plug,$visibility)
{
$conn = Get_Conn();
$sql = "update Plugs set display '$visibility' where plug = '$plug'";
if(mysqli_query($conn,$sql))
{
echo "Updated: '$plug' visibility to '$visibility'";
}
else
{
echo "Error updating: " . mysqli_error($conn);
}
mysqli_close($conn);
}
?>