/
rest.php
77 lines (58 loc) · 1.64 KB
/
rest.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
header("Access-Control-Allow-Origin: *", true);
ini_set('display_errors', 1);
ini_set('display_starup_errors', 1);
error_reporting(E_ALL);
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
function dbConnect() {
$dbHost = "localhost";
$dbName = "Sunny";
$dbUser = "root";
$dbPass = "root";
$db = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
return $db;
}
echo "success_jsonp({";
echo '"api": {';
$app->post('/reading', 'insertReading');
$app->get('/reading', 'insertReading');
function insertReading() {
$db = dbConnect();
$value = $_GET['value'];
$room = $_GET['room'];
$insert = $db->prepare("INSERT INTO yun (value,room) VALUES ('$value','$room')");
$insert->execute();
echo '"status": "success"';
}
$app->get('/readings/', 'selectReadings');
function selectReadings() {
$db = dbConnect();
// $select = $db->query("SELECT * FROM yun");
$select = $db->query("SELECT * FROM yun ORDER BY time DESC LIMIT 1");
echo '"value": ' . "[";
$values = "";
/*while($row = $select->fetch()) {
$values .= $row['value'] . ",";
}*/
$values = $select->fetch()['value'];
$values = rtrim($values, ",");
echo $values;
echo "], ";
// $select = $db->query("SELECT * FROM yun");
$select = $db->query("SELECT * FROM yun ORDER BY time DESC LIMIT 1");
echo '"room": ' . '["';
$rooms = "";
/*while($row = $select->fetch()) {
$rooms =
}*/
$rooms = $select->fetch()['room'];
$rooms = rtrim($rooms, ",");
echo $rooms;
echo '"]';
}
$app->run();
echo "}";
echo "})";
?>