forked from chriseng/nestgraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect.php
60 lines (52 loc) · 2.03 KB
/
collect.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
<?php
require 'inc/config.php';
require 'nest-api-master/nest.class.php';
define('USERNAME', $config['nest_user']);
define('PASSWORD', $config['nest_pass']);
date_default_timezone_set($config['local_tz']);
function get_nest_data() {
$nest = new Nest();
$info = $nest->getDeviceInfo();
if (preg_match("/away/", $info->current_state->mode) || preg_match("/range/", $info->current_state->mode)) {
if ($info->current_state->temperature > $info->target->temperature[1]) {
//Hotter then upper temp
$targetTemp = $info->target->temperature[1];
}
else if ($info->current_state->temperature < $info->target->temperature[0]) {
//Colder then lower temp
$targetTemp = $info->target->temperature[0];
}
else {
if (($info->target->temperature[1] - $info->current_state->temperature) <
($info->current_state->temperature - $info->target->temperature[0]))
{
//Closer to upper temp
$targetTemp = $info->target->temperature[1];
}
else
{
//Closer to lower temp
$targetTemp = $info->target->temperature[0];
}
}
}
else {
$targetTemp = $info->target->temperature;
}
$data = array('heating' => ($info->current_state->heat == 1 ? 1 : 0),
'cooling' => ($info->current_state->ac == 1 ? 1 : 0),
'fan' => ($info->current_state->fan == 1 ? 1 : 0),
'autoAway' => ($info->current_state->auto_away == 1 ? 1 : ($info->current_state->auto_away == -1 ? -1 : 0)),
'manualAway' => ($info->current_state->manual_away == 1 ? 1 : 0),
'leaf' => ($info->current_state->leaf == 1 ? 1 : 0),
'timestamp' => $info->network->last_connection,
'target_temp' => sprintf("%.02f", $targetTemp),
'current_temp' => sprintf("%.02f", $info->current_state->temperature),
'humidity' => $info->current_state->humidity
);
return $data;
}
function c_to_f($c) {
return ($c * 1.8) + 32;
}
?>