-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathajax.php
102 lines (84 loc) · 2.67 KB
/
ajax.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
<?php
include("PachubeAPI/PachubeAPI.php");
include("config.php");
function getInfoscreenData($config) {
// Try to see if there's some recent cached output available
if (file_exists($config->cache) && (time() < filemtime($config->cache) + $config->cache_expire)) {
// It is. Let's use that as our output.
return json_decode(file_get_contents($config->cache),true);
} else {
// No recent cached output. We need to get it from Cosm
$pachube = new PachubeAPI($config->api_key);
// Get the feed from the Cosm API, as a json
$json = $pachube->getFeed("json", $config->feed);
$data = array();
// iterate through all data streams to get the values we need
foreach (json_decode($json)->datastreams as $datastream) {
switch ($datastream->id) {
case $config->temperature:
$data["temperature"] = $datastream->current_value;
break;
case $config->power:
$data["power"] = $datastream->current_value;
break;
case $config->door:
$data["door"] = $datastream->current_value;
break;
case $config->devices:
$data["devices"] = $datastream->current_value;
break;
case $config->members:
$data["members"] = $datastream->current_value;
$data["memberratio"] = "" . (round(($data["members"]/314931)*100,3));
break;
case $config->account:
$data["account"] = $datastream->current_value;
break;
case $config->internet_up:
$data["internet_up"] = $datastream->current_value;
break;
case $config->internet_down:
$data["internet_down"] = $datastream->current_value;
break;
case $config->payback:
$data["payback"] = $datastream->current_value;
break;
}
}
$data["mqtt"] = readMQTTData();
// Write to cache
$data["source"] = "cache";
file_put_contents($config->cache, json_encode($data));
// Return live output
$data["source"] = "live";
return $data;
}
}
/**
* Parses the FHEM JSON tree and extract the devices and their state.
*/
function readMQTTData () {
$mqttData = array();
$json = json_decode(file_get_contents("http://infra.raumzeitlabor.de/fhem?cmd=jsonlist&XHR=1"));
foreach ($json->Results as $result) {
if ($result->list == "PCA301") {
foreach ($result->devices as $device) {
$mqttData[] = array(
"alias" => $device->ATTR->alias,
"state" => $device->STATE);
}
}
}
return $mqttData;
}
$config = new Configuration();
readMQTTData();
if(realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
// Send header
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 19 Jul 1997 00:00:00 GMT');
header('Content-type: application/json');
// Send output
echo json_encode(getInfoscreenData($config));
}
?>