-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcronLog.php
More file actions
79 lines (61 loc) · 2.01 KB
/
Copy pathcronLog.php
File metadata and controls
79 lines (61 loc) · 2.01 KB
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
<?php
/*
Require https://github.com/KiboOst/php-devoloDHC
Use this script as cron task to log consumptions and batteries levels everyday.
*/
//logs paths:
$consumptionsLogPath = $_SERVER['DOCUMENT_ROOT'].'/path/to/DHCconsumptions.json';
$batteriesLogPath = $_SERVER['DOCUMENT_ROOT'].'/path/to/DHCbatteries.json';
//API path:
require($_SERVER['DOCUMENT_ROOT'].'/path/to/phpDevoloAPI.php');
$_DHC = new DevoloDHC($login, $password);
if (isset($_DHC->error)) die($_DHC->error);
//log Devolo Wall Plugs Consumptions:
$_DHC->logConsumption($consumptionsLogPath);
//log Devolo batteries levels:
logBatteries($batteriesLogPath);
function logBatteries($filePath='/') //@log file path | always @return['result'] array of yesterday total consumptions, @return['error'] if can't write file
{
global $_DHC;
if (@file_exists($filePath))
{
$prevDatas = json_decode(file_get_contents($filePath), true);
}
else
{
$prevDatas = array();
}
//get today sums for each device:
$today = date('d.m.Y');
$datasArray = array();
$BatLevels = $_DHC->getAllBatteries();
foreach ($BatLevels['result'] as $device)
{
$name = $device['name'];
$level = $device['battery_percent'];
$datasArray[$today][$name] = $level;
}
ksort($datasArray[$today]);
//add today to previously loaded datas:
$prevDatas[$today] = $datasArray[$today];
//set recent up:
$keys = array_keys($prevDatas);
usort($keys, 'sortByDate');
$newArray = array();
foreach ($keys as $key)
{
$newArray[$key] = $prevDatas[$key];
}
$prevDatas = $newArray;
//write it to file:
@$put = file_put_contents($filePath, json_encode($prevDatas, JSON_PRETTY_PRINT));
if ($put) return array('result'=>$datasArray);
return array('result'=>$datasArray, 'error'=>'Unable to write file!');
}
function sortByDate($a, $b)
{
$t1 = strtotime($a);
$t2 = strtotime($b);
return ($t2 - $t1);
}
?>