-
Notifications
You must be signed in to change notification settings - Fork 10
/
linkyCron.php
78 lines (60 loc) · 2.14 KB
/
linkyCron.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
<?php
require($_SERVER['DOCUMENT_ROOT'].'/path/to/phpLinkyAPI.php'); //Linky custom API
$filePath = $_SERVER['DOCUMENT_ROOT'].'/path/to/linkyLog.json'; //Json data file
$enedis_user = 'mylogin';
$enedis_pass = 'myPass';
$_Linky = new Linky($enedis_user, $enedis_pass, true);
if (isset($_Linky->error)) echo '__ERROR__: ', $_Linky->error, "<br>";
$newJson = $_Linky->_data;
if (@file_exists($filePath))
{
$prevDatas = json_decode(file_get_contents($filePath), true);
}
else
{
$prevDatas = $newJson;
}
//_____Update current month:
$timezone = 'Europe/Paris';
$today = new DateTime('NOW', new DateTimeZone($timezone));
$thisMonth = $today->format('M Y');
$updateTime = $today->format('d/m/Y H:i:s');
$prevDatas['Update'] = $updateTime;
if ($newJson['months'][$thisMonth] != null) $prevDatas['months'][$thisMonth] = $newJson['months'][$thisMonth];
//if first day of month, update previous month:
if ($today->format('d') == '01')
{
$prevMonth = clone $today;
$prevMonth->sub(new DateInterval('P1M'));
$prevMonth = $prevMonth->format('M Y');
$prevDatas['months'][$prevMonth] = $newJson['months'][$prevMonth];
}
//_____Update current year:
$thisYear = new DateTime();
$thisYear = $thisYear->format('Y');
if ($newJson['years'][$thisYear] != null) $prevDatas['years'][$thisYear] = $newJson['years'][$thisYear];
//if first day of year, update previous year:
if ($today->format('d/m') == '01/01')
{
$prevYear = clone $today;
$prevYear->sub(new DateInterval('P1Y'));
$prevYear = $prevYear->format('Y');
$prevDatas['years'][$prevYear] = $newJson['years'][$prevYear];
}
//_____Does yesterday hours exists ?
$yesterday = clone $today;
$yesterday->sub(new DateInterval('P1D'));
$yesterday = $yesterday->format('d/m/Y');
//avoid empty data:
if (!isset($prevDatas['hours'][$yesterday]))
{
$h = $newJson['hours'][$yesterday]["00:00"];
if ($h != 'kW' and $h != '-2kW') $prevDatas['hours'][$yesterday] = $newJson['hours'][$yesterday];
}
//_____Add yesterday day:
if (!isset($prevDatas['days'][$yesterday]))
{
$prevDatas['days'][$yesterday] = $newJson['days'][$yesterday];
}
file_put_contents($filePath, json_encode($prevDatas, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
?>