/
temperature.php
61 lines (60 loc) · 2.35 KB
/
temperature.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
<?php
function echoJSON($withStatus,$andMessage){
$data = array('status' => $withStatus, 'message' => $andMessage);
$jsonstring = json_encode($data);
header('Content-Type: application/json');
echo $jsonstring;
}
$user = 'root';
$password = 'root';
$db = 'temperatureData';
$host = 'localhost';
$port = 8889;
$link = mysqli_init();
$success = mysqli_real_connect(
$link,
$host,
$user,
$password,
$db,
$port
);
$privateKey = "lsw";
if($success){
$key = $_GET["key"];
if($key == $privateKey){
$query = $_GET["query"];
switch ($query){
case "get":
$result = mysqli_query($link,"SELECT * FROM `Temperature`");
$row = mysqli_fetch_array($result);
$data = array('status' => true, 'date' => $row["Date"], 'value' => (double)$row["Value"]);
$jsonstring = json_encode($data);
header('Content-Type: application/json');
echo $jsonstring;
break;
case "set":
$value = $_GET["value"];
$valueDouble = (double)$value;
if($valueDouble){
mysqli_query($link,"DELETE FROM `Temperature` WHERE 1");
mysqli_query($link,"INSERT INTO `Temperature`(`Date`, `Value`) VALUES (CURRENT_TIMESTAMP,$valueDouble);");
$data = array('status' => true, 'message' => 'setting success');
$jsonstring = json_encode($data);
header('Content-Type: application/json');
echo $jsonstring;
}else{
echoJSON(false,"invalid value");
}
break;
default:
echoJSON(false,"unsupported query");
}
}else{
echoJSON(false,"invalid key");
}
}else{
echoJSON(false,"Connect Error: " . mysqli_connect_error());
}
mysqli_close($link);
?>