-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.php
executable file
·55 lines (43 loc) · 1.39 KB
/
data.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
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
$servername = "localhost";
$username = "openhab"; // TODO: change to your username
$password = "openhab"; // TODO: change your password
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
class LogEntry
{
public $timestamp;
public $value;
}
$response = array();
if (isset($_GET["item"]))
{
$itemname = $conn->real_escape_string($_GET["item"]);
$sql = "SELECT ItemId FROM openhab.Items WHERE ItemName='$itemname' limit 1;";
$result = $conn->query($sql);
$itemid = $result->fetch_assoc()["ItemId"];
$sql = "SELECT Time, Value FROM openhab.Item".$itemid." ORDER BY Time DESC LIMIT 6;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$entry = new LogEntry();
// some DateTime parsing to drop the "seconds" part (space is precious on our widgets)
$new_date = DateTime::createFromFormat('Y-m-d H:i:s', $row["Time"]);
$entry->timestamp = $new_date->format('Y-m-d H:i');
$entry->value = $row["Value"];
$response[] = $entry;
}
}
}
header('Content-Type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);
$conn->close();
?>