forked from GregOriol/php-LinkyAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.php
73 lines (53 loc) · 1.8 KB
/
cron.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
<?php
require_once './Linky.php';
require_once './EnedisCredentials.php';
require_once './LinkyException.php';
require_once './DataHelper.php';
use Linky\EnedisCredentials;
use Linky\Linky;
use Linky\LinkyException;
use Linky\DataHelper;
/*
* Configuration
*/
$storage = './linky-data.json';
$enedisCredentials = new EnedisCredentials('mylogin', 'mypass');
/*
* Main
*/
$data = array(
Linky::DATASET_HOURLY => null,
Linky::DATASET_DAILY => null,
Linky::DATASET_MONTHLY => null,
Linky::DATASET_YEARLY => null,
);
if (file_exists($storage)) {
$data = json_decode(file_get_contents($storage), true);
}
try {
$linky = new Linky($enedisCredentials);
$today = new \DateTime('NOW', new \DateTimeZone(Linky::TIMEZONE));
$yesterday = clone $today;
$yesterday->sub(new \DateInterval('P1D')); // Enedis' last data are for yesterday
// Hourly
$hourlyData = $linky->getHourlyData($yesterday);
DataHelper::merge($data, Linky::DATASET_HOURLY, $hourlyData);
// Daily
$daysAgo = clone $yesterday;
$daysAgo->sub(new \DateInterval('P3D'));
$dailyData = $linky->getDailyData($daysAgo, $yesterday);
DataHelper::merge($data, Linky::DATASET_DAILY, $dailyData);
// Monthly
$monthsAgo = clone $yesterday;
$monthsAgo->sub(new \DateInterval('P3M'));
$monthsAgo->setDate($monthsAgo->format('Y'), $monthsAgo->format('m'), '01');
$monthlyData = $linky->getMonthlyData($monthsAgo, $yesterday);
DataHelper::merge($data, Linky::DATASET_MONTHLY, $monthlyData);
// Yearly
$yearlyData = $linky->getYearlyData();
DataHelper::merge($data, Linky::DATASET_YEARLY, $yearlyData);
} catch (LinkyException $e) {
echo $e->getMessage().PHP_EOL;
exit;
}
file_put_contents($storage, json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));