-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqivivoLog.php
89 lines (75 loc) · 2.71 KB
/
qivivoLog.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
<?php
require($_SERVER['DOCUMENT_ROOT'].'/path/to/splQivivoAPI.php'); //Qivivo SDK API
$logfilePath = $_SERVER['DOCUMENT_ROOT'].'/path/to/qivivoLog.json';
$logMaxDays = 90;
$_qivivo = new splQivivoAPI($clienID, $secretID);
//LOGGING:
$log = qiLog();
function qiLog($filePath='/')
{
global $_qivivo;
global $logfilePath;
global $logMaxDays;
if (@file_exists($logfilePath))
{
$prevDatas = json_decode(file_get_contents($logfilePath), true);
}
else
{
$prevDatas = array();
}
//get today sums for each device:
$today = date('d.m.Y');
$nowTime = date('H:i');
//if Qivivo servers are online:
if (!isset($_qivivo->error))
{
//Qivivo data:
$thermostatTemperature = $_qivivo->getThermostatTemperature();
$current_temperature_order = $thermostatTemperature['current_temperature_order'];
$current_temperature = $thermostatTemperature['temperature'];
$thermostatHumidity = $_qivivo->getThermostatHumidity();
$current_humidity = $thermostatHumidity['humidity'];
$thermostatPresence = $_qivivo->getThermostatPresence();
$var = $thermostatPresence['presence_detected'];
$presence_detected = 0;
if ($var == true) $presence_detected = 1;
//log all these:
$prevDatas[$today][$nowTime] = array(
'current_temperature_order' => $current_temperature_order,
'current_temperature' => $current_temperature,
'current_humidity' => $current_humidity,
'presence_detected' => $presence_detected,
'exterior_temperature' => $exterior_temperature
);
}
else
{
if (isset($prevDatas[$today]))
{
$prevTime = end($prevDatas[$today]);
$prevDatas[$today][$nowTime] = $prevTime;
}
}
//set recent up:
$keys = array_keys($prevDatas);
usort($keys, 'sortByDate');
$newArray = array();
foreach ($keys as $key)
{
$newArray[$key] = $prevDatas[$key];
}
$prevDatas = $newArray;
if (count($prevDatas) > $logMaxDays) array_pop($prevDatas);
//write it to file:
@$put = file_put_contents($logfilePath, json_encode($prevDatas, JSON_PRETTY_PRINT));
if ($put) return array('result'=>$prevDatas);
return array('result'=>$prevDatas, 'error'=>'Unable to write file!');
}
function sortByDate($a, $b)
{
$t1 = strtotime($a);
$t2 = strtotime($b);
return ($t2 - $t1);
}
?>