-
Notifications
You must be signed in to change notification settings - Fork 2
/
Index.php
453 lines (392 loc) · 20 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
header("Access-Control-Allow-Origin: *");
header(
"Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept" //not recommeneded if you are deploying it as a product ,only for testing and prototyping
);
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
// Database credentials
$host = "127.0.0.1"; //I am using local host if you are using a remote server replace with its address
$username = "put your name here";
$password = "put your password here";
$database = "Home_data";
if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
// Respond to the OPTIONS request with the appropriate headers
http_response_code(200); // OK
header("Content-Length: 0");
header("Content-Type: text/plain");
exit();
}
// Function to establish a database connection
function connectToDatabase($host, $username, $password, $database)
{
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
die("Database connection failed: " . $conn->connect_error);
}
return $conn;
}
if ($_SERVER["REQUEST_METHOD"] === "GET") {
// If it's a GET request, respond with "Server alive!"
echo "Server alive!";
} elseif ($_SERVER["REQUEST_METHOD"] === "POST") {
// Get the raw HTTP request data
$request_data = file_get_contents("php://input");
// Decode the JSON request data
$json_data = json_decode($request_data, true);
if ($json_data !== null) {
// Establish the database connection
$conn = connectToDatabase($host, $username, $password, $database);
if (isset($json_data["Mac"])) {
// Check if the "Mac" key exists in the JSON data
$mac = $json_data["Mac"];
// Check if the provided Mac exists in the database
$check_sql = "SELECT Room_id FROM Room_data WHERE Mac = ?";
$check_stmt = $conn->prepare($check_sql);
$check_stmt->bind_param("s", $mac);
$check_stmt->execute();
$check_stmt->bind_result($room_id);
$check_stmt->fetch();
$check_stmt->close();
if ($room_id) {
// If the Mac exists, return the corresponding Room_id
$response = ["Room_id" => $room_id];
echo json_encode($response);
} else {
// If the Mac doesn't exist, insert a new entry and return the Room_id
$insert_sql = "INSERT INTO Room_data (Mac) VALUES (?)";
$insert_stmt = $conn->prepare($insert_sql);
$insert_stmt->bind_param("s", $mac);
$insert_stmt->execute();
$new_room_id = $insert_stmt->insert_id;
$insert_stmt->close();
$response = ["Room_id" => $new_room_id];
echo json_encode($response);
}
// Close the database connection
$conn->close();
} elseif ($json_data !== null && isset($json_data["Case"])) {
// Establish the database connection
$conn = connectToDatabase($host, $username, $password, $database);
switch ($json_data["Case"]) {
case 1:
if (
isset($json_data["Room"]) &&
isset($json_data["States"])
) {
// Extract values from JSON
$room = $json_data["Room"];
$states = $json_data["States"];
// Check and update the Dash_data table
$state_chars = str_split($states);
foreach ($state_chars as $appliance_id => $state_char) {
$appliance_id++; // Appliance_id starts from 1
// Check if the state in JSON data matches the state in Current_home_state
$check_sql =
"SELECT State FROM Current_home_state WHERE Room_id = ? AND Appliance_id = ?";
$check_stmt = $conn->prepare($check_sql);
$check_stmt->bind_param("ii", $room, $appliance_id);
$check_stmt->execute();
$check_stmt->bind_result($current_state);
$check_stmt->fetch();
$check_stmt->close();
// Update the Current_home_state table
$update_sql =
"UPDATE Current_home_state SET State = ? WHERE Room_id = ? AND Appliance_id = ?";
$update_stmt = $conn->prepare($update_sql);
$update_stmt->bind_param(
"sii",
$state_char,
$room,
$appliance_id
);
$update_stmt->execute();
}
echo "Data updated successfully.";
} else {
echo "Invalid JSON data or missing 'Room' or 'States' element.";
}
break;
case 2:
if (
isset($json_data["Ssid"]) &&
isset($json_data["Password"]) &&
isset($json_data["Label"])
) {
// Extract values from JSON
$ssid = $json_data["Ssid"];
$password = $json_data["Password"];
$label = $json_data["Label"];
// Update the Wifi_data table
$sql =
"UPDATE Wifi_data SET ssid = ?, password = ?, label = ? WHERE wifi_id = 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sss", $ssid, $password, $label); // Note: "sss" for three string parameters
$stmt->execute();
echo "Data updated successfully.";
} else {
echo "Invalid JSON data or missing 'ssid', 'password', or 'label' element.";
}
break;
case 3:
if (
isset($json_data["Room"]) &&
isset($json_data["Room_name"])
) {
// Extract values from JSON
$room = $json_data["Room"];
$room_name = $json_data["Room_name"];
// Update the Room_data table
$sql =
"UPDATE Room_data SET Room_name = ? WHERE Room_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $room_name, $room);
$stmt->execute();
echo "Data updated successfully.";
} else {
echo "Invalid JSON data or missing 'Room' or 'Room_name' element.";
}
break;
case 4:
if (
isset($json_data["Room"]) &&
isset($json_data["Appliance"]) &&
isset($json_data["Appliance_name"])
) {
// Extract values from JSON and convert Room and Appliance to integers
$room = is_numeric($json_data["Room"])
? (int) $json_data["Room"]
: $json_data["Room"];
$appliance = is_numeric($json_data["Appliance"])
? (int) $json_data["Appliance"]
: $json_data["Appliance"];
$appliance_name = $json_data["Appliance_name"];
// Update the Appliance_data table
$sql =
"UPDATE Appliance_data SET Appliance_name = ? WHERE Room_id = ? AND Appliance_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param(
"sii",
$appliance_name,
$room,
$appliance
);
$stmt->execute();
echo "Data updated successfully.";
} else {
echo "Invalid JSON data or missing 'Room', 'Appliance', or 'Appliance_name' element.";
}
break;
case 5:
if (
isset($json_data["Room"]) &&
isset($json_data["Sensor"]) &&
isset($json_data["Sensor_name"]) &&
isset($json_data["Readings"])
) {
// Extract values from JSON and convert Room and Sensor to integers
$room = is_numeric($json_data["Room"])
? (int) $json_data["Room"]
: $json_data["Room"];
$sensor = is_numeric($json_data["Sensor"])
? (int) $json_data["Sensor"]
: $json_data["Sensor"];
$sensor_name = $json_data["Sensor_name"];
$sensor_readings = $json_data["Readings"];
// Update the Current_sensor_data table
$sql =
"UPDATE Current_sensor_data SET Sensor_name = ?, Sensor_readings = ? WHERE Room_id = ? AND Sensor_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param(
"ssii",
$sensor_name,
$sensor_readings,
$room,
$sensor
);
$stmt->execute();
echo "Data updated successfully.";
} else {
echo "Invalid JSON data or missing 'Room', 'Sensor', 'sensor name', or 'Readings' element.";
}
break;
case 6:
if (
isset($json_data["Room"]) &&
isset($json_data["Request"])
) {
$room = is_numeric($json_data["Room"])
? (int) $json_data["Room"]
: $json_data["Room"];
$request = $json_data["Request"];
switch ($request) {
case 1:
// Retrieve and concatenate State values
$sql =
"SELECT GROUP_CONCAT(State) as State FROM Current_home_state WHERE Room_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $room);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$state_string = $row["State"];
// Create the JSON response with the State string
$response = ["State" => $state_string];
// Convert the response to JSON and echo it
echo json_encode($response);
} else {
echo "No data found for the specified Room.";
}
break;
case 2:
// Handle Case 2 within Case 6
$sql =
"SELECT Sensor_name, Sensor_readings FROM Current_sensor_data WHERE Room_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $room);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$sensor_names = [];
$sensor_data = [];
while ($row = $result->fetch_assoc()) {
$sensor_names[] = $row["Sensor_name"];
$sensor_data[] =
$row["Sensor_readings"];
}
// Create the JSON response with Sensor_names and Data separated by "|"
$response = [
"Sensor_names" => implode(
"|",
$sensor_names
),
"Data" => implode("|", $sensor_data),
];
// Convert the response to JSON and echo it
echo json_encode($response);
} else {
echo "No sensor data found for the specified Room.";
}
break;
case 3:
// Handle Case 3 within Case 6
$sql =
"SELECT Room_name FROM Room_data WHERE Room_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $room);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$room_name = $row["Room_name"];
// Create the JSON response with Room_name
$response = ["Room_name" => $room_name];
// Convert the response to JSON and echo it
echo json_encode($response);
} else {
echo "No room data found for the specified Room.";
}
break;
case 4:
// Handle Case 4 within Case 6
$sql =
"SELECT Appliance_name FROM Appliance_data WHERE Room_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $room);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$appliance_names = [];
while ($row = $result->fetch_assoc()) {
$appliance_names[] =
$row["Appliance_name"];
}
// Create the JSON response with Appliance names separated by "|"
$response = [
"Appliances" => implode(
"|",
$appliance_names
),
];
// Convert the response to JSON and echo it
echo json_encode($response);
} else {
echo "No appliance data found for the specified Room.";
}
break;
case 5:
// Handle Case 5 within Case 6
$sql =
"SELECT ssid, password FROM Wifi_data WHERE wifi_id = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$ssid = $row["ssid"];
$password = $row["password"];
// Create the JSON response with ssid and password
$response = [
"ssid" => $ssid,
"password" => $password,
];
// Convert the response to JSON and echo it
echo json_encode($response);
} else {
echo "No data found for wifi_id 1.";
}
break;
case 6:
// Handle Case 6 within Case 6
// Query to count the rows in the Room_data table
$count_sql =
"SELECT COUNT(Room_id) as Count FROM Room_data";
$count_result = $conn->query($count_sql);
if (
$count_result &&
$count_result->num_rows > 0
) {
$row = $count_result->fetch_assoc();
$count = $row["Count"];
} else {
echo "Error getting count from the database.";
break;
}
// Query to fetch room names
$names_sql =
"SELECT Room_name FROM Room_data ORDER BY Room_id";
$names_result = $conn->query($names_sql);
$names = [];
while (
$names_row = $names_result->fetch_assoc()
) {
$names[] = $names_row["Room_name"];
}
$response = [
"Count" => $count,
"Names" => $names,
];
echo json_encode($response);
break;
default:
echo "Invalid 'Request' value.";
break;
}
} else {
echo "Invalid JSON data or missing 'Room' or 'Request' element.";
}
break;
default:
echo "Invalid 'Case' value.";
break;
}
// Close the database connection
$conn->close();
} else {
echo "Invalid JSON data or 'Case' element is missing.";
}
}
} else {
// For other request methods, return an error response
http_response_code(405); // Method Not Allowed
echo "Method not allowed.";
}
?>